1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1995-1997 Sun Microsystems, Inc.
|
3 |
|
|
'\"
|
4 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
5 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
6 |
|
|
'\"
|
7 |
|
|
'\" RCS: @(#) $Id: http.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
8 |
|
|
'\"
|
9 |
|
|
.so man.macros
|
10 |
|
|
.TH "Http" n 8.0 Tcl "Tcl Built-In Commands"
|
11 |
|
|
.BS
|
12 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
13 |
|
|
.SH NAME
|
14 |
|
|
Http \- Client-side implementation of the HTTP/1.0 protocol.
|
15 |
|
|
.SH SYNOPSIS
|
16 |
|
|
\fBpackage require http ?2.0?\fP
|
17 |
|
|
.sp
|
18 |
|
|
\fB::http::config \fI?options?\fR
|
19 |
|
|
.sp
|
20 |
|
|
\fB::http::geturl \fIurl ?options?\fR
|
21 |
|
|
.sp
|
22 |
|
|
\fB::http::formatQuery \fIlist\fR
|
23 |
|
|
.sp
|
24 |
|
|
\fB::http::reset \fItoken\fR
|
25 |
|
|
.sp
|
26 |
|
|
\fB::http::wait \fItoken\fR
|
27 |
|
|
.sp
|
28 |
|
|
\fB::http::status \fItoken\fR
|
29 |
|
|
.sp
|
30 |
|
|
\fB::http::size \fItoken\fR
|
31 |
|
|
.sp
|
32 |
|
|
\fB::http::code \fItoken\fR
|
33 |
|
|
.sp
|
34 |
|
|
\fB::http::data \fItoken\fR
|
35 |
|
|
.BE
|
36 |
|
|
|
37 |
|
|
.SH DESCRIPTION
|
38 |
|
|
.PP
|
39 |
|
|
The \fBhttp\fR package provides the client side of the HTTP/1.0
|
40 |
|
|
protocol. The package implements the GET, POST, and HEAD operations
|
41 |
|
|
of HTTP/1.0. It allows configuration of a proxy host to get through
|
42 |
|
|
firewalls. The package is compatible with the \fBSafesock\fR security
|
43 |
|
|
policy, so it can be used by untrusted applets to do URL fetching from
|
44 |
|
|
a restricted set of hosts.
|
45 |
|
|
.PP
|
46 |
|
|
The \fB::http::geturl\fR procedure does a HTTP transaction.
|
47 |
|
|
Its \fIoptions \fR determine whether a GET, POST, or HEAD transaction
|
48 |
|
|
is performed.
|
49 |
|
|
The return value of \fB::http::geturl\fR is a token for the transaction.
|
50 |
|
|
The value is also the name of an array in the ::http namespace
|
51 |
|
|
that contains state
|
52 |
|
|
information about the transaction. The elements of this array are
|
53 |
|
|
described in the STATE ARRAY section.
|
54 |
|
|
.PP
|
55 |
|
|
If the \fB-command\fP option is specified, then
|
56 |
|
|
the HTTP operation is done in the background.
|
57 |
|
|
\fB::http::geturl\fR returns immediately after generating the
|
58 |
|
|
HTTP request and the callback is invoked
|
59 |
|
|
when the transaction completes. For this to work, the Tcl event loop
|
60 |
|
|
must be active. In Tk applications this is always true. For pure-Tcl
|
61 |
|
|
applications, the caller can use \fB::http::wait\fR after calling
|
62 |
|
|
\fB::http::geturl\fR to start the event loop.
|
63 |
|
|
.SH COMMANDS
|
64 |
|
|
.TP
|
65 |
|
|
\fB::http::config\fP ?\fIoptions\fR?
|
66 |
|
|
The \fB::http::config\fR command is used to set and query the name of the
|
67 |
|
|
proxy server and port, and the User-Agent name used in the HTTP
|
68 |
|
|
requests. If no options are specified, then the current configuration
|
69 |
|
|
is returned. If a single argument is specified, then it should be one
|
70 |
|
|
of the flags described below. In this case the current value of
|
71 |
|
|
that setting is returned. Otherwise, the options should be a set of
|
72 |
|
|
flags and values that define the configuration:
|
73 |
|
|
.RS
|
74 |
|
|
.TP
|
75 |
|
|
\fB\-accept\fP \fImimetypes\fP
|
76 |
|
|
The Accept header of the request. The default is */*, which means that
|
77 |
|
|
all types of documents are accepted. Otherwise you can supply a
|
78 |
|
|
comma separated list of mime type patterns that you are
|
79 |
|
|
willing to receive. For example, "image/gif, image/jpeg, text/*".
|
80 |
|
|
.TP
|
81 |
|
|
\fB\-proxyhost\fP \fIhostname\fP
|
82 |
|
|
The name of the proxy host, if any. If this value is the
|
83 |
|
|
empty string, the URL host is contacted directly.
|
84 |
|
|
.TP
|
85 |
|
|
\fB\-proxyport\fP \fInumber\fP
|
86 |
|
|
The proxy port number.
|
87 |
|
|
.TP
|
88 |
|
|
\fB\-proxyfilter\fP \fIcommand\fP
|
89 |
|
|
The command is a callback that is made during
|
90 |
|
|
\fB::http::geturl\fR
|
91 |
|
|
to determine if a proxy is required for a given host. One argument, a
|
92 |
|
|
host name, is added to \fIcommand\fR when it is invoked. If a proxy
|
93 |
|
|
is required, the callback should return a two element list containing
|
94 |
|
|
the proxy server and proxy port. Otherwise the filter should return
|
95 |
|
|
an empty list. The default filter returns the values of the
|
96 |
|
|
\fB\-proxyhost\fR and \fB\-proxyport\fR settings if they are
|
97 |
|
|
non-empty.
|
98 |
|
|
.TP
|
99 |
|
|
\fB\-useragent\fP \fIstring\fP
|
100 |
|
|
The value of the User-Agent header in the HTTP request. The default
|
101 |
|
|
is \fB"Tcl http client package 2.0."\fR
|
102 |
|
|
.RE
|
103 |
|
|
.TP
|
104 |
|
|
\fB::http::geturl\fP \fIurl\fP ?\fIoptions\fP?
|
105 |
|
|
The \fB::http::geturl \fR command is the main procedure in the package.
|
106 |
|
|
The \fB\-query\fR option causes a POST operation and
|
107 |
|
|
the \fB\-validate\fR option causes a HEAD operation;
|
108 |
|
|
otherwise, a GET operation is performed. The \fB::http::geturl\fR command
|
109 |
|
|
returns a \fItoken\fR value that can be used to get
|
110 |
|
|
information about the transaction. See the STATE ARRAY section for
|
111 |
|
|
details. The \fB::http::geturl\fR command blocks until the operation
|
112 |
|
|
completes, unless the \fB\-command\fR option specifies a callback
|
113 |
|
|
that is invoked when the HTTP transaction completes.
|
114 |
|
|
\fB::http::geturl\fR takes several options:
|
115 |
|
|
.RS
|
116 |
|
|
.TP
|
117 |
|
|
\fB\-blocksize\fP \fIsize\fP
|
118 |
|
|
The blocksize used when reading the URL.
|
119 |
|
|
At most
|
120 |
|
|
\fIsize\fR
|
121 |
|
|
bytes are read at once. After each block, a call to the
|
122 |
|
|
\fB\-progress\fR
|
123 |
|
|
callback is made.
|
124 |
|
|
.TP
|
125 |
|
|
\fB\-channel\fP \fIname\fP
|
126 |
|
|
Copy the URL contents to channel \fIname\fR instead of saving it in
|
127 |
|
|
\fBstate(body)\fR.
|
128 |
|
|
.TP
|
129 |
|
|
\fB\-command\fP \fIcallback\fP
|
130 |
|
|
Invoke \fIcallback\fP after the HTTP transaction completes.
|
131 |
|
|
This option causes \fB::http::geturl\fP to return immediately.
|
132 |
|
|
The \fIcallback\fP gets an additional argument that is the \fItoken\fR returned
|
133 |
|
|
from \fB::http::geturl\fR. This token is the name of an array that is
|
134 |
|
|
described in the STATE ARRAY section. Here is a template for the
|
135 |
|
|
callback:
|
136 |
|
|
.RS
|
137 |
|
|
.CS
|
138 |
|
|
proc httpCallback {token} {
|
139 |
|
|
upvar #0 $token state
|
140 |
|
|
# Access state as a Tcl array
|
141 |
|
|
}
|
142 |
|
|
.CE
|
143 |
|
|
.RE
|
144 |
|
|
.TP
|
145 |
|
|
\fB\-handler\fP \fIcallback\fP
|
146 |
|
|
Invoke \fIcallback\fP whenever HTTP data is available; if present, nothing
|
147 |
|
|
else will be done with the HTTP data. This procedure gets two additional
|
148 |
|
|
arguments: the socket for the HTTP data and the \fItoken\fR returned from
|
149 |
|
|
\fB::http::geturl\fR. The token is the name of a global array that is described
|
150 |
|
|
in the STATE ARRAY section. The procedure is expected to return the number
|
151 |
|
|
of bytes read from the socket. Here is a template for the callback:
|
152 |
|
|
.RS
|
153 |
|
|
.CS
|
154 |
|
|
proc httpHandlerCallback {socket token} {
|
155 |
|
|
upvar #0 $token state
|
156 |
|
|
# Access socket, and state as a Tcl array
|
157 |
|
|
...
|
158 |
|
|
(example: set data [read $socket 1000];set nbytes [string length $data])
|
159 |
|
|
...
|
160 |
|
|
return nbytes
|
161 |
|
|
}
|
162 |
|
|
.CE
|
163 |
|
|
.RE
|
164 |
|
|
.TP
|
165 |
|
|
\fB\-headers\fP \fIkeyvaluelist\fP
|
166 |
|
|
This option is used to add extra headers to the HTTP request. The
|
167 |
|
|
\fIkeyvaluelist\fR argument must be a list with an even number of
|
168 |
|
|
elements that alternate between keys and values. The keys become
|
169 |
|
|
header field names. Newlines are stripped from the values so the
|
170 |
|
|
header cannot be corrupted. For example, if \fIkeyvaluelist\fR is
|
171 |
|
|
\fBPragma no-cache\fR then the following header is included in the
|
172 |
|
|
HTTP request:
|
173 |
|
|
.CS
|
174 |
|
|
Pragma: no-cache
|
175 |
|
|
.CE
|
176 |
|
|
.TP
|
177 |
|
|
\fB\-progress\fP \fIcallback\fP
|
178 |
|
|
The \fIcallback\fR is made after each transfer of data from the URL.
|
179 |
|
|
The callback gets three additional arguments: the \fItoken\fR from
|
180 |
|
|
\fB::http::geturl\fR, the expected total size of the contents from the
|
181 |
|
|
\fBContent-Length\fR meta-data, and the current number of bytes
|
182 |
|
|
transferred so far. The expected total size may be unknown, in which
|
183 |
|
|
case zero is passed to the callback. Here is a template for the
|
184 |
|
|
progress callback:
|
185 |
|
|
.RS
|
186 |
|
|
.CS
|
187 |
|
|
proc httpProgress {token total current} {
|
188 |
|
|
upvar #0 $token state
|
189 |
|
|
}
|
190 |
|
|
.CE
|
191 |
|
|
.RE
|
192 |
|
|
.TP
|
193 |
|
|
\fB\-query\fP \fIquery\fP
|
194 |
|
|
This flag causes \fB::http::geturl\fR to do a POST request that passes the
|
195 |
|
|
\fIquery\fR to the server. The \fIquery\fR must be a x-url-encoding
|
196 |
|
|
formatted query. The \fB::http::formatQuery\fR procedure can be used to
|
197 |
|
|
do the formatting.
|
198 |
|
|
.TP
|
199 |
|
|
\fB\-timeout\fP \fImilliseconds\fP
|
200 |
|
|
If \fImilliseconds\fR is non-zero, then \fB::http::geturl\fR sets up a timeout
|
201 |
|
|
to occur after the specified number of milliseconds.
|
202 |
|
|
A timeout results in a call to \fB::http::reset\fP and to
|
203 |
|
|
the \fB-command\fP callback, if specified.
|
204 |
|
|
The return value of \fB::http::status\fP is \fBtimeout\fP
|
205 |
|
|
after a timeout has occurred.
|
206 |
|
|
.TP
|
207 |
|
|
\fB\-validate\fP \fIboolean\fP
|
208 |
|
|
If \fIboolean\fR is non-zero, then \fB::http::geturl\fR does an HTTP HEAD
|
209 |
|
|
request. This request returns meta information about the URL, but the
|
210 |
|
|
contents are not returned. The meta information is available in the
|
211 |
|
|
\fBstate(meta) \fR variable after the transaction. See the STATE
|
212 |
|
|
ARRAY section for details.
|
213 |
|
|
.RE
|
214 |
|
|
.TP
|
215 |
|
|
\fB::http::formatQuery\fP \fIkey value\fP ?\fIkey value\fP ...?
|
216 |
|
|
This procedure does x-url-encoding of query data. It takes an even
|
217 |
|
|
number of arguments that are the keys and values of the query. It
|
218 |
|
|
encodes the keys and values, and generates one string that has the
|
219 |
|
|
proper & and = separators. The result is suitable for the
|
220 |
|
|
\fB\-query\fR value passed to \fB::http::geturl\fR.
|
221 |
|
|
.TP
|
222 |
|
|
\fB::http::reset\fP \fItoken\fP ?\fIwhy\fP?
|
223 |
|
|
This command resets the HTTP transaction identified by \fItoken\fR, if
|
224 |
|
|
any. This sets the \fBstate(status)\fP value to \fIwhy\fP, which defaults to \fBreset\fR, and then calls the registered \fB\-command\fR callback.
|
225 |
|
|
.TP
|
226 |
|
|
\fB::http::wait\fP \fItoken\fP
|
227 |
|
|
This is a convenience procedure that blocks and waits for the
|
228 |
|
|
transaction to complete. This only works in trusted code because it
|
229 |
|
|
uses \fBvwait\fR.
|
230 |
|
|
.TP
|
231 |
|
|
\fB::http::data\fP \fItoken\fP
|
232 |
|
|
This is a convenience procedure that returns the \fBbody\fP element
|
233 |
|
|
(i.e., the URL data) of the state array.
|
234 |
|
|
.TP
|
235 |
|
|
\fB::http::status\fP \fItoken\fP
|
236 |
|
|
This is a convenience procedure that returns the \fBstatus\fP element of
|
237 |
|
|
the state array.
|
238 |
|
|
.TP
|
239 |
|
|
\fB::http::code\fP \fItoken\fP
|
240 |
|
|
This is a convenience procedure that returns the \fBhttp\fP element of the
|
241 |
|
|
state array.
|
242 |
|
|
.TP
|
243 |
|
|
\fB::http::size\fP \fItoken\fP
|
244 |
|
|
This is a convenience procedure that returns the \fBcurrentsize\fP
|
245 |
|
|
element of the state array.
|
246 |
|
|
.SH "STATE ARRAY"
|
247 |
|
|
The \fB::http::geturl\fR procedure returns a \fItoken\fR that can be used to
|
248 |
|
|
get to the state of the HTTP transaction in the form of a Tcl array.
|
249 |
|
|
Use this construct to create an easy-to-use array variable:
|
250 |
|
|
.CS
|
251 |
|
|
upvar #0 $token state
|
252 |
|
|
.CE
|
253 |
|
|
The following elements of the array are supported:
|
254 |
|
|
.RS
|
255 |
|
|
.TP
|
256 |
|
|
\fBbody\fR
|
257 |
|
|
The contents of the URL. This will be empty if the \fB\-channel\fR
|
258 |
|
|
option has been specified. This value is returned by the \fB::http::data\fP command.
|
259 |
|
|
.TP
|
260 |
|
|
\fBcurrentsize\fR
|
261 |
|
|
The current number of bytes fetched from the URL.
|
262 |
|
|
This value is returned by the \fB::http::size\fP command.
|
263 |
|
|
.TP
|
264 |
|
|
\fBerror\fR
|
265 |
|
|
If defined, this is the error string seen when the HTTP transaction
|
266 |
|
|
was aborted.
|
267 |
|
|
.TP
|
268 |
|
|
\fBhttp\fR
|
269 |
|
|
The HTTP status reply from the server. This value
|
270 |
|
|
is returned by the \fB::http::code\fP command. The format of this value is:
|
271 |
|
|
.RS
|
272 |
|
|
.CS
|
273 |
|
|
\fIcode string\fP
|
274 |
|
|
.CE
|
275 |
|
|
The \fIcode\fR is a three-digit number defined in the HTTP standard.
|
276 |
|
|
A code of 200 is OK. Codes beginning with 4 or 5 indicate errors.
|
277 |
|
|
Codes beginning with 3 are redirection errors. In this case the
|
278 |
|
|
\fBLocation\fR meta-data specifies a new URL that contains the
|
279 |
|
|
requested information.
|
280 |
|
|
.RE
|
281 |
|
|
.TP
|
282 |
|
|
\fBmeta\fR
|
283 |
|
|
The HTTP protocol returns meta-data that describes the URL contents.
|
284 |
|
|
The \fBmeta\fR element of the state array is a list of the keys and
|
285 |
|
|
values of the meta-data. This is in a format useful for initializing
|
286 |
|
|
an array that just contains the meta-data:
|
287 |
|
|
.RS
|
288 |
|
|
.CS
|
289 |
|
|
array set meta $state(meta)
|
290 |
|
|
.CE
|
291 |
|
|
Some of the meta-data keys are listed below, but the HTTP standard defines
|
292 |
|
|
more, and servers are free to add their own.
|
293 |
|
|
.TP
|
294 |
|
|
\fBContent-Type\fR
|
295 |
|
|
The type of the URL contents. Examples include \fBtext/html\fR,
|
296 |
|
|
\fBimage/gif,\fR \fBapplication/postscript\fR and
|
297 |
|
|
\fBapplication/x-tcl\fR.
|
298 |
|
|
.TP
|
299 |
|
|
\fBContent-Length\fR
|
300 |
|
|
The advertised size of the contents. The actual size obtained by
|
301 |
|
|
\fB::http::geturl\fR is available as \fBstate(size)\fR.
|
302 |
|
|
.TP
|
303 |
|
|
\fBLocation\fR
|
304 |
|
|
An alternate URL that contains the requested data.
|
305 |
|
|
.RE
|
306 |
|
|
.TP
|
307 |
|
|
\fBstatus\fR
|
308 |
|
|
Either \fBok\fR, for successful completion, \fBreset\fR for
|
309 |
|
|
user-reset, or \fBerror\fR for an error condition. During the
|
310 |
|
|
transaction this value is the empty string.
|
311 |
|
|
.TP
|
312 |
|
|
\fBtotalsize\fR
|
313 |
|
|
A copy of the \fBContent-Length\fR meta-data value.
|
314 |
|
|
.TP
|
315 |
|
|
\fBtype\fR
|
316 |
|
|
A copy of the \fBContent-Type\fR meta-data value.
|
317 |
|
|
.TP
|
318 |
|
|
\fBurl\fR
|
319 |
|
|
The requested URL.
|
320 |
|
|
.RE
|
321 |
|
|
.SH EXAMPLE
|
322 |
|
|
.DS
|
323 |
|
|
# Copy a URL to a file and print meta-data
|
324 |
|
|
proc ::http::copy { url file {chunk 4096} } {
|
325 |
|
|
set out [open $file w]
|
326 |
|
|
set token [geturl $url -channel $out -progress ::http::Progress \\
|
327 |
|
|
-blocksize $chunk]
|
328 |
|
|
close $out
|
329 |
|
|
# This ends the line started by http::Progress
|
330 |
|
|
puts stderr ""
|
331 |
|
|
upvar #0 $token state
|
332 |
|
|
set max 0
|
333 |
|
|
foreach {name value} $state(meta) {
|
334 |
|
|
if {[string length $name] > $max} {
|
335 |
|
|
set max [string length $name]
|
336 |
|
|
}
|
337 |
|
|
if {[regexp -nocase ^location$ $name]} {
|
338 |
|
|
# Handle URL redirects
|
339 |
|
|
puts stderr "Location:$value"
|
340 |
|
|
return [copy [string trim $value] $file $chunk]
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
incr max
|
344 |
|
|
foreach {name value} $state(meta) {
|
345 |
|
|
puts [format "%-*s %s" $max $name: $value]
|
346 |
|
|
}
|
347 |
|
|
|
348 |
|
|
return $token
|
349 |
|
|
}
|
350 |
|
|
proc ::http::Progress {args} {
|
351 |
|
|
puts -nonewline stderr . ; flush stderr
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
.DE
|
355 |
|
|
.SH "SEE ALSO"
|
356 |
|
|
safe(n), socket(n), safesock(n)
|
357 |
|
|
.SH KEYWORDS
|
358 |
|
|
security policy, socket
|
359 |
|
|
|
360 |
|
|
|