Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/http.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 20.9 KB
Line 
1'\"
2'\" Copyright (c) 1995-1997 Sun Microsystems, Inc.
3'\" Copyright (c) 1998-2000 by Ajuba Solutions.
4'\" Copyright (c) 2004 ActiveState Corporation.
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: http.n,v 1.36 2008/03/12 10:01:02 hobbs Exp $
10'\"
11.so man.macros
12.TH "http" n 2.7 http "Tcl Bundled Packages"
13.BS
14'\" Note:  do not modify the .SH NAME line immediately below!
15.SH NAME
16http \- Client-side implementation of the HTTP/1.1 protocol
17.SH SYNOPSIS
18\fBpackage require http ?2.7?\fR
19.\" See Also -useragent option documentation in body!
20.sp
21\fB::http::config \fI?options?\fR
22.sp
23\fB::http::geturl \fIurl ?options?\fR
24.sp
25\fB::http::formatQuery\fR \fIkey value\fR ?\fIkey value\fR ...?
26.sp
27\fB::http::reset\fR \fItoken\fR ?\fIwhy\fR?
28.sp
29\fB::http::wait \fItoken\fR
30.sp
31\fB::http::status \fItoken\fR
32.sp
33\fB::http::size \fItoken\fR
34.sp
35\fB::http::code \fItoken\fR
36.sp
37\fB::http::ncode \fItoken\fR
38.sp
39\fB::http::meta \fItoken\fR
40.sp
41\fB::http::data \fItoken\fR
42.sp
43\fB::http::error \fItoken\fR
44.sp
45\fB::http::cleanup \fItoken\fR
46.sp
47\fB::http::register \fIproto port command\fR
48.sp
49\fB::http::unregister \fIproto\fR
50.BE
51.SH DESCRIPTION
52.PP
53The \fBhttp\fR package provides the client side of the HTTP/1.1
54protocol.  The package implements the GET, POST, and HEAD operations
55of HTTP/1.1.  It allows configuration of a proxy host to get through
56firewalls.  The package is compatible with the \fBSafesock\fR security
57policy, so it can be used by untrusted applets to do URL fetching from
58a restricted set of hosts. This package can be extended to support
59additional HTTP transport protocols, such as HTTPS, by providing
60a custom \fBsocket\fR command, via \fB::http::register\fR.
61.PP
62The \fB::http::geturl\fR procedure does a HTTP transaction.
63Its \fIoptions \fR determine whether a GET, POST, or HEAD transaction
64is performed. 
65The return value of \fB::http::geturl\fR is a token for the transaction.
66The value is also the name of an array in the ::http namespace
67that contains state information about the transaction.  The elements
68of this array are described in the \fBSTATE ARRAY\fR section.
69.PP
70If the \fB\-command\fR option is specified, then
71the HTTP operation is done in the background.
72\fB::http::geturl\fR returns immediately after generating the
73HTTP request and the callback is invoked
74when the transaction completes.  For this to work, the Tcl event loop
75must be active.  In Tk applications this is always true.  For pure-Tcl
76applications, the caller can use \fB::http::wait\fR after calling
77\fB::http::geturl\fR to start the event loop.
78.SH COMMANDS
79.TP
80\fB::http::config\fR ?\fIoptions\fR?
81The \fB::http::config\fR command is used to set and query the name of the
82proxy server and port, and the User-Agent name used in the HTTP
83requests.  If no options are specified, then the current configuration
84is returned.  If a single argument is specified, then it should be one
85of the flags described below.  In this case the current value of
86that setting is returned.  Otherwise, the options should be a set of
87flags and values that define the configuration:
88.RS
89.TP
90\fB\-accept\fR \fImimetypes\fR
91The Accept header of the request.  The default is */*, which means that
92all types of documents are accepted.  Otherwise you can supply a
93comma-separated list of mime type patterns that you are
94willing to receive.  For example,
95.QW "image/gif, image/jpeg, text/*" .
96.TP
97\fB\-proxyhost\fR \fIhostname\fR
98The name of the proxy host, if any.  If this value is the
99empty string, the URL host is contacted directly.
100.TP
101\fB\-proxyport\fR \fInumber\fR
102The proxy port number.
103.TP
104\fB\-proxyfilter\fR \fIcommand\fR
105The command is a callback that is made during
106\fB::http::geturl\fR
107to determine if a proxy is required for a given host.  One argument, a
108host name, is added to \fIcommand\fR when it is invoked.  If a proxy
109is required, the callback should return a two-element list containing
110the proxy server and proxy port.  Otherwise the filter should return
111an empty list.  The default filter returns the values of the
112\fB\-proxyhost\fR and \fB\-proxyport\fR settings if they are
113non-empty.
114.TP
115\fB\-urlencoding\fR \fIencoding\fR
116The \fIencoding\fR used for creating the x-url-encoded URLs with
117\fB::http::formatQuery\fR.  The default is \fButf-8\fR, as specified by RFC
1182718.  Prior to http 2.5 this was unspecified, and that behavior can be
119returned by specifying the empty string (\fB{}\fR), although
120\fIiso8859-1\fR is recommended to restore similar behavior but without the
121\fB::http::formatQuery\fR throwing an error processing non-latin-1
122characters.
123.TP
124\fB\-useragent\fR \fIstring\fR
125The value of the User-Agent header in the HTTP request.  The default is
126.QW "\fBTcl http client package 2.7\fR" .
127.RE
128.TP
129\fB::http::geturl\fR \fIurl\fR ?\fIoptions\fR?
130The \fB::http::geturl\fR command is the main procedure in the package.
131The \fB\-query\fR option causes a POST operation and
132the \fB\-validate\fR option causes a HEAD operation;
133otherwise, a GET operation is performed.  The \fB::http::geturl\fR command
134returns a \fItoken\fR value that can be used to get
135information about the transaction.  See the \fBSTATE ARRAY\fR and
136\fBERRORS\fR section for
137details.  The \fB::http::geturl\fR command blocks until the operation
138completes, unless the \fB\-command\fR option specifies a callback
139that is invoked when the HTTP transaction completes.
140\fB::http::geturl\fR takes several options:
141.RS
142.TP
143\fB\-binary\fR \fIboolean\fR
144Specifies whether to force interpreting the URL data as binary.  Normally
145this is auto-detected (anything not beginning with a \fBtext\fR content
146type or whose content encoding is \fBgzip\fR or \fBcompress\fR is
147considered binary data).
148.TP
149\fB\-blocksize\fR \fIsize\fR
150The block size used when reading the URL.
151At most \fIsize\fR bytes are read at once.  After each block, a call to the
152\fB\-progress\fR callback is made (if that option is specified).
153.TP
154\fB\-channel\fR \fIname\fR
155Copy the URL contents to channel \fIname\fR instead of saving it in
156\fBstate(body)\fR.
157.TP
158\fB\-command\fR \fIcallback\fR
159Invoke \fIcallback\fR after the HTTP transaction completes.
160This option causes \fB::http::geturl\fR to return immediately.
161The \fIcallback\fR gets an additional argument that is the \fItoken\fR returned
162from \fB::http::geturl\fR. This token is the name of an array that is
163described in the \fBSTATE ARRAY\fR section.  Here is a template for the
164callback:
165.RS
166.CS
167proc httpCallback {token} {
168    upvar #0 $token state
169    # Access state as a Tcl array
170}
171.CE
172.RE
173.TP
174\fB\-handler\fR \fIcallback\fR
175Invoke \fIcallback\fR whenever HTTP data is available; if present, nothing
176else will be done with the HTTP data.  This procedure gets two additional
177arguments: the socket for the HTTP data and the \fItoken\fR returned from
178\fB::http::geturl\fR.  The token is the name of a global array that is
179described in the \fBSTATE ARRAY\fR section.  The procedure is expected
180to return the number of bytes read from the socket.  Here is a
181template for the callback:
182.RS
183.CS
184proc httpHandlerCallback {socket token} {
185    upvar #0 $token state
186    # Access socket, and state as a Tcl array
187    # For example...
188    ...
189    set data [read $socket 1000]
190    set nbytes [string length $data]
191    ...
192    return $nbytes
193}
194.CE
195.RE
196.TP
197\fB\-headers\fR \fIkeyvaluelist\fR
198This option is used to add extra headers to the HTTP request.  The
199\fIkeyvaluelist\fR argument must be a list with an even number of
200elements that alternate between keys and values.  The keys become
201header field names.  Newlines are stripped from the values so the
202header cannot be corrupted.  For example, if \fIkeyvaluelist\fR is
203\fBPragma no-cache\fR then the following header is included in the
204HTTP request:
205.CS
206Pragma: no-cache
207.CE
208.TP
209\fB\-keepalive\fR \fIboolean\fR
210If true, attempt to keep the connection open for servicing
211multiple requests.  Default is 0.
212.TP
213\fB\-method\fR \fItype\fR
214Force the HTTP request method to \fItype\fR. \fB::http::geturl\fR will
215auto-select GET, POST or HEAD based on other options, but this option
216enables choices like PUT and DELETE for webdav support.
217.TP
218\fB\-myaddr\fR \fIaddress\fR
219Pass an specific local address to the underlying \fBsocket\fR call in case
220multiple interfaces are available.
221.TP
222\fB\-progress\fR \fIcallback\fR
223The \fIcallback\fR is made after each transfer of data from the URL.
224The callback gets three additional arguments: the \fItoken\fR from
225\fB::http::geturl\fR, the expected total size of the contents from the
226\fBContent-Length\fR meta-data, and the current number of bytes
227transferred so far.  The expected total size may be unknown, in which
228case zero is passed to the callback.  Here is a template for the
229progress callback:
230.RS
231.CS
232proc httpProgress {token total current} {
233    upvar #0 $token state
234}
235.CE
236.RE
237.TP
238\fB\-protocol\fR \fIversion\fR
239Select the HTTP protocol version to use. This should be 1.0 or 1.1 (the
240default). Should only be necessary for servers that do not understand or
241otherwise complain about HTTP/1.1.
242.TP
243\fB\-query\fR \fIquery\fR
244This flag causes \fB::http::geturl\fR to do a POST request that passes the
245\fIquery\fR to the server. The \fIquery\fR must be an x-url-encoding
246formatted query.  The \fB::http::formatQuery\fR procedure can be used to
247do the formatting.
248.TP
249\fB\-queryblocksize\fR \fIsize\fR
250The block size used when posting query data to the URL.
251At most
252\fIsize\fR
253bytes are written at once.  After each block, a call to the
254\fB\-queryprogress\fR
255callback is made (if that option is specified).
256.TP
257\fB\-querychannel\fR \fIchannelID\fR
258This flag causes \fB::http::geturl\fR to do a POST request that passes the
259data contained in \fIchannelID\fR to the server. The data contained in
260\fIchannelID\fR must be an x-url-encoding
261formatted query unless the \fB\-type\fR option below is used.
262If a Content-Length header is not specified via the \fB\-headers\fR options,
263\fB::http::geturl\fR attempts to determine the size of the post data
264in order to create that header.  If it is
265unable to determine the size, it returns an error.
266.TP
267\fB\-queryprogress\fR \fIcallback\fR
268The \fIcallback\fR is made after each transfer of data to the URL
269(i.e. POST) and acts exactly like the \fB\-progress\fR option (the
270callback format is the same).
271.TP
272\fB\-strict\fR \fIboolean\fR
273Whether to enforce RFC 3986 URL validation on the request.  Default is 1.
274.TP
275\fB\-timeout\fR \fImilliseconds\fR
276If \fImilliseconds\fR is non-zero, then \fB::http::geturl\fR sets up a timeout
277to occur after the specified number of milliseconds.
278A timeout results in a call to \fB::http::reset\fR and to
279the \fB\-command\fR callback, if specified.
280The return value of \fB::http::status\fR is \fBtimeout\fR
281after a timeout has occurred.
282.TP
283\fB\-type\fR \fImime-type\fR
284Use \fImime-type\fR as the \fBContent-Type\fR value, instead of the
285default value (\fBapplication/x-www-form-urlencoded\fR) during a
286POST operation.
287.TP
288\fB\-validate\fR \fIboolean\fR
289If \fIboolean\fR is non-zero, then \fB::http::geturl\fR does an HTTP HEAD
290request.  This request returns meta information about the URL, but the
291contents are not returned.  The meta information is available in the
292\fBstate(meta) \fR variable after the transaction.  See the
293\fBSTATE ARRAY\fR section for details.
294.RE
295.TP
296\fB::http::formatQuery\fR \fIkey value\fR ?\fIkey value\fR ...?
297This procedure does x-url-encoding of query data.  It takes an even
298number of arguments that are the keys and values of the query.  It
299encodes the keys and values, and generates one string that has the
300proper & and = separators.  The result is suitable for the
301\fB\-query\fR value passed to \fB::http::geturl\fR.
302.TP
303\fB::http::reset\fR \fItoken\fR ?\fIwhy\fR?
304This command resets the HTTP transaction identified by \fItoken\fR, if
305any.  This sets the \fBstate(status)\fR value to \fIwhy\fR, which defaults to \fBreset\fR, and then calls the registered \fB\-command\fR callback.
306.TP
307\fB::http::wait\fR \fItoken\fR
308This is a convenience procedure that blocks and waits for the
309transaction to complete.  This only works in trusted code because it
310uses \fBvwait\fR.  Also, it is not useful for the case where
311\fB::http::geturl\fR is called \fIwithout\fR the \fB\-command\fR option
312because in this case the \fB::http::geturl\fR call does not return
313until the HTTP transaction is complete, and thus there is nothing to
314wait for.
315.TP
316\fB::http::data\fR \fItoken\fR
317This is a convenience procedure that returns the \fBbody\fR element
318(i.e., the URL data) of the state array.
319.TP
320\fB::http::error\fR \fItoken\fR
321This is a convenience procedure that returns the \fBerror\fR element
322of the state array.
323.TP
324\fB::http::status\fR \fItoken\fR
325This is a convenience procedure that returns the \fBstatus\fR element of
326the state array.
327.TP
328\fB::http::code\fR \fItoken\fR
329This is a convenience procedure that returns the \fBhttp\fR element of the
330state array.
331.TP
332\fB::http::ncode\fR \fItoken\fR
333This is a convenience procedure that returns just the numeric return
334code (200, 404, etc.) from the \fBhttp\fR element of the state array.
335.TP
336\fB::http::size\fR \fItoken\fR
337This is a convenience procedure that returns the \fBcurrentsize\fR
338element of the state array, which represents the number of bytes
339received from the URL in the \fB::http::geturl\fR call.
340.TP
341\fB::http::meta\fR \fItoken\fR
342This is a convenience procedure that returns the \fBmeta\fR
343element of the state array which contains the HTTP response
344headers. See below for an explanation of this element.
345.TP
346\fB::http::cleanup\fR \fItoken\fR
347This procedure cleans up the state associated with the connection
348identified by \fItoken\fR.  After this call, the procedures
349like \fB::http::data\fR cannot be used to get information
350about the operation.  It is \fIstrongly\fR recommended that you call
351this function after you are done with a given HTTP request.  Not doing
352so will result in memory not being freed, and if your app calls
353\fB::http::geturl\fR enough times, the memory leak could cause a
354performance hit...or worse.
355.TP
356\fB::http::register\fR \fIproto port command\fR
357This procedure allows one to provide custom HTTP transport types
358such as HTTPS, by registering a prefix, the default port, and the
359command to execute to create the Tcl \fBchannel\fR. E.g.:
360.RS
361.CS
362package require http
363package require tls
364
365::http::register https 443 ::tls::socket
366
367set token [::http::geturl https://my.secure.site/]
368.CE
369.RE
370.TP
371\fB::http::unregister\fR \fIproto\fR
372This procedure unregisters a protocol handler that was previously
373registered via \fB::http::register\fR.
374.SH ERRORS
375The \fB::http::geturl\fR procedure will raise errors in the following cases:
376invalid command line options,
377an invalid URL,
378a URL on a non-existent host,
379or a URL at a bad port on an existing host.
380These errors mean that it
381cannot even start the network transaction.
382It will also raise an error if it gets an I/O error while
383writing out the HTTP request header.
384For synchronous \fB::http::geturl\fR calls (where \fB\-command\fR is
385not specified), it will raise an error if it gets an I/O error while
386reading the HTTP reply headers or data.  Because \fB::http::geturl\fR
387does not return a token in these cases, it does all the required
388cleanup and there is no issue of your app having to call
389\fB::http::cleanup\fR.
390.PP
391For asynchronous \fB::http::geturl\fR calls, all of the above error
392situations apply, except that if there is any error while reading the
393HTTP reply headers or data, no exception is thrown.  This is because
394after writing the HTTP headers, \fB::http::geturl\fR returns, and the
395rest of the HTTP transaction occurs in the background.  The command
396callback can check if any error occurred during the read by calling
397\fB::http::status\fR to check the status and if its \fIerror\fR,
398calling \fB::http::error\fR to get the error message.
399.PP
400Alternatively, if the main program flow reaches a point where it needs
401to know the result of the asynchronous HTTP request, it can call
402\fB::http::wait\fR and then check status and error, just as the
403callback does.
404.PP
405In any case, you must still call
406\fB::http::cleanup\fR to delete the state array when you are done.
407.PP
408There are other possible results of the HTTP transaction
409determined by examining the status from \fB::http::status\fR.
410These are described below.
411.TP
412ok
413If the HTTP transaction completes entirely, then status will be \fBok\fR.
414However, you should still check the \fB::http::code\fR value to get
415the HTTP status.  The \fB::http::ncode\fR procedure provides just
416the numeric error (e.g., 200, 404 or 500) while the \fB::http::code\fR
417procedure returns a value like
418.QW "HTTP 404 File not found" .
419.TP
420eof
421If the server closes the socket without replying, then no error
422is raised, but the status of the transaction will be \fBeof\fR.
423.TP
424error
425The error message will also be stored in the \fBerror\fR status
426array element, accessible via \fB::http::error\fR.
427.PP
428Another error possibility is that \fB::http::geturl\fR is unable to
429write all the post query data to the server before the server
430responds and closes the socket.
431The error message is saved in the \fBposterror\fR status array
432element and then  \fB::http::geturl\fR attempts to complete the
433transaction.
434If it can read the server's response
435it will end up with an \fBok\fR status, otherwise it will have
436an \fBeof\fR status.
437.SH "STATE ARRAY"
438The \fB::http::geturl\fR procedure returns a \fItoken\fR that can be used to
439get to the state of the HTTP transaction in the form of a Tcl array.
440Use this construct to create an easy-to-use array variable:
441.CS
442upvar #0 $token state
443.CE
444Once the data associated with the URL is no longer needed, the state
445array should be unset to free up storage.
446The \fB::http::cleanup\fR procedure is provided for that purpose.
447The following elements of
448the array are supported:
449.RS
450.TP
451\fBbody\fR
452The contents of the URL.  This will be empty if the \fB\-channel\fR
453option has been specified.  This value is returned by the \fB::http::data\fR command.
454.TP
455\fBcharset\fR
456The value of the charset attribute from the \fBContent-Type\fR meta-data
457value.  If none was specified, this defaults to the RFC standard
458\fBiso8859-1\fR, or the value of \fB$::http::defaultCharset\fR.  Incoming
459text data will be automatically converted from this charset to utf-8.
460.TP
461\fBcoding\fR
462A copy of the \fBContent-Encoding\fR meta-data value.
463.TP
464\fBcurrentsize\fR
465The current number of bytes fetched from the URL.
466This value is returned by the \fB::http::size\fR command.
467.TP
468\fBerror\fR
469If defined, this is the error string seen when the HTTP transaction
470was aborted.
471.TP
472\fBhttp\fR
473The HTTP status reply from the server.  This value
474is returned by the \fB::http::code\fR command.  The format of this value is:
475.RS
476.CS
477\fIHTTP/1.1 code string\fR
478.CE
479The \fIcode\fR is a three-digit number defined in the HTTP standard.
480A code of 200 is OK.  Codes beginning with 4 or 5 indicate errors.
481Codes beginning with 3 are redirection errors.  In this case the
482\fBLocation\fR meta-data specifies a new URL that contains the
483requested information.
484.RE
485.TP
486\fBmeta\fR
487The HTTP protocol returns meta-data that describes the URL contents.
488The \fBmeta\fR element of the state array is a list of the keys and
489values of the meta-data.  This is in a format useful for initializing
490an array that just contains the meta-data:
491.RS
492.CS
493array set meta $state(meta)
494.CE
495Some of the meta-data keys are listed below, but the HTTP standard defines
496more, and servers are free to add their own.
497.TP
498\fBContent-Type\fR
499The type of the URL contents.  Examples include \fBtext/html\fR,
500\fBimage/gif,\fR \fBapplication/postscript\fR and
501\fBapplication/x-tcl\fR.
502.TP
503\fBContent-Length\fR
504The advertised size of the contents.  The actual size obtained by
505\fB::http::geturl\fR is available as \fBstate(size)\fR.
506.TP
507\fBLocation\fR
508An alternate URL that contains the requested data.
509.RE
510.TP
511\fBposterror\fR
512The error, if any, that occurred while writing
513the post query data to the server.
514.TP
515\fBstatus\fR
516Either \fBok\fR, for successful completion, \fBreset\fR for
517user-reset, \fBtimeout\fR if a timeout occurred before the transaction
518could complete, or \fBerror\fR for an error condition.  During the
519transaction this value is the empty string.
520.TP
521\fBtotalsize\fR
522A copy of the \fBContent-Length\fR meta-data value.
523.TP
524\fBtype\fR
525A copy of the \fBContent-Type\fR meta-data value.
526.TP
527\fBurl\fR
528The requested URL.
529.RE
530.SH EXAMPLE
531.CS
532# Copy a URL to a file and print meta-data
533proc httpcopy { url file {chunk 4096} } {
534   set out [open $file w]
535   set token [\fB::http::geturl\fR $url -channel $out \e
536          -progress httpCopyProgress -blocksize $chunk]
537   close $out
538
539   # This ends the line started by httpCopyProgress
540   puts stderr ""
541
542   upvar #0 $token state
543   set max 0
544   foreach {name value} $state(meta) {
545      if {[string length $name] > $max} {
546         set max [string length $name]
547      }
548      if {[regexp -nocase ^location$ $name]} {
549         # Handle URL redirects
550         puts stderr "Location:$value"
551         return [httpcopy [string trim $value] $file $chunk]
552      }
553   }
554   incr max
555   foreach {name value} $state(meta) {
556      puts [format "%-*s %s" $max $name: $value]
557   }
558
559   return $token
560}
561proc httpCopyProgress {args} {
562   puts -nonewline stderr .
563   flush stderr
564}
565.CE
566.SH "SEE ALSO"
567safe(n), socket(n), safesock(n)
568.SH KEYWORDS
569security policy, socket
Note: See TracBrowser for help on using the repository browser.