1mNAME0m
       Http - Client-side implementation of the HTTP/1.0 protocol.

1mSYNOPSIS0m
       1mpackage require http ?2.3?0m

       1m::http::config 4m22m?options?0m

       1m::http::geturl 4m22murl24m 4m?options?0m

       1m::http::formatQuery 4m22mlist0m

       1m::http::reset 4m22mtoken0m

       1m::http::wait 4m22mtoken0m

       1m::http::status 4m22mtoken0m

       1m::http::size 4m22mtoken0m

       1m::http::code 4m22mtoken0m

       1m::http::ncode 4m22mtoken0m

       1m::http::data 4m22mtoken0m

       1m::http::error 4m22mtoken0m

       1m::http::cleanup 4m22mtoken0m

       1m::http::register 4m22mproto24m 4mport24m 4mcommand0m

       1m::http::unregister 4m22mproto0m


1mDESCRIPTION0m
       The  1mhttp  22mpackage  provides  the client side of the HTTP/1.0 protocol.
       The package implements the GET, POST, and HEAD operations of  HTTP/1.0.
       It  allows configuration of a proxy host to get through firewalls.  The
       package is compatible with the 1mSafesock 22msecurity policy, so it  can  be
       used  by  untrusted applets to do URL fetching from a restricted set of
       hosts. This package can be extened to support additional HTTP transport
       protocols,  such  as  HTTPS,  by providing a custom 1msocket 22mcommand, via
       1mhttp::register22m.

       The 1m::http::geturl 22mprocedure does  a  HTTP  transaction.   Its  4moptions0m
       determine  whether  a GET, POST, or HEAD transaction is performed.  The
       return value of 1m::http::geturl 22mis a token  for  the  transaction.   The
       value  is  also  the name of an array in the ::http namespace that con-
       tains state information about the transaction.  The  elements  of  this
       array are described in the STATE ARRAY section.

       If the 1m-command 22moption is specified, then the HTTP operation is done in
       the background.  1m::http::geturl 22mreturns  immediately  after  generating
       the  HTTP request and the callback is invoked when the transaction com-
       pletes.  For this to work, the Tcl event loop must be  active.   In  Tk
       applications  this  is  always  true.   For  pure-Tcl applications, the
       caller can use 1m::http::wait 22mafter calling 1m::http::geturl 22mto  start  the
       event loop.

