⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02_04.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>The <em class="emphasis">Content-Base</em><a name="INDEX-385" /><a name="INDEX-386" /> field contains a URL to use as thebase for relative URLs in HTML documents. Using the &lt;BASEHREF=...&gt; tag in the head of the document accomplishes the samething and is more common.</p></div><a name="ch02-22-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.2. Content-Length</h3><p>As with request headers, the <a name="INDEX-387" /><a name="INDEX-388" /><a name="INDEX-389" /><em class="emphasis">Content-Length</em>field in response headers contains the length of the body of theresponse. Browsers use this to detect an interrupted transaction orto tell the user the percentage of the download that is complete.</p></div><a name="ch02-23-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.3. Content-Type</h3><p>You will use the <a name="INDEX-390" /> <a name="INDEX-391" /><em class="emphasis">Content-Type</em> headervery often in your CGI scripts. This field is provided with everyresponse containing a body and must be included for all requestsaccompanied by a status code of <em class="emphasis">200</em>. The mostcommon value for this response is <em class="emphasis">text/html</em>,which is what is returned with HTML documents. Other examples are<em class="emphasis">text/plain</em> for text documents and<em class="emphasis">application/pdf</em> for Adobe PDF documents.</p><p>Because this field originally derived from a similar MIME field, thisfield is often referred to as the <em class="firstterm">MIME type</em><a name="INDEX-392" />of the message. However, this term is notaccurate because the possible values for this field differs for theWeb than for Internet email. The IANA maintains a registry of<a name="INDEX-393" /> <a name="INDEX-394" />registered media types for the Web,which may be viewed at <a href="http://www.isi.edu/in-notes/iana/assignments/media-types/">http://www.isi.edu/in-notes/iana/assignments/media-types/</a>.Although you could invent your media type values, it is a good ideato stick with these registered ones since web browsers need to knowhow to handle the associated documents.</p></div><a name="ch02-24-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.4. Date</h3><p>HTTP 1.1 <a name="INDEX-395" /><a name="INDEX-396" />requires that servers send the<em class="emphasis">Date</em> header with all responses. It specifies thedate and time the response is sent. Three different date<a name="INDEX-397" />formats are acceptable in HTTP:</p><blockquote><pre class="code">Mon, 06 Aug 1999 19:01:42 GMTMonday, 06-Aug-99 19:01:42 GMTMon Aug  6 19:01:42 1999</pre></blockquote><p>The HTTP specification recommends the first option, but all should besupported by HTTP applications. The last is the format generated byPerl's <tt class="function">gmtime</tt> function.<a href="#FOOTNOTE-4">[4]</a></p><blockquote><a name="FOOTNOTE-4" /><p>[4]More specifically, <tt class="function">gmtime</tt> generates a datestring like this when it is called in a <a name="INDEX-398" /><a name="INDEX-399" /> <a name="INDEX-400" />scalar context. In list context, itreturns a list of date elements instead. If this distinction seemsunclear, then you may want to refer to a good Perl book like<em class="citetitle">Programming Perl</em> for the difference betweenlist and scalar context.</p></blockquote></div><a name="ch02-25-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.5. ETag</h3><p>The <em class="firstterm">ETag</em><a name="INDEX-401" /> <a name="INDEX-402" /> header specifies an <em class="firstterm">entitytag</em> corresponding to the requested<a name="INDEX-403" />resource. Entity tags were added toHTTP 1.1 to address problems with<a name="INDEX-404" /> <a name="INDEX-405" /> <a name="INDEX-406" />caching. Although HTTP 1.1 does notspecify any particular way for a server to generate an entity tag,they are analogous to a <a name="INDEX-407" /><a name="INDEX-408" />message digest or checksum for a file.Clients and proxies can assume that all copies of a resource with thesame URL and same entity tag are identical. Thus, generating a HEADrequest and checking the ETag header of the response is an effectiveway for a browser to determine whether a previously cached responseneeds to be fetched again. Web servers typically do not generatethese for CGI scripts, although you can generate your own if you wishto have greater control over how HTTP 1.1 clients cache yourresponses.</p></div><a name="ch02-26-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.6. Last-Modified</h3><p>The <em class="emphasis">Last-Modified</em> header returns the <a name="INDEX-409" /> <a name="INDEX-410" /><a name="INDEX-411" /> <a name="INDEX-412" />date and time that the requested resourcewas last updated. This was intended to support caching, but it didnot always work as well as hoped in HTTP 1.0, so the<em class="emphasis">ETag</em> header now supplements it. The<em class="firstterm">Last-Modified</em> header is restrictive because itimplies that HTTP resources are static files, which is obviously notalways the case. For example, for CGI scripts the value of this fieldmust reflect the last time the output changed (possibly due to achange in a data source), and not the date and time that the CGIscript itself was last updated. Like the <em class="emphasis">ETag</em>header, the web server does not typically generate the <em class="emphasis">Last-Modified</em>header for your CGI scripts, although you can output it yourself ifyou desire.</p></div><a name="ch02-27-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.7. Location</h3><p>The <em class="emphasis">Location</em> <a name="INDEX-413" /><a name="INDEX-414" /> <a name="INDEX-415" /><a name="INDEX-416" /> header is used to inform aclient that it should look elsewhere for the requested resource. Thevalue should contain an absolute URL to the new location. This headershould be accompanied by a <em class="emphasis">3xx</em> series statuscode. Browsers generally fetch the resource from the new locationautomatically for the user. Responses with a<em class="emphasis">Location</em> field may also contain contents withinstructions for the user since very old browsers may not respond tothe <em class="emphasis">Location</em> field.</p></div><a name="ch02-28-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.8. Server</h3><p>The <em class="emphasis">Server</em> header provides the<a name="INDEX-417" /><a name="INDEX-418" /> <a name="INDEX-419" />name and version of the applicationacting as the web server. The web server automatically generates thisfor standard responses. There are circumstances when you shouldgenerate this yourself, which we will see in the next chapter.</p></div><a name="ch02-29-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.9. Set-Cookie</h3><p>The <em class="emphasis">Set-Cookie</em> <a name="INDEX-420" /><a name="INDEX-421" />header asks the browser to remember aname-value pair and send this data back on subsequent requests tothis server. The server can specify how long the browser shouldremember the cookie and to what hosts or domains the browser shouldprovide it. We'll discuss cookies in detail in our discussionof maintaining state in <a href="ch11_01.htm">Chapter 11, "Maintaining State"</a>.</p></div><a name="ch02-30-fm2xml" /><div class="sect3"><h3 class="sect3">2.4.2.10. WWW-Authenticate</h3><p>As we discussed earlier in <a href="ch02_03.htm#ch02-23130">Section 2.3.2.4, "Authorization"</a>, webservers can restrict certain resources to users who provide a validusername and password. The<em class="firstterm">WWW-Authenticate</em><a name="INDEX-422" /><a name="INDEX-423" /><a name="INDEX-424" /><a name="INDEX-425" /><a name="INDEX-426" /><a name="INDEX-427" /> field is used along with a status codeof <em class="emphasis">401</em> to indicate that the requested resourcerequires a such a login. The value of this field should contain theform of authentication and the realm for which the authorizationapplies. An authorization realm generally maps to a certain directoryon the web server, and a username and password pair should apply to<a name="INDEX-428" /><a name="INDEX-429" /><a name="INDEX-430" />allresources <a name="INDEX-431" /> <a name="INDEX-432" /> <a name="INDEX-433" />within a realm.</p></div></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch02_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch02_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">2.3. Browser Requests</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">2.5. Proxies</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -