OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / man / mann / http.n
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 .TH "http" n 2.7 http "Tcl Bundled Packages"
10 .\" The -*- nroff -*- definitions below are for supplemental macros used
11 .\" in Tcl/Tk manual entries.
12 .\"
13 .\" .AP type name in/out ?indent?
14 .\"     Start paragraph describing an argument to a library procedure.
15 .\"     type is type of argument (int, etc.), in/out is either "in", "out",
16 .\"     or "in/out" to describe whether procedure reads or modifies arg,
17 .\"     and indent is equivalent to second arg of .IP (shouldn't ever be
18 .\"     needed;  use .AS below instead)
19 .\"
20 .\" .AS ?type? ?name?
21 .\"     Give maximum sizes of arguments for setting tab stops.  Type and
22 .\"     name are examples of largest possible arguments that will be passed
23 .\"     to .AP later.  If args are omitted, default tab stops are used.
24 .\"
25 .\" .BS
26 .\"     Start box enclosure.  From here until next .BE, everything will be
27 .\"     enclosed in one large box.
28 .\"
29 .\" .BE
30 .\"     End of box enclosure.
31 .\"
32 .\" .CS
33 .\"     Begin code excerpt.
34 .\"
35 .\" .CE
36 .\"     End code excerpt.
37 .\"
38 .\" .VS ?version? ?br?
39 .\"     Begin vertical sidebar, for use in marking newly-changed parts
40 .\"     of man pages.  The first argument is ignored and used for recording
41 .\"     the version when the .VS was added, so that the sidebars can be
42 .\"     found and removed when they reach a certain age.  If another argument
43 .\"     is present, then a line break is forced before starting the sidebar.
44 .\"
45 .\" .VE
46 .\"     End of vertical sidebar.
47 .\"
48 .\" .DS
49 .\"     Begin an indented unfilled display.
50 .\"
51 .\" .DE
52 .\"     End of indented unfilled display.
53 .\"
54 .\" .SO ?manpage?
55 .\"     Start of list of standard options for a Tk widget. The manpage
56 .\"     argument defines where to look up the standard options; if
57 .\"     omitted, defaults to "options". The options follow on successive
58 .\"     lines, in three columns separated by tabs.
59 .\"
60 .\" .SE
61 .\"     End of list of standard options for a Tk widget.
62 .\"
63 .\" .OP cmdName dbName dbClass
64 .\"     Start of description of a specific option.  cmdName gives the
65 .\"     option's name as specified in the class command, dbName gives
66 .\"     the option's name in the option database, and dbClass gives
67 .\"     the option's class in the option database.
68 .\"
69 .\" .UL arg1 arg2
70 .\"     Print arg1 underlined, then print arg2 normally.
71 .\"
72 .\" .QW arg1 ?arg2?
73 .\"     Print arg1 in quotes, then arg2 normally (for trailing punctuation).
74 .\"
75 .\" .PQ arg1 ?arg2?
76 .\"     Print an open parenthesis, arg1 in quotes, then arg2 normally
77 .\"     (for trailing punctuation) and then a closing parenthesis.
78 .\"
79 .\"     # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
80 .if t .wh -1.3i ^B
81 .nr ^l \n(.l
82 .ad b
83 .\"     # Start an argument description
84 .de AP
85 .ie !"\\$4"" .TP \\$4
86 .el \{\
87 .   ie !"\\$2"" .TP \\n()Cu
88 .   el          .TP 15
89 .\}
90 .ta \\n()Au \\n()Bu
91 .ie !"\\$3"" \{\
92 \&\\$1 \\fI\\$2\\fP (\\$3)
93 .\".b
94 .\}
95 .el \{\
96 .br
97 .ie !"\\$2"" \{\
98 \&\\$1  \\fI\\$2\\fP
99 .\}
100 .el \{\
101 \&\\fI\\$1\\fP
102 .\}
103 .\}
104 ..
105 .\"     # define tabbing values for .AP
106 .de AS
107 .nr )A 10n
108 .if !"\\$1"" .nr )A \\w'\\$1'u+3n
109 .nr )B \\n()Au+15n
110 .\"
111 .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
112 .nr )C \\n()Bu+\\w'(in/out)'u+2n
113 ..
114 .AS Tcl_Interp Tcl_CreateInterp in/out
115 .\"     # BS - start boxed text
116 .\"     # ^y = starting y location
117 .\"     # ^b = 1
118 .de BS
119 .br
120 .mk ^y
121 .nr ^b 1u
122 .if n .nf
123 .if n .ti 0
124 .if n \l'\\n(.lu\(ul'
125 .if n .fi
126 ..
127 .\"     # BE - end boxed text (draw box now)
128 .de BE
129 .nf
130 .ti 0
131 .mk ^t
132 .ie n \l'\\n(^lu\(ul'
133 .el \{\
134 .\"     Draw four-sided box normally, but don't draw top of
135 .\"     box if the box started on an earlier page.
136 .ie !\\n(^b-1 \{\
137 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
138 .\}
139 .el \}\
140 \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
141 .\}
142 .\}
143 .fi
144 .br
145 .nr ^b 0
146 ..
147 .\"     # VS - start vertical sidebar
148 .\"     # ^Y = starting y location
149 .\"     # ^v = 1 (for troff;  for nroff this doesn't matter)
150 .de VS
151 .if !"\\$2"" .br
152 .mk ^Y
153 .ie n 'mc \s12\(br\s0
154 .el .nr ^v 1u
155 ..
156 .\"     # VE - end of vertical sidebar
157 .de VE
158 .ie n 'mc
159 .el \{\
160 .ev 2
161 .nf
162 .ti 0
163 .mk ^t
164 \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
165 .sp -1
166 .fi
167 .ev
168 .\}
169 .nr ^v 0
170 ..
171 .\"     # Special macro to handle page bottom:  finish off current
172 .\"     # box/sidebar if in box/sidebar mode, then invoked standard
173 .\"     # page bottom macro.
174 .de ^B
175 .ev 2
176 'ti 0
177 'nf
178 .mk ^t
179 .if \\n(^b \{\
180 .\"     Draw three-sided box if this is the box's first page,
181 .\"     draw two sides but no top otherwise.
182 .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
183 .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
184 .\}
185 .if \\n(^v \{\
186 .nr ^x \\n(^tu+1v-\\n(^Yu
187 \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
188 .\}
189 .bp
190 'fi
191 .ev
192 .if \\n(^b \{\
193 .mk ^y
194 .nr ^b 2
195 .\}
196 .if \\n(^v \{\
197 .mk ^Y
198 .\}
199 ..
200 .\"     # DS - begin display
201 .de DS
202 .RS
203 .nf
204 .sp
205 ..
206 .\"     # DE - end display
207 .de DE
208 .fi
209 .RE
210 .sp
211 ..
212 .\"     # SO - start of list of standard options
213 .de SO
214 'ie '\\$1'' .ds So \\fBoptions\\fR
215 'el .ds So \\fB\\$1\\fR
216 .SH "STANDARD OPTIONS"
217 .LP
218 .nf
219 .ta 5.5c 11c
220 .ft B
221 ..
222 .\"     # SE - end of list of standard options
223 .de SE
224 .fi
225 .ft R
226 .LP
227 See the \\*(So manual entry for details on the standard options.
228 ..
229 .\"     # OP - start of full description for a single option
230 .de OP
231 .LP
232 .nf
233 .ta 4c
234 Command-Line Name:      \\fB\\$1\\fR
235 Database Name:  \\fB\\$2\\fR
236 Database Class: \\fB\\$3\\fR
237 .fi
238 .IP
239 ..
240 .\"     # CS - begin code excerpt
241 .de CS
242 .RS
243 .nf
244 .ta .25i .5i .75i 1i
245 ..
246 .\"     # CE - end code excerpt
247 .de CE
248 .fi
249 .RE
250 ..
251 .\"     # UL - underline word
252 .de UL
253 \\$1\l'|0\(ul'\\$2
254 ..
255 .\"     # QW - apply quotation marks to word
256 .de QW
257 .ie '\\*(lq'"' ``\\$1''\\$2
258 .\"" fix emacs highlighting
259 .el \\*(lq\\$1\\*(rq\\$2
260 ..
261 .\"     # PQ - apply parens and quotation marks to word
262 .de PQ
263 .ie '\\*(lq'"' (``\\$1''\\$2)\\$3
264 .\"" fix emacs highlighting
265 .el (\\*(lq\\$1\\*(rq\\$2)\\$3
266 ..
267 .\"     # QR - quoted range
268 .de QR
269 .ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
270 .\"" fix emacs highlighting
271 .el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
272 ..
273 .\"     # MT - "empty" string
274 .de MT
275 .QW ""
276 ..
277 .BS
278 '\" Note:  do not modify the .SH NAME line immediately below!
279 .SH NAME
280 http \- Client-side implementation of the HTTP/1.1 protocol
281 .SH SYNOPSIS
282 \fBpackage require http ?2.7?\fR
283 .\" See Also -useragent option documentation in body!
284 .sp
285 \fB::http::config ?\fI\-option value\fR ...?
286 .sp
287 \fB::http::geturl \fIurl\fR ?\fI\-option value\fR ...?
288 .sp
289 \fB::http::formatQuery\fR \fIkey value\fR ?\fIkey value\fR ...?
290 .sp
291 \fB::http::reset\fR \fItoken\fR ?\fIwhy\fR?
292 .sp
293 \fB::http::wait \fItoken\fR
294 .sp
295 \fB::http::status \fItoken\fR
296 .sp
297 \fB::http::size \fItoken\fR
298 .sp
299 \fB::http::code \fItoken\fR
300 .sp
301 \fB::http::ncode \fItoken\fR
302 .sp
303 \fB::http::meta \fItoken\fR
304 .sp
305 \fB::http::data \fItoken\fR
306 .sp
307 \fB::http::error \fItoken\fR
308 .sp
309 \fB::http::cleanup \fItoken\fR
310 .sp
311 \fB::http::register \fIproto port command\fR
312 .sp
313 \fB::http::unregister \fIproto\fR
314 .BE
315 .SH DESCRIPTION
316 .PP
317 The \fBhttp\fR package provides the client side of the HTTP/1.1
318 protocol, as defined in RFC 2616.
319 The package implements the GET, POST, and HEAD operations
320 of HTTP/1.1.  It allows configuration of a proxy host to get through
321 firewalls.  The package is compatible with the \fBSafesock\fR security
322 policy, so it can be used by untrusted applets to do URL fetching from
323 a restricted set of hosts. This package can be extended to support
324 additional HTTP transport protocols, such as HTTPS, by providing
325 a custom \fBsocket\fR command, via \fB::http::register\fR.
326 .PP
327 The \fB::http::geturl\fR procedure does a HTTP transaction.
328 Its \fIoptions \fR determine whether a GET, POST, or HEAD transaction
329 is performed.  
330 The return value of \fB::http::geturl\fR is a token for the transaction.
331 The value is also the name of an array in the ::http namespace
332 that contains state information about the transaction.  The elements
333 of this array are described in the \fBSTATE ARRAY\fR section.
334 .PP
335 If the \fB\-command\fR option is specified, then
336 the HTTP operation is done in the background.
337 \fB::http::geturl\fR returns immediately after generating the
338 HTTP request and the callback is invoked
339 when the transaction completes.  For this to work, the Tcl event loop
340 must be active.  In Tk applications this is always true.  For pure-Tcl
341 applications, the caller can use \fB::http::wait\fR after calling
342 \fB::http::geturl\fR to start the event loop.
343 .SH COMMANDS
344 .TP
345 \fB::http::config\fR ?\fIoptions\fR?
346 .
347 The \fB::http::config\fR command is used to set and query the name of the
348 proxy server and port, and the User-Agent name used in the HTTP
349 requests.  If no options are specified, then the current configuration
350 is returned.  If a single argument is specified, then it should be one
351 of the flags described below.  In this case the current value of
352 that setting is returned.  Otherwise, the options should be a set of
353 flags and values that define the configuration:
354 .RS
355 .TP
356 \fB\-accept\fR \fImimetypes\fR
357 .
358 The Accept header of the request.  The default is */*, which means that
359 all types of documents are accepted.  Otherwise you can supply a 
360 comma-separated list of mime type patterns that you are
361 willing to receive.  For example,
362 .QW "image/gif, image/jpeg, text/*" .
363 .TP
364 \fB\-proxyhost\fR \fIhostname\fR
365 .
366 The name of the proxy host, if any.  If this value is the
367 empty string, the URL host is contacted directly.
368 .TP
369 \fB\-proxyport\fR \fInumber\fR
370 .
371 The proxy port number.
372 .TP
373 \fB\-proxyfilter\fR \fIcommand\fR
374 .
375 The command is a callback that is made during
376 \fB::http::geturl\fR
377 to determine if a proxy is required for a given host.  One argument, a
378 host name, is added to \fIcommand\fR when it is invoked.  If a proxy
379 is required, the callback should return a two-element list containing
380 the proxy server and proxy port.  Otherwise the filter should return
381 an empty list.  The default filter returns the values of the
382 \fB\-proxyhost\fR and \fB\-proxyport\fR settings if they are
383 non-empty.
384 .TP
385 \fB\-urlencoding\fR \fIencoding\fR
386 .
387 The \fIencoding\fR used for creating the x-url-encoded URLs with
388 \fB::http::formatQuery\fR.  The default is \fButf-8\fR, as specified by RFC
389 2718.  Prior to http 2.5 this was unspecified, and that behavior can be
390 returned by specifying the empty string (\fB{}\fR), although
391 \fIiso8859-1\fR is recommended to restore similar behavior but without the
392 \fB::http::formatQuery\fR throwing an error processing non-latin-1
393 characters.
394 .TP
395 \fB\-useragent\fR \fIstring\fR
396 .
397 The value of the User-Agent header in the HTTP request.  The default is
398 .QW "\fBTcl http client package 2.7\fR" .
399 .RE
400 .TP
401 \fB::http::geturl\fR \fIurl\fR ?\fIoptions\fR? 
402 .
403 The \fB::http::geturl\fR command is the main procedure in the package.
404 The \fB\-query\fR option causes a POST operation and
405 the \fB\-validate\fR option causes a HEAD operation;
406 otherwise, a GET operation is performed.  The \fB::http::geturl\fR command
407 returns a \fItoken\fR value that can be used to get
408 information about the transaction.  See the \fBSTATE ARRAY\fR and
409 \fBERRORS\fR section for
410 details.  The \fB::http::geturl\fR command blocks until the operation
411 completes, unless the \fB\-command\fR option specifies a callback
412 that is invoked when the HTTP transaction completes.
413 \fB::http::geturl\fR takes several options:
414 .RS
415 .TP
416 \fB\-binary\fR \fIboolean\fR
417 .
418 Specifies whether to force interpreting the URL data as binary.  Normally
419 this is auto-detected (anything not beginning with a \fBtext\fR content
420 type or whose content encoding is \fBgzip\fR or \fBcompress\fR is
421 considered binary data).
422 .TP
423 \fB\-blocksize\fR \fIsize\fR
424 .
425 The block size used when reading the URL.
426 At most \fIsize\fR bytes are read at once.  After each block, a call to the
427 \fB\-progress\fR callback is made (if that option is specified).
428 .TP
429 \fB\-channel\fR \fIname\fR
430 .
431 Copy the URL contents to channel \fIname\fR instead of saving it in
432 \fBstate(body)\fR.
433 .TP
434 \fB\-command\fR \fIcallback\fR
435 .
436 Invoke \fIcallback\fR after the HTTP transaction completes.
437 This option causes \fB::http::geturl\fR to return immediately.
438 The \fIcallback\fR gets an additional argument that is the \fItoken\fR returned
439 from \fB::http::geturl\fR. This token is the name of an array that is
440 described in the \fBSTATE ARRAY\fR section.  Here is a template for the
441 callback:
442 .RS
443 .PP
444 .CS
445 proc httpCallback {token} {
446     upvar #0 $token state
447     # Access state as a Tcl array
448 }
449 .CE
450 .RE
451 .TP
452 \fB\-handler\fR \fIcallback\fR
453 .
454 Invoke \fIcallback\fR whenever HTTP data is available; if present, nothing
455 else will be done with the HTTP data.  This procedure gets two additional
456 arguments: the socket for the HTTP data and the \fItoken\fR returned from
457 \fB::http::geturl\fR.  The token is the name of a global array that is
458 described in the \fBSTATE ARRAY\fR section.  The procedure is expected
459 to return the number of bytes read from the socket.  Here is a
460 template for the callback:
461 .RS
462 .PP
463 .CS
464 proc httpHandlerCallback {socket token} {
465     upvar #0 $token state
466     # Access socket, and state as a Tcl array
467     # For example...
468     ...
469     set data [read $socket 1000]
470     set nbytes [string length $data]
471     ...
472     return $nbytes
473 }
474 .CE
475 .RE
476 .TP
477 \fB\-headers\fR \fIkeyvaluelist\fR
478 .
479 This option is used to add headers not already specified
480 by \fB::http::config\fR to the HTTP request.  The
481 \fIkeyvaluelist\fR argument must be a list with an even number of
482 elements that alternate between keys and values.  The keys become
483 header field names.  Newlines are stripped from the values so the
484 header cannot be corrupted.  For example, if \fIkeyvaluelist\fR is
485 \fBPragma no-cache\fR then the following header is included in the
486 HTTP request:
487 .RS
488 .PP
489 .CS
490 Pragma: no-cache
491 .CE
492 .RE
493 .TP
494 \fB\-keepalive\fR \fIboolean\fR
495 .
496 If true, attempt to keep the connection open for servicing
497 multiple requests.  Default is 0.
498 .TP
499 \fB\-method\fR \fItype\fR
500 .
501 Force the HTTP request method to \fItype\fR. \fB::http::geturl\fR will
502 auto-select GET, POST or HEAD based on other options, but this option
503 enables choices like PUT and DELETE for webdav support.
504 .TP
505 \fB\-myaddr\fR \fIaddress\fR
506 .
507 Pass an specific local address to the underlying \fBsocket\fR call in case
508 multiple interfaces are available.
509 .TP
510 \fB\-progress\fR \fIcallback\fR
511 .
512 The \fIcallback\fR is made after each transfer of data from the URL.
513 The callback gets three additional arguments: the \fItoken\fR from
514 \fB::http::geturl\fR, the expected total size of the contents from the
515 \fBContent-Length\fR meta-data, and the current number of bytes
516 transferred so far.  The expected total size may be unknown, in which
517 case zero is passed to the callback.  Here is a template for the
518 progress callback:
519 .RS
520 .PP
521 .CS
522 proc httpProgress {token total current} {
523     upvar #0 $token state
524 }
525 .CE
526 .RE
527 .TP
528 \fB\-protocol\fR \fIversion\fR
529 .
530 Select the HTTP protocol version to use. This should be 1.0 or 1.1 (the
531 default). Should only be necessary for servers that do not understand or
532 otherwise complain about HTTP/1.1.
533 .TP
534 \fB\-query\fR \fIquery\fR
535 .
536 This flag causes \fB::http::geturl\fR to do a POST request that passes the
537 \fIquery\fR to the server. The \fIquery\fR must be an x-url-encoding
538 formatted query.  The \fB::http::formatQuery\fR procedure can be used to
539 do the formatting.
540 .TP
541 \fB\-queryblocksize\fR \fIsize\fR
542 .
543 The block size used when posting query data to the URL.
544 At most 
545 \fIsize\fR
546 bytes are written at once.  After each block, a call to the
547 \fB\-queryprogress\fR
548 callback is made (if that option is specified).
549 .TP
550 \fB\-querychannel\fR \fIchannelID\fR
551 .
552 This flag causes \fB::http::geturl\fR to do a POST request that passes the
553 data contained in \fIchannelID\fR to the server. The data contained in
554 \fIchannelID\fR must be an x-url-encoding
555 formatted query unless the \fB\-type\fR option below is used.
556 If a Content-Length header is not specified via the \fB\-headers\fR options,
557 \fB::http::geturl\fR attempts to determine the size of the post data
558 in order to create that header.  If it is
559 unable to determine the size, it returns an error.
560 .TP
561 \fB\-queryprogress\fR \fIcallback\fR
562 .
563 The \fIcallback\fR is made after each transfer of data to the URL
564 (i.e. POST) and acts exactly like the \fB\-progress\fR option (the
565 callback format is the same).
566 .TP
567 \fB\-strict\fR \fIboolean\fR
568 .
569 Whether to enforce RFC 3986 URL validation on the request.  Default is 1.
570 .TP
571 \fB\-timeout\fR \fImilliseconds\fR
572 .
573 If \fImilliseconds\fR is non-zero, then \fB::http::geturl\fR sets up a timeout
574 to occur after the specified number of milliseconds.
575 A timeout results in a call to \fB::http::reset\fR and to
576 the \fB\-command\fR callback, if specified.
577 The return value of \fB::http::status\fR is \fBtimeout\fR
578 after a timeout has occurred.
579 .TP
580 \fB\-type\fR \fImime-type\fR
581 .
582 Use \fImime-type\fR as the \fBContent-Type\fR value, instead of the
583 default value (\fBapplication/x-www-form-urlencoded\fR) during a
584 POST operation.
585 .TP
586 \fB\-validate\fR \fIboolean\fR
587 .
588 If \fIboolean\fR is non-zero, then \fB::http::geturl\fR does an HTTP HEAD
589 request.  This request returns meta information about the URL, but the
590 contents are not returned.  The meta information is available in the
591 \fBstate(meta) \fR variable after the transaction.  See the
592 \fBSTATE ARRAY\fR section for details.
593 .RE
594 .TP
595 \fB::http::formatQuery\fR \fIkey value\fR ?\fIkey value\fR ...?
596 .
597 This procedure does x-url-encoding of query data.  It takes an even
598 number of arguments that are the keys and values of the query.  It
599 encodes the keys and values, and generates one string that has the
600 proper & and = separators.  The result is suitable for the
601 \fB\-query\fR value passed to \fB::http::geturl\fR.
602 .TP
603 \fB::http::reset\fR \fItoken\fR ?\fIwhy\fR?
604 .
605 This command resets the HTTP transaction identified by \fItoken\fR, if any.
606 This sets the \fBstate(status)\fR value to \fIwhy\fR, which defaults to
607 \fBreset\fR, and then calls the registered \fB\-command\fR callback.
608 .TP
609 \fB::http::wait\fR \fItoken\fR
610 .
611 This is a convenience procedure that blocks and waits for the
612 transaction to complete.  This only works in trusted code because it
613 uses \fBvwait\fR.  Also, it is not useful for the case where
614 \fB::http::geturl\fR is called \fIwithout\fR the \fB\-command\fR option
615 because in this case the \fB::http::geturl\fR call does not return
616 until the HTTP transaction is complete, and thus there is nothing to
617 wait for.
618 .TP
619 \fB::http::data\fR \fItoken\fR
620 .
621 This is a convenience procedure that returns the \fBbody\fR element
622 (i.e., the URL data) of the state array.
623 .TP
624 \fB::http::error\fR \fItoken\fR
625 .
626 This is a convenience procedure that returns the \fBerror\fR element
627 of the state array.
628 .TP
629 \fB::http::status\fR \fItoken\fR
630 .
631 This is a convenience procedure that returns the \fBstatus\fR element of
632 the state array.
633 .TP
634 \fB::http::code\fR \fItoken\fR
635 .
636 This is a convenience procedure that returns the \fBhttp\fR element of the
637 state array.
638 .TP
639 \fB::http::ncode\fR \fItoken\fR
640 .
641 This is a convenience procedure that returns just the numeric return
642 code (200, 404, etc.) from the \fBhttp\fR element of the state array.
643 .TP
644 \fB::http::size\fR \fItoken\fR
645 .
646 This is a convenience procedure that returns the \fBcurrentsize\fR
647 element of the state array, which represents the number of bytes
648 received from the URL in the \fB::http::geturl\fR call.
649 .TP
650 \fB::http::meta\fR \fItoken\fR
651 .
652 This is a convenience procedure that returns the \fBmeta\fR
653 element of the state array which contains the HTTP response
654 headers. See below for an explanation of this element.
655 .TP
656 \fB::http::cleanup\fR \fItoken\fR
657 .
658 This procedure cleans up the state associated with the connection
659 identified by \fItoken\fR.  After this call, the procedures
660 like \fB::http::data\fR cannot be used to get information
661 about the operation.  It is \fIstrongly\fR recommended that you call
662 this function after you are done with a given HTTP request.  Not doing
663 so will result in memory not being freed, and if your app calls
664 \fB::http::geturl\fR enough times, the memory leak could cause a
665 performance hit...or worse.
666 .TP
667 \fB::http::register\fR \fIproto port command\fR
668 .
669 This procedure allows one to provide custom HTTP transport types
670 such as HTTPS, by registering a prefix, the default port, and the
671 command to execute to create the Tcl \fBchannel\fR. E.g.:
672 .RS
673 .PP
674 .CS
675 package require http
676 package require tls
677
678 ::http::register https 443 ::tls::socket
679
680 set token [::http::geturl https://my.secure.site/]
681 .CE
682 .RE
683 .TP
684 \fB::http::unregister\fR \fIproto\fR
685 .
686 This procedure unregisters a protocol handler that was previously
687 registered via \fB::http::register\fR.
688 .SH ERRORS
689 The \fB::http::geturl\fR procedure will raise errors in the following cases:
690 invalid command line options,
691 an invalid URL,
692 a URL on a non-existent host,
693 or a URL at a bad port on an existing host.
694 These errors mean that it
695 cannot even start the network transaction.
696 It will also raise an error if it gets an I/O error while
697 writing out the HTTP request header.
698 For synchronous \fB::http::geturl\fR calls (where \fB\-command\fR is
699 not specified), it will raise an error if it gets an I/O error while
700 reading the HTTP reply headers or data.  Because \fB::http::geturl\fR
701 does not return a token in these cases, it does all the required
702 cleanup and there is no issue of your app having to call
703 \fB::http::cleanup\fR.
704 .PP
705 For asynchronous \fB::http::geturl\fR calls, all of the above error
706 situations apply, except that if there is any error while reading the
707 HTTP reply headers or data, no exception is thrown.  This is because
708 after writing the HTTP headers, \fB::http::geturl\fR returns, and the
709 rest of the HTTP transaction occurs in the background.  The command
710 callback can check if any error occurred during the read by calling
711 \fB::http::status\fR to check the status and if its \fIerror\fR,
712 calling \fB::http::error\fR to get the error message.
713 .PP
714 Alternatively, if the main program flow reaches a point where it needs
715 to know the result of the asynchronous HTTP request, it can call
716 \fB::http::wait\fR and then check status and error, just as the
717 callback does.
718 .PP
719 In any case, you must still call
720 \fB::http::cleanup\fR to delete the state array when you are done.
721 .PP
722 There are other possible results of the HTTP transaction
723 determined by examining the status from \fB::http::status\fR.
724 These are described below.
725 .TP
726 \fBok\fR
727 .
728 If the HTTP transaction completes entirely, then status will be \fBok\fR.
729 However, you should still check the \fB::http::code\fR value to get
730 the HTTP status.  The \fB::http::ncode\fR procedure provides just
731 the numeric error (e.g., 200, 404 or 500) while the \fB::http::code\fR
732 procedure returns a value like
733 .QW "HTTP 404 File not found" .
734 .TP
735 \fBeof\fR
736 .
737 If the server closes the socket without replying, then no error
738 is raised, but the status of the transaction will be \fBeof\fR.
739 .TP
740 \fBerror\fR
741 .
742 The error message will also be stored in the \fBerror\fR status
743 array element, accessible via \fB::http::error\fR.
744 .PP
745 Another error possibility is that \fB::http::geturl\fR is unable to
746 write all the post query data to the server before the server
747 responds and closes the socket.
748 The error message is saved in the \fBposterror\fR status array
749 element and then  \fB::http::geturl\fR attempts to complete the
750 transaction.
751 If it can read the server's response
752 it will end up with an \fBok\fR status, otherwise it will have
753 an \fBeof\fR status.
754 .SH "STATE ARRAY"
755 The \fB::http::geturl\fR procedure returns a \fItoken\fR that can be used to
756 get to the state of the HTTP transaction in the form of a Tcl array.
757 Use this construct to create an easy-to-use array variable:
758 .PP
759 .CS
760 upvar #0 $token state
761 .CE
762 .PP
763 Once the data associated with the URL is no longer needed, the state
764 array should be unset to free up storage.
765 The \fB::http::cleanup\fR procedure is provided for that purpose.
766 The following elements of
767 the array are supported:
768 .RS
769 .TP
770 \fBbody\fR
771 .
772 The contents of the URL.  This will be empty if the \fB\-channel\fR
773 option has been specified.  This value is returned by the \fB::http::data\fR command.
774 .TP
775 \fBcharset\fR
776 .
777 The value of the charset attribute from the \fBContent-Type\fR meta-data
778 value.  If none was specified, this defaults to the RFC standard
779 \fBiso8859-1\fR, or the value of \fB$::http::defaultCharset\fR.  Incoming
780 text data will be automatically converted from this charset to utf-8.
781 .TP
782 \fBcoding\fR
783 .
784 A copy of the \fBContent-Encoding\fR meta-data value.
785 .TP
786 \fBcurrentsize\fR
787 .
788 The current number of bytes fetched from the URL.
789 This value is returned by the \fB::http::size\fR command.
790 .TP
791 \fBerror\fR
792 .
793 If defined, this is the error string seen when the HTTP transaction
794 was aborted.
795 .TP
796 \fBhttp\fR
797 .
798 The HTTP status reply from the server.  This value
799 is returned by the \fB::http::code\fR command.  The format of this value is:
800 .RS
801 .PP
802 .CS
803 \fIHTTP/1.1 code string\fR
804 .CE
805 .PP
806 The \fIcode\fR is a three-digit number defined in the HTTP standard.
807 A code of 200 is OK.  Codes beginning with 4 or 5 indicate errors.
808 Codes beginning with 3 are redirection errors.  In this case the
809 \fBLocation\fR meta-data specifies a new URL that contains the
810 requested information.
811 .RE
812 .TP
813 \fBmeta\fR
814 .
815 The HTTP protocol returns meta-data that describes the URL contents.
816 The \fBmeta\fR element of the state array is a list of the keys and
817 values of the meta-data.  This is in a format useful for initializing
818 an array that just contains the meta-data:
819 .RS
820 .PP
821 .CS
822 array set meta $state(meta)
823 .CE
824 .PP
825 Some of the meta-data keys are listed below, but the HTTP standard defines
826 more, and servers are free to add their own.
827 .TP
828 \fBContent-Type\fR
829 .
830 The type of the URL contents.  Examples include \fBtext/html\fR,
831 \fBimage/gif,\fR \fBapplication/postscript\fR and
832 \fBapplication/x-tcl\fR.
833 .TP
834 \fBContent-Length\fR
835 .
836 The advertised size of the contents.  The actual size obtained by
837 \fB::http::geturl\fR is available as \fBstate(currentsize)\fR.
838 .TP
839 \fBLocation\fR
840 .
841 An alternate URL that contains the requested data.
842 .RE
843 .TP
844 \fBposterror\fR
845 .
846 The error, if any, that occurred while writing
847 the post query data to the server.
848 .TP
849 \fBstatus\fR
850 .
851 Either \fBok\fR, for successful completion, \fBreset\fR for
852 user-reset, \fBtimeout\fR if a timeout occurred before the transaction
853 could complete, or \fBerror\fR for an error condition.  During the
854 transaction this value is the empty string.
855 .TP
856 \fBtotalsize\fR
857 .
858 A copy of the \fBContent-Length\fR meta-data value.
859 .TP
860 \fBtype\fR
861 .
862 A copy of the \fBContent-Type\fR meta-data value.
863 .TP
864 \fBurl\fR
865 .
866 The requested URL.
867 .RE
868 .SH EXAMPLE
869 .PP
870 This example creates a procedure to copy a URL to a file while printing a
871 progress meter, and prints the meta-data associated with the URL.
872 .PP
873 .CS
874 proc httpcopy { url file {chunk 4096} } {
875     set out [open $file w]
876     set token [\fB::http::geturl\fR $url -channel $out \e
877             -progress httpCopyProgress -blocksize $chunk]
878     close $out
879
880     # This ends the line started by httpCopyProgress
881     puts stderr ""
882
883     upvar #0 $token state
884     set max 0
885     foreach {name value} $state(meta) {
886         if {[string length $name] > $max} {
887             set max [string length $name]
888         }
889         if {[regexp -nocase ^location$ $name]} {
890             # Handle URL redirects
891             puts stderr "Location:$value"
892             return [httpcopy [string trim $value] $file $chunk]
893         }
894     }
895     incr max
896     foreach {name value} $state(meta) {
897         puts [format "%-*s %s" $max $name: $value]
898     }
899
900     return $token
901 }
902 proc httpCopyProgress {args} {
903     puts -nonewline stderr .
904     flush stderr
905 }
906 .CE
907 .SH "SEE ALSO"
908 safe(n), socket(n), safesock(n)
909 .SH KEYWORDS
910 internet, security policy, socket, www
911 '\" Local Variables:
912 '\" mode: nroff
913 '\" End: