7.3.3 The Hyper-Text Transfer Protocol

The initial motivation for including web functionality in Guile, rather
than rely on an external package, was to establish a standard base on
which people can share code. To that end, we continue the focus on data
types by providing a number of low-level parsers and unparsers for
elements of the HTTP protocol.

If you are want to skip the low-level details for now and move on to web
pages, see Web Client, and see Web Server. Otherwise, load the
HTTP module, and read on.

(use-modules (web http))

The focus of the (web http) module is to parse and unparse
standard HTTP headers, representing them to Guile as native data
structures. For example, a Date: header will be represented as a
SRFI-19 date record (see SRFI-19), rather than as a string.

Guile tries to follow RFCs fairly strictly—the road to perdition being
paved with compatibility hacks—though some allowances are made for
not-too-divergent texts.

Guile keeps a registry of known headers, their string names, and some
parsing and serialization procedures. If a header is unknown, its
string name is simply its symbol name in title-case.

Scheme Procedure: known-header?sym

Return #t if sym is a known header, with associated
parsers and serialization procedures, or #f otherwise.

Scheme Procedure: header-parsersym

Return the value parser for headers named sym. The result is a
procedure that takes one argument, a string, and returns the parsed
value. If the header isn’t known to Guile, a default parser is returned
that passes through the string unchanged.

Scheme Procedure: header-validatorsym

Return a predicate which returns #t if the given value is valid
for headers named sym. The default validator for unknown headers
is string?.

Scheme Procedure: header-writersym

Return a procedure that writes values for headers named sym to a
port. The resulting procedure takes two arguments: a value and a port.
The default writer is display.

For more on the set of headers that Guile knows about out of the box,
see HTTP Headers. To add your own, use the declare-header!
procedure:

For example, let’s say you are running a web server behind some sort of
proxy, and your proxy adds an X-Client-Address header, indicating
the IPv4 address of the original client. You would like for the HTTP
request record to parse out this header to a Scheme value, instead of
leaving it as a string. You could register this header with Guile’s
HTTP stack like this: