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_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.
proc httpCallback {token} { upvar #0 $token state # Access state as a Tcl array }
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 }
Pragma: no-cache
proc httpProgress {token total current} { upvar #0 $token state }
The following elements of the array are supported:upvar #0 $token state
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.code string
Some of the meta-data keys are listed below, but the HTTP standard defines more, and servers are free to add their own.array set meta $state(meta)
# 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 }
Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1997 Sun Microsystems, Inc.