web.texi 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2010, 2011, 2012, 2013, 2015 Free Software Foundation, Inc.
  4. @c See the file guile.texi for copying conditions.
  5. @node Web
  6. @section @acronym{HTTP}, the Web, and All That
  7. @cindex Web
  8. @cindex WWW
  9. @cindex HTTP
  10. It has always been possible to connect computers together and share
  11. information between them, but the rise of the World Wide Web over the
  12. last couple of decades has made it much easier to do so. The result is
  13. a richly connected network of computation, in which Guile forms a part.
  14. By ``the web'', we mean the HTTP protocol@footnote{Yes, the P is for
  15. protocol, but this phrase appears repeatedly in RFC 2616.} as handled by
  16. servers, clients, proxies, caches, and the various kinds of messages and
  17. message components that can be sent and received by that protocol,
  18. notably HTML.
  19. On one level, the web is text in motion: the protocols themselves are
  20. textual (though the payload may be binary), and it's possible to create
  21. a socket and speak text to the web. But such an approach is obviously
  22. primitive. This section details the higher-level data types and
  23. operations provided by Guile: URIs, HTTP request and response records,
  24. and a conventional web server implementation.
  25. The material in this section is arranged in ascending order, in which
  26. later concepts build on previous ones. If you prefer to start with the
  27. highest-level perspective, @pxref{Web Examples}, and work your way
  28. back.
  29. @menu
  30. * Types and the Web:: Types prevent bugs and security problems.
  31. * URIs:: Universal Resource Identifiers.
  32. * HTTP:: The Hyper-Text Transfer Protocol.
  33. * HTTP Headers:: How Guile represents specific header values.
  34. * Transfer Codings:: HTTP Transfer Codings.
  35. * Requests:: HTTP requests.
  36. * Responses:: HTTP responses.
  37. * Web Client:: Accessing web resources over HTTP.
  38. * Web Server:: Serving HTTP to the internet.
  39. * Web Examples:: How to use this thing.
  40. @end menu
  41. @node Types and the Web
  42. @subsection Types and the Web
  43. It is a truth universally acknowledged, that a program with good use of
  44. data types, will be free from many common bugs. Unfortunately, the
  45. common practice in web programming seems to ignore this maxim. This
  46. subsection makes the case for expressive data types in web programming.
  47. By ``expressive data types'', we mean that the data types @emph{say}
  48. something about how a program solves a problem. For example, if we
  49. choose to represent dates using SRFI 19 date records (@pxref{SRFI-19}),
  50. this indicates that there is a part of the program that will always have
  51. valid dates. Error handling for a number of basic cases, like invalid
  52. dates, occurs on the boundary in which we produce a SRFI 19 date record
  53. from other types, like strings.
  54. With regards to the web, data types are helpful in the two broad phases
  55. of HTTP messages: parsing and generation.
  56. Consider a server, which has to parse a request, and produce a response.
  57. Guile will parse the request into an HTTP request object
  58. (@pxref{Requests}), with each header parsed into an appropriate Scheme
  59. data type. This transition from an incoming stream of characters to
  60. typed data is a state change in a program---the strings might parse, or
  61. they might not, and something has to happen if they do not. (Guile
  62. throws an error in this case.) But after you have the parsed request,
  63. ``client'' code (code built on top of the Guile web framework) will not
  64. have to check for syntactic validity. The types already make this
  65. information manifest.
  66. This state change on the parsing boundary makes programs more robust,
  67. as they themselves are freed from the need to do a number of common
  68. error checks, and they can use normal Scheme procedures to handle a
  69. request instead of ad-hoc string parsers.
  70. The need for types on the response generation side (in a server) is more
  71. subtle, though not less important. Consider the example of a POST
  72. handler, which prints out the text that a user submits from a form.
  73. Such a handler might include a procedure like this:
  74. @example
  75. ;; First, a helper procedure
  76. (define (para . contents)
  77. (string-append "<p>" (string-concatenate contents) "</p>"))
  78. ;; Now the meat of our simple web application
  79. (define (you-said text)
  80. (para "You said: " text))
  81. (display (you-said "Hi!"))
  82. @print{} <p>You said: Hi!</p>
  83. @end example
  84. This is a perfectly valid implementation, provided that the incoming
  85. text does not contain the special HTML characters @samp{<}, @samp{>}, or
  86. @samp{&}. But this provision of a restricted character set is not
  87. reflected anywhere in the program itself: we must @emph{assume} that the
  88. programmer understands this, and performs the check elsewhere.
  89. Unfortunately, the short history of the practice of programming does not
  90. bear out this assumption. A @dfn{cross-site scripting} (@acronym{XSS})
  91. vulnerability is just such a common error in which unfiltered user input
  92. is allowed into the output. A user could submit a crafted comment to
  93. your web site which results in visitors running malicious Javascript,
  94. within the security context of your domain:
  95. @example
  96. (display (you-said "<script src=\"http://bad.com/nasty.js\" />"))
  97. @print{} <p>You said: <script src="http://bad.com/nasty.js" /></p>
  98. @end example
  99. The fundamental problem here is that both user data and the program
  100. template are represented using strings. This identity means that types
  101. can't help the programmer to make a distinction between these two, so
  102. they get confused.
  103. There are a number of possible solutions, but perhaps the best is to
  104. treat HTML not as strings, but as native s-expressions: as SXML. The
  105. basic idea is that HTML is either text, represented by a string, or an
  106. element, represented as a tagged list. So @samp{foo} becomes
  107. @samp{"foo"}, and @samp{<b>foo</b>} becomes @samp{(b "foo")}.
  108. Attributes, if present, go in a tagged list headed by @samp{@@}, like
  109. @samp{(img (@@ (src "http://example.com/foo.png")))}. @xref{SXML}, for
  110. more information.
  111. The good thing about SXML is that HTML elements cannot be confused with
  112. text. Let's make a new definition of @code{para}:
  113. @example
  114. (define (para . contents)
  115. `(p ,@@contents))
  116. (use-modules (sxml simple))
  117. (sxml->xml (you-said "Hi!"))
  118. @print{} <p>You said: Hi!</p>
  119. (sxml->xml (you-said "<i>Rats, foiled again!</i>"))
  120. @print{} <p>You said: &lt;i&gt;Rats, foiled again!&lt;/i&gt;</p>
  121. @end example
  122. So we see in the second example that HTML elements cannot be unwittingly
  123. introduced into the output. However it is now perfectly acceptable to
  124. pass SXML to @code{you-said}; in fact, that is the big advantage of SXML
  125. over everything-as-a-string.
  126. @example
  127. (sxml->xml (you-said (you-said "<Hi!>")))
  128. @print{} <p>You said: <p>You said: &lt;Hi!&gt;</p></p>
  129. @end example
  130. The SXML types allow procedures to @emph{compose}. The types make
  131. manifest which parts are HTML elements, and which are text. So you
  132. needn't worry about escaping user input; the type transition back to a
  133. string handles that for you. @acronym{XSS} vulnerabilities are a thing
  134. of the past.
  135. Well. That's all very nice and opinionated and such, but how do I use
  136. the thing? Read on!
  137. @node URIs
  138. @subsection Universal Resource Identifiers
  139. Guile provides a standard data type for Universal Resource Identifiers
  140. (URIs), as defined in RFC 3986.
  141. The generic URI syntax is as follows:
  142. @example
  143. URI := scheme ":" ["//" [userinfo "@@"] host [":" port]] path \
  144. [ "?" query ] [ "#" fragment ]
  145. @end example
  146. For example, in the URI, @indicateurl{http://www.gnu.org/help/}, the
  147. scheme is @code{http}, the host is @code{www.gnu.org}, the path is
  148. @code{/help/}, and there is no userinfo, port, query, or fragment. All
  149. URIs have a scheme and a path (though the path might be empty). Some
  150. URIs have a host, and some of those have ports and userinfo. Any URI
  151. might have a query part or a fragment.
  152. There is also a ``URI-reference'' data type, which is the same as a URI
  153. but where the scheme is optional. In this case, the scheme is taken to
  154. be relative to some other related URI. A common use of URI references
  155. is when you want to be vague regarding the choice of HTTP or HTTPS --
  156. serving a web page referring to @code{/foo.css} will use HTTPS if loaded
  157. over HTTPS, or HTTP otherwise.
  158. Userinfo is something of an abstraction, as some legacy URI schemes
  159. allowed userinfo of the form @code{@var{username}:@var{passwd}}. But
  160. since passwords do not belong in URIs, the RFC does not want to condone
  161. this practice, so it calls anything before the @code{@@} sign
  162. @dfn{userinfo}.
  163. Properly speaking, a fragment is not part of a URI. For example, when a
  164. web browser follows a link to @indicateurl{http://example.com/#foo}, it
  165. sends a request for @indicateurl{http://example.com/}, then looks in the
  166. resulting page for the fragment identified @code{foo} reference. A
  167. fragment identifies a part of a resource, not the resource itself. But
  168. it is useful to have a fragment field in the URI record itself, so we
  169. hope you will forgive the inconsistency.
  170. @example
  171. (use-modules (web uri))
  172. @end example
  173. The following procedures can be found in the @code{(web uri)}
  174. module. Load it into your Guile, using a form like the above, to have
  175. access to them.
  176. @deffn {Scheme Procedure} build-uri scheme @
  177. [#:userinfo=@code{#f}] [#:host=@code{#f}] [#:port=@code{#f}] @
  178. [#:path=@code{""}] [#:query=@code{#f}] [#:fragment=@code{#f}] @
  179. [#:validate?=@code{#t}]
  180. Construct a URI object. @var{scheme} should be a symbol, @var{port}
  181. either a positive, exact integer or @code{#f}, and the rest of the
  182. fields are either strings or @code{#f}. If @var{validate?} is true,
  183. also run some consistency checks to make sure that the constructed URI
  184. is valid.
  185. @end deffn
  186. @deffn {Scheme Procedure} build-uri-reference [#:scheme=@code{#f}]@
  187. [#:userinfo=@code{#f}] [#:host=@code{#f}] [#:port=@code{#f}] @
  188. [#:path=@code{""}] [#:query=@code{#f}] [#:fragment=@code{#f}] @
  189. [#:validate?=@code{#t}]
  190. Like @code{build-uri}, but with an optional scheme.
  191. @end deffn
  192. In Guile, both URI and URI reference data types are represented in the
  193. same way, as URI objects.
  194. @deffn {Scheme Procedure} uri? obj
  195. @deffnx {Scheme Procedure} uri-scheme uri
  196. @deffnx {Scheme Procedure} uri-userinfo uri
  197. @deffnx {Scheme Procedure} uri-host uri
  198. @deffnx {Scheme Procedure} uri-port uri
  199. @deffnx {Scheme Procedure} uri-path uri
  200. @deffnx {Scheme Procedure} uri-query uri
  201. @deffnx {Scheme Procedure} uri-fragment uri
  202. A predicate and field accessors for the URI record type. The URI scheme
  203. will be a symbol, or @code{#f} if the object is a URI reference but not
  204. a URI. The port will be either a positive, exact integer or @code{#f},
  205. and the rest of the fields will be either strings or @code{#f} if not
  206. present.
  207. @end deffn
  208. @deffn {Scheme Procedure} string->uri string
  209. Parse @var{string} into a URI object. Return @code{#f} if the string
  210. could not be parsed.
  211. @end deffn
  212. @deffn {Scheme Procedure} string->uri-reference string
  213. Parse @var{string} into a URI object, while not requiring a scheme.
  214. Return @code{#f} if the string could not be parsed.
  215. @end deffn
  216. @deffn {Scheme Procedure} uri->string uri
  217. Serialize @var{uri} to a string. If the URI has a port that is the
  218. default port for its scheme, the port is not included in the
  219. serialization.
  220. @end deffn
  221. @deffn {Scheme Procedure} declare-default-port! scheme port
  222. Declare a default port for the given URI scheme.
  223. @end deffn
  224. @deffn {Scheme Procedure} uri-decode str [#:encoding=@code{"utf-8"}] [#:decode-plus-to-space? #t]
  225. Percent-decode the given @var{str}, according to @var{encoding}, which
  226. should be the name of a character encoding.
  227. Note that this function should not generally be applied to a full URI
  228. string. For paths, use @code{split-and-decode-uri-path} instead. For
  229. query strings, split the query on @code{&} and @code{=} boundaries, and
  230. decode the components separately.
  231. Note also that percent-encoded strings encode @emph{bytes}, not
  232. characters. There is no guarantee that a given byte sequence is a valid
  233. string encoding. Therefore this routine may signal an error if the
  234. decoded bytes are not valid for the given encoding. Pass @code{#f} for
  235. @var{encoding} if you want decoded bytes as a bytevector directly.
  236. @xref{Ports, @code{set-port-encoding!}}, for more information on
  237. character encodings.
  238. If @var{decode-plus-to-space?} is true, which is the default, also
  239. replace instances of the plus character @samp{+} with a space character.
  240. This is needed when parsing @code{application/x-www-form-urlencoded}
  241. data.
  242. Returns a string of the decoded characters, or a bytevector if
  243. @var{encoding} was @code{#f}.
  244. @end deffn
  245. @deffn {Scheme Procedure} uri-encode str [#:encoding=@code{"utf-8"}] [#:unescaped-chars]
  246. Percent-encode any character not in the character set,
  247. @var{unescaped-chars}.
  248. The default character set includes alphanumerics from ASCII, as well as
  249. the special characters @samp{-}, @samp{.}, @samp{_}, and @samp{~}. Any
  250. other character will be percent-encoded, by writing out the character to
  251. a bytevector within the given @var{encoding}, then encoding each byte as
  252. @code{%@var{HH}}, where @var{HH} is the hexadecimal representation of
  253. the byte.
  254. @end deffn
  255. @deffn {Scheme Procedure} split-and-decode-uri-path path
  256. Split @var{path} into its components, and decode each component,
  257. removing empty components.
  258. For example, @code{"/foo/bar%20baz/"} decodes to the two-element list,
  259. @code{("foo" "bar baz")}.
  260. @end deffn
  261. @deffn {Scheme Procedure} encode-and-join-uri-path parts
  262. URI-encode each element of @var{parts}, which should be a list of
  263. strings, and join the parts together with @code{/} as a delimiter.
  264. For example, the list @code{("scrambled eggs" "biscuits&gravy")} encodes
  265. as @code{"scrambled%20eggs/biscuits%26gravy"}.
  266. @end deffn
  267. @node HTTP
  268. @subsection The Hyper-Text Transfer Protocol
  269. The initial motivation for including web functionality in Guile, rather
  270. than rely on an external package, was to establish a standard base on
  271. which people can share code. To that end, we continue the focus on data
  272. types by providing a number of low-level parsers and unparsers for
  273. elements of the HTTP protocol.
  274. If you are want to skip the low-level details for now and move on to web
  275. pages, @pxref{Web Client}, and @pxref{Web Server}. Otherwise, load the
  276. HTTP module, and read on.
  277. @example
  278. (use-modules (web http))
  279. @end example
  280. The focus of the @code{(web http)} module is to parse and unparse
  281. standard HTTP headers, representing them to Guile as native data
  282. structures. For example, a @code{Date:} header will be represented as a
  283. SRFI-19 date record (@pxref{SRFI-19}), rather than as a string.
  284. Guile tries to follow RFCs fairly strictly---the road to perdition being
  285. paved with compatibility hacks---though some allowances are made for
  286. not-too-divergent texts.
  287. Header names are represented as lower-case symbols.
  288. @deffn {Scheme Procedure} string->header name
  289. Parse @var{name} to a symbolic header name.
  290. @end deffn
  291. @deffn {Scheme Procedure} header->string sym
  292. Return the string form for the header named @var{sym}.
  293. @end deffn
  294. For example:
  295. @example
  296. (string->header "Content-Length")
  297. @result{} content-length
  298. (header->string 'content-length)
  299. @result{} "Content-Length"
  300. (string->header "FOO")
  301. @result{} foo
  302. (header->string 'foo)
  303. @result{} "Foo"
  304. @end example
  305. Guile keeps a registry of known headers, their string names, and some
  306. parsing and serialization procedures. If a header is unknown, its
  307. string name is simply its symbol name in title-case.
  308. @deffn {Scheme Procedure} known-header? sym
  309. Return @code{#t} if @var{sym} is a known header, with associated
  310. parsers and serialization procedures, or @code{#f} otherwise.
  311. @end deffn
  312. @deffn {Scheme Procedure} header-parser sym
  313. Return the value parser for headers named @var{sym}. The result is a
  314. procedure that takes one argument, a string, and returns the parsed
  315. value. If the header isn't known to Guile, a default parser is returned
  316. that passes through the string unchanged.
  317. @end deffn
  318. @deffn {Scheme Procedure} header-validator sym
  319. Return a predicate which returns @code{#t} if the given value is valid
  320. for headers named @var{sym}. The default validator for unknown headers
  321. is @code{string?}.
  322. @end deffn
  323. @deffn {Scheme Procedure} header-writer sym
  324. Return a procedure that writes values for headers named @var{sym} to a
  325. port. The resulting procedure takes two arguments: a value and a port.
  326. The default writer is @code{display}.
  327. @end deffn
  328. For more on the set of headers that Guile knows about out of the box,
  329. @pxref{HTTP Headers}. To add your own, use the @code{declare-header!}
  330. procedure:
  331. @deffn {Scheme Procedure} declare-header! name parser validator writer @
  332. [#:multiple?=@code{#f}]
  333. Declare a parser, validator, and writer for a given header.
  334. @end deffn
  335. For example, let's say you are running a web server behind some sort of
  336. proxy, and your proxy adds an @code{X-Client-Address} header, indicating
  337. the IPv4 address of the original client. You would like for the HTTP
  338. request record to parse out this header to a Scheme value, instead of
  339. leaving it as a string. You could register this header with Guile's
  340. HTTP stack like this:
  341. @example
  342. (declare-header! "X-Client-Address"
  343. (lambda (str)
  344. (inet-aton str))
  345. (lambda (ip)
  346. (and (integer? ip) (exact? ip) (<= 0 ip #xffffffff)))
  347. (lambda (ip port)
  348. (display (inet-ntoa ip) port)))
  349. @end example
  350. @deffn {Scheme Procedure} declare-opaque-header! name
  351. A specialised version of @code{declare-header!} for the case in which
  352. you want a header's value to be returned/written ``as-is''.
  353. @end deffn
  354. @deffn {Scheme Procedure} valid-header? sym val
  355. Return a true value if @var{val} is a valid Scheme value for the header
  356. with name @var{sym}, or @code{#f} otherwise.
  357. @end deffn
  358. Now that we have a generic interface for reading and writing headers, we
  359. do just that.
  360. @deffn {Scheme Procedure} read-header port
  361. Read one HTTP header from @var{port}. Return two values: the header
  362. name and the parsed Scheme value. May raise an exception if the header
  363. was known but the value was invalid.
  364. Returns the end-of-file object for both values if the end of the message
  365. body was reached (i.e., a blank line).
  366. @end deffn
  367. @deffn {Scheme Procedure} parse-header name val
  368. Parse @var{val}, a string, with the parser for the header named
  369. @var{name}. Returns the parsed value.
  370. @end deffn
  371. @deffn {Scheme Procedure} write-header name val port
  372. Write the given header name and value to @var{port}, using the writer
  373. from @code{header-writer}.
  374. @end deffn
  375. @deffn {Scheme Procedure} read-headers port
  376. Read the headers of an HTTP message from @var{port}, returning them
  377. as an ordered alist.
  378. @end deffn
  379. @deffn {Scheme Procedure} write-headers headers port
  380. Write the given header alist to @var{port}. Doesn't write the final
  381. @samp{\r\n}, as the user might want to add another header.
  382. @end deffn
  383. The @code{(web http)} module also has some utility procedures to read
  384. and write request and response lines.
  385. @deffn {Scheme Procedure} parse-http-method str [start] [end]
  386. Parse an HTTP method from @var{str}. The result is an upper-case symbol,
  387. like @code{GET}.
  388. @end deffn
  389. @deffn {Scheme Procedure} parse-http-version str [start] [end]
  390. Parse an HTTP version from @var{str}, returning it as a major--minor
  391. pair. For example, @code{HTTP/1.1} parses as the pair of integers,
  392. @code{(1 . 1)}.
  393. @end deffn
  394. @deffn {Scheme Procedure} parse-request-uri str [start] [end]
  395. Parse a URI from an HTTP request line. Note that URIs in requests do not
  396. have to have a scheme or host name. The result is a URI object.
  397. @end deffn
  398. @deffn {Scheme Procedure} read-request-line port
  399. Read the first line of an HTTP request from @var{port}, returning three
  400. values: the method, the URI, and the version.
  401. @end deffn
  402. @deffn {Scheme Procedure} write-request-line method uri version port
  403. Write the first line of an HTTP request to @var{port}.
  404. @end deffn
  405. @deffn {Scheme Procedure} read-response-line port
  406. Read the first line of an HTTP response from @var{port}, returning three
  407. values: the HTTP version, the response code, and the ``reason phrase''.
  408. @end deffn
  409. @deffn {Scheme Procedure} write-response-line version code reason-phrase port
  410. Write the first line of an HTTP response to @var{port}.
  411. @end deffn
  412. @node HTTP Headers
  413. @subsection HTTP Headers
  414. In addition to defining the infrastructure to parse headers, the
  415. @code{(web http)} module defines specific parsers and unparsers for all
  416. headers defined in the HTTP/1.1 standard.
  417. For example, if you receive a header named @samp{Accept-Language} with a
  418. value @samp{en, es;q=0.8}, Guile parses it as a quality list (defined
  419. below):
  420. @example
  421. (parse-header 'accept-language "en, es;q=0.8")
  422. @result{} ((1000 . "en") (800 . "es"))
  423. @end example
  424. The format of the value for @samp{Accept-Language} headers is defined
  425. below, along with all other headers defined in the HTTP standard. (If
  426. the header were unknown, the value would have been returned as a
  427. string.)
  428. For brevity, the header definitions below are given in the form,
  429. @var{Type} @code{@var{name}}, indicating that values for the header
  430. @code{@var{name}} will be of the given @var{Type}. Since Guile
  431. internally treats header names in lower case, in this document we give
  432. types title-cased names. A short description of the each header's
  433. purpose and an example follow.
  434. For full details on the meanings of all of these headers, see the HTTP
  435. 1.1 standard, RFC 2616.
  436. @subsubsection HTTP Header Types
  437. Here we define the types that are used below, when defining headers.
  438. @deftp {HTTP Header Type} Date
  439. A SRFI-19 date.
  440. @end deftp
  441. @deftp {HTTP Header Type} KVList
  442. A list whose elements are keys or key-value pairs. Keys are parsed to
  443. symbols. Values are strings by default. Non-string values are the
  444. exception, and are mentioned explicitly below, as appropriate.
  445. @end deftp
  446. @deftp {HTTP Header Type} SList
  447. A list of strings.
  448. @end deftp
  449. @deftp {HTTP Header Type} Quality
  450. An exact integer between 0 and 1000. Qualities are used to express
  451. preference, given multiple options. An option with a quality of 870,
  452. for example, is preferred over an option with quality 500.
  453. (Qualities are written out over the wire as numbers between 0.0 and
  454. 1.0, but since the standard only allows three digits after the decimal,
  455. it's equivalent to integers between 0 and 1000, so that's what Guile
  456. uses.)
  457. @end deftp
  458. @deftp {HTTP Header Type} QList
  459. A quality list: a list of pairs, the car of which is a quality, and the
  460. cdr a string. Used to express a list of options, along with their
  461. qualities.
  462. @end deftp
  463. @deftp {HTTP Header Type} ETag
  464. An entity tag, represented as a pair. The car of the pair is an opaque
  465. string, and the cdr is @code{#t} if the entity tag is a ``strong'' entity
  466. tag, and @code{#f} otherwise.
  467. @end deftp
  468. @subsubsection General Headers
  469. General HTTP headers may be present in any HTTP message.
  470. @deftypevr {HTTP Header} KVList cache-control
  471. A key-value list of cache-control directives. See RFC 2616, for more
  472. details.
  473. If present, parameters to @code{max-age}, @code{max-stale},
  474. @code{min-fresh}, and @code{s-maxage} are all parsed as non-negative
  475. integers.
  476. If present, parameters to @code{private} and @code{no-cache} are parsed
  477. as lists of header names, as symbols.
  478. @example
  479. (parse-header 'cache-control "no-cache,no-store"
  480. @result{} (no-cache no-store)
  481. (parse-header 'cache-control "no-cache=\"Authorization,Date\",no-store"
  482. @result{} ((no-cache . (authorization date)) no-store)
  483. (parse-header 'cache-control "no-cache=\"Authorization,Date\",max-age=10"
  484. @result{} ((no-cache . (authorization date)) (max-age . 10))
  485. @end example
  486. @end deftypevr
  487. @deftypevr {HTTP Header} List connection
  488. A list of header names that apply only to this HTTP connection, as
  489. symbols. Additionally, the symbol @samp{close} may be present, to
  490. indicate that the server should close the connection after responding to
  491. the request.
  492. @example
  493. (parse-header 'connection "close")
  494. @result{} (close)
  495. @end example
  496. @end deftypevr
  497. @deftypevr {HTTP Header} Date date
  498. The date that a given HTTP message was originated.
  499. @example
  500. (parse-header 'date "Tue, 15 Nov 1994 08:12:31 GMT")
  501. @result{} #<date ...>
  502. @end example
  503. @end deftypevr
  504. @deftypevr {HTTP Header} KVList pragma
  505. A key-value list of implementation-specific directives.
  506. @example
  507. (parse-header 'pragma "no-cache, broccoli=tasty")
  508. @result{} (no-cache (broccoli . "tasty"))
  509. @end example
  510. @end deftypevr
  511. @deftypevr {HTTP Header} List trailer
  512. A list of header names which will appear after the message body, instead
  513. of with the message headers.
  514. @example
  515. (parse-header 'trailer "ETag")
  516. @result{} (etag)
  517. @end example
  518. @end deftypevr
  519. @deftypevr {HTTP Header} List transfer-encoding
  520. A list of transfer codings, expressed as key-value lists. The only
  521. transfer coding defined by the specification is @code{chunked}.
  522. @example
  523. (parse-header 'transfer-encoding "chunked")
  524. @result{} ((chunked))
  525. @end example
  526. @end deftypevr
  527. @deftypevr {HTTP Header} List upgrade
  528. A list of strings, indicating additional protocols that a server could use
  529. in response to a request.
  530. @example
  531. (parse-header 'upgrade "WebSocket")
  532. @result{} ("WebSocket")
  533. @end example
  534. @end deftypevr
  535. FIXME: parse out more fully?
  536. @deftypevr {HTTP Header} List via
  537. A list of strings, indicating the protocol versions and hosts of
  538. intermediate servers and proxies. There may be multiple @code{via}
  539. headers in one message.
  540. @example
  541. (parse-header 'via "1.0 venus, 1.1 mars")
  542. @result{} ("1.0 venus" "1.1 mars")
  543. @end example
  544. @end deftypevr
  545. @deftypevr {HTTP Header} List warning
  546. A list of warnings given by a server or intermediate proxy. Each
  547. warning is a itself a list of four elements: a code, as an exact integer
  548. between 0 and 1000, a host as a string, the warning text as a string,
  549. and either @code{#f} or a SRFI-19 date.
  550. There may be multiple @code{warning} headers in one message.
  551. @example
  552. (parse-header 'warning "123 foo \"core breach imminent\"")
  553. @result{} ((123 "foo" "core-breach imminent" #f))
  554. @end example
  555. @end deftypevr
  556. @subsubsection Entity Headers
  557. Entity headers may be present in any HTTP message, and refer to the
  558. resource referenced in the HTTP request or response.
  559. @deftypevr {HTTP Header} List allow
  560. A list of allowed methods on a given resource, as symbols.
  561. @example
  562. (parse-header 'allow "GET, HEAD")
  563. @result{} (GET HEAD)
  564. @end example
  565. @end deftypevr
  566. @deftypevr {HTTP Header} List content-encoding
  567. A list of content codings, as symbols.
  568. @example
  569. (parse-header 'content-encoding "gzip")
  570. @result{} (gzip)
  571. @end example
  572. @end deftypevr
  573. @deftypevr {HTTP Header} List content-language
  574. The languages that a resource is in, as strings.
  575. @example
  576. (parse-header 'content-language "en")
  577. @result{} ("en")
  578. @end example
  579. @end deftypevr
  580. @deftypevr {HTTP Header} UInt content-length
  581. The number of bytes in a resource, as an exact, non-negative integer.
  582. @example
  583. (parse-header 'content-length "300")
  584. @result{} 300
  585. @end example
  586. @end deftypevr
  587. @deftypevr {HTTP Header} URI content-location
  588. The canonical URI for a resource, in the case that it is also accessible
  589. from a different URI.
  590. @example
  591. (parse-header 'content-location "http://example.com/foo")
  592. @result{} #<<uri> ...>
  593. @end example
  594. @end deftypevr
  595. @deftypevr {HTTP Header} String content-md5
  596. The MD5 digest of a resource.
  597. @example
  598. (parse-header 'content-md5 "ffaea1a79810785575e29e2bd45e2fa5")
  599. @result{} "ffaea1a79810785575e29e2bd45e2fa5"
  600. @end example
  601. @end deftypevr
  602. @deftypevr {HTTP Header} List content-range
  603. A range specification, as a list of three elements: the symbol
  604. @code{bytes}, either the symbol @code{*} or a pair of integers,
  605. indicating the byte rage, and either @code{*} or an integer, for the
  606. instance length. Used to indicate that a response only includes part of
  607. a resource.
  608. @example
  609. (parse-header 'content-range "bytes 10-20/*")
  610. @result{} (bytes (10 . 20) *)
  611. @end example
  612. @end deftypevr
  613. @deftypevr {HTTP Header} List content-type
  614. The MIME type of a resource, as a symbol, along with any parameters.
  615. @example
  616. (parse-header 'content-type "text/plain")
  617. @result{} (text/plain)
  618. (parse-header 'content-type "text/plain;charset=utf-8")
  619. @result{} (text/plain (charset . "utf-8"))
  620. @end example
  621. Note that the @code{charset} parameter is something is a misnomer, and
  622. the HTTP specification admits this. It specifies the @emph{encoding} of
  623. the characters, not the character set.
  624. @end deftypevr
  625. @deftypevr {HTTP Header} Date expires
  626. The date/time after which the resource given in a response is considered
  627. stale.
  628. @example
  629. (parse-header 'expires "Tue, 15 Nov 1994 08:12:31 GMT")
  630. @result{} #<date ...>
  631. @end example
  632. @end deftypevr
  633. @deftypevr {HTTP Header} Date last-modified
  634. The date/time on which the resource given in a response was last
  635. modified.
  636. @example
  637. (parse-header 'expires "Tue, 15 Nov 1994 08:12:31 GMT")
  638. @result{} #<date ...>
  639. @end example
  640. @end deftypevr
  641. @subsubsection Request Headers
  642. Request headers may only appear in an HTTP request, not in a response.
  643. @deftypevr {HTTP Header} List accept
  644. A list of preferred media types for a response. Each element of the
  645. list is itself a list, in the same format as @code{content-type}.
  646. @example
  647. (parse-header 'accept "text/html,text/plain;charset=utf-8")
  648. @result{} ((text/html) (text/plain (charset . "utf-8")))
  649. @end example
  650. Preference is expressed with quality values:
  651. @example
  652. (parse-header 'accept "text/html;q=0.8,text/plain;q=0.6")
  653. @result{} ((text/html (q . 800)) (text/plain (q . 600)))
  654. @end example
  655. @end deftypevr
  656. @deftypevr {HTTP Header} QList accept-charset
  657. A quality list of acceptable charsets. Note again that what HTTP calls
  658. a ``charset'' is what Guile calls a ``character encoding''.
  659. @example
  660. (parse-header 'accept-charset "iso-8859-5, unicode-1-1;q=0.8")
  661. @result{} ((1000 . "iso-8859-5") (800 . "unicode-1-1"))
  662. @end example
  663. @end deftypevr
  664. @deftypevr {HTTP Header} QList accept-encoding
  665. A quality list of acceptable content codings.
  666. @example
  667. (parse-header 'accept-encoding "gzip,identity=0.8")
  668. @result{} ((1000 . "gzip") (800 . "identity"))
  669. @end example
  670. @end deftypevr
  671. @deftypevr {HTTP Header} QList accept-language
  672. A quality list of acceptable languages.
  673. @example
  674. (parse-header 'accept-language "cn,en=0.75")
  675. @result{} ((1000 . "cn") (750 . "en"))
  676. @end example
  677. @end deftypevr
  678. @deftypevr {HTTP Header} Pair authorization
  679. Authorization credentials. The car of the pair indicates the
  680. authentication scheme, like @code{basic}. For basic authentication, the
  681. cdr of the pair will be the base64-encoded @samp{@var{user}:@var{pass}}
  682. string. For other authentication schemes, like @code{digest}, the cdr
  683. will be a key-value list of credentials.
  684. @example
  685. (parse-header 'authorization "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
  686. @result{} (basic . "QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
  687. @end example
  688. @end deftypevr
  689. @deftypevr {HTTP Header} List expect
  690. A list of expectations that a client has of a server. The expectations
  691. are key-value lists.
  692. @example
  693. (parse-header 'expect "100-continue")
  694. @result{} ((100-continue))
  695. @end example
  696. @end deftypevr
  697. @deftypevr {HTTP Header} String from
  698. The email address of a user making an HTTP request.
  699. @example
  700. (parse-header 'from "bob@@example.com")
  701. @result{} "bob@@example.com"
  702. @end example
  703. @end deftypevr
  704. @deftypevr {HTTP Header} Pair host
  705. The host for the resource being requested, as a hostname-port pair. If
  706. no port is given, the port is @code{#f}.
  707. @example
  708. (parse-header 'host "gnu.org:80")
  709. @result{} ("gnu.org" . 80)
  710. (parse-header 'host "gnu.org")
  711. @result{} ("gnu.org" . #f)
  712. @end example
  713. @end deftypevr
  714. @deftypevr {HTTP Header} *|List if-match
  715. A set of etags, indicating that the request should proceed if and only
  716. if the etag of the resource is in that set. Either the symbol @code{*},
  717. indicating any etag, or a list of entity tags.
  718. @example
  719. (parse-header 'if-match "*")
  720. @result{} *
  721. (parse-header 'if-match "asdfadf")
  722. @result{} (("asdfadf" . #t))
  723. (parse-header 'if-match W/"asdfadf")
  724. @result{} (("asdfadf" . #f))
  725. @end example
  726. @end deftypevr
  727. @deftypevr {HTTP Header} Date if-modified-since
  728. Indicates that a response should proceed if and only if the resource has
  729. been modified since the given date.
  730. @example
  731. (parse-header 'if-modified-since "Tue, 15 Nov 1994 08:12:31 GMT")
  732. @result{} #<date ...>
  733. @end example
  734. @end deftypevr
  735. @deftypevr {HTTP Header} *|List if-none-match
  736. A set of etags, indicating that the request should proceed if and only
  737. if the etag of the resource is not in the set. Either the symbol
  738. @code{*}, indicating any etag, or a list of entity tags.
  739. @example
  740. (parse-header 'if-none-match "*")
  741. @result{} *
  742. @end example
  743. @end deftypevr
  744. @deftypevr {HTTP Header} ETag|Date if-range
  745. Indicates that the range request should proceed if and only if the
  746. resource matches a modification date or an etag. Either an entity tag,
  747. or a SRFI-19 date.
  748. @example
  749. (parse-header 'if-range "\"original-etag\"")
  750. @result{} ("original-etag" . #t)
  751. @end example
  752. @end deftypevr
  753. @deftypevr {HTTP Header} Date if-unmodified-since
  754. Indicates that a response should proceed if and only if the resource has
  755. not been modified since the given date.
  756. @example
  757. (parse-header 'if-not-modified-since "Tue, 15 Nov 1994 08:12:31 GMT")
  758. @result{} #<date ...>
  759. @end example
  760. @end deftypevr
  761. @deftypevr {HTTP Header} UInt max-forwards
  762. The maximum number of proxy or gateway hops that a request should be
  763. subject to.
  764. @example
  765. (parse-header 'max-forwards "10")
  766. @result{} 10
  767. @end example
  768. @end deftypevr
  769. @deftypevr {HTTP Header} Pair proxy-authorization
  770. Authorization credentials for a proxy connection. See the documentation
  771. for @code{authorization} above for more information on the format.
  772. @example
  773. (parse-header 'proxy-authorization "Digest foo=bar,baz=qux"
  774. @result{} (digest (foo . "bar") (baz . "qux"))
  775. @end example
  776. @end deftypevr
  777. @deftypevr {HTTP Header} Pair range
  778. A range request, indicating that the client wants only part of a
  779. resource. The car of the pair is the symbol @code{bytes}, and the cdr
  780. is a list of pairs. Each element of the cdr indicates a range; the car
  781. is the first byte position and the cdr is the last byte position, as
  782. integers, or @code{#f} if not given.
  783. @example
  784. (parse-header 'range "bytes=10-30,50-")
  785. @result{} (bytes (10 . 30) (50 . #f))
  786. @end example
  787. @end deftypevr
  788. @deftypevr {HTTP Header} URI referer
  789. The URI of the resource that referred the user to this resource. The
  790. name of the header is a misspelling, but we are stuck with it.
  791. @example
  792. (parse-header 'referer "http://www.gnu.org/")
  793. @result{} #<uri ...>
  794. @end example
  795. @end deftypevr
  796. @deftypevr {HTTP Header} List te
  797. A list of transfer codings, expressed as key-value lists. A common
  798. transfer coding is @code{trailers}.
  799. @example
  800. (parse-header 'te "trailers")
  801. @result{} ((trailers))
  802. @end example
  803. @end deftypevr
  804. @deftypevr {HTTP Header} String user-agent
  805. A string indicating the user agent making the request. The
  806. specification defines a structured format for this header, but it is
  807. widely disregarded, so Guile does not attempt to parse strictly.
  808. @example
  809. (parse-header 'user-agent "Mozilla/5.0")
  810. @result{} "Mozilla/5.0"
  811. @end example
  812. @end deftypevr
  813. @subsubsection Response Headers
  814. @deftypevr {HTTP Header} List accept-ranges
  815. A list of range units that the server supports, as symbols.
  816. @example
  817. (parse-header 'accept-ranges "bytes")
  818. @result{} (bytes)
  819. @end example
  820. @end deftypevr
  821. @deftypevr {HTTP Header} UInt age
  822. The age of a cached response, in seconds.
  823. @example
  824. (parse-header 'age "3600")
  825. @result{} 3600
  826. @end example
  827. @end deftypevr
  828. @deftypevr {HTTP Header} ETag etag
  829. The entity-tag of the resource.
  830. @example
  831. (parse-header 'etag "\"foo\"")
  832. @result{} ("foo" . #t)
  833. @end example
  834. @end deftypevr
  835. @deftypevr {HTTP Header} URI-reference location
  836. A URI reference on which a request may be completed. Used in
  837. combination with a redirecting status code to perform client-side
  838. redirection.
  839. @example
  840. (parse-header 'location "http://example.com/other")
  841. @result{} #<uri ...>
  842. @end example
  843. @end deftypevr
  844. @deftypevr {HTTP Header} List proxy-authenticate
  845. A list of challenges to a proxy, indicating the need for authentication.
  846. @example
  847. (parse-header 'proxy-authenticate "Basic realm=\"foo\"")
  848. @result{} ((basic (realm . "foo")))
  849. @end example
  850. @end deftypevr
  851. @deftypevr {HTTP Header} UInt|Date retry-after
  852. Used in combination with a server-busy status code, like 503, to
  853. indicate that a client should retry later. Either a number of seconds,
  854. or a date.
  855. @example
  856. (parse-header 'retry-after "60")
  857. @result{} 60
  858. @end example
  859. @end deftypevr
  860. @deftypevr {HTTP Header} String server
  861. A string identifying the server.
  862. @example
  863. (parse-header 'server "My first web server")
  864. @result{} "My first web server"
  865. @end example
  866. @end deftypevr
  867. @deftypevr {HTTP Header} *|List vary
  868. A set of request headers that were used in computing this response.
  869. Used to indicate that server-side content negotiation was performed, for
  870. example in response to the @code{accept-language} header. Can also be
  871. the symbol @code{*}, indicating that all headers were considered.
  872. @example
  873. (parse-header 'vary "Accept-Language, Accept")
  874. @result{} (accept-language accept)
  875. @end example
  876. @end deftypevr
  877. @deftypevr {HTTP Header} List www-authenticate
  878. A list of challenges to a user, indicating the need for authentication.
  879. @example
  880. (parse-header 'www-authenticate "Basic realm=\"foo\"")
  881. @result{} ((basic (realm . "foo")))
  882. @end example
  883. @end deftypevr
  884. @node Transfer Codings
  885. @subsection Transfer Codings
  886. HTTP 1.1 allows for various transfer codings to be applied to message
  887. bodies. These include various types of compression, and HTTP chunked
  888. encoding. Currently, only chunked encoding is supported by guile.
  889. Chunked coding is an optional coding that may be applied to message
  890. bodies, to allow messages whose length is not known beforehand to be
  891. returned. Such messages can be split into chunks, terminated by a final
  892. zero length chunk.
  893. In order to make dealing with encodings more simple, guile provides
  894. procedures to create ports that ``wrap'' existing ports, applying
  895. transformations transparently under the hood.
  896. These procedures are in the @code{(web http)} module.
  897. @example
  898. (use-modules (web http))
  899. @end example
  900. @deffn {Scheme Procedure} make-chunked-input-port port [#:keep-alive?=#f]
  901. Returns a new port, that transparently reads and decodes chunk-encoded
  902. data from @var{port}. If no more chunk-encoded data is available, it
  903. returns the end-of-file object. When the port is closed, @var{port} will
  904. also be closed, unless @var{keep-alive?} is true.
  905. @end deffn
  906. @example
  907. (use-modules (ice-9 rdelim))
  908. (define s "5\r\nFirst\r\nA\r\n line\n Sec\r\n8\r\nond line\r\n0\r\n")
  909. (define p (make-chunked-input-port (open-input-string s)))
  910. (read-line s)
  911. @result{} "First line"
  912. (read-line s)
  913. @result{} "Second line"
  914. @end example
  915. @deffn {Scheme Procedure} make-chunked-output-port port [#:keep-alive?=#f]
  916. Returns a new port, which transparently encodes data as chunk-encoded
  917. before writing it to @var{port}. Whenever a write occurs on this port,
  918. it buffers it, until the port is flushed, at which point it writes a
  919. chunk containing all the data written so far. When the port is closed,
  920. the data remaining is written to @var{port}, as is the terminating zero
  921. chunk. It also causes @var{port} to be closed, unless @var{keep-alive?}
  922. is true.
  923. Note. Forcing a chunked output port when there is no data is buffered
  924. does not write a zero chunk, as this would cause the data to be
  925. interpreted incorrectly by the client.
  926. @end deffn
  927. @example
  928. (call-with-output-string
  929. (lambda (out)
  930. (define out* (make-chunked-output-port out #:keep-alive? #t))
  931. (display "first chunk" out*)
  932. (force-output out*)
  933. (force-output out*) ; note this does not write a zero chunk
  934. (display "second chunk" out*)
  935. (close-port out*)))
  936. @result{} "b\r\nfirst chunk\r\nc\r\nsecond chunk\r\n0\r\n"
  937. @end example
  938. @node Requests
  939. @subsection HTTP Requests
  940. @example
  941. (use-modules (web request))
  942. @end example
  943. The request module contains a data type for HTTP requests.
  944. @subsubsection An Important Note on Character Sets
  945. HTTP requests consist of two parts: the request proper, consisting of a
  946. request line and a set of headers, and (optionally) a body. The body
  947. might have a binary content-type, and even in the textual case its
  948. length is specified in bytes, not characters.
  949. Therefore, HTTP is a fundamentally binary protocol. However the request
  950. line and headers are specified to be in a subset of ASCII, so they can
  951. be treated as text, provided that the port's encoding is set to an
  952. ASCII-compatible one-byte-per-character encoding. ISO-8859-1 (latin-1)
  953. is just such an encoding, and happens to be very efficient for Guile.
  954. So what Guile does when reading requests from the wire, or writing them
  955. out, is to set the port's encoding to latin-1, and treating the request
  956. headers as text.
  957. The request body is another issue. For binary data, the data is
  958. probably in a bytevector, so we use the R6RS binary output procedures to
  959. write out the binary payload. Textual data usually has to be written
  960. out to some character encoding, usually UTF-8, and then the resulting
  961. bytevector is written out to the port.
  962. In summary, Guile reads and writes HTTP over latin-1 sockets, without
  963. any loss of generality.
  964. @subsubsection Request API
  965. @deffn {Scheme Procedure} request? obj
  966. @deffnx {Scheme Procedure} request-method request
  967. @deffnx {Scheme Procedure} request-uri request
  968. @deffnx {Scheme Procedure} request-version request
  969. @deffnx {Scheme Procedure} request-headers request
  970. @deffnx {Scheme Procedure} request-meta request
  971. @deffnx {Scheme Procedure} request-port request
  972. A predicate and field accessors for the request type. The fields are as
  973. follows:
  974. @table @code
  975. @item method
  976. The HTTP method, for example, @code{GET}.
  977. @item uri
  978. The URI as a URI record.
  979. @item version
  980. The HTTP version pair, like @code{(1 . 1)}.
  981. @item headers
  982. The request headers, as an alist of parsed values.
  983. @item meta
  984. An arbitrary alist of other data, for example information returned in
  985. the @code{sockaddr} from @code{accept} (@pxref{Network Sockets and
  986. Communication}).
  987. @item port
  988. The port on which to read or write a request body, if any.
  989. @end table
  990. @end deffn
  991. @deffn {Scheme Procedure} read-request port [meta='()]
  992. Read an HTTP request from @var{port}, optionally attaching the given
  993. metadata, @var{meta}.
  994. As a side effect, sets the encoding on @var{port} to ISO-8859-1
  995. (latin-1), so that reading one character reads one byte. See the
  996. discussion of character sets above, for more information.
  997. Note that the body is not part of the request. Once you have read a
  998. request, you may read the body separately, and likewise for writing
  999. requests.
  1000. @end deffn
  1001. @deffn {Scheme Procedure} build-request uri [#:method='GET] @
  1002. [#:version='(1 . 1)] [#:headers='()] [#:port=#f] [#:meta='()] @
  1003. [#:validate-headers?=#t]
  1004. Construct an HTTP request object. If @var{validate-headers?} is true,
  1005. the headers are each run through their respective validators.
  1006. @end deffn
  1007. @deffn {Scheme Procedure} write-request r port
  1008. Write the given HTTP request to @var{port}.
  1009. Return a new request, whose @code{request-port} will continue writing
  1010. on @var{port}, perhaps using some transfer encoding.
  1011. @end deffn
  1012. @deffn {Scheme Procedure} read-request-body r
  1013. Reads the request body from @var{r}, as a bytevector. Return @code{#f}
  1014. if there was no request body.
  1015. @end deffn
  1016. @deffn {Scheme Procedure} write-request-body r bv
  1017. Write @var{bv}, a bytevector, to the port corresponding to the HTTP
  1018. request @var{r}.
  1019. @end deffn
  1020. The various headers that are typically associated with HTTP requests may
  1021. be accessed with these dedicated accessors. @xref{HTTP Headers}, for
  1022. more information on the format of parsed headers.
  1023. @deffn {Scheme Procedure} request-accept request [default='()]
  1024. @deffnx {Scheme Procedure} request-accept-charset request [default='()]
  1025. @deffnx {Scheme Procedure} request-accept-encoding request [default='()]
  1026. @deffnx {Scheme Procedure} request-accept-language request [default='()]
  1027. @deffnx {Scheme Procedure} request-allow request [default='()]
  1028. @deffnx {Scheme Procedure} request-authorization request [default=#f]
  1029. @deffnx {Scheme Procedure} request-cache-control request [default='()]
  1030. @deffnx {Scheme Procedure} request-connection request [default='()]
  1031. @deffnx {Scheme Procedure} request-content-encoding request [default='()]
  1032. @deffnx {Scheme Procedure} request-content-language request [default='()]
  1033. @deffnx {Scheme Procedure} request-content-length request [default=#f]
  1034. @deffnx {Scheme Procedure} request-content-location request [default=#f]
  1035. @deffnx {Scheme Procedure} request-content-md5 request [default=#f]
  1036. @deffnx {Scheme Procedure} request-content-range request [default=#f]
  1037. @deffnx {Scheme Procedure} request-content-type request [default=#f]
  1038. @deffnx {Scheme Procedure} request-date request [default=#f]
  1039. @deffnx {Scheme Procedure} request-expect request [default='()]
  1040. @deffnx {Scheme Procedure} request-expires request [default=#f]
  1041. @deffnx {Scheme Procedure} request-from request [default=#f]
  1042. @deffnx {Scheme Procedure} request-host request [default=#f]
  1043. @deffnx {Scheme Procedure} request-if-match request [default=#f]
  1044. @deffnx {Scheme Procedure} request-if-modified-since request [default=#f]
  1045. @deffnx {Scheme Procedure} request-if-none-match request [default=#f]
  1046. @deffnx {Scheme Procedure} request-if-range request [default=#f]
  1047. @deffnx {Scheme Procedure} request-if-unmodified-since request [default=#f]
  1048. @deffnx {Scheme Procedure} request-last-modified request [default=#f]
  1049. @deffnx {Scheme Procedure} request-max-forwards request [default=#f]
  1050. @deffnx {Scheme Procedure} request-pragma request [default='()]
  1051. @deffnx {Scheme Procedure} request-proxy-authorization request [default=#f]
  1052. @deffnx {Scheme Procedure} request-range request [default=#f]
  1053. @deffnx {Scheme Procedure} request-referer request [default=#f]
  1054. @deffnx {Scheme Procedure} request-te request [default=#f]
  1055. @deffnx {Scheme Procedure} request-trailer request [default='()]
  1056. @deffnx {Scheme Procedure} request-transfer-encoding request [default='()]
  1057. @deffnx {Scheme Procedure} request-upgrade request [default='()]
  1058. @deffnx {Scheme Procedure} request-user-agent request [default=#f]
  1059. @deffnx {Scheme Procedure} request-via request [default='()]
  1060. @deffnx {Scheme Procedure} request-warning request [default='()]
  1061. Return the given request header, or @var{default} if none was present.
  1062. @end deffn
  1063. @deffn {Scheme Procedure} request-absolute-uri r [default-host=#f] @
  1064. [default-port=#f] [default-scheme=#f]
  1065. A helper routine to determine the absolute URI of a request, using the
  1066. @code{host} header and the default scheme, host and port. If there is
  1067. no default scheme and the URI is not itself absolute, an error is
  1068. signalled.
  1069. @end deffn
  1070. @node Responses
  1071. @subsection HTTP Responses
  1072. @example
  1073. (use-modules (web response))
  1074. @end example
  1075. As with requests (@pxref{Requests}), Guile offers a data type for HTTP
  1076. responses. Again, the body is represented separately from the request.
  1077. @deffn {Scheme Procedure} response? obj
  1078. @deffnx {Scheme Procedure} response-version response
  1079. @deffnx {Scheme Procedure} response-code response
  1080. @deffnx {Scheme Procedure} response-reason-phrase response
  1081. @deffnx {Scheme Procedure} response-headers response
  1082. @deffnx {Scheme Procedure} response-port response
  1083. A predicate and field accessors for the response type. The fields are as
  1084. follows:
  1085. @table @code
  1086. @item version
  1087. The HTTP version pair, like @code{(1 . 1)}.
  1088. @item code
  1089. The HTTP response code, like @code{200}.
  1090. @item reason-phrase
  1091. The reason phrase, or the standard reason phrase for the response's
  1092. code.
  1093. @item headers
  1094. The response headers, as an alist of parsed values.
  1095. @item port
  1096. The port on which to read or write a response body, if any.
  1097. @end table
  1098. @end deffn
  1099. @deffn {Scheme Procedure} read-response port
  1100. Read an HTTP response from @var{port}.
  1101. As a side effect, sets the encoding on @var{port} to ISO-8859-1
  1102. (latin-1), so that reading one character reads one byte. See the
  1103. discussion of character sets in @ref{Responses}, for more information.
  1104. @end deffn
  1105. @deffn {Scheme Procedure} build-response [#:version='(1 . 1)] [#:code=200] [#:reason-phrase=#f] [#:headers='()] [#:port=#f] [#:validate-headers?=#t]
  1106. Construct an HTTP response object. If @var{validate-headers?} is true,
  1107. the headers are each run through their respective validators.
  1108. @end deffn
  1109. @deffn {Scheme Procedure} adapt-response-version response version
  1110. Adapt the given response to a different HTTP version. Return a new HTTP
  1111. response.
  1112. The idea is that many applications might just build a response for the
  1113. default HTTP version, and this method could handle a number of
  1114. programmatic transformations to respond to older HTTP versions (0.9 and
  1115. 1.0). But currently this function is a bit heavy-handed, just updating
  1116. the version field.
  1117. @end deffn
  1118. @deffn {Scheme Procedure} write-response r port
  1119. Write the given HTTP response to @var{port}.
  1120. Return a new response, whose @code{response-port} will continue writing
  1121. on @var{port}, perhaps using some transfer encoding.
  1122. @end deffn
  1123. @deffn {Scheme Procedure} response-must-not-include-body? r
  1124. Some responses, like those with status code 304, are specified as never
  1125. having bodies. This predicate returns @code{#t} for those responses.
  1126. Note also, though, that responses to @code{HEAD} requests must also not
  1127. have a body.
  1128. @end deffn
  1129. @deffn {Scheme Procedure} response-body-port r [#:decode?=#t] [#:keep-alive?=#t]
  1130. Return an input port from which the body of @var{r} can be read. The encoding
  1131. of the returned port is set according to @var{r}'s @code{content-type} header,
  1132. when it's textual, except if @var{decode?} is @code{#f}. Return @code{#f}
  1133. when no body is available.
  1134. When @var{keep-alive?} is @code{#f}, closing the returned port also closes
  1135. @var{r}'s response port.
  1136. @end deffn
  1137. @deffn {Scheme Procedure} read-response-body r
  1138. Read the response body from @var{r}, as a bytevector. Returns @code{#f}
  1139. if there was no response body.
  1140. @end deffn
  1141. @deffn {Scheme Procedure} write-response-body r bv
  1142. Write @var{bv}, a bytevector, to the port corresponding to the HTTP
  1143. response @var{r}.
  1144. @end deffn
  1145. As with requests, the various headers that are typically associated with
  1146. HTTP responses may be accessed with these dedicated accessors.
  1147. @xref{HTTP Headers}, for more information on the format of parsed
  1148. headers.
  1149. @deffn {Scheme Procedure} response-accept-ranges response [default=#f]
  1150. @deffnx {Scheme Procedure} response-age response [default='()]
  1151. @deffnx {Scheme Procedure} response-allow response [default='()]
  1152. @deffnx {Scheme Procedure} response-cache-control response [default='()]
  1153. @deffnx {Scheme Procedure} response-connection response [default='()]
  1154. @deffnx {Scheme Procedure} response-content-encoding response [default='()]
  1155. @deffnx {Scheme Procedure} response-content-language response [default='()]
  1156. @deffnx {Scheme Procedure} response-content-length response [default=#f]
  1157. @deffnx {Scheme Procedure} response-content-location response [default=#f]
  1158. @deffnx {Scheme Procedure} response-content-md5 response [default=#f]
  1159. @deffnx {Scheme Procedure} response-content-range response [default=#f]
  1160. @deffnx {Scheme Procedure} response-content-type response [default=#f]
  1161. @deffnx {Scheme Procedure} response-date response [default=#f]
  1162. @deffnx {Scheme Procedure} response-etag response [default=#f]
  1163. @deffnx {Scheme Procedure} response-expires response [default=#f]
  1164. @deffnx {Scheme Procedure} response-last-modified response [default=#f]
  1165. @deffnx {Scheme Procedure} response-location response [default=#f]
  1166. @deffnx {Scheme Procedure} response-pragma response [default='()]
  1167. @deffnx {Scheme Procedure} response-proxy-authenticate response [default=#f]
  1168. @deffnx {Scheme Procedure} response-retry-after response [default=#f]
  1169. @deffnx {Scheme Procedure} response-server response [default=#f]
  1170. @deffnx {Scheme Procedure} response-trailer response [default='()]
  1171. @deffnx {Scheme Procedure} response-transfer-encoding response [default='()]
  1172. @deffnx {Scheme Procedure} response-upgrade response [default='()]
  1173. @deffnx {Scheme Procedure} response-vary response [default='()]
  1174. @deffnx {Scheme Procedure} response-via response [default='()]
  1175. @deffnx {Scheme Procedure} response-warning response [default='()]
  1176. @deffnx {Scheme Procedure} response-www-authenticate response [default=#f]
  1177. Return the given response header, or @var{default} if none was present.
  1178. @end deffn
  1179. @deffn {Scheme Procedure} text-content-type? @var{type}
  1180. Return @code{#t} if @var{type}, a symbol as returned by
  1181. @code{response-content-type}, represents a textual type such as
  1182. @code{text/plain}.
  1183. @end deffn
  1184. @node Web Client
  1185. @subsection Web Client
  1186. @code{(web client)} provides a simple, synchronous HTTP client, built on
  1187. the lower-level HTTP, request, and response modules.
  1188. @example
  1189. (use-modules (web client))
  1190. @end example
  1191. @deffn {Scheme Procedure} open-socket-for-uri uri
  1192. Return an open input/output port for a connection to URI. Guile
  1193. dynamically loads GnuTLS for HTTPS support.
  1194. @xref{Guile Preparations,
  1195. how to install the GnuTLS bindings for Guile,, gnutls-guile,
  1196. GnuTLS-Guile}, for more information.
  1197. @end deffn
  1198. @deffn {Scheme Procedure} http-get uri arg...
  1199. @deffnx {Scheme Procedure} http-head uri arg...
  1200. @deffnx {Scheme Procedure} http-post uri arg...
  1201. @deffnx {Scheme Procedure} http-put uri arg...
  1202. @deffnx {Scheme Procedure} http-delete uri arg...
  1203. @deffnx {Scheme Procedure} http-trace uri arg...
  1204. @deffnx {Scheme Procedure} http-options uri arg...
  1205. Connect to the server corresponding to @var{uri} and make a request over
  1206. HTTP, using the appropriate method (@code{GET}, @code{HEAD}, etc.).
  1207. All of these procedures have the same prototype: a URI followed by an
  1208. optional sequence of keyword arguments. These keyword arguments allow
  1209. you to modify the requests in various ways, for example attaching a body
  1210. to the request, or setting specific headers. The following table lists
  1211. the keyword arguments and their default values.
  1212. @table @code
  1213. @item #:body #f
  1214. @item #:port (open-socket-for-uri @var{uri})]
  1215. @item #:version '(1 . 1)
  1216. @item #:keep-alive? #f
  1217. @item #:headers '()
  1218. @item #:decode-body? #t
  1219. @item #:streaming? #f
  1220. @end table
  1221. If you already have a port open, pass it as @var{port}. Otherwise, a
  1222. connection will be opened to the server corresponding to @var{uri}. Any
  1223. extra headers in the alist @var{headers} will be added to the request.
  1224. If @var{body} is not @code{#f}, a message body will also be sent with
  1225. the HTTP request. If @var{body} is a string, it is encoded according to
  1226. the content-type in @var{headers}, defaulting to UTF-8. Otherwise
  1227. @var{body} should be a bytevector, or @code{#f} for no body. Although a
  1228. message body may be sent with any request, usually only @code{POST} and
  1229. @code{PUT} requests have bodies.
  1230. If @var{decode-body?} is true, as is the default, the body of the
  1231. response will be decoded to string, if it is a textual content-type.
  1232. Otherwise it will be returned as a bytevector.
  1233. However, if @var{streaming?} is true, instead of eagerly reading the
  1234. response body from the server, this function only reads off the headers.
  1235. The response body will be returned as a port on which the data may be
  1236. read.
  1237. Unless @var{keep-alive?} is true, the port will be closed after the full
  1238. response body has been read.
  1239. Returns two values: the response read from the server, and the response
  1240. body as a string, bytevector, #f value, or as a port (if
  1241. @var{streaming?} is true).
  1242. @end deffn
  1243. @code{http-get} is useful for making one-off requests to web sites. If
  1244. you are writing a web spider or some other client that needs to handle a
  1245. number of requests in parallel, it's better to build an event-driven URL
  1246. fetcher, similar in structure to the web server (@pxref{Web Server}).
  1247. Another option, good but not as performant, would be to use threads,
  1248. possibly via par-map or futures.
  1249. @deffn {Scheme Parameter} current-http-proxy
  1250. Either @code{#f} or a non-empty string containing the URL of the HTTP
  1251. proxy server to be used by the procedures in the @code{(web client)}
  1252. module, including @code{open-socket-for-uri}. Its initial value is
  1253. based on the @env{http_proxy} environment variable.
  1254. @example
  1255. (current-http-proxy) @result{} "http://localhost:8123/"
  1256. (parameterize ((current-http-proxy #f))
  1257. (http-get "http://example.com/")) ; temporarily bypass proxy
  1258. (current-http-proxy) @result{} "http://localhost:8123/"
  1259. @end example
  1260. @end deffn
  1261. @node Web Server
  1262. @subsection Web Server
  1263. @code{(web server)} is a generic web server interface, along with a main
  1264. loop implementation for web servers controlled by Guile.
  1265. @example
  1266. (use-modules (web server))
  1267. @end example
  1268. The lowest layer is the @code{<server-impl>} object, which defines a set
  1269. of hooks to open a server, read a request from a client, write a
  1270. response to a client, and close a server. These hooks -- @code{open},
  1271. @code{read}, @code{write}, and @code{close}, respectively -- are bound
  1272. together in a @code{<server-impl>} object. Procedures in this module take a
  1273. @code{<server-impl>} object, if needed.
  1274. A @code{<server-impl>} may also be looked up by name. If you pass the
  1275. @code{http} symbol to @code{run-server}, Guile looks for a variable
  1276. named @code{http} in the @code{(web server http)} module, which should
  1277. be bound to a @code{<server-impl>} object. Such a binding is made by
  1278. instantiation of the @code{define-server-impl} syntax. In this way the
  1279. run-server loop can automatically load other backends if available.
  1280. The life cycle of a server goes as follows:
  1281. @enumerate
  1282. @item
  1283. The @code{open} hook is called, to open the server. @code{open} takes
  1284. zero or more arguments, depending on the backend, and returns an opaque
  1285. server socket object, or signals an error.
  1286. @item
  1287. The @code{read} hook is called, to read a request from a new client.
  1288. The @code{read} hook takes one argument, the server socket. It should
  1289. return three values: an opaque client socket, the request, and the
  1290. request body. The request should be a @code{<request>} object, from
  1291. @code{(web request)}. The body should be a string or a bytevector, or
  1292. @code{#f} if there is no body.
  1293. If the read failed, the @code{read} hook may return #f for the client
  1294. socket, request, and body.
  1295. @item
  1296. A user-provided handler procedure is called, with the request and body
  1297. as its arguments. The handler should return two values: the response,
  1298. as a @code{<response>} record from @code{(web response)}, and the
  1299. response body as bytevector, or @code{#f} if not present.
  1300. The respose and response body are run through @code{sanitize-response},
  1301. documented below. This allows the handler writer to take some
  1302. convenient shortcuts: for example, instead of a @code{<response>}, the
  1303. handler can simply return an alist of headers, in which case a default
  1304. response object is constructed with those headers. Instead of a
  1305. bytevector for the body, the handler can return a string, which will be
  1306. serialized into an appropriate encoding; or it can return a procedure,
  1307. which will be called on a port to write out the data. See the
  1308. @code{sanitize-response} documentation, for more.
  1309. @item
  1310. The @code{write} hook is called with three arguments: the client
  1311. socket, the response, and the body. The @code{write} hook returns no
  1312. values.
  1313. @item
  1314. At this point the request handling is complete. For a loop, we
  1315. loop back and try to read a new request.
  1316. @item
  1317. If the user interrupts the loop, the @code{close} hook is called on
  1318. the server socket.
  1319. @end enumerate
  1320. A user may define a server implementation with the following form:
  1321. @deffn {Scheme Syntax} define-server-impl name open read write close
  1322. Make a @code{<server-impl>} object with the hooks @var{open},
  1323. @var{read}, @var{write}, and @var{close}, and bind it to the symbol
  1324. @var{name} in the current module.
  1325. @end deffn
  1326. @deffn {Scheme Procedure} lookup-server-impl impl
  1327. Look up a server implementation. If @var{impl} is a server
  1328. implementation already, it is returned directly. If it is a symbol, the
  1329. binding named @var{impl} in the @code{(web server @var{impl})} module is
  1330. looked up. Otherwise an error is signaled.
  1331. Currently a server implementation is a somewhat opaque type, useful only
  1332. for passing to other procedures in this module, like @code{read-client}.
  1333. @end deffn
  1334. The @code{(web server)} module defines a number of routines that use
  1335. @code{<server-impl>} objects to implement parts of a web server. Given
  1336. that we don't expose the accessors for the various fields of a
  1337. @code{<server-impl>}, indeed these routines are the only procedures with
  1338. any access to the impl objects.
  1339. @deffn {Scheme Procedure} open-server impl open-params
  1340. Open a server for the given implementation. Return one value, the new
  1341. server object. The implementation's @code{open} procedure is applied to
  1342. @var{open-params}, which should be a list.
  1343. @end deffn
  1344. @deffn {Scheme Procedure} read-client impl server
  1345. Read a new client from @var{server}, by applying the implementation's
  1346. @code{read} procedure to the server. If successful, return three
  1347. values: an object corresponding to the client, a request object, and the
  1348. request body. If any exception occurs, return @code{#f} for all three
  1349. values.
  1350. @end deffn
  1351. @deffn {Scheme Procedure} handle-request handler request body state
  1352. Handle a given request, returning the response and body.
  1353. The response and response body are produced by calling the given
  1354. @var{handler} with @var{request} and @var{body} as arguments.
  1355. The elements of @var{state} are also passed to @var{handler} as
  1356. arguments, and may be returned as additional values. The new
  1357. @var{state}, collected from the @var{handler}'s return values, is then
  1358. returned as a list. The idea is that a server loop receives a handler
  1359. from the user, along with whatever state values the user is interested
  1360. in, allowing the user's handler to explicitly manage its state.
  1361. @end deffn
  1362. @deffn {Scheme Procedure} sanitize-response request response body
  1363. ``Sanitize'' the given response and body, making them appropriate for
  1364. the given request.
  1365. As a convenience to web handler authors, @var{response} may be given as
  1366. an alist of headers, in which case it is used to construct a default
  1367. response. Ensures that the response version corresponds to the request
  1368. version. If @var{body} is a string, encodes the string to a bytevector,
  1369. in an encoding appropriate for @var{response}. Adds a
  1370. @code{content-length} and @code{content-type} header, as necessary.
  1371. If @var{body} is a procedure, it is called with a port as an argument,
  1372. and the output collected as a bytevector. In the future we might try to
  1373. instead use a compressing, chunk-encoded port, and call this procedure
  1374. later, in the write-client procedure. Authors are advised not to rely on
  1375. the procedure being called at any particular time.
  1376. @end deffn
  1377. @deffn {Scheme Procedure} write-client impl server client response body
  1378. Write an HTTP response and body to @var{client}. If the server and
  1379. client support persistent connections, it is the implementation's
  1380. responsibility to keep track of the client thereafter, presumably by
  1381. attaching it to the @var{server} argument somehow.
  1382. @end deffn
  1383. @deffn {Scheme Procedure} close-server impl server
  1384. Release resources allocated by a previous invocation of
  1385. @code{open-server}.
  1386. @end deffn
  1387. Given the procedures above, it is a small matter to make a web server:
  1388. @deffn {Scheme Procedure} serve-one-client handler impl server state
  1389. Read one request from @var{server}, call @var{handler} on the request
  1390. and body, and write the response to the client. Return the new state
  1391. produced by the handler procedure.
  1392. @end deffn
  1393. @deffn {Scheme Procedure} run-server handler @
  1394. [impl='http] [open-params='()] @
  1395. arg @dots{}
  1396. Run Guile's built-in web server.
  1397. @var{handler} should be a procedure that takes two or more arguments,
  1398. the HTTP request and request body, and returns two or more values, the
  1399. response and response body.
  1400. For examples, skip ahead to the next section, @ref{Web Examples}.
  1401. The response and body will be run through @code{sanitize-response}
  1402. before sending back to the client.
  1403. Additional arguments to @var{handler} are taken from @var{arg}
  1404. @enddots{}. These arguments comprise a @dfn{state}. Additional return
  1405. values are accumulated into a new state, which will be used for
  1406. subsequent requests. In this way a handler can explicitly manage its
  1407. state.
  1408. @end deffn
  1409. The default web server implementation is @code{http}, which binds to a
  1410. socket, listening for request on that port.
  1411. @deffn {HTTP Implementation} http [#:host=#f] @
  1412. [#:family=AF_INET] @
  1413. [#:addr=INADDR_LOOPBACK] @
  1414. [#:port 8080] [#:socket]
  1415. The default HTTP implementation. We document it as a function with
  1416. keyword arguments, because that is precisely the way that it is -- all
  1417. of the @var{open-params} to @code{run-server} get passed to the
  1418. implementation's open function.
  1419. @example
  1420. ;; The defaults: localhost:8080
  1421. (run-server handler)
  1422. ;; Same thing
  1423. (run-server handler 'http '())
  1424. ;; On a different port
  1425. (run-server handler 'http '(#:port 8081))
  1426. ;; IPv6
  1427. (run-server handler 'http '(#:family AF_INET6 #:port 8081))
  1428. ;; Custom socket
  1429. (run-server handler 'http `(#:socket ,(sudo-make-me-a-socket)))
  1430. @end example
  1431. @end deffn
  1432. @node Web Examples
  1433. @subsection Web Examples
  1434. Well, enough about the tedious internals. Let's make a web application!
  1435. @subsubsection Hello, World!
  1436. The first program we have to write, of course, is ``Hello, World!''.
  1437. This means that we have to implement a web handler that does what we
  1438. want.
  1439. Now we define a handler, a function of two arguments and two return
  1440. values:
  1441. @example
  1442. (define (handler request request-body)
  1443. (values @var{response} @var{response-body}))
  1444. @end example
  1445. In this first example, we take advantage of a short-cut, returning an
  1446. alist of headers instead of a proper response object. The response body
  1447. is our payload:
  1448. @example
  1449. (define (hello-world-handler request request-body)
  1450. (values '((content-type . (text/plain)))
  1451. "Hello World!"))
  1452. @end example
  1453. Now let's test it, by running a server with this handler. Load up the
  1454. web server module if you haven't yet done so, and run a server with this
  1455. handler:
  1456. @example
  1457. (use-modules (web server))
  1458. (run-server hello-world-handler)
  1459. @end example
  1460. By default, the web server listens for requests on
  1461. @code{localhost:8080}. Visit that address in your web browser to
  1462. test. If you see the string, @code{Hello World!}, sweet!
  1463. @subsubsection Inspecting the Request
  1464. The Hello World program above is a general greeter, responding to all
  1465. URIs. To make a more exclusive greeter, we need to inspect the request
  1466. object, and conditionally produce different results. So let's load up
  1467. the request, response, and URI modules, and do just that.
  1468. @example
  1469. (use-modules (web server)) ; you probably did this already
  1470. (use-modules (web request)
  1471. (web response)
  1472. (web uri))
  1473. (define (request-path-components request)
  1474. (split-and-decode-uri-path (uri-path (request-uri request))))
  1475. (define (hello-hacker-handler request body)
  1476. (if (equal? (request-path-components request)
  1477. '("hacker"))
  1478. (values '((content-type . (text/plain)))
  1479. "Hello hacker!")
  1480. (not-found request)))
  1481. (run-server hello-hacker-handler)
  1482. @end example
  1483. Here we see that we have defined a helper to return the components of
  1484. the URI path as a list of strings, and used that to check for a request
  1485. to @code{/hacker/}. Then the success case is just as before -- visit
  1486. @code{http://localhost:8080/hacker/} in your browser to check.
  1487. You should always match against URI path components as decoded by
  1488. @code{split-and-decode-uri-path}. The above example will work for
  1489. @code{/hacker/}, @code{//hacker///}, and @code{/h%61ck%65r}.
  1490. But we forgot to define @code{not-found}! If you are pasting these
  1491. examples into a REPL, accessing any other URI in your web browser will
  1492. drop your Guile console into the debugger:
  1493. @example
  1494. <unnamed port>:38:7: In procedure module-lookup:
  1495. <unnamed port>:38:7: Unbound variable: not-found
  1496. Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
  1497. scheme@@(guile-user) [1]>
  1498. @end example
  1499. So let's define the function, right there in the debugger. As you
  1500. probably know, we'll want to return a 404 response.
  1501. @example
  1502. ;; Paste this in your REPL
  1503. (define (not-found request)
  1504. (values (build-response #:code 404)
  1505. (string-append "Resource not found: "
  1506. (uri->string (request-uri request)))))
  1507. ;; Now paste this to let the web server keep going:
  1508. ,continue
  1509. @end example
  1510. Now if you access @code{http://localhost/foo/}, you get this error
  1511. message. (Note that some popular web browsers won't show
  1512. server-generated 404 messages, showing their own instead, unless the 404
  1513. message body is long enough.)
  1514. @subsubsection Higher-Level Interfaces
  1515. The web handler interface is a common baseline that all kinds of Guile
  1516. web applications can use. You will usually want to build something on
  1517. top of it, however, especially when producing HTML. Here is a simple
  1518. example that builds up HTML output using SXML (@pxref{SXML}).
  1519. First, load up the modules:
  1520. @example
  1521. (use-modules (web server)
  1522. (web request)
  1523. (web response)
  1524. (sxml simple))
  1525. @end example
  1526. Now we define a simple templating function that takes a list of HTML
  1527. body elements, as SXML, and puts them in our super template:
  1528. @example
  1529. (define (templatize title body)
  1530. `(html (head (title ,title))
  1531. (body ,@@body)))
  1532. @end example
  1533. For example, the simplest Hello HTML can be produced like this:
  1534. @example
  1535. (sxml->xml (templatize "Hello!" '((b "Hi!"))))
  1536. @print{}
  1537. <html><head><title>Hello!</title></head><body><b>Hi!</b></body></html>
  1538. @end example
  1539. Much better to work with Scheme data types than to work with HTML as
  1540. strings. Now we define a little response helper:
  1541. @example
  1542. (define* (respond #:optional body #:key
  1543. (status 200)
  1544. (title "Hello hello!")
  1545. (doctype "<!DOCTYPE html>\n")
  1546. (content-type-params '((charset . "utf-8")))
  1547. (content-type 'text/html)
  1548. (extra-headers '())
  1549. (sxml (and body (templatize title body))))
  1550. (values (build-response
  1551. #:code status
  1552. #:headers `((content-type
  1553. . (,content-type ,@@content-type-params))
  1554. ,@@extra-headers))
  1555. (lambda (port)
  1556. (if sxml
  1557. (begin
  1558. (if doctype (display doctype port))
  1559. (sxml->xml sxml port))))))
  1560. @end example
  1561. Here we see the power of keyword arguments with default initializers. By
  1562. the time the arguments are fully parsed, the @code{sxml} local variable
  1563. will hold the templated SXML, ready for sending out to the client.
  1564. Also, instead of returning the body as a string, @code{respond} gives a
  1565. procedure, which will be called by the web server to write out the
  1566. response to the client.
  1567. Now, a simple example using this responder, which lays out the incoming
  1568. headers in an HTML table.
  1569. @example
  1570. (define (debug-page request body)
  1571. (respond
  1572. `((h1 "hello world!")
  1573. (table
  1574. (tr (th "header") (th "value"))
  1575. ,@@(map (lambda (pair)
  1576. `(tr (td (tt ,(with-output-to-string
  1577. (lambda () (display (car pair))))))
  1578. (td (tt ,(with-output-to-string
  1579. (lambda ()
  1580. (write (cdr pair))))))))
  1581. (request-headers request))))))
  1582. (run-server debug-page)
  1583. @end example
  1584. Now if you visit any local address in your web browser, we actually see
  1585. some HTML, finally.
  1586. @subsubsection Conclusion
  1587. Well, this is about as far as Guile's built-in web support goes, for
  1588. now. There are many ways to make a web application, but hopefully by
  1589. standardizing the most fundamental data types, users will be able to
  1590. choose the approach that suits them best, while also being able to
  1591. switch between implementations of the server. This is a relatively new
  1592. part of Guile, so if you have feedback, let us know, and we can take it
  1593. into account. Happy hacking on the web!
  1594. @c Local Variables:
  1595. @c TeX-master: "guile.texi"
  1596. @c End: