http.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:20k
- '"
- '" Copyright (c) 1995-1997 Sun Microsystems, Inc.
- '" Copyright (c) 1998-2000 by Ajuba Solutions.
- '" Copyright (c) 2004 ActiveState Corporation.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: http.n,v 1.18.2.3 2004/10/27 12:52:40 dkf Exp $
- '"
- .so man.macros
- .TH "http" n 2.5 http "Tcl Bundled Packages"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- http - Client-side implementation of the HTTP/1.0 protocol.
- .SH SYNOPSIS
- fBpackage require http ?2.5?fR
- .sp
- fB::http::config fI?options?fR
- .sp
- fB::http::geturl fIurl ?options?fR
- .sp
- fB::http::formatQueryfP fIkey valuefP ?fIkey valuefP ...?
- .sp
- fB::http::resetfP fItokenfP ?fIwhyfP?
- .sp
- fB::http::wait fItokenfR
- .sp
- fB::http::status fItokenfR
- .sp
- fB::http::size fItokenfR
- .sp
- fB::http::code fItokenfR
- .sp
- fB::http::ncode fItokenfR
- .sp
- fB::http::data fItokenfR
- .sp
- fB::http::error fItokenfR
- .sp
- fB::http::cleanup fItokenfR
- .sp
- fB::http::register fIproto port commandfR
- .sp
- fB::http::unregister fIprotofR
- .BE
- .SH DESCRIPTION
- .PP
- The fBhttpfR 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 fBSafesockfR security
- policy, so it can be used by untrusted applets to do URL fetching from
- a restricted set of hosts. This package can be extended to support
- additional HTTP transport protocols, such as HTTPS, by providing
- a custom fBsocketfR command, via fB::http::registerfR.
- .PP
- The fB::http::geturlfR procedure does a HTTP transaction.
- Its fIoptions fR determine whether a GET, POST, or HEAD transaction
- is performed.
- The return value of fB::http::geturlfR is a token for the transaction.
- The value is also the name of an array in the ::http namespace
- that contains state information about the transaction. The elements
- of this array are described in the STATE ARRAY section.
- .PP
- If the fB-commandfP option is specified, then
- the HTTP operation is done in the background.
- fB::http::geturlfR 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 fB::http::waitfR after calling
- fB::http::geturlfR to start the event loop.
- .SH COMMANDS
- .TP
- fB::http::configfP ?fIoptionsfR?
- The fB::http::configfR 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:
- .RS
- .TP
- fB-acceptfP fImimetypesfP
- 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/*".
- .TP
- fB-proxyhostfP fIhostnamefP
- The name of the proxy host, if any. If this value is the
- empty string, the URL host is contacted directly.
- .TP
- fB-proxyportfP fInumberfP
- The proxy port number.
- .TP
- fB-proxyfilterfP fIcommandfP
- The command is a callback that is made during
- fB::http::geturlfR
- to determine if a proxy is required for a given host. One argument, a
- host name, is added to fIcommandfR 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
- fB-proxyhostfR and fB-proxyportfR settings if they are
- non-empty.
- .TP
- fB-urlencodingfP fIencodingfP
- The fIencodingfR used for creating the x-url-encoded URLs with
- fB::http::formatQueryfR. The default is fButf-8fR, as specified by RFC
- 2718. Prior to http 2.5 this was unspecified, and that behavior can be
- returned by specifying the empty string (fB{}fR), although
- fIiso8859-1fR is recommended to restore similar behavior but without the
- fB::http::formatQueryfR throwing an error processing non-latin-1
- characters.
- .TP
- fB-useragentfP fIstringfP
- The value of the User-Agent header in the HTTP request. The default
- is fB"Tcl http client package 2.4."fR
- .RE
- .TP
- fB::http::geturlfP fIurlfP ?fIoptionsfP?
- The fB::http::geturlfR command is the main procedure in the package.
- The fB-queryfR option causes a POST operation and
- the fB-validatefR option causes a HEAD operation;
- otherwise, a GET operation is performed. The fB::http::geturlfR command
- returns a fItokenfR value that can be used to get
- information about the transaction. See the STATE ARRAY and ERRORS section for
- details. The fB::http::geturlfR command blocks until the operation
- completes, unless the fB-commandfR option specifies a callback
- that is invoked when the HTTP transaction completes.
- fB::http::geturlfR takes several options:
- .RS
- .TP
- fB-binaryfP fIbooleanfP
- Specifies whether to force interpreting the URL data as binary. Normally
- this is auto-detected (anything not beginning with a fBtextfR content
- type or whose content encoding is fBgzipfR or fBcompressfR is
- considered binary data).
- .TP
- fB-blocksizefP fIsizefP
- The block size used when reading the URL.
- At most fIsizefR bytes are read at once. After each block, a call to the
- fB-progressfR callback is made (if that option is specified).
- .TP
- fB-channelfP fInamefP
- Copy the URL contents to channel fInamefR instead of saving it in
- fBstate(body)fR.
- .TP
- fB-commandfP fIcallbackfP
- Invoke fIcallbackfP after the HTTP transaction completes.
- This option causes fB::http::geturlfP to return immediately.
- The fIcallbackfP gets an additional argument that is the fItokenfR returned
- from fB::http::geturlfR. This token is the name of an array that is
- described in the STATE ARRAY section. Here is a template for the
- callback:
- .RS
- .CS
- proc httpCallback {token} {
- upvar #0 $token state
- # Access state as a Tcl array
- }
- .CE
- .RE
- .TP
- fB-handlerfP fIcallbackfP
- Invoke fIcallbackfP 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 fItokenfR returned from
- fB::http::geturlfR. 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:
- .RS
- .CS
- 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
- }
- .CE
- .RE
- .TP
- fB-headersfP fIkeyvaluelistfP
- This option is used to add extra headers to the HTTP request. The
- fIkeyvaluelistfR 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 fIkeyvaluelistfR is
- fBPragma no-cachefR then the following header is included in the
- HTTP request:
- .CS
- Pragma: no-cache
- .CE
- .TP
- fB-progressfP fIcallbackfP
- The fIcallbackfR is made after each transfer of data from the URL.
- The callback gets three additional arguments: the fItokenfR from
- fB::http::geturlfR, the expected total size of the contents from the
- fBContent-LengthfR 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:
- .RS
- .CS
- proc httpProgress {token total current} {
- upvar #0 $token state
- }
- .CE
- .RE
- .TP
- fB-queryfP fIqueryfP
- This flag causes fB::http::geturlfR to do a POST request that passes the
- fIqueryfR to the server. The fIqueryfR must be an x-url-encoding
- formatted query. The fB::http::formatQueryfR procedure can be used to
- do the formatting.
- .TP
- fB-queryblocksizefP fIsizefP
- The block size used when posting query data to the URL.
- At most
- fIsizefR
- bytes are written at once. After each block, a call to the
- fB-queryprogressfR
- callback is made (if that option is specified).
- .TP
- fB-querychannelfP fIchannelIDfP
- This flag causes fB::http::geturlfR to do a POST request that passes the
- data contained in fIchannelIDfR to the server. The data contained in
- fIchannelIDfR must be an x-url-encoding
- formatted query unless the fB-typefP option below is used.
- If a Content-Length header is not specified via the fB-headersfR options,
- fB::http::geturlfR attempts 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.
- .TP
- fB-queryprogressfP fIcallbackfP
- The fIcallbackfR is made after each transfer of data to the URL
- (i.e. POST) and acts exactly like the fB-progressfR option (the
- callback format is the same).
- .TP
- fB-timeoutfP fImillisecondsfP
- If fImillisecondsfR is non-zero, then fB::http::geturlfR sets up a timeout
- to occur after the specified number of milliseconds.
- A timeout results in a call to fB::http::resetfP and to
- the fB-commandfP callback, if specified.
- The return value of fB::http::statusfP is fBtimeoutfP
- after a timeout has occurred.
- .TP
- fB-typefP fImime-typefP
- Use fImime-typefR as the fBContent-TypefR value, instead of the
- default value (fBapplication/x-www-form-urlencodedfR) during a
- POST operation.
- .TP
- fB-validatefP fIbooleanfP
- If fIbooleanfR is non-zero, then fB::http::geturlfR 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
- fBstate(meta) fR variable after the transaction. See the STATE
- ARRAY section for details.
- .RE
- .TP
- fB::http::formatQueryfP fIkey valuefP ?fIkey valuefP ...?
- 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
- fB-queryfR value passed to fB::http::geturlfR.
- .TP
- fB::http::resetfP fItokenfP ?fIwhyfP?
- This command resets the HTTP transaction identified by fItokenfR, if
- any. This sets the fBstate(status)fP value to fIwhyfP, which defaults to fBresetfR, and then calls the registered fB-commandfR callback.
- .TP
- fB::http::waitfP fItokenfP
- This is a convenience procedure that blocks and waits for the
- transaction to complete. This only works in trusted code because it
- uses fBvwaitfR. Also, it's not useful for the case where
- fB::http::geturlfP is called fIwithoutfP the fB-commandfP option
- because in this case the fB::http::geturlfP call doesn't return
- until the HTTP transaction is complete, and thus there's nothing to
- wait for.
- .TP
- fB::http::datafP fItokenfP
- This is a convenience procedure that returns the fBbodyfP element
- (i.e., the URL data) of the state array.
- .TP
- fB::http::errorfP fItokenfP
- This is a convenience procedure that returns the fBerrorfP element
- of the state array.
- .TP
- fB::http::statusfP fItokenfP
- This is a convenience procedure that returns the fBstatusfP element of
- the state array.
- .TP
- fB::http::codefP fItokenfP
- This is a convenience procedure that returns the fBhttpfP element of the
- state array.
- .TP
- fB::http::ncodefP fItokenfP
- This is a convenience procedure that returns just the numeric return
- code (200, 404, etc.) from the fBhttpfP element of the state array.
- .TP
- fB::http::sizefP fItokenfP
- This is a convenience procedure that returns the fBcurrentsizefP
- element of the state array, which represents the number of bytes
- received from the URL in the fB::http::geturlfP call.
- .TP
- fB::http::cleanupfP fItokenfP
- This procedure cleans up the state associated with the connection
- identified by fItokenfP. After this call, the procedures
- like fB::http::datafP cannot be used to get information
- about the operation. It is fIstronglyfP 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
- fB::http::geturlfP enough times, the memory leak could cause a
- performance hit...or worse.
- .TP
- fB::http::registerfP fIproto port commandfP
- 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 fBchannelfR. E.g.:
- .RS
- .CS
- package require http
- package require tls
- ::http::register https 443 ::tls::socket
- set token [::http::geturl https://my.secure.site/]
- .CE
- .RE
- .TP
- fB::http::unregisterfP fIprotofP
- This procedure unregisters a protocol handler that was previously
- registered via fB::http::registerfR.
- .SH "ERRORS"
- The fB::http::geturlfP procedure 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 fB::http::geturlfP calls (where fB-commandfP is
- not specified), it will raise an error if it gets an I/O error while
- reading the HTTP reply headers or data. Because fB::http::geturlfP
- doesn't return a token in these cases, it does all the required
- cleanup and there's no issue of your app having to call
- fB::http::cleanupfP.
- .PP
- For asynchronous fB::http::geturlfP calls, all of the above error
- situations 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, fB::http::geturlfP returns, 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
- fB::http::statusfP to check the status and if its fIerrorfP,
- calling fB::http::errorfP to get the error message.
- .PP
- Alternatively, if the main program flow reaches a point where it needs
- to know the result of the asynchronous HTTP request, it can call
- fB::http::waitfP and then check status and error, just as the
- callback does.
- .PP
- In any case, you must still call
- fB::http::cleanupfP to delete the state array when you're done.
- .PP
- There are other possible results of the HTTP transaction
- determined by examining the status from fB::http::statusfP.
- These are described below.
- .TP
- ok
- If the HTTP transaction completes entirely, then status will be fBokfP.
- However, you should still check the fB::http::codefP value to get
- the HTTP status. The fB::http::ncodefP procedure provides just
- the numeric error (e.g., 200, 404 or 500) while the fB::http::codefP
- procedure returns a value like "HTTP 404 File not found".
- .TP
- eof
- If the server closes the socket without replying, then no error
- is raised, but the status of the transaction will be fBeoffP.
- .TP
- error
- The error message will also be stored in the fBerrorfP status
- array element, accessible via fB::http::errorfP.
- .PP
- Another error possibility is that fB::http::geturlfP is 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 fBposterrorfP status array
- element and then fB::http::geturlfP attempts to complete the
- transaction.
- If it can read the server's response
- it will end up with an fBokfP status, otherwise it will have
- an fBeoffP status.
- .SH "STATE ARRAY"
- The fB::http::geturlfR procedure returns a fItokenfR 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:
- .CS
- upvar #0 $token state
- .CE
- Once the data associated with the URL is no longer needed, the state
- array should be unset to free up storage.
- The fB::http::cleanupfP procedure is provided for that purpose.
- The following elements of
- the array are supported:
- .RS
- .TP
- fBbodyfR
- The contents of the URL. This will be empty if the fB-channelfR
- option has been specified. This value is returned by the fB::http::datafP command.
- .TP
- fBcharsetfR
- The value of the charset attribute from the fBContent-TypefR meta-data
- value. If none was specified, this defaults to the RFC standard
- fBiso8859-1fR, or the value of fB$::http::defaultCharsetfR. Incoming
- text data will be automatically converted from this charset to utf-8.
- .TP
- fBcodingfR
- A copy of the fBContent-EncodingfR meta-data value.
- .TP
- fBcurrentsizefR
- The current number of bytes fetched from the URL.
- This value is returned by the fB::http::sizefP command.
- .TP
- fBerrorfR
- If defined, this is the error string seen when the HTTP transaction
- was aborted.
- .TP
- fBhttpfR
- The HTTP status reply from the server. This value
- is returned by the fB::http::codefP command. The format of this value is:
- .RS
- .CS
- fIHTTP/1.0 code stringfP
- .CE
- The fIcodefR 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
- fBLocationfR meta-data specifies a new URL that contains the
- requested information.
- .RE
- .TP
- fBmetafR
- The HTTP protocol returns meta-data that describes the URL contents.
- The fBmetafR 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:
- .RS
- .CS
- array set meta $state(meta)
- .CE
- Some of the meta-data keys are listed below, but the HTTP standard defines
- more, and servers are free to add their own.
- .TP
- fBContent-TypefR
- The type of the URL contents. Examples include fBtext/htmlfR,
- fBimage/gif,fR fBapplication/postscriptfR and
- fBapplication/x-tclfR.
- .TP
- fBContent-LengthfR
- The advertised size of the contents. The actual size obtained by
- fB::http::geturlfR is available as fBstate(size)fR.
- .TP
- fBLocationfR
- An alternate URL that contains the requested data.
- .RE
- .TP
- fBposterrorfR
- The error, if any, that occurred while writing
- the post query data to the server.
- .TP
- fBstatusfR
- Either fBokfR, for successful completion, fBresetfR for
- user-reset, fBtimeoutfP if a timeout occurred before the transaction
- could complete, or fBerrorfR for an error condition. During the
- transaction this value is the empty string.
- .TP
- fBtotalsizefR
- A copy of the fBContent-LengthfR meta-data value.
- .TP
- fBtypefR
- A copy of the fBContent-TypefR meta-data value.
- .TP
- fBurlfR
- The requested URL.
- .RE
- .SH EXAMPLE
- .CS
- # Copy a URL to a file and print meta-data
- proc httpcopy { url file {chunk 4096} } {
- set out [open $file w]
- set token [fB::http::geturlfR $url -channel $out \
- -progress httpCopyProgress -blocksize $chunk]
- close $out
- # This ends the line started by httpCopyProgress
- 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 [httpcopy [string trim $value] $file $chunk]
- }
- }
- incr max
- foreach {name value} $state(meta) {
- puts [format "%-*s %s" $max $name: $value]
- }
- return $token
- }
- proc httpCopyProgress {args} {
- puts -nonewline stderr .
- flush stderr
- }
- .CE
- .SH "SEE ALSO"
- safe(n), socket(n), safesock(n)
- .SH KEYWORDS
- security policy, socket