📄 ch02_03.htm
字号:
<?label 2.3. Browser Requests?><html><head><title>Browser Requests (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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">2.3. Browser Requests</h2><p>Every HTTP interaction starts with a request from a client, typicallya web <a name="INDEX-291" /> <a name="INDEX-292" />browser. A user provides a URL to thebrowser by typing it in, clicking on a hyperlink, or selecting abookmark, and the browser fetches the corresponding document. To dothat, it must create an HTTP request (see <a href="ch02_03.htm#ch02-98871">Figure 2-4</a>).</p><a name="ch02-98871" /><div class="figure"><img width="260" src="figs/cgi2.0204.gif" height="45" alt="Figure 2-4" /></div><h4 class="objtitle">Figure 2-4. The structure of HTTP request headers</h4><p>Recall that in our previous example, a web browser generated thefollowing request when it was asked to fetch the URL <em class="emphasis">http://localhost/index.html </em>:</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><p>From our discussion of URLs, you know that the URL can be broken downinto multiple elements. The browser creates a network connection byusing the hostname and the port number (<tt class="literal">80</tt> bydefault). The scheme (<tt class="literal">http</tt>) tells our web browserthat it is using the HTTP protocol, so once the connection isestablished, it sends an HTTP request for the resource. The firstline of an HTTP request is the <a name="INDEX-293" /> <a name="INDEX-294" />request line, which includes afull virtual path and query string (if present); see <a href="ch02_03.htm#ch02-76971">Figure 2-5</a>.</p><a name="ch02-76971" /><div class="figure"><img width="199" src="figs/cgi2.0205.gif" height="46" alt="Figure 2-5" /></div><h4 class="objtitle">Figure 2-5. The request line</h4><a name="ch02-6-fm2xml" /><div class="sect2"><h3 class="sect2">2.3.1. The Request Line</h3><p>The first line of an HTTP <a name="INDEX-295" /><a name="INDEX-296" /> <a name="INDEX-297" />request includes the request method, a URLto the resource being requested, and the <a name="INDEX-298" />version string of the protocol.<a name="INDEX-299" /> <a name="INDEX-300" /> <a name="INDEX-301" /> <a name="INDEX-302" />Request methods arecase-sensitive and uppercase. There are several request methodsdefined by HTTP although a web server may not make all of themavailable for each resource (see <a href="ch02_03.htm#ch02-57144">Table 2-1</a>). Theversion string is the name and version of the protocol separated by aslash. HTTP 1.0 and HTTP 1.1 are represented as<tt class="literal">HTTP/1.0</tt> and <tt class="literal">HTTP/1.1</tt>. Notethat <tt class="literal">https</tt> requests also produce one of these twoHTTP protocol strings.</p><a name="ch02-57144" /><h4 class="objtitle">Table 2-1. HTTP Request Methods</h4><table border="1"><tr><th><p>Method</p></th><th><p>Description</p></th></tr><tr><td><p>GET</p></td><td><p>Asks the server for the given resource</p></td></tr><tr><td><p>HEAD</p></td><td><p>Used in the same cases that a GET is used but it only returns HTTPheaders and no content</p></td></tr><tr><td><p>POST</p></td><td><p>Asks the server to modify information stored on the server</p></td></tr><tr><td><p>PUT</p></td><td><p>Asks the server to create or replace a resource on the server</p></td></tr><tr><td><p>DELETE</p></td><td><p>Asks the server to delete a resource on the server</p></td></tr><tr><td><p>CONNECT</p></td><td><p>Used to allow secure SSL connections to tunnel through HTTPconnections</p></td></tr><tr><td><p>OPTIONS</p></td><td><p>Asks the server to list the request methods available for the givenresource</p></td></tr><tr><td><p>TRACE</p></td><td><p>Asks the server to echo back the request headers as it received them</p></td></tr></table><p>Of the request methods listed in <a href="ch02_03.htm#ch02-57144">Table 2-1</a>, thethree you will encounter most often when writing CGI scripts are GET,HEAD, and POST. However, let's first take a look at why the PUTand DELETE methods are not used with CGI.</p><a name="ch02-7-fm2xml" /><div class="sect3"><h3 class="sect3">2.3.1.1. PUT and DELETE</h3><p>The Web was originally conceived as a medium where users could bothread and write content. However, the Web took off initially as aread-only medium and it is only through Web Distributed Authoring andVersioning (WebDAV) that interest is returning to the ability towrite content to the Web. The <a name="INDEX-303" /> <a name="INDEX-304" /> <a name="INDEX-305" /><a name="INDEX-306" /><a name="INDEX-307" />PUT and DELETE methods tell the serverto create, replace, or remove the resource they are directed at. Notethat this means that if one of these requests is targeted at a CGIscript (assuming the request is valid), the CGI script will bereplaced or removed, but not executed. Thus, you do not need to worryabout these request methods within your CGI scripts. While it mightbe possible to remap a PUT or DELETE request directed at a particularURL so that a different CGI script handles it, such a discussion ofWebDAV implementation is beyond the scope of this book.</p></div><a name="ch02-8-fm2xml" /><div class="sect3"><h3 class="sect3">2.3.1.2. GET</h3><p><em class="firstterm">GET</em><a name="INDEX-308" /><a name="INDEX-309" /><a name="INDEX-310" /> is the standard request method forretrieving a document via HTTP on the Web. When you click on ahyperlink, type a location into your browser, or click on a bookmark,the browser generally creates a GET request for the URL yourequested. GET requests are intended only to retrieve resources andshould not have side effects. They should not alter informationmaintained on the web server; POST is intended for that purpose. GETrequests do not have a content body.</p><p>In practice, some CGI developers do not understand nor follow thepolicy that GET requests should not have side effects, even though itis a good idea to do so. Because web browsers assume that GETrequests have no side effects, they may be carefree about makingmultiple requests for the same document. For instance, if the userpresses the browser's "back" button to return to apage that was originally requested via GET and is no longer in thebrowser's cache, the browser may GET a new copy. If theoriginal request was via POST, however, the user would insteadreceive a message that the document is no longer available in thecache. If the user then decides to reload the request, he or she willgenerally receive a dialog confirming that they wish to resend thePOST request. These features help the user avoid mistakenly sending arequest multiple times when the request would modify informationstored on the server.</p></div><a name="ch02-9-fm2xml" /><div class="sect3"><h3 class="sect3">2.3.1.3. HEAD</h3><p>You <a name="INDEX-311" /><a name="INDEX-312" /><a name="INDEX-313" />may havenoticed that we said that your web browser<em class="emphasis">generally</em> creates a GET request to fetchresources you have requested. If your browser has previouslyretrieved a resource, it may be stored within the browser'scache. In order for the browser to know whether to display the cachedcopy or whether to request a fresh copy, the browser can send a<em class="firstterm">HEAD</em> request. HEAD requests are formattedexactly like GET requests, and the server responds to it exactly likea GET request with one exception: it sends only the HTTP headers, itdoesn't send the content. The browser can then check themeta-information contained in the headers, such as the modificationdate of the resource, to see if it has changed and whether it shouldreplace the cached version with the newer version. HEAD requests donot have a content body either.</p><p>In practice, you can treat HEAD requests the same as GET requests inyour CGI scripts, and the web server will truncate the content ofyour responses and return only headers. For this reason, we willrarely discuss to the HEAD request method in this book. If you areconcerned about performance, you may wish to check the request methodyourself and conserve resources by not generating content for HEADrequests. We will see how your script can determine the requestmethod in the next chapter.</p></div><a name="ch02-10-fm2xml" /><div class="sect3"><h3 class="sect3">2.3.1.4. POST</h3><p><em class="firstterm">POST</em><a name="INDEX-314" /> <a name="INDEX-315" /><a name="INDEX-316" /> is usedwith HTML forms to submit information that alters data stored on theweb server. POST requests always include a body containing thesubmitted information formatted like a query string. POST requeststhus require additional headers specifying the length of the contentand its format. These headers are described in the following section.</p><p>Although POST requests should only be used to modify data on theserver, CGI developers commonly use POST requests for CGI scriptsthat simply return information, but do not modify data. This practiceis more common and less dangerous than the reversesituation -- using GET to modify data on the server. Developersuse POST for any number of reasons:</p><ul><li><p>Some developers believe that forms submitted via POST offer greatersecurity over those submitted via GET because a user cannot modifythe values within the URL in the browser as they can with GET. Thisreasoning is flawed. Knowledgeable users, as we will see in oursecurity discussion in <a href="ch08_01.htm">Chapter 8, "Security"</a>, can easily findways around this.</p></li><li><p>The responses to resources received via POST cannot be bookmarked orhyperlinked (at least without using a bookmarklet; see <a href="ch07_01.htm">Chapter 7, "JavaScript"</a>). Although this is generally inconvenient forthe user, sometimes this is the preferred behavior.</p></li></ul><p>Note that users may encounter browser warnings about expired pages ifthey attempt to revisit cached pages obtained <a name="INDEX-317" /> <a name="INDEX-318" /> <a name="INDEX-319" />via POST.</p></div></div><a name="ch02-11-fm2xml" /><div class="sect2"><h3 class="sect2">2.3.2. Request Header Field Lines</h3><p>The client generally sends several <a name="INDEX-320" /><a name="INDEX-321" /><a name="INDEX-322" />header fields with its request. Asmentioned earlier, these consist of a field name, a colon, somecombination of spaces or tabs (although one space is most common),and a value (see <a href="ch02_03.htm#ch02-60820">Figure 2-6</a>). These fields are usedto pass additional information about the request or about the client,or to add conditions to the request. We'll discuss the commonbrowser headers here; they are listed in <a href="ch02_03.htm#ch02-38477">Table 2-2</a>. Those connected with content negotiation andcaching are discussed later in this chapter.</p><a name="ch02-60820" /><div class="figure"><img width="177" src="figs/cgi2.0206.gif" height="45" alt="Figure 2-6" /></div><h4 class="objtitle">Figure 2-6. A header field line</h4><a name="ch02-38477" /><h4 class="objtitle">Table 2-2. Common HTTP Request Headers</h4><table border="1"><tr><th><p>Header</p></th><th><p>Description</p></th></tr><tr><td><p>Host</p></td><td><p>Specifies the target hostname</p></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -