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

📄 ch19_10.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.9. Debugging the Raw HTTP Exchange (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:42Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"><LINKREL="next"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.8. Redirecting to a Different Location"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.10. Managing Cookies"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-41372">19.9. Debugging the Raw HTTP Exchange</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-954">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005482-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005482-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005482-2"></A></A></H3><PCLASS="para">Your CGI script is misbehaving strangely with your browser, and you suspect something in the HTTP header is missing. You want to find out exactly what your browser is sending to the server in the HTTP header.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-960">Solution</A></H3><PCLASS="para">Create your own fake web server, and point your browser at it, as shown in <ACLASS="xref"HREF="ch19_10.htm#ch19-25651"TITLE="dummyhttpd">Example 19.6</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-25651">Example 19.6: dummyhttpd</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch19-idx-1000005483-0"></A>dummyhttpd - start an HTTP daemon and print what the client sendsuse strict;use HTTP::Daemon;  # need LWP-5.32 or bettermy $server = HTTP::Daemon-&gt;new(Timeout =&gt; 60, LocalPort =&gt; 8989);print &quot;Please contact me at: &lt;URL:&quot;, $server-&gt;url, &quot;&gt;\n&quot;;while (my $client = $server-&gt;accept) {  CONNECTION:    while (my $answer = $client-&gt;get_request) {        print $answer-&gt;as_string;        $client-&gt;autoflush;      RESPONSE:        while (&lt;STDIN&gt;) {            last RESPONSE   if $_ eq &quot;.\n&quot;;            last CONNECTION if $_ eq &quot;..\n&quot;;            print $client $_;        }        print &quot;\nEOF\n&quot;;    }    print &quot;CLOSE: &quot;, $client-&gt;reason, &quot;\n&quot;;    $client-&gt;close;    undef $client;}</PRE></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1018">Discussion</A></H3><PCLASS="para">It's hard to keep track of which versions of all the different browsers still have which bugs. The fake server program can save you days of head scratching, because sometimes a misbehaving browser doesn't send the server the right thing. Historically, we have seen aberrant browsers lose their cookies, mis-escape a URL, send the wrong status line, and do other even less obvious things.</P><PCLASS="para">To use the fake server, it's best to run it on the same machine as the real server. That way your browser will still send it any cookies destined for that domain. Then instead of pointing your browser at:</P><PRECLASS="programlisting">http://somewhere.com/cgi-bin/whatever</PRE><PCLASS="para">use the alternate port given in the <CODECLASS="literal">new</CODE> constructor above. You don't need to be the superuser to run the server if you use the alternate port.</P><PRECLASS="programlisting">http://somewhere.com:8989/cgi-bin/whatever</PRE><PCLASS="para">If you convince yourself that the client is behaving properly but wonder about the server, it's easiest to use the <EMCLASS="emphasis">telnet</EM> program to manually talk to the remote server.</P><PRECLASS="programlisting">% telnet www.perl.com 80<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>GET /bogotic HTTP/1.0</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;blank line here&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>HTTP/1.1 404 File Not Found</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Date: Tue, 21 Apr 1998 11:25:43 GMT</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Server: Apache/1.2.4</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Connection: close</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Content-Type: text/html</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;HTML&gt;&lt;HEAD&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;TITLE&gt;404 File Not Found&lt;/TITLE&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/HEAD&gt;&lt;BODY&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;H1&gt;File Not Found&lt;/H1&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The requested URL /bogotic was not found on this server.&lt;P&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/BODY&gt;&lt;/HTML&gt;</I></CODE></B></CODE></PRE><PCLASS="para">If you have LWP installed on your system, you can use the <ICLASS="function">GET</I> alias for the <EMCLASS="emphasis">lwp-request</EM> program. This will follow any redirection chains, which can shed light on your problem. For example:</P><PRECLASS="programlisting">% GET -esuSU http://mox.perl.com/perl/bogotic<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>GET http://language.perl.com/bogotic</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Host: mox.perl.com</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>User-Agent: lwp-request/1.32</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>GET http://mox.perl.com/perl/bogotic --&gt; 302 Moved Temporarily</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>GET http://www.perl.com/perl/bogotic --&gt; 302 Moved Temporarily</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>GET http://language.perl.com/bogotic --&gt; 404 File Not Found</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Connection: close</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Date: Tue, 21 Apr 1998 11:29:03 GMT</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Server: Apache/1.2.4</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Content-Type: text/html</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Client-Date: Tue, 21 Apr 1998 12:29:01 GMT</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Client-Peer: 208.201.239.47:80</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Title: Broken perl.com Links</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;HTML&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;HEAD&gt;&lt;TITLE&gt;An Error Occurred&lt;/TITLE&gt;&lt;/HEAD&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;BODY&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;H1&gt;An Error Occurred&lt;/h1&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>404 File Not Found</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/BODY&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/HTML&gt;</I></CODE></B></CODE><ACLASS="indexterm"NAME="ch19-idx-1000005485-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005485-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005485-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1112">See Also</A></H3><PCLASS="para">The documentation for the standard CGI module; <ACLASS="xref"HREF="ch19_11.htm"TITLE="Managing Cookies">Recipe 19.10</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.8. Redirecting to a Different Location"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.10. Managing Cookies"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.8. Redirecting to a Different Location</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.10. Managing Cookies</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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