1http(n)                      Tcl Bundled Packages                      http(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       http - Client-side implementation of the HTTP/1.0 protocol.
9

SYNOPSIS

11       package require http ?2.5?
12
13       ::http::config ?options?
14
15       ::http::geturl url ?options?
16
17       ::http::formatQuery key value ?key value ...?
18
19       ::http::reset token ?why?
20
21       ::http::wait token
22
23       ::http::status token
24
25       ::http::size token
26
27       ::http::code token
28
29       ::http::ncode token
30
31       ::http::data token
32
33       ::http::error token
34
35       ::http::cleanup token
36
37       ::http::register proto port command
38
39       ::http::unregister proto
40_________________________________________________________________
41
42

DESCRIPTION

44       The  http  package  provides  the client side of the HTTP/1.0 protocol.
45       The package implements the GET, POST, and HEAD operations of  HTTP/1.0.
46       It  allows configuration of a proxy host to get through firewalls.  The
47       package is compatible with the Safesock security policy, so it  can  be
48       used  by  untrusted applets to do URL fetching from a restricted set of
49       hosts. This package can be extended to support additional  HTTP  trans‐
50       port  protocols,  such  as HTTPS, by providing a custom socket command,
51       via ::http::register.
52
53       The ::http::geturl procedure does  a  HTTP  transaction.   Its  options
54       determine  whether  a GET, POST, or HEAD transaction is performed.  The
55       return value of ::http::geturl is a token  for  the  transaction.   The
56       value  is  also  the name of an array in the ::http namespace that con‐
57       tains state information about the transaction.  The  elements  of  this
58       array are described in the STATE ARRAY section.
59
60       If the -command option is specified, then the HTTP operation is done in
61       the background.  ::http::geturl returns  immediately  after  generating
62       the  HTTP request and the callback is invoked when the transaction com‐
63       pletes.  For this to work, the Tcl event loop must be  active.   In  Tk
64       applications this is always true.  For pure-Tcl applications, the call‐
65       er can use ::http::wait after calling ::http::geturl to start the event
66       loop.
67

COMMANDS

69       ::http::config ?options?
70              The  ::http::config command is used to set and query the name of
71              the proxy server and port, and the User-Agent name used  in  the
72              HTTP  requests.   If  no options are specified, then the current
73              configuration is returned.  If a single argument  is  specified,
74              then  it  should  be  one of the flags described below.  In this
75              case the current value of that setting is returned.   Otherwise,
76              the  options should be a set of flags and values that define the
77              configuration:
78
79              -accept mimetypes
80                     The Accept header of the request.  The  default  is  */*,
81                     which  means  that  all  types of documents are accepted.
82                     Otherwise you can supply a comma-separated list  of  mime
83                     type patterns that you are willing to receive.  For exam‐
84                     ple, "image/gif, image/jpeg, text/*".
85
86              -proxyhost hostname
87                     The name of the proxy host, if any.  If this value is the
88                     empty string, the URL host is contacted directly.
89
90              -proxyport number
91                     The proxy port number.
92
93              -proxyfilter command
94                     The   command   is   a   callback  that  is  made  during
95                     ::http::geturl to determine if a proxy is required for  a
96                     given  host.  One argument, a host name, is added to com‐
97                     mand when it is invoked.  If a  proxy  is  required,  the
98                     callback  should return a two-element list containing the
99                     proxy server and proxy port.  Otherwise the filter should
100                     return  an  empty  list.   The default filter returns the
101                     values of the -proxyhost and -proxyport settings if  they
102                     are non-empty.
103
104              -urlencoding encoding
105                     The  encoding  used  for  creating the x-url-encoded URLs
106                     with ::http::formatQuery.  The default is utf-8, as spec‐
107                     ified  by  RFC 2718.  Prior to http 2.5 this was unspeci‐
108                     fied, and that behavior can be returned by specifying the
109                     empty  string  ({}), although iso8859-1 is recommended to
110                     restore similar behavior but without the  ::http::format‐
111                     Query  throwing  an  error processing non-latin-1 charac‐
112                     ters.
113
114              -useragent string
115                     The value of the User-Agent header in the  HTTP  request.
116                     The default is "Tcl http client package 2.4."
117
118       ::http::geturl url ?options?
119              The ::http::geturl command is the main procedure in the package.
120              The -query option causes a  POST  operation  and  the  -validate
121              option  causes  a  HEAD operation; otherwise, a GET operation is
122              performed.  The ::http::geturl command  returns  a  token  value
123              that  can be used to get information about the transaction.  See
124              the  STATE  ARRAY  and  ERRORS   section   for   details.    The
125              ::http::geturl  command  blocks  until  the operation completes,
126              unless the -command option specifies a callback that is  invoked
127              when  the HTTP transaction completes.  ::http::geturl takes sev‐
128              eral options:
129
130              -binary boolean
131                     Specifies whether to force interpreting the URL  data  as
132                     binary.   Normally  this  is  auto-detected (anything not
133                     beginning with a  text  content  type  or  whose  content
134                     encoding is gzip or compress is considered binary data).
135
136              -blocksize size
137                     The  block  size used when reading the URL.  At most size
138                     bytes are read at once.  After each block, a call to  the
139                     -progress callback is made (if that option is specified).
140
141              -channel name
142                     Copy  the  URL contents to channel name instead of saving
143                     it in state(body).
144
145              -command callback
146                     Invoke callback after  the  HTTP  transaction  completes.
147                     This  option causes ::http::geturl to return immediately.
148                     The callback gets an  additional  argument  that  is  the
149                     token  returned  from  ::http::geturl.  This token is the
150                     name of an array that is described  in  the  STATE  ARRAY
151                     section.  Here is a template for the callback:
152                            proc httpCallback {token} {
153                                upvar #0 $token state
154                                # Access state as a Tcl array
155                            }
156
157              -handler callback
158                     Invoke  callback  whenever  HTTP  data  is  available; if
159                     present, nothing else will be done with  the  HTTP  data.
160                     This  procedure gets two additional arguments: the socket
161                     for  the  HTTP  data  and   the   token   returned   from
162                     ::http::geturl.   The token is the name of a global array
163                     that is described in the STATE ARRAY section.  The proce‐
164                     dure  is expected to return the number of bytes read from
165                     the socket.  Here is a template for the callback:
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
175              -headers keyvaluelist
176                     This option is used to add  extra  headers  to  the  HTTP
177                     request.   The  keyvaluelist argument must be a list with
178                     an even number of elements that  alternate  between  keys
179                     and  values.   The  keys become header field names.  New‐
180                     lines are stripped from the values so the  header  cannot
181                     be corrupted.  For example, if keyvaluelist is Pragma no-
182                     cache then the following header is included in  the  HTTP
183                     request:
184                     Pragma: no-cache
185
186              -progress callback
187                     The callback is made after each transfer of data from the
188                     URL.  The callback gets three additional  arguments:  the
189                     token from ::http::geturl, the expected total size of the
190                     contents from the Content-Length meta-data, and the  cur‐
191                     rent  number  of  bytes transferred so far.  The expected
192                     total size may be unknown, in which case zero  is  passed
193                     to  the  callback.   Here  is a template for the progress
194                     callback:
195                            proc httpProgress {token total current} {
196                                upvar #0 $token state
197                            }
198
199              -query query
200                     This flag causes ::http::geturl to do a POST request that
201                     passes  the  query to the server. The query must be an x-
202                     url-encoding formatted  query.   The  ::http::formatQuery
203                     procedure can be used to do the formatting.
204
205              -queryblocksize size
206                     The  block  size used when posting query data to the URL.
207                     At most size bytes  are  written  at  once.   After  each
208                     block,  a call to the -queryprogress callback is made (if
209                     that option is specified).
210
211              -querychannel channelID
212                     This flag causes ::http::geturl to do a POST request that
213                     passes the data contained in channelID to the server. The
214                     data contained in channelID  must  be  an  x-url-encoding
215                     formatted  query  unless  the -type option below is used.
216                     If a Content-Length  header  is  not  specified  via  the
217                     -headers  options,  ::http::geturl  attempts to determine
218                     the size of the post data in order to create that header.
219                     If  it  is  unable  to  determine the size, it returns an
220                     error.
221
222              -queryprogress callback
223                     The callback is made after each transfer of data  to  the
224                     URL  (i.e.  POST)  and  acts  exactly  like the -progress
225                     option (the callback format is the same).
226
227              -timeout milliseconds
228                     If milliseconds is non-zero, then ::http::geturl sets  up
229                     a  timeout  to  occur  after the specified number of mil‐
230                     liseconds.  A timeout results in a call to  ::http::reset
231                     and  to  the -command callback, if specified.  The return
232                     value of ::http::status is timeout after  a  timeout  has
233                     occurred.
234
235              -type mime-type
236                     Use  mime-type  as the Content-Type value, instead of the
237                     default value (application/x-www-form-urlencoded)  during
238                     a POST operation.
239
240              -validate boolean
241                     If  boolean is non-zero, then ::http::geturl does an HTTP
242                     HEAD request.   This  request  returns  meta  information
243                     about  the  URL,  but the contents are not returned.  The
244                     meta information is available in the  state(meta)   vari‐
245                     able  after the transaction.  See the STATE ARRAY section
246                     for details.
247
248       ::http::formatQuery key value ?key value ...?
249              This procedure does x-url-encoding of query data.  It  takes  an
250              even  number  of  arguments  that are the keys and values of the
251              query.  It encodes the keys and values, and generates one string
252              that  has the proper & and = separators.  The result is suitable
253              for the -query value passed to ::http::geturl.
254
255       ::http::reset token ?why?
256              This command resets the HTTP transaction identified by token, if
257              any.   This  sets the state(status) value to why, which defaults
258              to reset, and then calls the registered -command callback.
259
260       ::http::wait token
261              This is a convenience procedure that blocks and  waits  for  the
262              transaction  to  complete.   This  only  works  in  trusted code
263              because it uses vwait.  Also, it's not useful for the case where
264              ::http::geturl  is called without the -command option because in
265              this case the ::http::geturl call doesn't return until the  HTTP
266              transaction is complete, and thus there's nothing to wait for.
267
268       ::http::data token
269              This  is  a  convenience procedure that returns the body element
270              (i.e., the URL data) of the state array.
271
272       ::http::error token
273              This is a convenience procedure that returns the  error  element
274              of the state array.
275
276       ::http::status token
277              This  is a convenience procedure that returns the status element
278              of the state array.
279
280       ::http::code token
281              This is a convenience procedure that returns the http element of
282              the state array.
283
284       ::http::ncode token
285              This  is  a  convenience procedure that returns just the numeric
286              return code (200, 404, etc.) from the http element of the  state
287              array.
288
289       ::http::size token
290              This  is  a  convenience  procedure that returns the currentsize
291              element of the state array, which represents the number of bytes
292              received from the URL in the ::http::geturl call.
293
294       ::http::cleanup token
295              This  procedure  cleans up the state associated with the connec‐
296              tion identified by token.  After this call, the procedures  like
297              ::http::data  cannot be used to get information about the opera‐
298              tion.  It is strongly recommended that you  call  this  function
299              after  you're done with a given HTTP request.  Not doing so will
300              result in  memory  not  being  freed,  and  if  your  app  calls
301              ::http::geturl  enough times, the memory leak could cause a per‐
302              formance hit...or worse.
303
304       ::http::register proto port command
305              This procedure allows one to provide custom HTTP transport types
306              such  as  HTTPS,  by registering a prefix, the default port, and
307              the command to execute to create the Tcl channel. E.g.:
308                     package require http
309                     package require tls
310
311                     ::http::register https 443 ::tls::socket
312
313                     set token [::http::geturl https://my.secure.site/]
314
315       ::http::unregister proto
316              This procedure unregisters a protocol handler  that  was  previ‐
317              ously registered via ::http::register.
318
319

ERRORS

321       The  ::http::geturl procedure will raise errors in the following cases:
322       invalid command line options, an invalid URL, a URL on  a  non-existent
323       host,  or  a  URL at a bad port on an existing host.  These errors mean
324       that it cannot even start the network transaction.  It will also  raise
325       an  error  if  it  gets an I/O error while writing out the HTTP request
326       header.  For synchronous ::http::geturl calls (where  -command  is  not
327       specified),  it will raise an error if it gets an I/O error while read‐
328       ing the HTTP reply headers or  data.   Because  ::http::geturl  doesn't
329       return  a  token  in  these cases, it does all the required cleanup and
330       there's no issue of your app having to call ::http::cleanup.
331
332       For asynchronous ::http::geturl calls, all of the  above  error  situa‐
333       tions  apply,  except  that if there's any error while reading the HTTP
334       reply headers or data, no exception is thrown.  This is  because  after
335       writing  the  HTTP headers, ::http::geturl returns, and the rest of the
336       HTTP transaction occurs in the background.  The  command  callback  can
337       check  if  any error occurred during the read by calling ::http::status
338       to check the status and if its error, calling ::http::error to get  the
339       error message.
340
341       Alternatively,  if the main program flow reaches a point where it needs
342       to know the result of  the  asynchronous  HTTP  request,  it  can  call
343       ::http::wait  and  then  check  status  and error, just as the callback
344       does.
345
346       In any case, you must still call ::http::cleanup to  delete  the  state
347       array when you're done.
348
349       There  are other possible results of the HTTP transaction determined by
350       examining the status from ::http::status.  These are described below.
351
352       ok     If the HTTP transaction completes entirely, then status will  be
353              ok.   However,  you should still check the ::http::code value to
354              get the HTTP status.  The ::http::ncode procedure provides  just
355              the numeric error (e.g., 200, 404 or 500) while the ::http::code
356              procedure returns a value like "HTTP 404 File not found".
357
358       eof    If the server closes the socket without replying, then no  error
359              is raised, but the status of the transaction will be eof.
360
361       error  The  error message will also be stored in the error status array
362              element, accessible via ::http::error.
363
364       Another error possibility is that ::http::geturl is unable to write all
365       the post query data to the server before the server responds and closes
366       the socket.  The error message is saved in the posterror  status  array
367       element  and then  ::http::geturl attempts to complete the transaction.
368       If it can read the server's response it will end up with an ok  status,
369       otherwise it will have an eof status.
370
371

STATE ARRAY

373       The ::http::geturl procedure returns a token that can be used to get to
374       the state of the HTTP transaction in the form of a Tcl array.  Use this
375       construct to create an easy-to-use array variable:
376              upvar #0 $token state
377       Once  the  data  associated with the URL is no longer needed, the state
378       array should be unset to free up storage.  The  ::http::cleanup  proce‐
379       dure is provided for that purpose.  The following elements of the array
380       are supported:
381
382              body   The contents of the URL.   This  will  be  empty  if  the
383                     -channel  option  has  been  specified.   This  value  is
384                     returned by the ::http::data command.
385
386              charset
387                     The value of the charset attribute from the  Content-Type
388                     meta-data value.  If none was specified, this defaults to
389                     the   RFC   standard   iso8859-1,   or   the   value   of
390                     $::http::defaultCharset.   Incoming  text  data  will  be
391                     automatically converted from this charset to utf-8.
392
393              coding A copy of the Content-Encoding meta-data value.
394
395              currentsize
396                     The current number of bytes fetched from the  URL.   This
397                     value is returned by the ::http::size command.
398
399              error  If  defined,  this is the error string seen when the HTTP
400                     transaction was aborted.
401
402              http   The HTTP status reply from the  server.   This  value  is
403                     returned by the ::http::code command.  The format of this
404                     value is:
405                            HTTP/1.0 code string
406                     The code is a three-digit  number  defined  in  the  HTTP
407                     standard.   A  code of 200 is OK.  Codes beginning with 4
408                     or 5 indicate errors.  Codes beginning with 3  are  redi‐
409                     rection  errors.   In  this  case  the Location meta-data
410                     specifies a new URL that contains the requested  informa‐
411                     tion.
412
413              meta   The  HTTP  protocol  returns meta-data that describes the
414                     URL contents.  The meta element of the state array  is  a
415                     list of the keys and values of the meta-data.  This is in
416                     a format useful for initializing an array that just  con‐
417                     tains the meta-data:
418                            array set meta $state(meta)
419                     Some of the meta-data keys are listed below, but the HTTP
420                     standard defines more, and servers are free to add  their
421                     own.
422
423                     Content-Type
424                            The  type  of  the URL contents.  Examples include
425                            text/html, image/gif,  application/postscript  and
426                            application/x-tcl.
427
428                     Content-Length
429                            The  advertised  size of the contents.  The actual
430                            size obtained by ::http::geturl  is  available  as
431                            state(size).
432
433                     Location
434                            An alternate URL that contains the requested data.
435
436              posterror
437                     The  error,  if any, that occurred while writing the post
438                     query data to the server.
439
440              status Either ok, for successful  completion,  reset  for  user-
441                     reset,  timeout if a timeout occurred before the transac‐
442                     tion could complete, or error  for  an  error  condition.
443                     During the transaction this value is the empty string.
444
445              totalsize
446                     A copy of the Content-Length meta-data value.
447
448              type   A copy of the Content-Type meta-data value.
449
450              url    The requested URL.
451

EXAMPLE

453              # Copy a URL to a file and print meta-data
454              proc httpcopy { url file {chunk 4096} } {
455                 set out [open $file w]
456                 set token [::http::geturl $url -channel $out \
457                        -progress httpCopyProgress -blocksize $chunk]
458                 close $out
459
460                 # This ends the line started by httpCopyProgress
461                 puts stderr ""
462
463                 upvar #0 $token state
464                 set max 0
465                 foreach {name value} $state(meta) {
466                    if {[string length $name] > $max} {
467                       set max [string length $name]
468                    }
469                    if {[regexp -nocase ^location$ $name]} {
470                       # Handle URL redirects
471                       puts stderr "Location:$value"
472                       return [httpcopy [string trim $value] $file $chunk]
473                    }
474                 }
475                 incr max
476                 foreach {name value} $state(meta) {
477                    puts [format "%-*s %s" $max $name: $value]
478                 }
479
480                 return $token
481              }
482              proc httpCopyProgress {args} {
483                 puts -nonewline stderr .
484                 flush stderr
485              }
486
487

SEE ALSO

489       safe(n), socket(n), safesock(n)
490
491

KEYWORDS

493       security policy, socket
494
495
496
497http                                  2.5                              http(n)
Impressum