📄 ch05.htm
字号:
<TD ALIGN="LEFT">"</TD>
<TD ALIGN="LEFT">%22</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">#</TD>
<TD ALIGN="LEFT">%23</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">%</TD>
<TD ALIGN="LEFT">%25</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">&</TD>
<TD ALIGN="LEFT">%26</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">(</TD>
<TD ALIGN="LEFT">%28</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">)</TD>
<TD ALIGN="LEFT">%29</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">,</TD>
<TD ALIGN="LEFT">%2C</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">.</TD>
<TD ALIGN="LEFT">%2E</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">/</TD>
<TD ALIGN="LEFT">%2F</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">:</TD>
<TD ALIGN="LEFT">%3A</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">;</TD>
<TD ALIGN="LEFT">%3B</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><</TD>
<TD ALIGN="LEFT">%3C</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">=</TD>
<TD ALIGN="LEFT">%3D</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">></TD>
<TD ALIGN="LEFT">%3E</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">?</TD>
<TD ALIGN="LEFT">%3F</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">@</TD>
<TD ALIGN="LEFT">%40</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">[</TD>
<TD ALIGN="LEFT">%5B</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">\</TD>
<TD ALIGN="LEFT">%5C</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">]</TD>
<TD ALIGN="LEFT">%5D</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">^</TD>
<TD ALIGN="LEFT">%5E</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">'</TD>
<TD ALIGN="LEFT">%60</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">{</TD>
<TD ALIGN="LEFT">%7B</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">|</TD>
<TD ALIGN="LEFT">%7C</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">}</TD>
<TD ALIGN="LEFT">%7D</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">~</TD>
<TD ALIGN="LEFT">%7E</TD>
</TR>
</TABLE>
So, if someone typed the following into a text field form:</P>
<PRE><FONT COLOR="#0066FF">My name is Chris, (ckemp@ro.com); or "http://ro.com/~ckemp".
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
ÂDo you like my home page?
</FONT></PRE>
<P>Netscape (or whatever) would encode it and send it to your CGI as:</P>
<PRE><FONT COLOR="#0066FF">My+name+is+Chris%2C+%28ckemp%40ro%2Ecom%29%3B+or+%22http%3A%2F%2Fro%2Ecom%2F%7Eckemp%22%2E
Â+Do+you+like+my+home+page%3F
</FONT></PRE>
<P>The special characters are simply replaced with their HEX equivalents. <B><TT>Headers</TT></B>
In a FullRequest, the browser may also send a series of headers in RFC-822 format.
The most common headers are <TT>Accept</TT>, which tells the server which MIME object
types the browser can handle, and <TT>User-Agent</TT>, which gives the type and version
of the browser. These headers are placed into the environment of your script by the
Web server when your script is run, then some additional variables are added to your
environment by the Web server. Thus, the variables in Table 5.3 are available to
your script.
<H4>Table 5.3. Headers.</H4>
<P>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>HTTP_ACCEPT</TT></TD>
<TD ALIGN="LEFT">All MIME types that the client will accept, as given by HTTP headers. Each item in
this list is separated by commas.Format: type/subtype, type/subtype</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>HTTP_USER_AGENT</TT></TD>
<TD ALIGN="LEFT">The browser the client is using to send the request. General Format: software/version
library/version</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>SERVER_SOFTWARE</TT></TD>
<TD ALIGN="LEFT">The name and version of the information server software answering the request (and
running the gateway). Format: name/version</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>SERVER_NAME</TT></TD>
<TD ALIGN="LEFT">The server's hostname, DNS alias, or IP address as it would appear in self-referencing
URLs.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>GATEWAY_INTERFACE</TT></TD>
<TD ALIGN="LEFT">The revision of the CGI specification to which this server complies. Format: CGI/revision</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>SERVER_PROTOCOL</TT></TD>
<TD ALIGN="LEFT">The name and revision of the information protocol this request came in with.Format:
protocol/revision</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>SERVER_PORT</TT></TD>
<TD ALIGN="LEFT">The port number to which the request was sent.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>REQUEST_METHOD</TT></TD>
<TD ALIGN="LEFT">The method with which the request was made. For HTTP, this is <TT>GET</TT>, <TT>HEAD</TT>,
<TT>POST</TT>, and so on.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>PATH_INFO</TT></TD>
<TD ALIGN="LEFT">The extra path information, as given by the client. In other words, scripts can be
accessed by their virtual pathname, followed by extra information at the end of this
path. The extra information is sent as <TT>PATH_INFO</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>PATH_TRANSLATED</TT></TD>
<TD ALIGN="LEFT">The server provides a translated version of <TT>PATH_INFO</TT>, which takes the path
and does any virtual-to-physical mapping to it.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>SCRIPT_NAME</TT></TD>
<TD ALIGN="LEFT">A virtual path to the script being executed, used for self-referencing URLs.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>QUERY_STRING</TT></TD>
<TD ALIGN="LEFT">The information that follows the <TT>?</TT> in the URI that referenced this script.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>REMOTE_HOST</TT></TD>
<TD ALIGN="LEFT">The hostname making the request.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>REMOTE_ADDR</TT></TD>
<TD ALIGN="LEFT">The IP address of the remote host making the request.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>AUTH_TYPE</TT></TD>
<TD ALIGN="LEFT">If the server supports user authentication and the script is protected, this is the
protocol-specific authentication method used to validate the user.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>REMOTE_USER</TT></TD>
<TD ALIGN="LEFT">If the server supports user authentication and the script is protected, this is the
username they have authenticated as.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>REMOTE_IDENT</TT></TD>
<TD ALIGN="LEFT">If the HTTP server supports RFC 931 identification, then this variable will be set
to the remote username retrieved from the server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>CONTENT_TYPE</TT></TD>
<TD ALIGN="LEFT">For queries that have attached information, such as HTTP <TT>POST</TT> and <TT>PUT</TT>,
this is the content-type of the data.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>CONTENT_LENGTH</TT></TD>
<TD ALIGN="LEFT">The length of the said content as given by the client.</TD>
</TR>
</TABLE>
Later, in Example 1, I'll show you how all of these headers are accessible as environment
variables by using CGI.pm and the HTTP::Headers class from within your script. So,
putting it all together, here's an example of a typical FullRequest HTTP request:</P>
<PRE><FONT COLOR="#0066FF">POST /cgi-bin/Echo2 HTTP/1.0
Accept: */*; q=0.300
Accept: application/octet-stream; q=0.100
Accept: text/plain
Accept: text/html
From: somebody@somewhere.com
User-Agent: Mozilla/5.0Platinum
Content-length: 588
Content-type: application/x-www-form-urlencoded
</FONT></PRE>
<P>Now that I've discussed the request, it's time to look at the response from the
Web server.
<CENTER>
<H4><A NAME="Heading10"></A><FONT COLOR="#000077">What Happens after the Request</FONT></H4>
</CENTER>
<P>After the HTTP request has occurred and the target of the request is your CGI
script, a few things occur. Unless your script is being run on an ISP's server using
a "CGI-wrap" type program (discussed in Chapter 3, "Security on the
Web"), the Web server executes your script. Every process (or running program)
in a UNIX environment is executed by (or owned by) the user who executes it. This
makes it easier to keep track of which users are allocating which resources and running
which programs. However, in the case of a CGI script, the user causing the script
to be run is unknown--some Web browser somewhere. The Web server software executes
your script as some non-privileged user like <TT>nobody</TT> or <TT>webuser</TT>.
(You can actually use any name you want; the name can be configured in your Web server's
config file.) For security reasons, this <TT>nobody</TT> user running your script
typically has almost no privileges on the system.</P>
<P>So, now you have a program running on the server as <TT>nobody</TT> or <TT>webuser</TT>
or something similar. It is important to know the name of the user that your script
is running as, especially if your program must read or write to any files. Remember
that in a UNIX environment, file permissions are very important. If the Web server
is running your CGI as <TT>nobody</TT> and it needs to write to a file owned by <TT>betty</TT>,
and this file is <TT>chmod</TT>'d to 744 (-rwxr- -r- -) so only the owner can write
to it, your CGI will fail because it can't write to the file.</P>
<P>After your program is run, all the variables discussed in the previous section
are stored in its environment and available to it. Using these variables in your
CGI is quite simple by using the Perl WWW libraries.</P>
<P>The following output is the actual environment that was generated by a Netscape
Communications Server after a request was sent from Netscape Navigator:</P>
<PRE><FONT COLOR="#0066FF">SERVER_SOFTWARE : Netscape-Communications/1.12
SERVER_NAME : somewhere.corp.sgi.com
GATEWAY_INTERFACE : CGI/1.1 <br>
SERVER_PROTOCOL : HTTP/1.0
SERVER_PORT : 80
REQUEST_METHOD : GET
PATH_INFO : /path/foo
PATH_TRANSLATED : /var/www/htdocs/path/foo
SCRIPT_NAME : /html_tutorial/cgi-bin/env.cgi
QUERY_STRING : query_string
REMOTE_HOST : amiga.huntsville.sgi.com
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -