OSDN Git Service

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