1mCOMMANDS0m
       1m::http::config 22m?4moptions24m?
              The  1m::http::config 22mcommand is used to set and query the name of
              the proxy server and port, and the User-Agent name used  in  the
              HTTP  requests.   If  no options are specified, then the current
              configuration is returned.  If a single argument  is  specified,
              then  it  should  be  one of the flags described below.  In this
              case the current value of that setting is returned.   Otherwise,
              the  options should be a set of flags and values that define the
              configuration:

              1m-accept 4m22mmimetypes0m
                     The Accept header of the request.  The  default  is  */*,
                     which  means  that  all  types of documents are accepted.
                     Otherwise you can supply a comma separated list  of  mime
                     type patterns that you are willing to receive.  For exam-
                     ple, "image/gif, image/jpeg, text/*".

              1m-proxyhost 4m22mhostname0m
                     The name of the proxy host, if any.  If this value is the
                     empty string, the URL host is contacted directly.

              1m-proxyport 4m22mnumber0m
                     The proxy port number.

              1m-proxyfilter 4m22mcommand0m
                     The   command   is   a   callback  that  is  made  during
                     1m::http::geturl 22mto determine if a proxy is required for  a
                     given  host.  One argument, a host name, is added to 4mcom-0m
                     4mmand24m when it is invoked.  If a  proxy  is  required,  the
                     callback  should return a two element list containing the
                     proxy server and proxy port.  Otherwise the filter should
                     return  an  empty  list.   The default filter returns the
                     values of the 1m-proxyhost 22mand 1m-proxyport 22msettings if  they
                     are non-empty.

              1m-useragent 4m22mstring0m
                     The  value  of the User-Agent header in the HTTP request.
                     The default is 1m"Tcl http client package 2.2."0m

       1m::http::geturl 4m22murl24m ?4moptions24m?
              The 1m::http::geturl  22mcommand is the main procedure in  the  pack-
              age.   The  1m-query 22moption causes a POST operation and the 1m-vali-0m
              1mdate 22moption causes a HEAD operation; otherwise, a GET  operation
              is  performed.  The 1m::http::geturl 22mcommand returns a 4mtoken24m value
              that can be used to get information about the transaction.   See
              the   STATE   ARRAY   and   ERRORS  section  for  details.   The
              1m::http::geturl 22mcommand blocks  until  the  operation  completes,
              unless  the 1m-command 22moption specifies a callback that is invoked
              when the HTTP transaction completes.  1m::http::geturl 22mtakes  sev-
              eral options:

              1m-blocksize 4m22msize0m
                     The  blocksize  used  when reading the URL.  At most 4msize0m
                     bytes are read at once.  After each block, a call to  the
                     1m-progress 22mcallback is made (if that option is specified).

              1m-channel 4m22mname0m
                     Copy the URL contents to channel 4mname24m instead  of  saving
                     it in 1mstate(body)22m.

              1m-command 4m22mcallback0m
                     Invoke  4mcallback24m  after  the  HTTP transaction completes.
                     This option causes 1m::http::geturl 22mto return  immediately.
                     The  4mcallback24m  gets  an  additional  argument that is the
                     4mtoken24m returned from 1m::http::geturl22m.  This  token  is  the
                     name  of  an  array  that is described in the STATE ARRAY
                     section.  Here is a template for the callback:
                             proc httpCallback {token} {
                                 upvar #0 $token state
                                 # Access state as a Tcl array
                             }

              1m-handler 4m22mcallback0m
                     Invoke 4mcallback24m  whenever  HTTP  data  is  available;  if
                     present,  nothing  else  will be done with the HTTP data.
                     This procedure gets two additional arguments: the  socket
                     for   the   HTTP   data   and  the  4mtoken24m  returned  from
                     1m::http::geturl22m.  The token is the name of a global  array
                     that is described in the STATE ARRAY section.  The proce-
                     dure is expected to return the number of bytes read  from
                     the socket.  Here is a template for the callback:
                             proc httpHandlerCallback {socket token} {
                                 upvar #0 $token state
                                 # Access socket, and state as a Tcl array
                                 ...
                                 (example: set data [read $socket 1000];set nbytes [string length $data])
                                 ...
                                 return nbytes
                             }

              1m-headers 4m22mkeyvaluelist0m
                     This  option  is  used  to  add extra headers to the HTTP
                     request.  The 4mkeyvaluelist24m argument must be a  list  with
                     an  even  number  of elements that alternate between keys
                     and values.  The keys become header  field  names.   New-
                     lines  are  stripped from the values so the header cannot
                     be corrupted.  For example, if 4mkeyvaluelist24m is 1mPragma no-0m
                     1mcache  22mthen  the following header is included in the HTTP
                     request:
                     Pragma: no-cache

              1m-progress 4m22mcallback0m
                     The 4mcallback24m is made after each transfer of data from the
                     URL.   The  callback gets three additional arguments: the
                     4mtoken24m from 1m::http::geturl22m, the expected total size of the
                     contents  from the 1mContent-Length 22mmeta-data, and the cur-
                     rent number of bytes transferred so  far.   The  expected
                     total  size  may be unknown, in which case zero is passed
                     to the callback.  Here is a  template  for  the  progress
                     callback:
                             proc httpProgress {token total current} {
                                 upvar #0 $token state
                             }

              1m-query 4m22mquery0m
                     This flag causes 1m::http::geturl 22mto do a POST request that
                     passes the 4mquery24m to the server. The 4mquery24m must  be  a  x-
                     url-encoding  formatted  query.   The 1m::http::formatQuery0m
                     procedure can be used to do the formatting.

              1m-queryblocksize 4m22msize0m
                     The blocksize used when posting query data  to  the  URL.
                     At  most  4msize24m  bytes  are  written  at once.  After each
                     block, a call to the 1m-queryprogress 22mcallback is made  (if
                     that option is specified).

              1m-querychannel 4m22mchannelID0m
                     This flag causes 1m::http::geturl 22mto do a POST request that
                     passes the data contained in 4mchannelID24m to the server. The
                     data contained in 4mchannelID24m must be a x-url-encoding for-
                     matted query unless the 1m-type 22moption below is used.  If a
                     Content-Length  header  is not specified via the 1m-headers0m
                     options, 1m::http::geturl 22mattempts to determine the size of
                     the  post  data in order to create that header.  If it is
                     unable to determine the size, it returns an error.

              1m-queryprogress 4m22mcallback0m
                     The 4mcallback24m is made after each transfer of data  to  the
                     URL  (i.e.  POST)  and  acts  exactly  like the 1m-progress0m
                     option (the callback format is the same).

              1m-timeout 4m22mmilliseconds0m
                     If 4mmilliseconds24m is non-zero, then 1m::http::geturl 22msets  up
                     a  timeout  to  occur  after the specified number of mil-
                     liseconds.  A timeout results in a call to  1m::http::reset0m
                     and  to  the 1m-command 22mcallback, if specified.  The return
                     value of 1m::http::status 22mis 1mtimeout 22mafter  a  timeout  has
                     occurred.

              1m-type 4m22mmime-type0m
                     Use  4mmime-type24m  as the 1mContent-Type 22mvalue, instead of the
                     default value (1mapplication/x-www-form-urlencoded22m)  during
                     a POST operation.

              1m-validate 4m22mboolean0m
                     If  4mboolean24m is non-zero, then 1m::http::geturl 22mdoes an HTTP
                     HEAD request.   This  request  returns  meta  information
                     about  the  URL,  but the contents are not returned.  The
                     meta information is available in the  1mstate(meta)   22mvari-
                     able  after the transaction.  See the STATE ARRAY section
                     for details.

       1m::http::formatQuery 4m22mkey24m 4mvalue24m ?4mkey24m 4mvalue24m ...?
              This procedure does x-url-encoding of query data.  It  takes  an
              even  number  of  arguments  that are the keys and values of the
              query.  It encodes the keys and values, and generates one string
              that  has the proper & and = separators.  The result is suitable
              for the 1m-query 22mvalue passed to 1m::http::geturl22m.

       1m::http::reset 4m22mtoken24m ?4mwhy24m?
              This command resets the HTTP transaction identified by 4mtoken24m, if
              any.   This  sets the 1mstate(status) 22mvalue to 4mwhy24m, which defaults
              to 1mreset22m, and then calls the registered 1m-command 22mcallback.

       1m::http::wait 4m22mtoken0m
              This is a convenience procedure that blocks and  waits  for  the
              transaction  to  complete.   This  only  works  in  trusted code
              because it uses 1mvwait22m.  Also, it's not useful for the case where
              1m::http::geturl  22mis called 4mwithout24m the 1m-command 22moption because in
              this case the 1m::http::geturl 22mcall doesn't return until the  HTTP
              transaction is complete, and thus there's nothing to wait for.

       1m::http::data 4m22mtoken0m
              This  is  a  convenience procedure that returns the 1mbody 22melement
              (i.e., the URL data) of the state array.

       1m::http::error 4m22mtoken0m
              This is a convenience procedure that returns the  1merror  22melement
              of the state array.

       1m::http::status 4m22mtoken0m
              This  is a convenience procedure that returns the 1mstatus 22melement
              of the state array.

       1m::http::code 4m22mtoken0m
              This is a convenience procedure that returns the 1mhttp 22melement of
              the state array.

       1m::http::ncode 4m22mtoken0m
              This  is  a  convenience procedure that returns just the numeric
              return code (200, 404, etc.) from the 1mhttp 22melement of the  state
              array.

       1m::http::size 4m22mtoken0m
              This  is  a  convenience  procedure that returns the 1mcurrentsize0m
              element of the state array, which represents the number of bytes
              received from the URL in the 1m::http::geturl 22mcall.

       1m::http::cleanup 4m22mtoken0m
              This  procedure  cleans up the state associated with the connec-
              tion identified by 4mtoken24m.  After this call, the procedures  like
              1m::http::data  22mcannot be used to get information about the opera-
              tion.  It is 4mstrongly24m recommended that you  call  this  function
              after  you're done with a given HTTP request.  Not doing so will
              result in  memory  not  being  freed,  and  if  your  app  calls
              1m::http::geturl  22menough times, the memory leak could cause a per-
              formance hit...or worse.

       1m::http::register 4m22mproto24m 4mport24m 4mcommand0m
              This procedure allows one to provide custom HTTP transport types
              such  as  HTTPS,  by registering a prefix, the default port, and
              the command to execute to create the Tcl 1mchannel22m. E.g.:
                     package require http
                     package require tls

                     http::register https 443 ::tls::socket

                     set token [http::geturl https://my.secure.site/]

       1m::http::unregister 4m22mproto0m
              This procedure unregisters a protocol handler  that  was  previ-
              ously registered via 1mhttp::register22m.


1mERRORS0m
       The  1mhttp::geturl  22mprocedure  will raise errors in the following cases:
       invalid command line options, an invalid URL, a URL on  a  non-existent
       host,  or  a  URL at a bad port on an existing host.  These errors mean
       that it cannot even start the network transaction.  It will also  raise
       an  error  if  it  gets an I/O error while writing out the HTTP request
       header.  For synchronous 1m::http::geturl 22mcalls (where  1m-command  22mis  not
       specified),  it will raise an error if it gets an I/O error while read-
       ing the HTTP reply headers or  data.   Because  1m::http::geturl  22mdoesn't
       return  a  token  in  these cases, it does all the required cleanup and
       there's no issue of your app having to call 1m::http::cleanup22m.

       For asynchronous 1m::http::geturl 22mcalls, all of the  above  error  situa-
       tions  apply,  except  that if there's any error while reading the HTTP
       reply headers or data, no exception is thrown.  This is  because  after
       writing  the  HTTP headers, 1m::http::geturl 22mreturns, and the rest of the
       HTTP transaction occurs in the background.  The  command  callback  can
       check  if  any error occurred during the read by calling 1m::http::status0m
       to check the status and if it's 4merror24m, calling 1m::http::error 22mto get the
       error message.

       Alternatively,  if the main program flow reaches a point where it needs
       to know the result of  the  asynchronous  HTTP  request,  it  can  call
       1m::http::wait  22mand  then  check  status  and error, just as the callback
       does.

       In any case, you must still call  1mhttp::cleanup  22mto  delete  the  state
       array when you're done.

       There  are other possible results of the HTTP transaction determined by
       examining the status from 1mhttp::status22m.  These are described below.

       ok     If the HTTP transaction completes entirely, then status will  be
              1mok22m.  However, you should still check the 1mhttp::code 22mvalue to get
              the HTTP status.  The 1mhttp::ncode 22mprocedure  provides  just  the
              numeric  error (e.g., 200, 404 or 500) while the 1mhttp::code 22mpro-
              cedure returns a value like "HTTP 404 File not found".

       eof    If the server closes the socket without replying, then no  error
              is raised, but the status of the transaction will be 1meof22m.

       error  The  error message will also be stored in the 1merror 22mstatus array
              element, accessible via 1m::http::error22m.

       Another error possibility is that 1mhttp::geturl 22mis unable to  write  all
       the post query data to the server before the server responds and closes
       the socket.  The error message is saved in the 1mposterror  22mstatus  array
       element  and  then   1mhttp::geturl 22mattempts to complete the transaction.
       If it can read the server's response it will end up with an 1mok  22mstatus,
       otherwise it will have an 1meof 22mstatus.


1mSTATE ARRAY0m
       The 1m::http::geturl 22mprocedure returns a 4mtoken24m that can be used to get to
       the state of the HTTP transaction in the form of a Tcl array.  Use this
       construct to create an easy-to-use array variable:
              upvar #0 $token state
       Once  the  data  associated with the url is no longer needed, the state
       array should be unset to free up storage.  The 1mhttp::cleanup  22mprocedure
       is  provided for that purpose.  The following elements of the array are
       supported:

              1mbody   22mThe contents of the URL.   This  will  be  empty  if  the
                     1m-channel  22moption  has  been  specified.   This  value  is
                     returned by the 1m::http::data 22mcommand.

              1mcurrentsize0m
                     The current number of bytes fetched from the  URL.   This
                     value is returned by the 1m::http::size 22mcommand.

              1merror  22mIf  defined,  this is the error string seen when the HTTP
                     transaction was aborted.

              1mhttp   22mThe HTTP status reply from the  server.   This  value  is
                     returned by the 1m::http::code 22mcommand.  The format of this
                     value is:
                             4mHTTP/1.024m 4mcode24m 4mstring0m
                     The 4mcode24m is a three-digit  number  defined  in  the  HTTP
                     standard.   A  code of 200 is OK.  Codes beginning with 4
                     or 5 indicate errors.  Codes beginning with 3  are  redi-
                     rection  errors.   In  this  case  the 1mLocation 22mmeta-data
                     specifies a new URL that contains the requested  informa-
                     tion.

              1mmeta   22mThe  HTTP  protocol  returns meta-data that describes the
                     URL contents.  The 1mmeta 22melement of the state array  is  a
                     list of the keys and values of the meta-data.  This is in
                     a format useful for initializing an array that just  con-
                     tains the meta-data:
                             array set meta $state(meta)
                     Some of the meta-data keys are listed below, but the HTTP
                     standard defines more, and servers are free to add  their
                     own.

                     1mContent-Type0m
                             The  type  of the URL contents.  Examples include
                             1mtext/html22m, 1mimage/gif, application/postscript  22mand
                             1mapplication/x-tcl22m.

                     1mContent-Length0m
                             The  advertised size of the contents.  The actual
                             size obtained by 1m::http::geturl 22mis  available  as
                             1mstate(size)22m.

                     1mLocation0m
                             An  alternate  URL  that  contains  the requested
                             data.

              1mposterror0m
                     The error, if any, that occurred while writing  the  post
                     query data to the server.

              1mstatus 22mEither  1mok22m,  for  successful  completion, 1mreset 22mfor user-
                     reset, 1mtimeout 22mif a timeout occurred before the  transac-
                     tion  could  complete,  or  1merror 22mfor an error condition.
                     During the transaction this value is the empty string.

              1mtotalsize0m
                     A copy of the 1mContent-Length 22mmeta-data value.

              1mtype   22mA copy of the 1mContent-Type 22mmeta-data value.

              1murl    22mThe requested URL.

1mEXAMPLE0m
              # Copy a URL to a file and print meta-data
              proc ::http::copy { url file {chunk 4096} } {
                  set out [open $file w]
                  set token [geturl $url -channel $out -progress ::http::Progress \
                -blocksize $chunk]
                  close $out
                  # This ends the line started by http::Progress
                  puts stderr ""
                  upvar #0 $token state
                  set max 0
                  foreach {name value} $state(meta) {
                if {[string length $name] > $max} {
                    set max [string length $name]
                }
                if {[regexp -nocase ^location$ $name]} {
                    # Handle URL redirects
                    puts stderr "Location:$value"
                    return [copy [string trim $value] $file $chunk]
                }
                  }
                  incr max
                  foreach {name value} $state(meta) {
                puts [format "%-*s %s" $max $name: $value]
                  }

                  return $token
              }
              proc ::http::Progress {args} {
                  puts -nonewline stderr . ; flush stderr
              }



1mSEE ALSO0m
       safe(n), socket(n), safesock(n)


1mKEYWORDS0m
