📄 ch05.htm
字号:
REMOTE_ADDR : 123.255.123.1
AUTH_TYPE :
REMOTE_USER :
REMOTE_IDENT :
CONTENT_TYPE :
CONTENT_LENGTH :
HTTP_ACCEPT : image/gif, image/jpeg, */*
HTTP_USER_AGENT : Mozilla/2.0S (X11; I; IRIX 6.2 IP22)
HTTP_REFERER : http://server.sgi.com/html/cgi.html
ANNOTATION_SERVER :
</FONT></PRE>
<CENTER>
<H4><A NAME="Heading11"></A><FONT COLOR="#000077">The Response</FONT></H4>
</CENTER>
<P>Now that your CGI script is running, a response must immediately be sent back
to the browser. The response follows a specific format, which will be discussed later,
called an HTTP Response Header. <B><TT>HTTP Response Headers</TT></B>
<DL>
<DT></DT>
</DL>
<CENTER>
<H3>
<HR WIDTH="83%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>An HTTP Response Header is sent by the server in response to any HTTP request,
not just requests for CGI scripts. If the client requests an HTML page, a response
header is generated by the server and is sent to the browser before the HTML page.
HTTP response headers are not typically generated for requests for GIF and JPEG images.<BR>
<HR>
</BLOCKQUOTE>
<P>Here is an example of an HTTP response header:</P>
<PRE><FONT COLOR="#0066FF">HTTP/1.0 200 OK
Server: Netscape-Communications/1.12
Date: Monday, 21-Oct-96 01:42:01 GMT
Last-modified: Thursday, 10-Oct-96 17:17:20 GMT
Content-length: 1048
Content-type: text/html
CrLf
</FONT></PRE>
<P>The contents (or body) of the message would follow after the Content-type and
a Carriage-<BR>
Return Line-Feed (<TT>CrLf</TT>) sequence. <B><TT>Parsed Header Output</TT></B> There
are two ways the response can be handled. By default, the Web server reads and parses
the output (STDOUT) of your CGI script before sending the output on to the browser.
The Web server will then automatically generate all necessary HTTP response headers.
This is called parsed header output. The output of your CGI script is actually appended
to the response headers generated by the Web server. If your script contains no output
(HTML, or text, or whatever), it is expected to output either a <TT>Location</TT>
or <TT>Status</TT> header (discussed below in the "Status Response Header"
and "Location Response Header" sections). Thus, in summary, the Web server
sends to the client in this order: a response header, a blank line (CrLf), then any
output from your CGI script. This presents some problems:
<UL>
<LI>All output from your CGI script must be processed by the Web server, which must
then generate response headers and append the output of your script to them. This
is very inefficient and significantly slows down your program.
</UL>
<UL>
<LI>The Web server is slowed down because of the additional processing that must
be done prior to sending the output to the client. The Web server is an unnecessary
middleman.
</UL>
<UL>
<LI>The Web server still parses and processes the output of your script, even if
you want your script to pass headers directly to the client.
</UL>
<P><B><TT>Non-Parsed Header Output</TT></B> Fortunately, there is an easy way around
this problem. By simply beginning the name of your scripts with <TT>nph-</TT>, the
Web server will not add any headers or interfere in any way with the output of your
script. So, if you were working on a script called mycgi.pl, just call it nph-mycgi.pl,
and the Web server won't interfere with its headers. This is called a non-parsed-header
script, or NPH script. The output of a NPH script is not parsed and is sent directly
to the client. This is highly recommended. On most popular Macintosh based Web servers,
all CGI scripts are treated as NPH scripts. Figure 5.2 depicts an HTTP transaction
with a NPH CGI script. <TT><BR>
<BR>
</TT><A HREF="06wpp02.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/06wpp02.jpg"><TT><B>Figure 5.2.</B></TT></A> The HTTP transaction
with a NPH CGI.
<DL>
<DT></DT>
</DL>
<CENTER>
<H3>
<HR WIDTH="83%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>This feature must be enabled explicitly in the config files for some servers.<BR>
<HR>
</BLOCKQUOTE>
<P>The CGI must respond with a valid and appropriate response header that tells the
browser what to expect or do next. If you use NPH scripts, you must be sure to output
the proper headers; otherwise, the client will see a <TT>500</TT> error message.</P>
<P>There are three basic Response headers: <TT>Status</TT>, <TT>Content-Type</TT>,
and <TT>Location</TT>. All responses must contain either a <TT>Status</TT> header
or a <TT>Location</TT> header that redirects the browser to another URI. If the script
will be outputting HTML, text, or some other MIME compliant data object, the <TT><BR>
Status</TT> header must be followed by a <TT>Content-Type</TT> header that specifies
the MIME type of the object that follows. Other RFC-822 compliant headers such as
<TT>Date</TT> and <TT>Server</TT> can also be <BR>
specified in the response header. <B><TT>Status Response Header</TT></B> The <TT>Status</TT>
response header (or status line) indicates which version of HTTP the server is running,
together with a result code and an optional message. It should not be sent if the
script returns a <TT>Location</TT> header.</P>
<P>The first line of the response from the server typically looks something like
this:</P>
<PRE><FONT COLOR="#0066FF">HTTP/1.0 200 OK
</FONT></PRE>
<P>where <TT>HTTP/1.0</TT> is the version of HTTP the server is running, <TT>200</TT>
is the result code, and <TT>OK</TT> is the message that describes the result code.
There are many other result codes besides <TT>200</TT> that can be sent. The following
<TT>Status</TT> response headers can be sent to the browser by your CGI in NPH mode.
<B><TT>Success 2xx</TT></B> Result codes in the <TT>200</TT>s indicate success. The
body section if present is the object returned by the request. The body must be in
MIME format and may be only in text/plain, text/html, or one of the formats specified
as acceptable in the request in the <TT>HTTP/ACCEPT</TT> header.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="31" ALIGN="LEFT" VALIGN="TOP"><TT>200</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OK</TT></TD>
<TD ALIGN="LEFT">The request was successful.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="31" ALIGN="LEFT" VALIGN="TOP"><TT>201</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>CREATED</TT></TD>
<TD ALIGN="LEFT">Following a <TT>POST</TT> command, indicates success, but the textual part of the
response indicates the URI by which the newly created document should be known.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="31" ALIGN="LEFT" VALIGN="TOP"><TT>202</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Accepted</TT></TD>
<TD ALIGN="LEFT">The request has been accepted for processing, but the processing has not been completed.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="31" ALIGN="LEFT" VALIGN="TOP"><TT>203</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Partial Information</TT></TD>
<TD ALIGN="LEFT">When received in the response to a <TT>GET</TT> command, this indicates that the
returned meta-information is not a definitive set of the object from a server with
a copy of the object but is from a private overlaid Web. This may include annotation
information about the object, for example.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="31" ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">204</FONT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">No Response</FONT></TD>
<TD ALIGN="LEFT"><FONT COLOR="#000000">Server has received the request, but there is no information
to send back, and the client should stay in the same document view. This is mainly
to allow input for scripts without changing the document at the same time.</FONT></TD>
</TR>
</TABLE>
</P>
<P><B><TT>Redirection 3xx</TT></B> Result codes in the <TT>300</TT>s indicate the
action to be taken (normally automatically) by the client in order to fulfill the
request.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>301</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Moved</TT></TD>
<TD ALIGN="LEFT">The data requested has been assigned a new URI; the change is permanent. The response
contains a header line of the form: <TT>URI: <url> CrLf</TT>, which specifies
an alternative address for the object in question.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>302</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Temporarily Moved</TT></TD>
<TD ALIGN="LEFT">The data requested actually resides under a different URL; however, this is not permanent.
The response format is the same as for <TT>Moved</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>303</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Method</TT></TD>
<TD ALIGN="LEFT">Like the found response, this suggests that the client go try another network address.
In this case, a different method may be used, too, rather than <TT>GET</TT>. Method:
<TT><method> <url>body-section</TT>. The <TT>body-section</TT> contains
the parameters to be used for the method. This allows a document to be a pointer
to a complex query operation.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">304</FONT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">Not Modified</FONT></TD>
<TD ALIGN="LEFT"><FONT COLOR="#000000">If the client performs a conditional GET request, but the document
has not been modified as specified in the If-Modified-Since variable, this response
is generated.</FONT></TD>
</TR>
</TABLE>
</P>
<PRE></PRE>
<P><B><TT>Client ERROR 4xx</TT></B> The 4xx codes are intended for cases in which
the client seems to have erred. The body section may contain a document describing
the error in human readable form. The document is in MIME format and may only be
in text/plain, text/html, or one of the formats specified as acceptable in the request.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>400</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Bad request</TT></TD>
<TD ALIGN="LEFT">The request had bad syntax or was impossible to be satisfied.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>401</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Unauthorized</TT></TD>
<TD ALIGN="LEFT">The parameter to this message gives a specification of authorization schemes that
are acceptable. The client should retry the request with a suitable Authorization
header.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>402</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Payment Required</TT></TD>
<TD ALIGN="LEFT">The parameter to this message gives a specification of charging schemes acceptable.
This code is not currently supported.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>403</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>Forbidden</TT></TD>
<TD ALIGN="LEFT">The request is for something forbidden. Authorization will not help.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">404</FONT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">Not found</FONT></TD>
<TD ALIGN="LEFT"><FONT COLOR="#000000">The server has not found anything matching the URI given.</FONT></TD>
</TR>
</TABLE>
</P>
<P><B><TT>Server ERROR 5xx</TT></B> The 5xx codes are for the cases in which the
server has erred, or all indications point to the server as being the cause of the
error. Like the 400 codes, the body section may contain a document describing the
error in human readable form.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>500</TT></TD>
<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Internal Error</TT></TD>
<TD ALIGN="LEFT">The server encountered an unexpected condition that prevented it from fulfilling
the request.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>501</TT></TD>
<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Not Implemented</TT></TD>
<TD ALIGN="LEFT">The server does not support the ability to satisfy this request.</TD>
</TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -