?? ch02_06.htm
字號:
<?label 2.6. Content Negotiation?><html><head><title>Content Negotiation (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_05.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_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">2.6. Content Negotiation</h2><p>People from <a name="INDEX-457" /> <a name="INDEX-458" /> <a name="INDEX-459" /> <a name="INDEX-460" />all over the world access the sameInternet, using many different languages, many different charactersets, and many different browsers. One representation of a documentis not going to satisfy the requirements of all these people. This iswhy HTTP provides something called <em class="firstterm">contentnegotiation</em>, which allows clients and servers tonegotiate the best possible format for each given resource.</p><p>For example, say you want to <a name="INDEX-461" />make a document available in multiplelanguages. You could store each translation of this documentseparately so that they each have a unique URL. This would be a badidea for a number of reasons, but most importantly because you wouldhave to advertise multiple URLs for the same resource. URLs have beendesigned to be easily exchanged offline as well as via hyperlinks,and there is no reason why people who speak different languagesshould not be able to share the same URL. By utilizing contentnegotiation, you can offer the appropriate translation of a requesteddocument automatically.</p><p>There are four primary forms of content negotiation: language,<a name="INDEX-462" />character set,<a name="INDEX-463" /><a name="INDEX-464" />media type, and encoding. Each have theirown corresponding headers, but the negotiation process works the sameway for all of them. Negotiation can be performed by the<a name="INDEX-465" /><a name="INDEX-466" />server or by the client. Inserver-side negotiation, the client sends a header indicating theforms of content it accepts, and the server responds by selecting oneof these options and returning the resource in the appropriateformat. In client-side negotiation, the client requests a resourcewithout special headers, the server sends a list of the availablecontents to the client, the client then makes an additional requestto specify the format of the resource desired, and the server thenreturns the resource in that format. Clearly there is more overheadin client-side negotiation (although caching helps), but the clientis generally better than the server at choosing the most appropriateformat.</p><a name="ch02-32-fm2xml" /><div class="sect2"><h3 class="sect2">2.6.1. Media Type</h3><p><a name="INDEX-467" /> <a name="INDEX-468" />Clients mayinclude a<a name="INDEX-469" /><a name="INDEX-470" />header with theirHTTP request indicating a list of preferred formats. The header formedia type looks like this:</p><blockquote><pre class="code">Accept: text/html;q=1, text/plain;q=0.8, image/jpeg, image/gif, */*;q=0.001</pre></blockquote><p>The <em class="emphasis">Accept</em><a name="INDEX-471" /> header list contains HTTP media types inthe <em class="emphasis">type/subtype</em> format used by the<a name="INDEX-472" /><em class="emphasis">Content-Type</em> header,followed by optional <a name="INDEX-473" /> <a name="INDEX-474" />qualityfactors (asterisks serve as wildcards). Quality factors arefloating-point numbers betweenand 1 that indicate a preference for a particular type; the defaultis 1. Servers are expected to examine the <em class="emphasis">Accept</em>media types and return data that is preferred by the browser. Whenmultiple values have the same quality factor, the more specific one(i.e., where the quality factor is specified or the media type is nota wildcard) has higher priority.</p><p>In the previous example, documents would be returned with thefollowing priority:</p><ol><li><p><em class="emphasis">text/html</em><a name="INDEX-475" /></p></li><li><p><em class="emphasis">image/jpeg</em><a name="INDEX-476" /> or <em class="emphasis">image/gif</em></p></li><li><p><em class="emphasis">text/plain</em><a name="INDEX-477" /></p></li><li><p><em class="emphasis">*/*</em><a name="INDEX-478" /> (anything else)</p></li></ol><p>In reality, media type negotiation is not often used because it isunwieldy for a browser to list the media types of all documents itsupports each time it makes a request. The majority of browsers todayspecify only new or less common <a name="INDEX-479" /><a name="INDEX-480" />image formats in addition to<em class="emphasis">*/*</em>. Examples of the newer formats are<em class="emphasis">image/p-jpeg</em> (progressive JPEG) or<em class="emphasis">image/png.</em> (PNG was created as an openalternative to GIF, which has patent issues; see <a href="ch13_01.htm">Chapter 13, "Creating Graphics on the Fly"</a>). Web servers generally do not support mediatype negotiation for static documents, but we will look at a CGIscript that does this in the next chapter.</p></div><a name="ch02-33-fm2xml" /><div class="sect2"><h3 class="sect2">2.6.2. Internationalization</h3><p>Although media type negotiation is becoming outdated, other forms of<a name="INDEX-481" /><a name="INDEX-482" />contentnegotiation are gaining much more importance. Internationalizationhas become a new arena where content negotiation plays an importantrole. Providing a document to members of other countries can mean twothings: supporting other translations and possibly supporting other<a name="INDEX-483" />character sets. The Romanalphabet, the Cyrillic alphabet, and Kanji, for example, usedifferent character sets. HTTP supports these forms of negotiationwith the <a name="INDEX-484" /><em class="emphasis">Accept-Language</em> and<em class="emphasis">Accept-Charset</em> headers. Examples of theseheaders are:</p><blockquote><pre class="code">Accept-Charset: iso-8859-5, iso-8859-1;q=0.5Accept-Language: ru, en-gb;q=0.5, en;q=0.4</pre></blockquote><p>The first line indicates that the server should return the content inCyrillic if possible or Western Roman otherwise. The languagespecifies Russian as the first choice, with British English as thesecond, and other forms of English as the third. Note that a singleasterisk can be used in place of any of these values to represent awildcard match. The default character set, unless specified, isUS-ASCII or ISO-8859-1 (US-ASCII is a subset of ISO-8859-1).</p><p>Most web servers support language negotiation automatically for<a name="INDEX-485" /><a name="INDEX-486" />static documents. For example, ifyou perform a new installation of Apache, it will install multiplecopies of the "It Worked!" welcome file in<em class="filename">/usr/local/apache/htdocs</em>. The files all sharethe <em class="filename">index.html</em> base name but have differentextensions indicating the <a name="INDEX-487" /><a name="INDEX-488" />language code:<em class="filename">index.html.en</em>,<em class="filename">index.html.fr</em>,<em class="filename">index.html.de</em>, etc. If you point your browser at<em class="filename">index.html</em>, change the preferred language inyour browser, and then reload the page, you should see it in anotherlanguage.</p></div><a name="ch02-34-fm2xml" /><div class="sect2"><h3 class="sect2">2.6.3. Encoding</h3><p>The final form of <a name="INDEX-489" /><a name="INDEX-490" />content negotiation supportsencoding. Options for encoding include<em class="firstterm">gzip</em><a name="INDEX-491" /><a name="INDEX-492" /><a name="INDEX-493" />,<em class="firstterm">compress</em>, and <em class="firstterm">identity</em>(no encoding). Here is an example header specifying that the browsersupports <em class="emphasis">compress</em> and <em class="emphasis">gzip</em>:</p><blockquote><pre class="code">Accept-Encoding: compress, gzip</pre></blockquote><p>A server may be able to speed up the download of a large document tothis client by sending an encoded version of the document. Thebrowser should decode the document automatically for the <a name="INDEX-494" /> <a name="INDEX-495" /> <a name="INDEX-496" /> <a name="INDEX-497" />user.</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_05.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_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">2.5. Proxies</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.7. Summary</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 © 2001</a> O'Reilly & 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 + -