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

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

Fix markup

File size: 16.9 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
77Requests are written using unparse procedures, which can be
78customized by overriding this parameter:
79
80<parameter>(request-unparsers [LIST])</parameter>
81
82The list is one of procedures which accept a request object and write
83to the request's output port and return the new, possibly updated
84request object. If the request object is not unparsed by this
85handler, it returns {{#f}}.
86
87=== Responses
88
89A response is also a [[defstruct]]-type record, much like a request:
90
91<procedure>(make-response #!key port (code 200) (reason "OK") (major 1) (minor 1) (headers (make-headers '())))</procedure>
92
93An existing response can be picked apart using the following procedures:
94* <procedure>(response-port RESPONSE) => PORT</procedure>
95* <procedure>(response-code RESPONSE) => NUMBER</procedure>
96* <procedure>(response-reason RESPONSE) => STRING</procedure>
97* <procedure>(response-major RESPONSE) => NUMBER</procedure>
98* <procedure>(response-minor RESPONSE) => NUMBER</procedure>
99* <procedure>(response-headers RESPONSE) => HEADERS</procedure>
100
101The port, major, minor and headers are the same as for requests. code
102and reason are an integer status code and the short message that
103belongs to it, as defined in the spec (examples include: 200 OK, 301
104Moved Permanently, etc).
105
106A server will usually write a response, a client will read it.
107To write a response, use the following procedure:
108
109<procedure>(write-response RESPONSE) => RESPONSE</procedure>
110
111If there is a response body, this must be written to the response-port
112after sending the response headers.
113
114<procedure>(read-response PORT) => RESPONSE</procedure>
115
116Reads a response object from the port. An optional response body can
117be read from the response-port after calling this procedure.
118
119Responses are parsed using parse procedures, which can be customized
120by overriding this parameter:
121
122<parameter>(response-parsers [LIST])</parameter>
123
124The list is one of procedures which accept a response line string,
125which produce a response object from that, or {{#f}} if the response
126is not of the type handled by that procedure.
127
128Responses are written using unparse procedures, which can be
129customized by overriding this parameter:
130
131<parameter>(response-unparsers [LIST])</parameter>
132
133The list is one of procedures which accept a response object and write
134to the response's output port and return the new, possibly updated
135response object. If the response object is not unparsed by this
136handler, it returns {{#f}}.
137
138=== Headers
139
140Requests and responses contain HTTP headers wrapped in a special
141header-object to ensure they are properly normalized.
142
143<procedure>(headers ALIST [HEADERS]) => HEADERS</procedure>
144
145This creates headers based on an input list. This list has the
146header-name as a symbol key, and a list of values as value:
147
148<example>
149<expr>
150(headers `((host ("example.com" . 8080))
151           (accept #(text/html ((q . 0.5)))
152                   #(text/xml ((q . 0.1)))))
153          old-headers)
154</expr>
155</example>
156
157This adds the named headers to the existing headers in
158{{old-headers}}. The host header is either a string with the hostname
159or a pair of hostname/port. The accept header is a list of allowed
160mime-type symbols. As can be seen here, optional parameters or
161"attributes" can be added to a header value by wrapping the value in a
162vector of length 2. The first entry in the vector is the header value,
163the second is an alist of attribute name/value pairs.
164
165The headers all have their own different types.  Here follows a list
166of headers with their value types:
167
168<table>
169<tr><th>Header name</th><th>Value type</th><th>Example value</th></tr>
170<tr>
171<td>{{accept}}</td>
172<td>List of mime-types (symbols), with optional {{q}} attribute
173indicating "quality" (preference level)</td>
174<td>{{(text/html #(text/xml ((q . 0.1))))}}</td>
175</tr>
176<tr>
177<td>{{accept-charset}}</td>
178<td>List of charset-names (symbols), with optional {{q}} attribute</td>
179<td>{{(utf-8 #(iso-8859-5 ((q . 0.1))))}}</td>
180</tr>
181<tr>
182<td>{{accept-encoding}}</td>
183<td>List of encoding-names (symbols), with optional {{q}} attribute</td>
184<td>{{(gzip #(identity ((q . 0))))}}</td>
185</tr>
186<tr>
187<td>{{accept-language}}</td>
188<td>List of language-names (symbols), with optional {{q}} attribute</td>
189<td>{{(en-gb #(nl ((q . 0.5))))}}</td>
190</tr>
191<tr>
192<td>{{accept-ranges}}</td>
193<td>List of range types acceptable (symbols). The spec only defines
194{{bytes}} and {{none}}.</td>
195<td>{{(bytes)}}</td>
196</tr>
197<tr>
198<td>{{age}}</td>
199<td>Age in seconds (number)</td>
200<td>{{(3600)}}</td>
201</tr>
202<tr>
203<td>{{allow}}</td>
204<td>List of methods that are allowed (symbols).</td>
205<td>{{(GET POST PUT DELETE)}}</td>
206</tr>
207<tr>
208<td>{{authorization}}</td>
209<td>Authorization information. This consists of a symbol identifying the
210authentication scheme, with scheme-specific attributes.</td>
211<td>{{(digest #((username . "foo")))}}</td>
212</tr>
213<tr>
214<td>{{cache-control}}</td>
215<td>An alist of key/value pairs. If no value is applicable, it is {{#t}}</td>
216<td>((public . #t) (max-stale . 10) (no-cache . (max-age set-cookie)))</td>
217</tr>
218<tr>
219<td>{{connection}}</td>
220<td>A list of connection options (symbols)</td>
221<td>{{(close)}}</td>
222</tr>
223<tr>
224<td>{{content-encoding}}</td>
225<td>A list of encodings (symbols) applied to the entity-body.</td>
226<td>{{(deflate gzip)}}</td>
227</tr>
228<tr>
229<td>{{content-language}}</td>
230<td>The natural language(s) of the "intended audience" (symbols)</td>
231<td>{{(de nl en-gb)}}</td>
232</tr>
233<tr>
234<td>{{content-length}}</td>
235<td>The number of bytes (an exact number) in the entity-body</td>
236<td>{{(10)}}</td>
237</tr>
238<tr>
239<td>{{content-location}}</td>
240<td>A location that the content can be retrieved from (a uri-generic object)</td>
241<td>{{(<#uri-generic# ...>)}}</td>
242</tr>
243<tr>
244<td>{{content-md5}}</td>
245<td>The MD5 checksum (a string) of the entity-body</td>
246<td>{{("12345ABCDEF")}}</td>
247</tr>
248<tr>
249<td>{{content-range}}</td>
250<td>Content range (pair with start- and endpoint) of the entity-body, if partially sent</td>
251<td>{{((25 . 120))}}</td>
252</tr>
253<tr>
254<td>{{content-type}}</td>
255<td>The mime type of the entity-body (a symbol)</td>
256<td>{{(text/html)}}</td>
257</tr>
258<tr>
259<td>{{date}}</td>
260<td>The date at which the message originated</td>
261<td>TODO</td>
262</tr>
263<tr>
264<td>{{etag}}</td>
265<td>An entity-tag (pair, car being either the symbol weak or strong, cdr being a symbol) that uniquely identifies the resource contents.</td>
266<td>{{((strong . foo123))}}</td>
267</tr>
268<tr>
269<td>{{expect}}</td>
270<td>Expectations of the server's behaviour (alist of symbol-string pairs), possibly with parameters.</td>
271<td>{{(#(((100-continue . #t)) ()))}}</td>
272</tr>
273<tr>
274<td>{{expires}}</td>
275<td>Expiry timestamp for the entity</td>
276<td>TODO</td>
277</tr>
278<tr>
279<td>{{from}}</td>
280<td>The e-mail address (a string) of the human user who controls the client</td>
281<td>{{("info@example.com")}}</td>
282</tr>
283<tr>
284<td>{{host}}</td>
285<td>The host to use (for virtual hosting). This is a pair of hostname and port</td>
286<td>{{(("example.com" . 80))}}</td>
287</tr>
288<tr>
289<td>{{if-match}}</td>
290<td>Entity-tags (pair, weak/strong symbol and unique entity identifier symbol) which must match.</td>
291<td>{{((strong . foo123) (strong . bar123))}}</td>
292</tr>
293<tr>
294<td>{{if-modified-since}}</td>
295<td>Timestamp which indicates since when the entity must have been modified.</td>
296<td>TODO</td>
297</tr>
298<tr>
299<td>{{if-none-match}}</td>
300<td>Entity tags (pair, weak/strong symbol and unique entity identifier symbol) which must not match.</td>
301<td>{{((strong . foo123) (strong . bar123))}}</td>
302</tr>
303<tr>
304<td>{{if-range}}</td>
305<td>The range to request, if the entity was unchanged</td>
306<td>TODO</td>
307</tr>
308<tr>
309<td>{{if-unmodified-since}}</td>
310<td>A timestamp since which the entity must not have been modified</td>
311<td>TODO</td>
312</tr>
313<tr>
314<td>{{last-modified}}</td>
315<td>A timestamp when the entity was last modified</td>
316<td>TODO</td>
317</tr>
318<tr>
319<td>{{location}}</td>
320<td>A location (an URI object) to which to redirect</td>
321<td>{{(<#uri-object ...>)}}</td>
322</tr>
323<tr>
324<td>{{max-forwards}}</td>
325<td>The maximum number of proxies that can forward a request</td>
326<td>{{(2)}}</td>
327</tr>
328<tr>
329<td>{{pragma}}</td>
330<td>An alist of symbols containing implementation-specific directives.</td>
331<td>{{((no-cache . #t) (my-extension . my-value))}}</td>
332</tr>
333<tr>
334<td>{{proxy-authenticate}}</td>
335<td>Proxy authentication options (authentication scheme symbol, with parameters)</td>
336<td>{{(digest #((username . "foo")))}}</td>
337</tr>
338<tr>
339<td>{{proxy-authorization}}</td>
340<td>Same as the above, only request-side instead of response-side</td>
341<td>{{(digest #((username . "foo")))}}</td>
342</tr>
343<tr>
344<td>{{range}}</td>
345<td>The range of bytes (a pair of start and end) to request from the server.</td>
346<td>{{((25 . 120))}}</td>
347</tr>
348<tr>
349<td>{{referer}}</td>
350<td>The referring URL (uri-generic object) that linked to this one.</td>
351<td>{{(<#uri-object ...>)}}</td>
352</tr>
353<tr>
354<td>{{retry-after}}</td>
355<td>Timestamp after which to retry the request if unavailable now.</td>
356<td>TODO</td>
357</tr>
358<tr>
359<td>{{server}}</td>
360<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>
361<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>
362</tr>
363<tr>
364<td>{{te}}</td>
365<td>Allowed transfer-encodings (symbols, with optional q attribute) for the response</td>
366<td>{{(deflate #(gzip ((q . 0.2))))}}</td>
367</tr>
368<tr>
369<td>{{trailer}}</td>
370<td>Names of header fields (symbols) available in the trailer/after body</td>
371<td>{{(range etag)}}</td>
372</tr>
373<tr>
374<td>{{transfer-encoding}}</td>
375<td>The encodings (symbols) used in the body</td>
376<td>{{(chunked)}}</td>
377</tr>
378<tr>
379<td>{{upgrade}}</td>
380<td>Product names to which must be upgraded (strings)</td>
381<td>TODO</td>
382</tr>
383<tr>
384<td>{{user-agent}}</td>
385<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>
386<td>{{((("Mozilla" "5.0" "X11; U; NetBSD amd64; en-US; rv:1.9.0.3") ("Gecko" "2008110501" #f) ("Minefield" "3.0.3" #f)))}}</td>
387</tr>
388<tr>
389<td>{{vary}}</td>
390<td>The names of headers that define variation in the resource body, to determine cachability (symbols)</td>
391<td>{{(range etag)}}</td>
392</tr>
393<tr>
394<td>{{via}}</td>
395<td>The intermediate hops through which the message is forwarded (strings)</td>
396<td>TODO</td>
397</tr>
398<tr>
399<td>{{warning}}</td>
400<td>Warning code for special status</td>
401<td>TODO</td>
402</tr>
403<tr>
404<td>{{www-authenticate}}</td>
405<td>If unauthorized, a challenge to authenticate (symbol, with attributes)</td>
406<td>{{(digest #((username . "foo")))}}</td>
407</tr>
408<tr>
409<td>{{set-cookie}}</td>
410<td>Cookies to set (name/value string pair, with attributes)</td>
411<td>{{(#(("foo" . "bar") ((max-age . 10))))}}</td>
412</tr>
413<tr>
414<td>{{cookie}}</td>
415<td>Cookies that were set (name/value string pair, with attributes)</td>
416<td>{{(#(("foo" . "bar") (($path . "/"))))}}</td>
417</tr>
418</table>
419
420Any unrecognised headers are assumed to be multi-headers, and the
421entire header lines are put unparsed into a list, one entry per line.
422
423==== Header-parsers
424
425The parsers used to read and write header values can be customized
426with the following parameters:
427
428* <parameter>(header-parsers [ALIST])</parameter>
429* <parameter>(header-unparsers [ALIST])</parameter>
430
431These parsers are indexed with as key the header name (a symbol) and
432the value being a procedure which accepts three values: the name of
433the header (symbol), the contents of the header (a string, without the
434leading header name and colon) and the preceding headers. It should
435merge the new header with the preceding headers and return the
436resulting headers.
437
438Header parsers are supposed to call these procedures to add headers:
439
440* <procedure>(replace-header-contents NAME CONTENTS HEADERS) => HEADERS</procedure>
441* <procedure>(replace-header-contents! NAME CONTENTS HEADERS) => HEADERS</procedure>
442* <procedure>(update-header-contents NAME CONTENTS HEADERS) => HEADERS</procedure>
443* <procedure>(update-header-contents! NAME CONTENTS HEADERS) => HEADERS</procedure>
444
445The {{replace}} procedures replace any existing contents of the named
446header with new ones, the {{update}} procedures add these contents to
447the existing header. The procedures with a name ending in bang are
448linear update variants of the ones without the bang. The header
449contents have to be normalized to be a 2-element vector, with the
450first element being the actual value and the second element being an
451alist (possibly empty) of parameters/attributes for that value.
452
453The update procedures append the value to the existing header if it is
454a multi-header, and act as a simple replace in the case of a
455single-header.
456
457Whether a header is allowed once or multiple times in a request or
458response is determined by this parameter:
459
460<parameter>(single-headers [LIST])</parameter>
461
462The value is a list of symbols that define header-names which are
463allowed to occur only once in a request/response.
464
465=== Changelog
466
467* 0.1 Initial version
468
469=== License
470
471  Copyright (c) 2008, Peter Bex
472  All rights reserved.
473 
474  Redistribution and use in source and binary forms, with or without
475  modification, are permitted provided that the following conditions are
476  met:
477 
478  Redistributions of source code must retain the above copyright
479  notice, this list of conditions and the following disclaimer.
480 
481  Redistributions in binary form must reproduce the above copyright
482  notice, this list of conditions and the following disclaimer in the
483  documentation and/or other materials provided with the distribution.
484 
485  Neither the name of the author nor the names of its contributors may
486  be used to endorse or promote products derived from this software
487  without specific prior written permission.
488 
489  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
490  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
491  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
492  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
493  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
494  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
495  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
496  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
497  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
498  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
499  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500  OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.