source: project/wiki/eggref/4/intarweb @ 12872

Last change on this file since 12872 was 12872, checked in by sjamaan, 12 years ago

Add some more documentation, so all(?) exported names are documented

File size: 20.0 KB
Line 
1[[tags: egg]]
2
3== Intarweb
4
5[[toc:]]
6
7=== Description
8
9Intarweb is an advanced http library.  It parses all headers into more
10useful Scheme values.
11
12=== Author
13
14[[Peter Bex]]
15
16=== Requirements
17
18Requires the [[defstruct]], [[base64]] and [[uri-generic]] extensions.
19
20=== Documentation
21
22The intarweb egg is set up to be used from a variety of
23situations. For this reason, it does not try to be a full HTTP client
24or server. If you need that kind of functionality, see eggs like
25[[spiffy]].
26
27=== Requests
28
29A request object (a [[defstruct]]-type record) can be created using
30the following procedure:
31
32<procedure>(make-request #!key uri port (method 'GET) (major 1) (minor 1) (headers (make-headers '())))</procedure>
33
34An existing request can be picked apart using the following procedures:
35
36* <procedure>(request-uri REQUEST) => URI</procedure>
37* <procedure>(request-port REQUEST) => PORT</procedure>
38* <procedure>(request-method REQUEST) => SYMBOL</procedure>
39* <procedure>(request-major REQUEST) => NUMBER</procedure>
40* <procedure>(request-minor REQUEST) => NUMBER</procedure>
41* <procedure>(request-headers REQUEST) => HEADERS</procedure>
42
43The uri defines the entity to retrieve on the server, which should be
44a [[uri-generic]]-type URI object. The port is the port where the
45request is written to or read from.  The method is a symbol that
46defines the HTTP method to use (case sensitive). major and minor
47identify the major and minor version of HTTP to use. Currently, 0.9,
481.0 and 1.1 are supported (but be careful with 0.9, it has some weird
49consequences and is not widely supported). Headers must be a headers
50object, which is described below.
51
52The client will generally write requests, while the server will read them.
53To write a request, use the following procedure:
54
55<procedure>(write-request REQUEST) => REQUEST</procedure>
56
57This will write a request line with headers to the server.  In case it
58is a request type that has any body data, this should be written to
59the the request's port. Beware that this port can be modified by
60write-request, so be sure to write to the port as it is returned by
61the write-request procedure!
62
63<procedure>(read-request PORT) => REQUEST</procedure>
64
65Reads a request object from the given input-port.  An optional request
66body can be read from the request-port after calling this procedure.
67
68Requests are parsed using parse procedures, which can be customized
69by overriding this parameter:
70
71<parameter>(request-parsers [LIST])</parameter>
72
73The list is one of procedures which accept a request line string,
74which produce a request object from that, or {{#f}} if the request
75is not of the type handled by that procedure.
76
77The predefined request parsers are the following:
78
79* <procedure>(http-0.9-request-parser STRING) => REQUEST</procedure>
80* <procedure>(http-1.x-request-parser STRING) => REQUEST</procedure>
81
82Requests are written using unparse procedures, which can be
83customized by overriding this parameter:
84
85<parameter>(request-unparsers [LIST])</parameter>
86
87The list is one of procedures which accept a request object and write
88to the request's output port and return the new, possibly updated
89request object. If the request object is not unparsed by this
90handler, it returns {{#f}}.
91
92The predefined request unparsers are the following:
93
94* <procedure>(http-0.9-request-unparser REQUEST) => REQUEST</procedure>
95* <procedure>(http-1.x-request-unparser REQUEST) => REQUEST</procedure>
96
97=== Responses
98
99A response is also a [[defstruct]]-type record, much like a request:
100
101<procedure>(make-response #!key port (code 200) (reason "OK") (major 1) (minor 1) (headers (make-headers '())))</procedure>
102
103An existing response can be picked apart using the following procedures:
104* <procedure>(response-port RESPONSE) => PORT</procedure>
105* <procedure>(response-code RESPONSE) => NUMBER</procedure>
106* <procedure>(response-reason RESPONSE) => STRING</procedure>
107* <procedure>(response-major RESPONSE) => NUMBER</procedure>
108* <procedure>(response-minor RESPONSE) => NUMBER</procedure>
109* <procedure>(response-headers RESPONSE) => HEADERS</procedure>
110
111The port, major, minor and headers are the same as for requests. code
112and reason are an integer status code and the short message that
113belongs to it, as defined in the spec (examples include: 200 OK, 301
114Moved Permanently, etc).
115
116A server will usually write a response, a client will read it.
117To write a response, use the following procedure:
118
119<procedure>(write-response RESPONSE) => RESPONSE</procedure>
120
121If there is a response body, this must be written to the response-port
122after sending the response headers.
123
124<procedure>(read-response PORT) => RESPONSE</procedure>
125
126Reads a response object from the port. An optional response body can
127be read from the response-port after calling this procedure.
128
129Responses are parsed using parse procedures, which can be customized
130by overriding this parameter:
131
132<parameter>(response-parsers [LIST])</parameter>
133
134The list is one of procedures which accept a response line string,
135which produce a response object from that, or {{#f}} if the response
136is not of the type handled by that procedure.
137
138The predefined response parsers are the following:
139
140* <procedure>(http-0.9-response-unparser REQUEST) => REQUEST</procedure>
141* <procedure>(http-1.x-response-unparser REQUEST) => REQUEST</procedure>
142
143Responses are written using unparse procedures, which can be
144customized by overriding this parameter:
145
146<parameter>(response-unparsers [LIST])</parameter>
147
148The list is one of procedures which accept a response object and write
149to the response's output port and return the new, possibly updated
150response object. If the response object is not unparsed by this
151handler, it returns {{#f}}.
152
153The predefined response unparsers are the following:
154
155* <procedure>(http-0.9-response-unparser REQUEST) => REQUEST</procedure>
156* <procedure>(http-1.x-response-unparser REQUEST) => REQUEST</procedure>
157
158=== Headers
159
160Requests and responses contain HTTP headers wrapped in a special
161header-object to ensure they are properly normalized.
162
163<procedure>(headers ALIST [HEADERS]) => HEADERS</procedure>
164
165This creates a header object based on an input list.
166
167<procedure>(headers->list HEADERS) => ALIST</procedure>
168
169This converts the header object back to a list.
170
171The above mentioned lists have header names (symbols) as keys, and
172lists of values as values:
173
174<example>
175<expr>
176(headers `((host ("example.com" . 8080))
177           (accept #(text/html ((q . 0.5)))
178                   #(text/xml ((q . 0.1)))))
179          old-headers)
180</expr>
181</example>
182
183This adds the named headers to the existing headers in
184{{old-headers}}. The host header is either a string with the hostname
185or a pair of hostname/port. The accept header is a list of allowed
186mime-type symbols. As can be seen here, optional parameters or
187"attributes" can be added to a header value by wrapping the value in a
188vector of length 2. The first entry in the vector is the header value,
189the second is an alist of attribute name/value pairs.
190
191To obtain the value of any particular header, you can use
192
193<procedure>(header-values NAME HEADERS) => LIST</procedures>
194
195The name of the header is a symbol, and it will return all the values
196of the header (for example, the Accept header will have several values
197that indicate the set of acceptable mime-types).
198
199If you know in advance that a header has only one value, you can use:
200
201<procedure>(header-value NAME HEADERS [DEFAULT]) => value</procedure>
202
203This will return the first value in the list, or the provided default
204if there is no value for that header.
205
206These are just shortcuts, the underlying procedures to query the raw
207contents of a header are these:
208
209* <procedure>(header-contents NAME HEADERS) => VECTOR</procedure>
210* <procedure>(get-value VECTOR) => value</procedure>
211* <procedure>(get-params VECTOR) => ALIST</procedure>
212* <procedure>(get-param PARAM VECTOR [DEFAULT]) => value</procedure>
213
214Header contents are 2-element vectors; the first value containing the
215value for the header and the second value containing an alist with
216"parameters" for that header value. Parameters are attribute/value
217pairs that define further specialization of a header's value. For
218example, the {{accept}} header consists of a list of mime-types, which
219optionally can have a quality parameter that defines the preference
220for that mime-type.  All parameter names are downcased symbols, just
221like header names.
222
223There are special-purpose procedures for obtaining information about
224specific header parameters when the spec defines certain behaviour for
225them, as well:
226
227* <procedure>(get-quality PARAM VECTOR [DEFAULT]) =>
228value</procedure>
229
230This obtains the value of the {{q}} parameter for the given header
231value, if any, or {{1}} if there is none defined.
232
233==== Header types
234
235The headers all have their own different types.  Here follows a list
236of headers with their value types:
237
238<table>
239<tr><th>Header name</th><th>Value type</th><th>Example value</th></tr>
240<tr>
241<td>{{accept}}</td>
242<td>List of mime-types (symbols), with optional {{q}} attribute
243indicating "quality" (preference level)</td>
244<td>{{(text/html #(text/xml ((q . 0.1))))}}</td>
245</tr>
246<tr>
247<td>{{accept-charset}}</td>
248<td>List of charset-names (symbols), with optional {{q}} attribute</td>
249<td>{{(utf-8 #(iso-8859-5 ((q . 0.1))))}}</td>
250</tr>
251<tr>
252<td>{{accept-encoding}}</td>
253<td>List of encoding-names (symbols), with optional {{q}} attribute</td>
254<td>{{(gzip #(identity ((q . 0))))}}</td>
255</tr>
256<tr>
257<td>{{accept-language}}</td>
258<td>List of language-names (symbols), with optional {{q}} attribute</td>
259<td>{{(en-gb #(nl ((q . 0.5))))}}</td>
260</tr>
261<tr>
262<td>{{accept-ranges}}</td>
263<td>List of range types acceptable (symbols). The spec only defines
264{{bytes}} and {{none}}.</td>
265<td>{{(bytes)}}</td>
266</tr>
267<tr>
268<td>{{age}}</td>
269<td>Age in seconds (number)</td>
270<td>{{(3600)}}</td>
271</tr>
272<tr>
273<td>{{allow}}</td>
274<td>List of methods that are allowed (symbols).</td>
275<td>{{(GET POST PUT DELETE)}}</td>
276</tr>
277<tr>
278<td>{{authorization}}</td>
279<td>Authorization information. This consists of a symbol identifying the
280authentication scheme, with scheme-specific attributes.</td>
281<td>{{(digest #((username . "foo")))}}</td>
282</tr>
283<tr>
284<td>{{cache-control}}</td>
285<td>An alist of key/value pairs. If no value is applicable, it is {{#t}}</td>
286<td>((public . #t) (max-stale . 10) (no-cache . (max-age set-cookie)))</td>
287</tr>
288<tr>
289<td>{{connection}}</td>
290<td>A list of connection options (symbols)</td>
291<td>{{(close)}}</td>
292</tr>
293<tr>
294<td>{{content-encoding}}</td>
295<td>A list of encodings (symbols) applied to the entity-body.</td>
296<td>{{(deflate gzip)}}</td>
297</tr>
298<tr>
299<td>{{content-language}}</td>
300<td>The natural language(s) of the "intended audience" (symbols)</td>
301<td>{{(de nl en-gb)}}</td>
302</tr>
303<tr>
304<td>{{content-length}}</td>
305<td>The number of bytes (an exact number) in the entity-body</td>
306<td>{{(10)}}</td>
307</tr>
308<tr>
309<td>{{content-location}}</td>
310<td>A location that the content can be retrieved from (a uri-generic object)</td>
311<td>{{(<#uri-generic# ...>)}}</td>
312</tr>
313<tr>
314<td>{{content-md5}}</td>
315<td>The MD5 checksum (a string) of the entity-body</td>
316<td>{{("12345ABCDEF")}}</td>
317</tr>
318<tr>
319<td>{{content-range}}</td>
320<td>Content range (pair with start- and endpoint) of the entity-body, if partially sent</td>
321<td>{{((25 . 120))}}</td>
322</tr>
323<tr>
324<td>{{content-type}}</td>
325<td>The mime type of the entity-body (a symbol)</td>
326<td>{{(text/html)}}</td>
327</tr>
328<tr>
329<td>{{date}}</td>
330<td>The date at which the message originated</td>
331<td>TODO</td>
332</tr>
333<tr>
334<td>{{etag}}</td>
335<td>An entity-tag (pair, car being either the symbol weak or strong, cdr being a symbol) that uniquely identifies the resource contents.</td>
336<td>{{((strong . foo123))}}</td>
337</tr>
338<tr>
339<td>{{expect}}</td>
340<td>Expectations of the server's behaviour (alist of symbol-string pairs), possibly with parameters.</td>
341<td>{{(#(((100-continue . #t)) ()))}}</td>
342</tr>
343<tr>
344<td>{{expires}}</td>
345<td>Expiry timestamp for the entity</td>
346<td>TODO</td>
347</tr>
348<tr>
349<td>{{from}}</td>
350<td>The e-mail address (a string) of the human user who controls the client</td>
351<td>{{("info@example.com")}}</td>
352</tr>
353<tr>
354<td>{{host}}</td>
355<td>The host to use (for virtual hosting). This is a pair of hostname and port</td>
356<td>{{(("example.com" . 80))}}</td>
357</tr>
358<tr>
359<td>{{if-match}}</td>
360<td>Entity-tags (pair, weak/strong symbol and unique entity identifier symbol) which must match.</td>
361<td>{{((strong . foo123) (strong . bar123))}}</td>
362</tr>
363<tr>
364<td>{{if-modified-since}}</td>
365<td>Timestamp which indicates since when the entity must have been modified.</td>
366<td>TODO</td>
367</tr>
368<tr>
369<td>{{if-none-match}}</td>
370<td>Entity tags (pair, weak/strong symbol and unique entity identifier symbol) which must not match.</td>
371<td>{{((strong . foo123) (strong . bar123))}}</td>
372</tr>
373<tr>
374<td>{{if-range}}</td>
375<td>The range to request, if the entity was unchanged</td>
376<td>TODO</td>
377</tr>
378<tr>
379<td>{{if-unmodified-since}}</td>
380<td>A timestamp since which the entity must not have been modified</td>
381<td>TODO</td>
382</tr>
383<tr>
384<td>{{last-modified}}</td>
385<td>A timestamp when the entity was last modified</td>
386<td>TODO</td>
387</tr>
388<tr>
389<td>{{location}}</td>
390<td>A location (an URI object) to which to redirect</td>
391<td>{{(<#uri-object ...>)}}</td>
392</tr>
393<tr>
394<td>{{max-forwards}}</td>
395<td>The maximum number of proxies that can forward a request</td>
396<td>{{(2)}}</td>
397</tr>
398<tr>
399<td>{{pragma}}</td>
400<td>An alist of symbols containing implementation-specific directives.</td>
401<td>{{((no-cache . #t) (my-extension . my-value))}}</td>
402</tr>
403<tr>
404<td>{{proxy-authenticate}}</td>
405<td>Proxy authentication options (authentication scheme symbol, with parameters)</td>
406<td>{{(digest #((username . "foo")))}}</td>
407</tr>
408<tr>
409<td>{{proxy-authorization}}</td>
410<td>Same as the above, only request-side instead of response-side</td>
411<td>{{(digest #((username . "foo")))}}</td>
412</tr>
413<tr>
414<td>{{range}}</td>
415<td>The range of bytes (a pair of start and end) to request from the server.</td>
416<td>{{((25 . 120))}}</td>
417</tr>
418<tr>
419<td>{{referer}}</td>
420<td>The referring URL (uri-generic object) that linked to this one.</td>
421<td>{{(<#uri-object ...>)}}</td>
422</tr>
423<tr>
424<td>{{retry-after}}</td>
425<td>Timestamp after which to retry the request if unavailable now.</td>
426<td>TODO</td>
427</tr>
428<tr>
429<td>{{server}}</td>
430<td>List of products the server uses (list of 3-tuple lists of strings; product name, product version, comment. Version and/or comment may be {{#f}}). Note that this is a single header, with a list inside it!</td>
431<td>{{((("Apache" "2.2.9" "Unix") ("mod_ssl" "2.2.9" #f) ("OpenSSL" "0.9.8e" #f) ("DAV" "2" #f) ("mod_fastcgi" "2.4.2" #f) ("mod_apreq2-20051231" "2.6.0" #f)))}}</td>
432</tr>
433<tr>
434<td>{{te}}</td>
435<td>Allowed transfer-encodings (symbols, with optional q attribute) for the response</td>
436<td>{{(deflate #(gzip ((q . 0.2))))}}</td>
437</tr>
438<tr>
439<td>{{trailer}}</td>
440<td>Names of header fields (symbols) available in the trailer/after body</td>
441<td>{{(range etag)}}</td>
442</tr>
443<tr>
444<td>{{transfer-encoding}}</td>
445<td>The encodings (symbols) used in the body</td>
446<td>{{(chunked)}}</td>
447</tr>
448<tr>
449<td>{{upgrade}}</td>
450<td>Product names to which must be upgraded (strings)</td>
451<td>TODO</td>
452</tr>
453<tr>
454<td>{{user-agent}}</td>
455<td>List of products the user agent uses (list of 3-tuple lists of strings; product name, product version, comment. Version and/or comment may be {{#f}}). Note that this is a single header, with a list inside it!</td>
456<td>{{((("Mozilla" "5.0" "X11; U; NetBSD amd64; en-US; rv:1.9.0.3") ("Gecko" "2008110501" #f) ("Minefield" "3.0.3" #f)))}}</td>
457</tr>
458<tr>
459<td>{{vary}}</td>
460<td>The names of headers that define variation in the resource body, to determine cachability (symbols)</td>
461<td>{{(range etag)}}</td>
462</tr>
463<tr>
464<td>{{via}}</td>
465<td>The intermediate hops through which the message is forwarded (strings)</td>
466<td>TODO</td>
467</tr>
468<tr>
469<td>{{warning}}</td>
470<td>Warning code for special status</td>
471<td>TODO</td>
472</tr>
473<tr>
474<td>{{www-authenticate}}</td>
475<td>If unauthorized, a challenge to authenticate (symbol, with attributes)</td>
476<td>{{(digest #((username . "foo")))}}</td>
477</tr>
478<tr>
479<td>{{set-cookie}}</td>
480<td>Cookies to set (name/value string pair, with attributes)</td>
481<td>{{(#(("foo" . "bar") ((max-age . 10))))}}</td>
482</tr>
483<tr>
484<td>{{cookie}}</td>
485<td>Cookies that were set (name/value string pair, with attributes)</td>
486<td>{{(#(("foo" . "bar") (($path . "/"))))}}</td>
487</tr>
488</table>
489
490Any unrecognised headers are assumed to be multi-headers, and the
491entire header lines are put unparsed into a list, one entry per line.
492
493
494==== Header parsers and unparsers
495
496The parsers and unparsers used to read and write header values can be
497customized with the following parameters:
498
499* <parameter>(header-parsers [ALIST])</parameter>
500* <parameter>(header-unparsers [ALIST])</parameter>
501
502These (un)parsers are indexed with as key the header name (a symbol)
503and the value being a procedure which accepts three values: the name
504of the header (symbol), the contents of the header (a string, without
505the leading header name and colon) and the preceding headers. It
506should merge the new header with the preceding headers and return the
507resulting headers.
508
509Header parsers are supposed to call these procedures to add headers:
510
511* <procedure>(replace-header-contents NAME CONTENTS HEADERS) => HEADERS</procedure>
512* <procedure>(replace-header-contents! NAME CONTENTS HEADERS) => HEADERS</procedure>
513* <procedure>(update-header-contents NAME CONTENTS HEADERS) => HEADERS</procedure>
514* <procedure>(update-header-contents! NAME CONTENTS HEADERS) => HEADERS</procedure>
515
516The {{replace}} procedures replace any existing contents of the named
517header with new ones, the {{update}} procedures add these contents to
518the existing header. The procedures with a name ending in bang are
519linear update variants of the ones without the bang. The header
520contents have to be normalized to be a 2-element vector, with the
521first element being the actual value and the second element being an
522alist (possibly empty) of parameters/attributes for that value.
523
524The update procedures append the value to the existing header if it is
525a multi-header, and act as a simple replace in the case of a
526single-header.
527
528Whether a header is allowed once or multiple times in a request or
529response is determined by this parameter:
530
531<parameter>(single-headers [LIST])</parameter>
532
533The value is a list of symbols that define header-names which are
534allowed to occur only once in a request/response.
535
536* <procedure>(http-name->symbol-name STRING) => SYMBOL</procedure>
537* <procedure>(symbol->http-name SYMBOL) => STRING</procedure>
538
539These procedures convert strings containing the name of a header or
540attribute (parameter name) to symbols representing the same. The
541symbols are completely downcased.  When converting this symbol back to
542a string, the initial letters of all the words in the header name or
543attribute are capitalized.
544
545=== Changelog
546
547* 0.1 Initial version
548
549=== License
550
551  Copyright (c) 2008, Peter Bex
552  All rights reserved.
553 
554  Redistribution and use in source and binary forms, with or without
555  modification, are permitted provided that the following conditions are
556  met:
557 
558  Redistributions of source code must retain the above copyright
559  notice, this list of conditions and the following disclaimer.
560 
561  Redistributions in binary form must reproduce the above copyright
562  notice, this list of conditions and the following disclaimer in the
563  documentation and/or other materials provided with the distribution.
564 
565  Neither the name of the author nor the names of its contributors may
566  be used to endorse or promote products derived from this software
567  without specific prior written permission.
568 
569  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
570  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
571  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
572  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
573  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
574  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
575  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
576  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
577  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
578  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
579  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
580  OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.