projman/hlp/ru/tcl/Http.html
2015-10-19 14:27:31 +04:00

451 lines
17 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Http</title>
</head>
<body>
<h1>Http</h1>
<p>ëÌÉÅÎÔÓËÁÑ ÞÁÓÔØ ÒÅÁÌÉÚÁÃÉÉ ÐÒÏÔÏËÏÌÁ HTTP/1.0.</p>
<h2><a name="óéîôáëóéó"></a>óéîôáëóéó</h2>
<pre>
<strong>package</strong> <strong>require</strong> <em>http</em> ?<em>2.0</em>?
<a href="#http::config"><strong>::http::config</strong></a> ?<em>options</em>?
<a href="#http::geturl_url"><strong>::http::geturl</strong> <strong>url</strong></a> ?<em>options</em>?
<a href="#http::formatQuery"><strong>::http::formatQuery</strong></a> <em>list</em>
<a href="#http::reset"><strong>::http::reset</strong></a> <em>token</em> ?<em>why</em>?
<a href="#http::wait"><strong>::http::wait</strong></a> <em>token</em>
<a href="#http::status"><strong>::http::status</strong></a> <em>token</em>
<a href="#http::size"><strong>::http::size</strong></a> <em>token</em>
<a href="#http::code"><strong>::http::code</strong></a> <em>token</em>
<a href="#http::data"><strong>::http::data</strong></a> <em>token</em>
</pre>
<h2><a name="ïðéóáîéå"></a>ïðéóáîéå</h2>
<p>ðÁËÅÔ <strong>http</strong> ÏÂÅÓÐÅÞÉ×ÁÅÔ ËÌÉÅÎÔÓËÕÀ ÞÁÓÔØ ÐÒÏÔÏËÏÌÁ
HTTP/1.0 É ÒÅÁÌÉÚÕÅÔ ÏÐÅÒÁÃÉÉ GET, POST É HEAD. ïÎ ÐÏÚ×ÏÌÑÅÔ
ËÏÎÆÉÇÕÒÉÒÏ×ÁÔØ ÓÅÒ×ÅÒ-ÐÒÅÄÓÔÁ×ÉÔÅÌØ (proxy) ÄÌÑ ×ÙÈÏÄÁ ÞÅÒÅÚ
ÍÅÖÓÅÔÅ×ÙÅ ÜËÒÁÎÙ. ðÁËÅÔ ÓÏ×ÍÅÓÔÉÍ Ó ÐÏÌÉÔÉËÏÊ ÂÅÚÏÐÁÓÎÏÓÔÉ
Safesock.</p>
<p>ðÒÏÃÅÄÕÒÁ <a href="#http::geturl_url"><strong>::http::geturl</strong></a>
×ÙÐÏÌÎÑÅÔ HTTP ÔÒÁÎÚÁËÃÉÀ. ÷ ÚÁ×ÉÓÉÍÏÓÔÉ ÏÔ ÚÁÄÁÎÎÏÊ ÏÐÃÉÉ ÜÔÏ
ÍÏÖÅÔ ÂÙÔØ GET, POST ÉÌÉ HEAD ÔÒÁÎÚÁËÃÉÑ. ÷ÅÌÉÞÉÎÁ,
×ÏÚ×ÒÁÝÁÅÍÁÑ ÐÒÏÃÅÄÕÒÏÊ <a href="#http::geturl_url">
<strong>::http::geturl</strong></a>, Ñ×ÌÑÅÔÓÑ ÐÒÉÚÎÁËÏÍ (token) ×ÙÐÏÌÎÅÎÉÑ
ÔÒÁÎÚÁËÃÉÉ. ëÒÏÍÅ ÔÏÇÏ, ÅÅ ÚÎÁÞÅÎÉÅ ÓÏ×ÐÁÄÁÅÔ Ó ÉÍÅÎÅÍ ÍÁÓÓÉ×Á ×
ÐÒÏÓÔÒÁÎÓÔ×Å ÉÍÅÎ <strong>::http</strong>, ËÏÔÏÒÙÊ ÓÏÄÅÒÖÉÔ ÉÎÆÏÒÍÁÃÉÀ Ï
×ÙÐÏÌÎÅÎÉÉ ÔÒÁÎÚÁËÃÉÉ. üÌÅÍÅÎÔÙ ÍÁÓÓÉ×Á ÏÐÉÓÁÎÙ ÎÉÖÅ, ÓÍ. &quot;<a
href="#íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé">íÁÓÓÉ×
ÓÏÓÔÏÑÎÉÑ ÔÒÁÎÚÁËÃÉÉ</a>&quot;.</p>
<p>åÓÌÉ ÐÒÏÃÅÄÕÒÁ ×ÙÚ×ÁÎÁ Ó ÏÐÃÉÅÊ <strong>-command</strong>, ÏÐÅÒÁÃÉÑ
×ÙÐÏÌÎÑÅÔÓÑ × ÆÏÎÏ×ÏÍ ÒÅÖÉÍÅ. ðÒÏÃÅÄÕÒÁ <a href=
"#http::geturl_url"><strong>::http::geturl</strong></a>
ÚÁ×ÅÒÛÁÅÔÓÑ ÓÒÁÚÕ ÐÏÓÌÅ ÆÏÒÍÉÒÏ×ÁÎÉÑ HTTP ÚÁÐÒÏÓÁ, Á ÒÅÚÕÌØÔÁÔÙ
ÚÁÐÒÏÓÁ ÏÂÒÁÂÁÔÙ×ÁÀÔÓÑ ÐÏÓÌÅ ÉÈ ÐÏÌÕÞÅÎÉÑ. äÌÑ ÕÓÐÅÛÎÏÊ ÒÁÂÏÔÙ ×
ÔÁËÏÍ ÒÅÖÉÍÅ ÎÅÏÂÈÏÄÉÍÏ, ÞÔÏÂÙ ÂÙÌ ÚÁÐÕÝÅÎ ÏÂÒÁÂÏÔÞÉË
ÓÏÂÙÔÉÊ. üÔÏ ×ÓÅÇÄÁ ÔÁË ÄÌÑ Tk-ÐÒÉÌÏÖÅÎÉÊ. ÷ ÞÉÓÔÏ Tcl -
ÐÒÉÌÏÖÅÎÉÑÈ ÍÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÐÒÏÃÅÄÕÒÕ <a
href="#http::wait"><strong>::http::wait</strong></a> ÄÌÑ ÚÁÐÕÓËÁ
ÏÂÒÁÂÏÔÞÉËÁ ÓÏÂÙÔÉÊ.</p>
<h2><a name="ëïíáîäù"></a>ëïíáîäù</h2>
<dl>
<dt><a name="http::config"></a><strong>::http::config</strong>
?<em>options?</em></dt>
<dd><p>ëÏÍÁÎÄÁ <strong>::http::config</strong> ÉÓÐÏÌØÚÕÅÔÓÑ, ÞÔÏÂÙ
ÕÓÔÁÎÏ×ÉÔØ ÉÌÉ ÚÁÐÒÏÓÉÔØ ÉÍÑ proxy-ÓÅÒ×ÅÒÁ, ÐÏÒÔÁ É
ÐÏÌØÚÏ×ÁÔÅÌØÓËÏÇÏ ÐÒÉÌÏÖÅÎÉÑ (User-Agent), ÉÓÐÏÌØÚÕÅÍÙÅ ×
HTTP ÚÁÐÒÏÓÁÈ. åÓÌÉ ÎÉËÁËÉÅ ÏÐÃÉÉ ÎÅ ÚÁÄÁÎÙ, ×ÏÚ×ÒÁÝÁÅÔÓÑ
ÔÅËÕÝÁÑ ËÏÎÆÉÇÕÒÁÃÉÑ. åÓÌÉ ÚÁÄÁÎ ÅÄÉÎÓÔ×ÅÎÎÙÊ ÁÒÇÕÍÅÎÔ,
ÔÏÇÄÁ, ÏÎ ÄÏÌÖÅÎ ÂÙÔØ ÉÍÅÎÅÍ ÏÄÎÏÊ ÉÚ ÏÐÃÉÊ, ÏÐÉÓÁÎÎÙÈ
ÎÉÖÅ. ÷ ÜÔÏÍ ÓÌÕÞÁÅ ×ÏÚ×ÒÁÝÁÅÔÓÑ ÔÅËÕÝÁÑ ×ÅÌÉÞÉÎÁ ÕËÁÚÁÎÎÏÊ
ÏÐÃÉÉ. ÷ ÐÒÏÔÉ×ÎÏÍ ÓÌÕÞÁÅ ÁÒÇÕÍÅÎÔÙ ÓÏÓÔÏÑÔ ÉÚ ÐÁÒ: ÉÍÑ
ÏÐÃÉÉ - ÐÒÉÓ×ÁÉ×ÁÅÍÏÅ ÚÎÁÞÅÎÉÅ.</p>
<dl>
<dt><strong>-accept</strong> <em>mimetypes</em></dt>
<dd>ïÐÒÅÄÅÌÑÅÔ ÔÉÐÙ ÄÏËÕÍÅÎÔÏ×, ËÏÔÏÒÙÅ ÍÏÇÕÔ ÂÙÔØ ÐÒÉÎÑÔÙ
ÐÏ ÚÁÐÒÏÓÕ. úÎÁÞÅÎÉÅ ÐÏ ÕÍÏÌÞÁÎÉÀ "*/*" ÏÚÎÁÞÁÅÔ, ÞÔÏ
ÍÏÇÕÔ ÂÙÔØ ÐÒÉÎÑÔÙ ÄÏËÕÍÅÎÔÙ ÌÀÂÏÇÏ ÔÉÐÁ. þÔÏÂÙ ÏÇÒÁÎÉÞÉÔØ
ÓÐÉÓÏË ÄÏÐÕÓÔÉÍÙÈ ÄÏËÕÍÅÎÔÏ×, ÍÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÓÐÉÓÏË
(ÞÅÒÅÚ ÚÁÐÑÔÕÀ) ÛÁÂÌÏÎÏ× ÄÏËÕÍÅÎÔÏ× ÓÌÅÄÕÀÝÅÇÏ ×ÉÄÁ:
"image/gif, image/jpeg, text/*".</dd>
<dt><strong>-proxyhost</strong> <em>hostname</em></dt>
<dd>éÍÑ proxy-ÓÅÒ×ÅÒÁ, ÞÅÒÅÚ ËÏÔÏÒÙÊ ÏÓÕÝÅÓÔ×ÌÑÅÔÓÑ Ó×ÑÚØ.
åÓÌÉ ÎÅ ÕËÁÚÁÎÏ, Ó×ÑÚØ ÏÓÕÝÅÓÔ×ÌÑÅÔÓÑ ÎÁÐÒÑÍÕÀ.</dd>
<dt><strong>-proxyport</strong> <em>number</em></dt>
<dd>éÍÑ proxy-ÐÏÒÔÁ.</dd>
<dt><strong>-proxyfilter</strong> <em>command</em></dt>
<dd>ïÐÒÅÄÅÌÑÅÔ ËÏÍÁÎÄÕ, ËÏÔÏÒÁÑ ×ÏÚ×ÒÁÝÁÅÔ ÉÍÅÎÁ
proxy-ÓÅÒ×ÅÒÁ É proxy-ÐÏÒÔÁ, ÎÅÏÂÈÏÄÉÍÙÅ ÄÌÑ ÄÁÎÎÏÊ
Ó×ÑÚÉ. ÷ ÐÒÏÔÉ×ÎÏÍ ÓÌÕÞÁÅ ×ÏÚ×ÒÁÝÁÅÔ ÐÕÓÔÏÅ ÚÎÁÞÅÎÉÅ. ÷
ËÁÞÅÓÔ×Å ÁÒÇÕÍÅÎÔÁ ÐÒÉ ×ÙÚÏ×Å ËÏÍÁÎÄÙ ÉÓÐÏÌØÚÕÅÔÓÑ ÉÍÑ
ÓÅÒ×ÅÒÁ (host). åÓÌÉ ËÏÍÁÎÄÁ ÎÅ ÚÁÄÁÎÁ, ÉÓÐÏÌØÚÕÀÔÓÑ
ÚÎÁÞÅÎÉÑ ÏÐÃÉÊ <strong>-proxyhost</strong> É
<strong>-proxyport</strong>.</dd>
<dt><strong>-useragent</strong> <em>string</em></dt>
<dd>ïÐÒÅÄÅÌÑÅÔ ÉÍÑ ÐÏÌØÚÏ×ÁÔÅÌØÓËÏÇÏ ÐÒÉÌÏÖÅÎÉÑ
(User-Agent). úÎÁÞÅÎÉÅ ÐÏ ÕÍÏÌÞÁÎÉÀ &quot;Tcl http client
package 2.0.&quot;</dd>
</dl>
</dd>
<dt><a name="http::geturl_url"></a><strong>::http::geturl url</strong>
?<em>options</em>?</dt>
<dd><p>ëÏÍÁÎÄÁ <strong>::http::geturl</strong> - ÏÓÎÏ×ÎÁÑ ËÏÍÁÎÄÁ ÐÁËÅÔÁ.
åÓÌÉ ÚÁÄÁÎÁ ÏÐÃÉÑ <strong>-query</strong>, ×ÙÐÏÌÎÑÅÔÓÑ ÏÐÅÒÁÃÉÑ POST,
ÅÓÌÉ ÚÁÄÁÎÁ ÏÐÃÉÑ <strong>-validate</strong>, ×ÙÐÏÌÎÑÅÔÓÑ ÏÐÅÒÁÃÉÑ
HEAD. ÷ ÐÒÏÔÉ×ÎÏÍ ÓÌÕÞÁÅ ×ÙÐÏÌÎÑÅÔÓÑ ÏÐÅÒÁÃÉÑ GET. ëÏÍÁÎÄÁ
×ÏÚ×ÒÁÝÁÅÔ ÐÒÉÚÎÁË - ÉÍÑ ÍÁÓÓÉ×Á, ËÏÔÏÒÙÊ ÍÏÖÅÔ ÂÙÔØ
ÉÓÐÏÌØÚÏ×ÁÎ ÄÌÑ ÐÏÌÕÞÅÎÉÑ ÄÏÐÏÌÎÉÔÅÌØÎÏÊ ÉÎÆÏÒÍÁÃÉÉ Ï
ÓÏÓÔÏÑÎÉÉ ÔÒÁÎÚÁËÃÉÉ. ðÏÄÒÏÂÎÏÓÔÉ ÓÍ. <a
href="#íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé">&quot;íÁÓÓÉ×
ÓÏÓÔÏÑÎÉÑ ÔÒÁÎÚÁËÃÉÉ&quot;</a>. ëÏÍÁÎÄÁ ÚÁ×ÅÒÛÁÅÔÓÑ ÐÏÓÌÅ
ÚÁ×ÅÒÛÅÎÉÑ ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÅÊ ÏÐÅÒÁÃÉÉ, ÅÓÌÉ ÏÎÁ ×ÙÚ×ÁÎÁ ÂÅÚ
ÏÐÃÉÉ <strong>-command</strong>. ÷ ÐÒÏÔÉ×ÎÏÍ ÓÌÕÞÁÅ ËÏÍÁÎÄÁ
<strong>::http::geturl</strong> ÚÁ×ÅÒÛÁÅÔÓÑ ÎÅÍÅÄÌÅÎÎÏ, Á ÐÏ
ÚÁ×ÅÒÛÅÎÉÉ ÏÐÅÒÁÃÉÉ ×ÙÚÙ×ÁÅÔÓÑ ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÁÑ ËÏÍÁÎÄÁ ÄÌÑ
ÏÂÒÁÂÏÔËÉ ÅÅ ÒÅÚÕÌØÔÁÔÏ×. ëÏÍÁÎÄÁ <strong>::http::geturl</strong>
ÍÏÖÅÔ ÉÓÐÏÌØÚÏ×ÁÔØÓÑ Ó ÒÁÚÌÉÞÎÙÍÉ ÏÐÃÉÑÍÉ:</p>
<dl>
<dt><strong>-blocksize</strong> <em>size</em></dt>
<dd>éÓÐÏÌØÚÕÅÔÓÑ ÐÒÉ ÞÔÅÎÉÉ ÉÎÆÏÒÍÁÃÉÉ. ïÐÒÅÄÅÌÑÅÔ
ÍÁËÓÉÍÁÌØÎÙÊ ÒÁÚÍÅÒ ÂÌÏËÁ (× ÂÁÊÔÁÈ), ËÏÔÏÒÙÊ ÍÏÖÅÔ ÂÙÔØ
ÐÒÏÞÉÔÁÎ ÚÁ ÏÄÉÎ ÒÁÚ. ðÏÓÌÅ ËÁÖÄÏÇÏ ÞÔÅÎÉÑ ÂÌÏËÁ
×ÙÚÙ×ÁÅÔÓÑ ËÏÍÁÎÄÁ, ÏÐÒÅÄÅÌÅÎÎÁÑ Ó ÐÏÍÏÝØÀ ÏÐÃÉÉ
<strong>-progress</strong>.
</dd>
<dt><strong>-channel</strong> <em>name</em></dt>
<dd>ðÅÒÅÎÁÐÒÁ×ÌÑÅÔ ÐÏÌÕÞÅÎÎÕÀ ÉÎÆÏÒÍÁÃÉÑ × ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÉÊ
ËÁÎÁÌ ×ÍÅÓÔÏ ÔÏÇÏ, ÞÔÏÂÙ ÓÏÈÒÁÎÑÔØ ÅÅ × ÐÅÒÅÍÅÎÎÏÊ
<em>state(body)</em>.
</dd>
<dt><strong>-command</strong> <em>callback</em></dt>
<dd><p>ïÂÅÓÐÅÞÉ×ÁÅÔ ×ÙÚÏ× ËÏÍÁÎÄÙ <em>callback</em> ÐÏÓÌÅ
ÚÁ×ÅÒÛÅÎÉÑ ÔÒÁÎÚÁËÃÉÉ. ðÒÉ ÉÓÐÏÌØÚÏ×ÁÎÉÉ ÜÔÏÊ ÏÐÃÉÉ
ËÏÍÁÎÄÁ <strong>::http::geturl</strong> ÚÁ×ÅÒÛÁÅÔÓÑ ÓÒÁÚÕ. ëÏÍÁÎÄÁ
<em>callback</em> ×ÙÚÙ×ÁÅÔÓÑ Ó ÁÒÇÕÍÅÎÔÏÍ <em>token</em>,
ËÏÔÏÒÙÊ ÓÏÄÅÒÖÉÔ ÉÍÑ ÍÁÓÓÉ×Á, ÏÐÉÓÁÎÎÏÇÏ ÎÉÖÅ, ÓÍ. <a
href="#íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé">&quot;íÁÓÓÉ×
ÓÏÓÔÏÑÎÉÑ ÔÒÁÎÚÁËÃÉÉ&quot;</a>. îÉÖÅ ÐÒÉ×ÅÄÅÎ ÛÁÂÌÏÎ
ÔÉÐÏ×ÏÊ ÐÒÏÃÅÄÕÒÙ ÄÌÑ ÉÓÐÏÌØÚÏ×ÁÎÉÑ × ÄÁÎÎÏÊ ÏÐÃÉÉ:</p>
<pre>
proc httpCallback {token} {
upvar #0 $token state
# äÁÌÅÅ ×ÏÚÍÏÖÎÁ ÒÁÂÏÔÁ ÓÏ state ËÁË Ó ÏÂÙÞÎÙÍ Tcl-ÍÁÓÓÉ×ÏÍ
}
</pre>
</dd>
<dt><strong>-handler</strong> <em>callback</em></dt>
<dd><p>ïÐÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ×ÙÚÏ× ËÏÍÁÎÄÙ <em>callback</em> ËÁË
ÔÏÌØËÏ HTTP ÄÁÎÎÙÅ ÐÏÌÕÞÅÎÙ. ëÏÍÁÎÄÁ ÐÏÌÕÞÁÅÔ Ä×Á
ÄÏÐÏÌÎÉÔÅÌØÎÙÈ ÁÒÇÕÍÅÎÔÁ: HTTP <em>socket</em> É ÉÍÑ
ÍÁÓÓÉ×Á <em>token</em>, ×ÏÚ×ÒÁÝÅÎÎÏÅ ËÏÍÁÎÄÏÊ
<strong>::http::geturl</strong> (ÓÍ. <a href=
"#íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé">&quot;íÁÓÓÉ× ÓÏÓÔÏÑÎÉÑ
ÔÒÁÎÚÁËÃÉÉ&quot;</a>). ëÏÍÁÎÄÁ ÄÏÌÖÎÁ ×ÏÚ×ÒÁÝÁÔØ ÞÉÓÌÏ
ÂÁÊÔÏ×, ÐÒÏÞÉÔÁÎÎÙÈ ÉÚ <em>socket</em>. îÉÖÅ ÐÒÉ×ÅÄÅÎ
ÛÁÂÌÏÎ ÐÏÄÏÂÎÏÊ ÐÒÏÃÅÄÕÒÙ:</p>
<pre>
proc httpHandlerCallback {socket token} {
upvar #0 $token state
# ðÏÌÕÞÅÎ ÄÏÓÔÕÐ Ë socket É Tcl-ÍÁÓÓÉ×Õ state
# ...
# (ÎÁÐÒÉÍÅÒ:
# set data [read $socket 1000];
# set nbytes [string length $data])
# ...
return nbytes
}
</pre>
</dd>
<dt><strong>-headers</strong> <em>keyvaluelist</em></dt>
<dd><p>ïÐÃÉÑ ÉÓÐÏÌØÚÕÅÔÓÑ ÄÌÑ ×ËÌÀÞÅÎÉÑ × ÚÁÇÏÌÏ×ÏË HTTP
ÚÁÐÒÏÓÁ ÄÏÐÏÌÎÉÔÅÌØÎÙÈ ÐÏÌÅÊ. áÒÇÕÍÅÎÔ ÄÏÌÖÅÎ ÂÙÔØ
ÐÒÁ×ÉÌØÎÙÍ ÓÐÉÓËÏÍ Ó ÞÅÔÎÙÍ ÞÉÓÌÏÍ ÜÌÅÍÅÎÔÏ×, ÓÏÓÔÏÑÝÉÍ
ÐÏÐÅÒÅÍÅÎÎÏ ÉÚ ËÌÀÞÅÊ É ÉÈ ÚÎÁÞÅÎÉÊ. ëÌÀÞÉ ÉÓÐÏÌØÚÕÀÔÓÑ
ËÁË ÉÍÅÎÁ ÐÏÌÅÊ ÚÁÇÏÌÏ×ËÁ. éÚ ÚÎÁÞÅÎÉÊ ÕÄÁÌÑÀÔÓÑ ÓÉÍ×ÏÌÙ
ÐÅÒÅÈÏÄÁ ÎÁ ÎÏ×ÕÀ ÓÔÒÏËÕ, ÞÔÏÂÙ ÉÚÂÅÖÁÔØ ÆÏÒÍÉÒÏ×ÁÎÉÑ
ÎÅÐÒÁ×ÉÌØÎÏÇÏ ÚÁÇÏÌÏ×ËÁ. îÁÐÒÉÍÅÒ, ÅÓÌÉ
<em>keyvaluelist</em> ÓÏÄÅÒÖÉÔ ÓÐÉÓÏË <em>{Pragma
no-cache}</em> ÂÕÄÅÔ ÓÆÏÒÍÉÒÏ×ÁÎ ÓÌÅÄÕÀÝÉÊ ÚÁÇÏÌÏ×ÏË
ÚÁÐÒÏÓÁ:</p>
<pre>
Pragma: no-cache
</pre>
</dd>
<dt><strong>-progress</strong> <em>callback</em></dt>
<dd><p>ïÐÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ×ÙÚÏ× ËÏÍÁÎÄÙ <em>callback</em> ÄÌÑ
ÏÂÒÁÂÏÔËÉ ÏÞÅÒÅÄÎÏÊ ÐÏÒÃÉÉ ÄÁÎÎÙÈ. ëÏÍÁÎÄÁ
<em>callback</em> ÐÏÌÕÞÁÅÔ ÔÒÉ ÁÒÇÕÍÅÎÔÁ: ÚÎÁÞÅÎÉÅ
<em>token</em>, ×ÏÚ×ÒÁÝÅÎÎÏÅ ËÏÍÁÎÄÏÊ
<strong>::http::geturl</strong>, ÐÒÅÄÐÏÌÁÇÁÅÍÙÊ ÐÏÌÎÙÊ ÒÁÚÍÅÒ
ÄÁÎÎÙÈ ÉÚ ÍÅÔÁ-ÄÁÎÎÙÈ É ÔÅËÕÝÅÅ ËÏÌÉÞÅÓÔ×Ï ÐÏÓÔÕÐÉ×ÛÉÈ
ÄÁÎÎÙÈ × ÂÁÊÔÁÈ. åÓÌÉ ÐÒÅÄÐÏÌÁÇÁÅÍÙÊ ÐÏÌÎÙÊ ÒÁÚÍÅÒ
ÎÅÉÚ×ÅÓÔÅÎ, ×ÍÅÓÔÏ ÎÅÇÏ ÐÏÄÓÔÁ×ÌÑÅÔÓÑ 0. îÉÖÅ ÐÒÉ×ÅÄÅÎ
ÛÁÂÌÏÎ ÄÌÑ ÐÒÏÃÅÄÕÒÙ, ×ÙÚÙ×ÁÅÍÏÊ ÐÏ ÏÐÃÉÉ
<strong>-progress</strong>:</p>
<pre>
proc httpProgress {token total current} {
upvar #0 $token state
}
</pre>
</dd>
<dt><strong>-query</strong> <em>query</em></dt>
<dd>åÓÌÉ ÕËÁÚÁÎÁ ÄÁÎÎÁÑ ÏÐÃÉÑ, <strong>::http::geturl</strong>
ÆÏÒÍÉÒÕÅÔ ÚÁÐÒÏÓ POST É ÐÅÒÅÄÁÅÔ ÅÇÏ ÎÁ ÓÅÒ×ÅÒ. úÁÐÒÏÓ
ÄÏÌÖÅÎ ÂÙÔØ ÓÆÏÒÍÁÔÉÒÏ×ÁÎ. äÌÑ ×ÙÐÏÌÎÅÎÉÑ ÆÏÒÍÁÔÉÒÏ×ÁÎÉÑ
ÍÏÖÅÔ ÉÓÐÏÌØÚÏ×ÁÔØÓÑ ÐÒÏÃÅÄÕÒÁ <a href=
"#http::formatQuery"><strong>::http::formatQuery</strong></a>.
</dd>
<dt><strong>-timeout</strong> <em>milliseconds</em></dt>
<dd>åÓÌÉ ÚÎÁÞÅÎÉÅ <em>milliseconds</em> ÎÅ ÒÁ×ÎÏ ÎÕÌÀ,
ÕÓÔÁÎÁ×ÌÉ×ÁÅÔÓÑ ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÅÅ ×ÒÅÍÑ ÚÁÄÅÒÖËÉ. úÁÄÅÒÖËÁ
×ÙÐÏÌÎÑÅÔÓÑ ÐÅÒÅÄ ×ÙÚÏ×ÏÍ ËÏÍÁÎÄÙ <a href=
"#http::reset"><strong>::http::reset</strong></a> É ËÏÍÁÎÄÙ,
ÚÁÄÁÎÎÏÊ ÏÐÃÉÅÊ <strong>-command</strong>. ÷Ï ×ÒÅÍÑ ÚÁÄÅÒÖËÉ ËÏÍÁÎÄÁ
<a href="#http::status"><strong>::http::status</strong></a>
×ÏÚ×ÒÁÝÁÅÔ ÚÎÁÞÅÎÉÅ <em>timeout</em>.
</dd>
<dt><strong>-validate</strong> <em>boolean</em></dt>
<dd>åÓÌÉ ÚÎÁÞÅÎÉÅ boolean ÎÅ ÒÁ×ÎÏ ÎÕÌÀ,
<strong>::http::geturl</strong> ×ÙÐÏÌÎÑÅÔ HTTP HEAD ÚÁÐÒÏÓ. ôÁËÏÊ
ÚÁÐÒÏÓ ×ÏÚ×ÒÁÝÁÅÔ ÍÅÔÁ ÉÎÆÏÒÍÁÃÉÀ Ï ÉÓÔÏÞÎÉËÅ ÄÁÎÎÙÈ
(URL), Á ÎÅ ÅÇÏ ÓÏÄÅÒÖÁÎÉÅ. íÅÔÁ ÉÎÆÏÒÍÁÃÉÑ ÓÏÄÅÒÖÉÔÓÑ ×
ÐÅÒÅÍÅÎÎÏÊ <em>state(meta)</em> (ÓÍ. &quot;<a
href="#íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé">íÁÓÓÉ×
ÓÏÓÔÏÑÎÉÑ ÔÒÁÎÚÁËÃÉÉ</a>&quot;).
</dd>
</dl>
</dd>
<dt><a name="http::formatQuery"></a><strong>::http::formatQuery</strong>
<em>key</em> <em>value</em> ?<em>key</em> <em>value</em>...?</dt>
<dd>ëÏÍÁÎÄÁ ×ÙÐÏÌÎÑÅÔ ÐÅÒÅËÏÄÉÒÏ×ÁÎÉÅ ÚÁÐÒÏÓÁ. ëÏÍÁÎÄÁ
ÉÓÐÏÌØÚÕÅÔ ÞÅÔÎÏÅ ÞÉÓÌÏ ÁÒÇÕÍÅÎÔÏ×, Ñ×ÌÑÀÝÉÈÓÑ ÓÏÏÔ×ÅÔÓÔ×ÅÎÎÏ
ËÌÀÞÁÍÉ ÚÁÐÒÏÓÁ É ÉÈ ÚÎÁÞÅÎÉÑÍÉ. ïÎÁ ÐÒÅÏÂÒÁÚÕÅÔ ËÌÀÞÉ É
ÚÎÁÞÅÎÉÑ É ×ÏÚ×ÒÁÝÁÅÔ ÏÄÎÕ ÓÔÒÏËÕ, × ËÏÔÏÒÏÊ ÒÁÓÓÔÁ×ÌÅÎÙ
ÎÅÏÂÈÏÄÉÍÙÅ &quot;&amp;&quot; É &quot;=&quot;
ÒÁÚÄÅÌÉÔÅÌÉ. òÅÚÕÌØÔÁÔ ÍÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ × ËÁÞÅÓÔ×Å ÚÎÁÞÅÎÉÑ
ÄÌÑ ÏÐÃÉÉ <strong>-query</strong> ËÏÍÁÎÄÙ <a href="#http::geturl_url">
<strong>::http::geturl</strong></a>.
</dd>
<dt><a name="http::reset"></a><strong>::http::reset</strong> <em>token</em>
?<em>why</em>?</dt>
<dd>ëÏÍÁÎÄÁ ÐÅÒÅÚÁÐÕÓËÁÅÔ HTTP ÔÒÁÎÚÁËÃÉÀ <em>token</em>, ÅÓÌÉ
ÔÁËÁÑ ÉÓÐÏÌÎÑÅÔÓÑ. úÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ <strong>state(status)</strong>
ÐÒÉ ÜÔÏÍ ÐÅÒÅÕÓÔÁÎÁ×ÌÉ×ÁÅÔÓÑ × <em>why</em> (ÐÏ ÕÍÏÌÞÁÎÉÀ -
<strong>reset</strong>) É ×ÙÚÙ×ÁÅÔÓÑ ËÏÍÁÎÄÁ, ÚÁÄÁÎÎÁÑ ÏÐÃÉÅÊ
<strong>-command</strong>.
</dd>
<dt><a name="http::wait"></a><strong>::http::wait</strong>
<em>token</em></dt>
<dd>üÔÁ ËÏÍÁÎÄÁ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÏÖÉÄÁÎÉÅ ÚÁ×ÅÒÛÅÎÉÑ ÔÒÁÎÚÁËÃÉÉ. ïÎÁ
ÒÁÂÏÔÁÅÔ ÔÏÌØËÏ × ÎÁÄÅÖÎÙÈ ÉÎÔÅÒÐÒÅÔÁÔÏÒÁÈ, ÔÁË ËÁË ÏÎÁ
ÉÓÐÏÌØÚÕÅÔ ËÏÍÁÎÄÕ <a href="vwait.html"><strong>vwait</strong></a>.
</dd>
<dt><a name="http::data"></a><strong>::http::data</strong>
<em>token</em></dt>
<dd>üÔÁ ËÏÍÁÎÄÁ ×ÏÚ×ÒÁÝÁÅÔ ÚÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ
<strong>state(body)</strong>.
</dd>
<dt><a name="http::status"></a><strong>::http::status</strong>
<em>token</em></dt>
<dd>üÔÁ ËÏÍÁÎÄÁ ×ÏÚ×ÒÁÝÁÅÔ ÚÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ
<strong>state(status)</strong>.
</dd>
<dt><a name="http::code"></a><strong>::http::code</strong>
<em>token</em></dt>
<dd>üÔÁ ËÏÍÁÎÄÁ ×ÏÚ×ÒÁÝÁÅÔ ÚÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ
<strong>state(http)</strong>.
</dd>
<dt><a name="http::size"></a><strong>::http::size</strong>
<em>token</em></dt>
<dd>üÔÁ ËÏÍÁÎÄÁ ×ÏÚ×ÒÁÝÁÅÔ ÚÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ
<strong>state(currentsize)</strong>.
</dd>
</dl>
<h2><a name="íáóóé÷_óïóôïñîéñ_ôòáîúáëãéé"></a>íáóóé÷ óïóôïñîéñ ôòáîúáëãéé</h2>
<p>ëÏÍÁÎÄÁ <a href="#http::geturl_url">
<strong>::http::geturl</strong></a> ×ÏÚ×ÒÁÝÁÅÔ <em>token</em> - ÉÍÑ
Tcl-ÍÁÓÓÉ×Á, ÓÏÄÅÒÖÁÝÅÇÏ ÉÎÆÏÒÍÁÃÉÀ Ï HTTP ÔÒÁÎÚÁËÃÉÉ. äÌÑ
ÕÐÒÏÝÅÎÉÑ ÄÏÓÔÕÐÁ Ë ÍÁÓÓÉ×Õ ÍÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÓÌÅÄÕÀÝÕÀ
ËÏÎÓÔÒÕËÃÉÀ:</p>
<pre>
upvar #0 $token state
</pre>
<p>íÁÓÓÉ× ÓÏÄÅÒÖÉÔ ÓÌÅÄÕÀÝÉÅ ÜÌÅÍÅÎÔÙ:</p>
<dl>
<dt>body</dt>
<dd>óÏÄÅÒÖÁÎÉÅ ÄÏËÕÍÅÎÔÁ, ÚÁÄÁÎÎÏÇÏ Ó ÐÏÍÏÝØÀ URL. ðÕÓÔÏ, ÅÓÌÉ
ÕËÁÚÁÎÁ ÏÐÃÉÑ <strong>-channel</strong>. úÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ ÍÏÖÎÏ
ÐÏÌÕÞÉÔØ ÔÁËÖÅ Ó ÐÏÍÏÝØÀ ËÏÍÁÎÄÙ <a href=
"#http::data"><strong>::http::data</strong></a>.
</dd>
<dt>Óurrentsize</dt>
<dd>ôÅËÕÝÉÊ ÏÂßÅÍ ÉÎÆÏÒÍÁÃÉÉ × ÂÁÊÔÁÈ, ÐÏÌÕÞÅÎÎÙÊ ÏÔ
ÉÓÔÏÞÎÉËÁ. úÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ ÍÏÖÎÏ ÐÏÌÕÞÉÔØ Ó ÐÏÍÏÝØÀ
ËÏÍÁÎÄÙ <a href="#http::size"><strong>::http::size</strong></a>.
</dd>
<dt>error</dt>
<dd>åÓÌÉ ÜÌÅÍÅÎÔ ÏÐÒÅÄÅÌÅÎ, ÏÎ ÓÏÄÅÒÖÉÔ ÓÔÒÏËÕ Ó ÓÏÏÂÝÅÎÉÅÍ ÏÂ
ÏÛÉÂËÅ, ÐÏÌÕÞÅÎÎÕÀ ÐÒÉ ÐÒÅÒÙ×ÁÎÉÉ HTTP ÔÒÁÎÚÁËÃÉÉ.</dd>
<dt>http</dt>
<dd>üÌÅÍÅÎÔ ÓÏÄÅÒÖÉÔ ÚÎÁÞÅÎÉÅ HTTP ÓÔÁÔÕÓÁ, ÐÏÌÕÞÅÎÎÏÅ ÏÔ
ÓÅÒ×ÅÒÁ. úÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ ÍÏÖÎÏ ÐÏÌÕÞÉÔØ ÔÁËÖÅ Ó ÐÏÍÏÝØÀ
ËÏÍÁÎÄÙ <a
href="#http::code"><strong>::http::code</strong></a>. óÔÁÔÕÓ
ÐÒÅÄÓÔÁ×ÌÑÅÔ ÓÏÂÏÊ ÓÔÒÏËÕ ÉÚ ÔÒÅÈ ÃÉÆÒ, ÚÎÁÞÅÎÉÑ ËÏÔÏÒÏÊ
ÓÏÏÔ×ÅÔÓÔ×ÕÀÔ HTTP ÓÔÁÎÄÁÒÔÕ. ëÏÄ 200 ÓÏÏÔ×ÅÔÓÔ×ÕÅÔ ÕÓÐÅÛÎÏÍÕ
×ÙÐÏÌÎÅÎÉÀ ÔÒÁÎÚÁËÃÉÉ. ëÏÄÙ, ÎÁÞÉÎÁÀÝÉÅÓÑ Ó '4' ÉÌÉ '5';,
ÕËÁÚÙ×ÁÀÔ ÎÁ ÏÛÉÂËÕ. ëÏÄÙ, ÎÁÞÉÎÁÀÝÉÅÓÑ Ó '3', ÓÏÏÔ×ÅÔÓÔ×ÕÀÔ
ÏÛÉÂËÁÍ ÐÅÒÅÎÁÐÒÁ×ÌÅÎÉÑ. ÷ ÜÔÏÍ ÓÌÕÞÁÅ ÍÅÔÁ ÄÁÎÎÙÅ
<strong>Location</strong> ÏÐÒÅÄÅÌÑÀÔ ÎÏ×ÙÊ ÉÓÔÏÞÎÉË ÉÎÆÏÒÍÁÃÉÉ, ËÏÔÏÒÙÊ
ÓÏÄÅÒÖÉÔ ÚÁÐÒÏÛÅÎÎÙÅ ÄÁÎÎÙÅ.</dd>
<dt>meta</dt>
<dd><p>íÅÔÁ ÄÁÎÎÙÅ, ÏÐÉÓÙ×ÁÀÝÉÅ ÓÏÄÅÒÖÁÎÉÅ ÄÏËÕÍÅÎÔÁ. äÁÎÎÙÊ
ÜÌÅÍÅÎÔ ÍÁÓÓÉ×Á ÓÏÄÅÒÖÉÔ ÓÐÉÓÏË ËÌÀÞÅÊ É ÉÈ ÚÎÁÞÅÎÉÊ. þÔÏÂÙ
ÏÂÌÅÇÞÉÔØ ÄÏÓÔÕÐ Ë ÄÁÎÎÙÍ ÍÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÓÌÅÄÕÀÝÕÀ
ËÏÎÓÔÒÕËÃÉÀ:</p>
<pre>
array set meta $state(meta)
</pre>
<p>îÅËÏÔÏÒÙÅ ËÌÀÞÉ ÍÅÔÁ ÄÁÎÎÙÈ ÐÅÒÅÞÉÓÌÅÎÙ ÎÉÖÅ, ÎÏ × HTTP
ÓÔÁÎÄÁÒÔÅ ÉÈ ÐÅÒÅÞÉÓÌÅÎÏ ÂÏÌØÛÅ, ËÒÏÍÅ ÔÏÇÏ, ÓÅÒ×ÅÒ ÍÏÖÅÔ
ÄÏÂÁ×ÌÑÔØ ÓÏÂÓÔ×ÅÎÎÙÅ.</p>
</dd>
<dt>Content-Type</dt>
<dd>ôÉÐ ÄÏËÕÍÅÎÔÁ. îÁÐÒÉÍÅÒ, <strong>text/html</strong>, <strong>image/gif</strong>,
<strong>application/postscript</strong> ÉÌÉ <strong>application/x-tcl</strong>.</dd>
<dt>Content-Length</dt>
<dd>ïÂßÑ×ÌÅÎÎÙÊ ÒÁÚÍÅÒ ÄÏËÕÍÅÎÔÁ. òÅÁÌØÎÙÊ ÏÂßÅÍ ÉÎÆÏÒÍÁÃÉÉ,
ÐÏÌÕÞÅÎÎÏÊ Ó ÐÏÍÏÝØÀ ËÏÍÁÎÄÙ <a href="#http::geturl_url">
<strong>::http::geturl</strong></a>, ÓÏÄÅÒÖÉÔÓÑ × ÐÅÒÅÍÅÎÎÏÊ
<strong>state(size)</strong>.
</dd>
<dt>Location</dt>
<dd>éÚÍÅÎÅÎÎÙÊ ÁÄÒÅÓ ÄÏËÕÍÅÎÔÁ.</dd>
<dt>status</dt>
<dd>÷ÏÚÍÏÖÎÙÅ ÚÎÁÞÅÎÉÑ <strong>ok</strong>,<strong>reset</strong> ÉÌÉ
<strong>error</strong>. ÷Ï ×ÒÅÍÑ ÔÒÁÎÚÁËÃÉÉ ÚÎÁÞÅÎÉÅ ÐÕÓÔÏÅ.</dd>
<dt>totalsize</dt>
<dd>ëÏÐÉÑ ÚÎÁÞÅÎÉÑ ÍÅÔÁ ÄÁÎÎÙÈ <strong>Content-Length</strong>.</dd>
<dt>type</dt>
<dd>ëÏÐÉÑ ÚÎÁÞÅÎÉÑ ÍÅÔÁ ÄÁÎÎÙÈ <strong>Content-Type</strong>.</dd>
<dt>url</dt>
<dd><p>úÁÐÒÏÛÅÎÎÙÊ ÁÄÒÅÓ. ðÒÉÍÅÒ:</p>
<pre>
# ëÏÐÉÒÏ×ÁÎÉÅ ÉÓÔÏÞÎÉËÁ × ÆÁÊÌ É ÐÅÞÁÔØ ÍÅÔÁ ÄÁÎÎÙÈ
proc ::http::copy { url file {chunk 4096} } {
set out [open $file w]
set token [geturl $url -channel $out -progress ::http::Progress \
-blocksize $chunk]
close $out
# óÌÅÄÕÀÝÁÑ ËÏÍÁÎÄÁ ÚÁ×ÅÒÛÁÅÔ ÓÔÒÏËÕ, ÎÁÞÁÔÕÀ
# ÐÒÏÃÅÄÕÒÏÊ http::Progress
puts stderr ""
upvar #0 $token state
set max 0
foreach {name value} $state(meta) {
if {[string length $name] &gt; $max} {
set max [string length $name]
}
if {[regexp -nocase ^location$ $name]} {
# ïÂÒÁÂÏÔËÁ ÐÅÒÅÎÁÐÒÁ×ÌÅÎÉÑ ÁÄÒÅÓÁ
puts stderr "Location:$value"
return [copy [string trim $value] $file $chunk]
}
}
incr max
foreach {name value} $state(meta) {
puts [format "%-*s %s" $max $name: $value]
}
return $token
}
proc ::http::Progress {args} {
puts -nonewline stderr . ;
flush stderr
}
</pre>
</dd>
</dl>
</body>
</html>