http.n
来自「tcl是工具命令语言」· N 代码 · 共 527 行 · 第 1/2 页
N
527 行
'\"'\" Copyright (c) 1995-1997 Sun Microsystems, Inc.'\" Copyright (c) 1998-2000 by Ajuba Solutions.'\"'\" 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 2002/07/23 18:17:12 jenglish Exp $'\" .so man.macros.TH "http" n 2.4 http "Tcl Bundled Packages".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEhttp \- Client-side implementation of the HTTP/1.0 protocol..SH SYNOPSIS\fBpackage require http ?2.4?\fR.sp\fB::http::config \fI?options?\fR.sp\fB::http::geturl \fIurl ?options?\fR.sp\fB::http::formatQuery \fIlist\fR.sp\fB::http::reset \fItoken\fR.sp\fB::http::wait \fItoken\fR.sp\fB::http::status \fItoken\fR.sp\fB::http::size \fItoken\fR.sp\fB::http::code \fItoken\fR.sp\fB::http::ncode \fItoken\fR.sp\fB::http::data \fItoken\fR.sp\fB::http::error \fItoken\fR.sp\fB::http::cleanup \fItoken\fR.sp\fB::http::register \fIproto port command\fR.sp\fB::http::unregister \fIproto\fR.BE.SH DESCRIPTION.PPThe \fBhttp\fR package provides the client side of the HTTP/1.0protocol. The package implements the GET, POST, and HEAD operationsof HTTP/1.0. It allows configuration of a proxy host to get throughfirewalls. The package is compatible with the \fBSafesock\fR securitypolicy, so it can be used by untrusted applets to do URL fetching froma restricted set of hosts. This package can be extended to supportadditional HTTP transport protocols, such as HTTPS, by providinga custom \fBsocket\fR command, via \fBhttp::register\fR..PPThe \fB::http::geturl\fR procedure does a HTTP transaction.Its \fIoptions \fR determine whether a GET, POST, or HEAD transactionis performed. The return value of \fB::http::geturl\fR is a token for the transaction.The value is also the name of an array in the ::http namespacethat contains state information about the transaction. The elementsof this array are described in the STATE ARRAY section..PPIf the \fB-command\fP option is specified, thenthe HTTP operation is done in the background.\fB::http::geturl\fR returns immediately after generating theHTTP request and the callback is invokedwhen the transaction completes. For this to work, the Tcl event loopmust be active. In Tk applications this is always true. For pure-Tclapplications, the caller can use \fB::http::wait\fR after calling\fB::http::geturl\fR to start the event loop..SH COMMANDS.TP\fB::http::config\fP ?\fIoptions\fR?The \fB::http::config\fR command is used to set and query the name of theproxy server and port, and the User-Agent name used in the HTTPrequests. If no options are specified, then the current configurationis returned. If a single argument is specified, then it should be oneof the flags described below. In this case the current value ofthat setting is returned. Otherwise, the options should be a set offlags and values that define the configuration:.RS.TP\fB\-accept\fP \fImimetypes\fPThe Accept header of the request. The default is */*, which means thatall types of documents are accepted. Otherwise you can supply a comma separated list of mime type patterns that you arewilling to receive. For example, "image/gif, image/jpeg, text/*"..TP\fB\-proxyhost\fP \fIhostname\fPThe name of the proxy host, if any. If this value is theempty string, the URL host is contacted directly..TP\fB\-proxyport\fP \fInumber\fPThe proxy port number..TP\fB\-proxyfilter\fP \fIcommand\fPThe command is a callback that is made during\fB::http::geturl\fRto determine if a proxy is required for a given host. One argument, ahost name, is added to \fIcommand\fR when it is invoked. If a proxyis required, the callback should return a two element list containingthe proxy server and proxy port. Otherwise the filter should returnan empty list. The default filter returns the values of the\fB\-proxyhost\fR and \fB\-proxyport\fR settings if they arenon-empty..TP\fB\-useragent\fP \fIstring\fPThe value of the User-Agent header in the HTTP request. The defaultis \fB"Tcl http client package 2.4."\fR.RE.TP\fB::http::geturl\fP \fIurl\fP ?\fIoptions\fP? The \fB::http::geturl\fR command is the main procedure in the package.The \fB\-query\fR option causes a POST operation andthe \fB\-validate\fR option causes a HEAD operation;otherwise, a GET operation is performed. The \fB::http::geturl\fR commandreturns a \fItoken\fR value that can be used to getinformation about the transaction. See the STATE ARRAY and ERRORS section fordetails. The \fB::http::geturl\fR command blocks until the operationcompletes, unless the \fB\-command\fR option specifies a callbackthat is invoked when the HTTP transaction completes.\fB::http::geturl\fR takes several options:.RS.TP\fB\-binary\fP \fIboolean\fPSpecifies whether to force interpreting the url data as binary. Normallythis is auto-detected (anything not beginning with a \fBtext\fR contenttype or whose content encoding is \fBgzip\fR or \fBcompress\fR isconsidered binary data)..TP\fB\-blocksize\fP \fIsize\fPThe blocksize used when reading the URL.At most \fIsize\fR bytes are read at once. After each block, a call to the\fB\-progress\fR callback is made (if that option is specified)..TP\fB\-channel\fP \fIname\fPCopy the URL contents to channel \fIname\fR instead of saving it in\fBstate(body)\fR..TP\fB\-command\fP \fIcallback\fPInvoke \fIcallback\fP after the HTTP transaction completes.This option causes \fB::http::geturl\fP to return immediately.The \fIcallback\fP gets an additional argument that is the \fItoken\fR returnedfrom \fB::http::geturl\fR. This token is the name of an array that isdescribed in the STATE ARRAY section. Here is a template for thecallback:.RS.CSproc httpCallback {token} { upvar #0 $token state # Access state as a Tcl array}.CE.RE.TP\fB\-handler\fP \fIcallback\fPInvoke \fIcallback\fP whenever HTTP data is available; if present, nothingelse will be done with the HTTP data. This procedure gets two additionalarguments: the socket for the HTTP data and the \fItoken\fR returned from\fB::http::geturl\fR. The token is the name of a global array that is describedin the STATE ARRAY section. The procedure is expected to return the numberof bytes read from the socket. Here is a template for the callback:.RS.CSproc 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\-headers\fP \fIkeyvaluelist\fPThis option is used to add extra headers to the HTTP request. The\fIkeyvaluelist\fR argument must be a list with an even number ofelements that alternate between keys and values. The keys becomeheader field names. Newlines are stripped from the values so theheader cannot be corrupted. For example, if \fIkeyvaluelist\fR is\fBPragma no-cache\fR then the following header is included in theHTTP request:.CSPragma: no-cache.CE.TP\fB\-progress\fP \fIcallback\fPThe \fIcallback\fR is made after each transfer of data from the URL.The callback gets three additional arguments: the \fItoken\fR from\fB::http::geturl\fR, the expected total size of the contents from the\fBContent-Length\fR meta-data, and the current number of bytestransferred so far. The expected total size may be unknown, in whichcase zero is passed to the callback. Here is a template for theprogress callback:.RS.CSproc httpProgress {token total current} { upvar #0 $token state}.CE.RE.TP\fB\-query\fP \fIquery\fPThis flag causes \fB::http::geturl\fR to do a POST request that passes the\fIquery\fR to the server. The \fIquery\fR must be a x-url-encodingformatted query. The \fB::http::formatQuery\fR procedure can be used todo the formatting..TP\fB\-queryblocksize\fP \fIsize\fPThe blocksize used when posting query data to the URL.At most \fIsize\fRbytes are written at once. After each block, a call to the\fB\-queryprogress\fRcallback is made (if that option is specified)..TP\fB\-querychannel\fP \fIchannelID\fPThis flag causes \fB::http::geturl\fR to do a POST request that passes thedata contained in \fIchannelID\fR to the server. The data contained in \fIchannelID\fR must be a x-url-encodingformatted query unless the \fB\-type\fP option below is used.If a Content-Length header is not specified via the \fB\-headers\fR options,\fB::http::geturl\fR attempts to determine the size of the post datain order to create that header. If it isunable to determine the size, it returns an error..TP\fB\-queryprogress\fP \fIcallback\fPThe \fIcallback\fR is made after each transfer of data to the URL(i.e. POST) and acts exactly like the \fB\-progress\fR option (thecallback format is the same)..TP\fB\-timeout\fP \fImilliseconds\fPIf \fImilliseconds\fR is non-zero, then \fB::http::geturl\fR sets up a timeoutto occur after the specified number of milliseconds.A timeout results in a call to \fB::http::reset\fP and tothe \fB-command\fP callback, if specified.The return value of \fB::http::status\fP is \fBtimeout\fPafter a timeout has occurred..TP\fB\-type\fP \fImime-type\fPUse \fImime-type\fR as the \fBContent-Type\fR value, instead of thedefault value (\fBapplication/x-www-form-urlencoded\fR) during aPOST operation..TP\fB\-validate\fP \fIboolean\fPIf \fIboolean\fR is non-zero, then \fB::http::geturl\fR does an HTTP HEADrequest. This request returns meta information about the URL, but thecontents are not returned. The meta information is available in the\fBstate(meta) \fR variable after the transaction. See the STATEARRAY section for details..RE.TP\fB::http::formatQuery\fP \fIkey value\fP ?\fIkey value\fP ...?This procedure does x-url-encoding of query data. It takes an evennumber of arguments that are the keys and values of the query. Itencodes the keys and values, and generates one string that has theproper & and = separators. The result is suitable for the\fB\-query\fR value passed to \fB::http::geturl\fR..TP\fB::http::reset\fP \fItoken\fP ?\fIwhy\fP?
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?