web.texi 75 KB

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