Tcl Built-In Commands


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


package require http 1.0

http_config ?options?

http_get url ?options?

http_formatQuery list

http_reset token

http_wait token


The http package 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 Safesock security policy, so it can be used by untrusted applets to do URL fetching from a restricted set of hosts.

The http_get procedure does a non-blocking HTTP transaction. Its options determine whether a GET, POST, or HEAD transaction is performed. http_get returns immediately after generating the HTTP request. The caller supplies a command callback that is called when the transaction completes. For this to work, the Tcl event loop must be active. In Tk applications this is always true. For pure-Tcl applications, the caller must use vwait after calling http_get to start the event loop.

The return value of http_get is a token for the transaction. The value is also the name of a global array that contains state information about the transaction. The elements of this array are described in the STATE ARRAY section.


http_config ?options?
The http_config command 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 these cases the current value of that setting is returned. Otherwise, the options should be a set of flags and values that define the configuration:

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

-proxyport number
The proxy port number.

-proxyfilter command
The command is a callback that is made during http_get to determine if a proxy is required for a given host. One argument, a host name, is added to command 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 -proxyhost and -proxyport settings if they are non-empty.

-useragent string
The value of the User-Agent header in the HTTP request. The default is "Tcl http client package 1.0."

http_get url ?options?
The http_get command is the main procedure in the package. The -query and -validate options determine if a GET, POST, or HEAD operation is performed. The http_get command returns immediately with a token value that can be used to get information about the transaction. See the STATE ARRAY section for details. The -command specifies a callback that is invoked when the HTTP transaction completes. http_get takes several options:

-blocksize size
The blocksize used when reading the URL. At most size bytes are read at once. After each block, a call to the -progress callback is made.

-channel name
Copy the URL contents to channel name instead of saving it in state(body).

-command callback
Invoke callback after the HTTP transaction completes. This procedure gets an additional argument that is the token returned from http_get. This token is the name of a global 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

-headers keyvaluelist
This option is used to add extra headers to the HTTP request. The keyvaluelist argument must be a list with an even number of elements that alternate between keys and values. The keys become header field names. Newlines are stripped from the values so the header cannot be corrupted. For example, if keyvaluelist is Pragma no-cache then the following header is included in the HTTP request:
Pragma: no-cache

-progress callback
The callback is made after each transfer of data from the URL. The callback gets three additional arguments: the token from http_get, the expected total size of the contents from the Content-Length meta-data, and the current 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

-query query
This flag causes http_get to do a POST request that passes the query to the server. The query must be a x-url-encoding formatted query. The http_formatQuery procedure can be used to do the formatting.

-validate boolean
If boolean is non-zero, then http_get does 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 state(meta) variable after the transaction. See the STATE ARRAY section for details.

http_formatQuery key value ?key value ...?
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 -query value passed to http_get.

http_reset token
This command resets the HTTP transaction identified by token, if any. This triggers a call to the registered -command callback with a reset status.

http_wait token
This is a convenience procedure that blocks and waits for the transaction to complete. This only works in trusted code because it uses vwait.


The http_get procedure returns a token 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
The following elements of the array are supported:

The contents of the URL. This will be empty if the -channel option has been specified.

The current number of bytes fetched from the URL.

If defined, this is the error string seen when the HTTP transaction was aborted.

The HTTP status reply from the server. The format of this value is:
code string
The code 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 redirection errors. In this case the Location meta-data specifies a new URL that contains the requested information.

The HTTP protocol returns meta-data that describes the URL contents. The meta element 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 contains 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.

The type of the URL contents. Examples include text/html, image/gif, application/postscript and application/x-tcl.

The advertised size of the contents. The actual size obtained by http_get is available as state(size).

An alternate URL that contains the requested data.

Either ok, for successful completion, reset for user-reset, or error for an error condition. During the transaction this value is the empty string.

A copy of the Content-Length meta-data value.

A copy of the Content-Type meta-data value.

The requested URL.


safe(n), socket(n), safesock(n)


security policy, socket

Last change: 8.0

[ tcl8.0a1 | tk8.0a1 | X-ref ]

Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.