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

📄 ch02_02.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
字号:
<?label 2.2. HTTP?><html><head><title>HTTP (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch02_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch02_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">2.2. HTTP</h2><p>Now that we have a clearer understanding of URLs, let's returnto the main focus of this chapter: HTTP, the protocol that clientsand servers use to communicate on the Web.</p><a name="ch02-52204" /><blockquote><h4 class="objtitle">The Secure Sockets Layer</h4><p>HTTP is not a secure protocol, and many networking protocols(like ethernet) allow the conversation between two computers to beoverheard by other computers on the same area of the network. Theresult is that it is very possible for a third party to eavesdrop onHTTP transactions and record authentication information, credit cardnumbers, and other important data.</p><p>Thus, Netscapedeveloped the<em class="firstterm">SSL</em><a name="INDEX-253" />(<em class="firstterm">Secure Sockets Layer</em>) protocol, whichprovides a secure communications channel that HTTP can operateacross, while also providing security against eavesdropping and otherprivacy attacks. SSL has developed into an IETF standard and is nowformally referred to as the<em class="firstterm">TLS</em><a name="INDEX-254" /> (<em class="firstterm">Transport LayerSecurity</em>) protocol (TLS 1.0 is essentially SSL 3.1). Notall browsers support TLS yet.</p><p>When your browserrequests a URL that begins with <tt class="literal">https</tt>, it createsan SSL/TLS connection to the remote server and performs its HTTPtransaction across this secure connection. Fortunately, youdon't need to understand the details of how this works to writescripts, because the web server transparently manages it for you.Standard <a name="INDEX-255" /> <a name="INDEX-256" /><a name="INDEX-257" />CGI scripts will work thesame in a secure environment as in a standard one. When your CGIscript receives a secure SSL/TLS connection, however, you are givenadditional information about the client and the connection, as wewill see in the next chapter.</p></blockquote><a name="ch02-3-fm2xml" /><div class="sect2"><h3 class="sect2">2.2.1. The Request and Response Cycle</h3><p>When a <a name="INDEX-258" /> <a name="INDEX-259" />web browser <a name="INDEX-260" /> <a name="INDEX-261" />requests a web page, it sends a requestmessage to a web server. The message always includes a<a name="INDEX-262" />header,and sometimes it also includes a<a name="INDEX-263" /><a name="INDEX-264" /><a name="INDEX-265" /><a name="INDEX-266" />body. The web server in turn replies with areply message. This message also always includes a header and itusually contains a body.</p><p>There are two features that are important in understanding HTTP:</p><ul><li><p>It is a request/response protocol: each response is preceded by arequest.</p></li><li><p>Although requests and responses each contain different information,the header/body structure is the same for both messages. The headercontains <em class="firstterm">meta-information</em><a name="INDEX-267" /><a name="INDEX-268" /> -- information about themessage -- and the body contains the<em class="firstterm">content</em> of the message.</p></li></ul><p><a href="ch02_02.htm#ch02-70636">Figure 2-2</a> shows an example of an HTTP transaction.Say you told your browser you wanted a document at <em class="emphasis">http://localhost/index.html</em>. The browserwould connect to the machine at <em class="emphasis">localhost</em> on port 80 and send it thefollowing message:</p><blockquote><pre class="code">GET /index.html HTTP/1.1Host: localhostAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/xbm, */*Accept-Language: enConnection: Keep-AliveUser-Agent: Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)</pre></blockquote><a name="ch02-70636" /><div class="figure"><img width="352" src="figs/cgi2.0202.gif" height="192" alt="Figure 2-2" /></div><h4 class="objtitle">Figure 2-2. The HTTP request/response cycle</h4><p>Assuming that a web server is running and the path maps to a validdocument, the server would reply with the following message:</p><blockquote><pre class="code">HTTP/1.1 200 OKDate: Sat, 18 Mar 2000 20:35:35 GMTServer: Apache/1.3.9 (Unix)Last-Modified: Wed, 20 May 1998 14:59:42 GMTETag: "74916-656-3562efde"Content-Length: 141Content-Type: text/html&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Sample Document&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;  &lt;H1&gt;Sample Document&lt;/H1&gt;  &lt;P&gt;This is a sample HTML document!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</pre></blockquote><p>In this example, the request includes a header but no content. The<a name="INDEX-269" />response includes both a headerand HTML <a name="INDEX-270" /><a name="INDEX-271" />content,<a name="INDEX-272" /><a name="INDEX-273" />separatedby a blank line (see <a href="ch02_02.htm#ch02-17272">Figure 2-3</a>).</p><a name="ch02-17272" /><div class="figure"><img width="234" src="figs/cgi2.0203.gif" height="128" alt="Figure 2-3" /></div><h4 class="objtitle">Figure 2-3. The HTTP header/body message structure</h4></div><a name="ch02-4-fm2xml" /><div class="sect2"><h3 class="sect2">2.2.2. HTTP Headers</h3><p>If you are familiar with the format of <a name="INDEX-275" /><a name="INDEX-276" />Internet email, this<a name="INDEX-277" /><a name="INDEX-278" /><a name="INDEX-279" />header and bodysyntax may look familiar to you. Historically, the format of HTTPmessages is based upon many of the conventions used by Internetemail, as established by <a name="INDEX-280" />MIME (Multipurpose Internet MailExtensions). Do not be tricked into thinking that HTTP and MIMEheaders are the same, however. The similarity extends only to certainfields, and many early similarities have changed in later versions ofHTTP.</p><p>Here are the important things to know about header syntax:</p><ul><li><p>The first line of the header has a unique format and special meaning.It is called a <em class="firstterm">requestline</em><a name="INDEX-281" /> <a name="INDEX-282" /> in requests and a <em class="filename">status line</em>in replies.</p></li><li><p>The remainder of the header lines contain<a name="INDEX-283" />name-value pairs. The name and valueare separated by a colon and any combination of spaces and/or tabs.These lines are called <em class="firstterm">headerfields</em><a name="INDEX-284" /><a name="INDEX-285" />.</p></li><li><p>Some header fields may have multiple values. This can be representedby having multiple header fields contain the same field name anddifferent values or by including all the values in the header fieldseparated by a comma.</p></li><li><p>Field names are not <a name="INDEX-286" />case-sensitive; e.g.,<tt class="literal">Content-Type</tt> is the same as<tt class="literal">Content-type</tt>.</p></li><li><p>Header fields don't have to appear in any special order.</p></li><li><p>Every line in the header must be terminated by a carriage return andline feed sequence, which is often abbreviated as<a name="INDEX-287" />CRLFand represented as <tt class="literal">\015\012</tt> in Perl on ASCIIsystems.</p></li><li><p>The header must be separated from the <a name="INDEX-288" />content by a blank line. In other words,the last header line must end with two CRLFs.</p></li></ul></div><a name="ch02-5-fm2xml" /><div class="sect2"><h3 class="sect2">2.2.3. HTTP 1.1 and HTTP 1.0</h3><p>This chapter discusses <a name="INDEX-289" /> <a name="INDEX-290" />HTTP 1.1, which includesseveral improvements to previous versions of HTTP. Although HTTP 1.1is backward-compatible, there are many new features in HTTP 1.1 notrecognized by HTTP 1.0 applications. There are even a few instanceswhere the new protocol can cause problematic behavior with olderapplications, especially with caching. Most major web servers andbrowsers are now HTTP 1.1-compliant as this book is being written.There will continue to be HTTP 1.0 applications on the Web for sometime, however. Features discussed in this chapter that differ betweenHTTP 1.1 and HTTP 1.0 will be noted.</p></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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">2. The Hypertext Transport Protocol </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.3. Browser Requests</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 + -