- NAME
- Http - Client-side implementation of the HTTP/1.0 protocol.
- SYNOPSIS
- package require http 1.0
- http_config ?options?
- http_get url ?options?
- http_formatQuery list
- http_reset token
- http_wait token
- http_status token
- http_size token
- http_code token
- http_data token
- DESCRIPTION
- COMMANDS
- http_config ?options?
- -accept mimetypes
- -proxyhost hostname
- -proxyport number
- -proxyfilter command
- -useragent string
- http_get url ?options?
- -blocksize size
- -channel name
- -command callback
- -handler callback
- -headers keyvaluelist
- -progress callback
- -query query
- -timeout milliseconds
- -validate boolean
- http_formatQuery key value ?key value ...?
- http_reset token ?why?
- http_wait token
- http_data token
- http_status token
- http_code token
- http_size token
- STATE ARRAY
- body
- currentsize
- error
- http
- meta
- Content-Type
- Content-Length
- Location
- status
- totalsize
- type
- url
- EXAMPLE
- SEE ALSO
- KEYWORDS
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
http_status token
http_size token
http_code token
http_data 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 HTTP transaction.
Its options determine whether a GET, POST, or HEAD transaction
is performed.
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.
If the -command option is specified, then
the HTTP operation is done in the background.
http_get returns immediately after generating the
HTTP request and the callback is invoked
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 can use http_wait after calling
http_get to start the event loop.
- 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 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:
- -accept mimetypes
-
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 example, "image/gif, image/jpeg, text/*".
- -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 option causes a POST operation and
the -validate option causes a HEAD operation;
otherwise, a GET operation is performed. The http_get command
returns a token value that can be used to get
information about the transaction. See the STATE ARRAY section for
details. The http_get command blocks until the operation
completes, unless the -command option 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 option causes http_get to return immediately.
The callback 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
}
- -handler callback
-
Invoke callback 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 token returned from
http_get. The token is the name of a global array that is described
in the STATE ARRAY section. The procedure 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
}
- -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.
- -timeout milliseconds
-
If milliseconds is non-zero, then http_get sets up a timeout
to occur after the specified number of milliseconds.
A timeout results in a call to http_reset and to
the -command callback, if specified.
The return value of http_status is timeout
after a timeout has occurred.
- -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 ?why?
-
This command resets the HTTP transaction identified by token, if
any. This sets the state(status) value to why, which defaults to reset, and then calls the registered -command callback.
- 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.
- http_data token
-
This is a convenience procedure that returns the body element
(i.e., the URL data) of the state array.
- http_status token
-
This is a convenience procedure that returns the status element of
the state array.
- http_code token
-
This is a convenience procedure that returns the http element of the
state array.
- http_size token
-
This is a convenience procedure that returns the currentsize
element of the state array.
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:
- body
-
The contents of the URL. This will be empty if the -channel
option has been specified. This value is returned by the http_data command.
- currentsize
-
The current number of bytes fetched from the URL.
This value is returned by the http_size command.
- error
-
If defined, this is the error string seen when the HTTP transaction
was aborted.
- http
-
The HTTP status reply from the server. This value
is returned by the http_code command. 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.
- meta
-
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.
- Content-Type
-
The type of the URL contents. Examples include text/html,
image/gif, application/postscript and
application/x-tcl.
- Content-Length
-
The advertised size of the contents. The actual size obtained by
http_get is available as state(size).
- Location
-
An alternate URL that contains the requested data.
- status
-
Either ok, for successful completion, reset for
user-reset, or error for an error condition. During the
transaction this value is the empty string.
- totalsize
-
A copy of the Content-Length meta-data value.
- type
-
A copy of the Content-Type meta-data value.
- url
-
The requested URL.
# Copy a URL to a file and print meta-data
proc Http_Copy { url file {chunk 4096} } {
set out [open $file w]
set token [http_get $url -channel $out -progress HttpProgress \
-blocksize $chunk]
close $out
# This ends the line started by HttpProgress
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 [Http_Copy [string trim $value] $file $chunk]
}
}
incr max
foreach {name value} $state(meta) {
puts [format "%-*s %s" $max $name: $value]
}
return $token
}
proc HttpProgress {args} {
puts -nonewline stderr . ; flush stderr
}
safe, socket, safesock
security policy, socket
Copyright © 1995-1996 Sun Microsystems, Inc.
Copyright © 1995-1997 Roger E. Critchlow Jr.