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

📄 ch3.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 4 页
字号:
looking for. <I>Something else</I>, well, there are a lot of things
that the server can do. It can start sending out a binary file,
it can send the wrong type of output, it can even send you to
another location entirely. In all cases, though, the output should
be controlled by the CGI program because if it's not, that's a
bigger problem.
<H3><A NAME="Headers">Headers</A></H3>
<P>
<I>Headers</I> are what CGI programs use to preface data and say
&quot;Hey, I'm sending you...&quot; so that the server knows what
to do with it. There are three primary types of headers that servers
return when used with CGI:
<UL>
<LI><FONT COLOR=#000000>Content-type</FONT>
<LI><FONT COLOR=#000000>Location</FONT>
<LI><FONT COLOR=#000000>Status</FONT>
</UL>
<P>
Each of these headers, regardless of type, is followed by a blank
line to indicate to the server that this is a header, not the
data, and that it's all done telling you about the header information.
<H4>Content-Type</H4>
<P>
You've had more Content-type headers directed at your browser
than you can easily count. Every time an HTML file or an image
comes in, they're preceded by a Content-type header that the server
automatically passes along with each document. The number of possible
types of data that can be passed back is pretty high because there
are lots of different types of files out there. Most of these
types are what's known as <I>MIME</I> <I>(Multipurpose Internet
Mail Extensions)</I> types. MIME types are just generic classifications
of documents and files that systems use to figure out what to
do with them. By default, your HTML browser knows how to deal
with the HTML type of content and normally images, as well.
<P>
The way these types of content are defined is through a combination
of types and subtypes. The following are the seven basic MIME
types for content:
<UL>
<LI><FONT COLOR=#000000>Text</FONT>
<LI><FONT COLOR=#000000>Multipart</FONT>
<LI><FONT COLOR=#000000>Message</FONT>
<LI><FONT COLOR=#000000>Application</FONT>
<LI><FONT COLOR=#000000>Image</FONT>
<LI><FONT COLOR=#000000>Audio</FONT>
<LI><FONT COLOR=#000000>Video</FONT>
</UL>
<P>
Within each one of these types there are different subtypes, just
like there are different brands and flavors of ice cream, even
though it's the same basic stuff. When a Content-type header gets
sent back, it specifies that it's a Content-type header, the MIME
type, and the MIME subtype, and hopes that the client on the other
end can figure out what the heck to do with it.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
MIME types and subtypes crop up all the time, especially with new client plug-ins and other software ideas that people are putting into motion. To be safe, use a standard MIME type and subtype wherever possible; otherwise, you're bound to get weird 
results.</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<P>
Some of the more common type/subtype combinations are
<UL>
<LI><FONT COLOR=#000000>text/html</FONT>
<LI><FONT COLOR=#000000>text/plain</FONT>
<LI><FONT COLOR=#000000>image/gif</FONT>
<LI><FONT COLOR=#000000>image/jpeg</FONT>
</UL>
<P>
Now, if a CGI program wants to send you back HTML, it's going
to have to tell your browser (and the server) that it's definitely
sending back something as text/html. The way it would do that
in a language like Perl would be as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print &quot;Content-type: text/html \n\n&quot;;
<BR>
print &quot;&lt;h1&gt;Hi there&lt;/h1&gt; \n&quot;;</FONT></TT>
</BLOCKQUOTE>
<P>
All that this does is send the <TT><FONT FACE="Courier">&quot;Content-type:
text/html&quot;</FONT></TT> definition, followed by a blank line
(<TT><FONT FACE="Courier">\n</FONT></TT> means &quot;start a new
line&quot; in Perl; two <TT><FONT FACE="Courier">\n</FONT></TT>
symbols are needed to get a new line followed by a blank line),
and then the HTML. Pretty easy, huh?
<H4>Location</H4>
<P>
If the CGI program doesn't really want to create a whole new pile
of HTML to send back to the user, it can do something else: point
them to a different location. That's right, a CGI program can
instruct your browser to go to a new location by specifying a
<I>Location header</I>. This is how random link programs work:
You start the CGI script, the random link program reads a bunch
of possible sites from a database,  picks one, and sends back
a Location header to your browser saying &quot;Go here.&quot;
<P>
Location headers are even easier to use than Content-type headers.
All you have to do is specify where to go. Listing 3.1 shows an
example in Perl.
<HR>
<BLOCKQUOTE>
<B>Listing 3.1. Returning a location header in Perl.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print &quot;Location: http://there.com/file.html
\n\n&quot;;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Again, the header has a blank line under it to show that it's
a header, is special, and should be dealt with first. So, if the
program doesn't have anything useful to say, it can just send
your browser off somewhere else and hope for the best.
<H4>Status</H4>
<P>
If something goes wrong with the CGI program, it has the option
to let you know. Wouldn't that be nice? A <I>Status header</I>
is just an easy way of saying, &quot;Okay, this happened, and
you know what to tell the user.&quot; What kinds of status codes
are there, and what do they mean? Table 3.5 takes a look at some
of the common ones.<BR>
<P>
<CENTER><B>Table 3.5. Common status codes.</B></CENTER>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><I>Code</I></TD><TD WIDTH=196><I>Result</I></TD>
<TD WIDTH=306><I>Description</I></TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">200</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">OK</FONT></TT></TD><TD WIDTH=306>Request worked just fine; no problems.
</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">202</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Accepted</FONT></TT></TD>
<TD WIDTH=306>The request is still being processed but was accepted.
</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">301</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Moved</FONT></TT></TD>
<TD WIDTH=306>The document has been moved to a new location.</TD>
</TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">302</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Found</FONT></TT></TD>
<TD WIDTH=306>The document isn't where it was specified to be, but it's been found somewhere else on the server.
</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">400</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Bad Request</FONT></TT>
</TD><TD WIDTH=306>The syntax of the HTTP request wasn't right.
</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">401</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Unauthorized</FONT></TT>
</TD><TD WIDTH=306>The document requires privileges to get.</TD>
</TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">403</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Forbidden</FONT></TT></TD>
<TD WIDTH=306>Server denied access to the document.</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">404</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Not Found</FONT></TT></TD>
<TD WIDTH=306>The server couldn't find any such document.</TD>
</TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">500</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Server Error</FONT></TT>
</TD><TD WIDTH=306>The server ran into big trouble.</TD></TR>
<TR><TD WIDTH=88><TT><FONT FACE="Courier">502</FONT></TT></TD>
<TD WIDTH=196><TT><FONT FACE="Courier">Service Overloaded</FONT></TT>
</TD><TD WIDTH=306>The server is too busy to help you.</TD></TR>
</TABLE></CENTER>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Some of these status codes occur when there's a problem in the CGI script. <A HREF="ch6.htm" >See Chapter 6</A>, &quot;Testing and Debugging,&quot; for more details on the possible causes if you're the one designing the CGI program.
</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="SomeThingstoConsider"><FONT SIZE=5 COLOR=#FF0000>Some
Things to Consider</FONT></A></H2>
<P>
With all the things you've seen about CGI programs, you may start
to wonder just how many uses you could possibly think of for them-probably
quite a lot. The best way to get an idea of what kinds of things
comprise a cool CGI application you can build, and how they work,
is to look around. When you find something that you don't quite
understand how it works, you can often talk to the people who
made it and see if they'll tell you how they did it. Plenty of
people make that kind of stuff available at no charge to other
people because it's how they learned in the first place-people
just helping out because they can.
<P>
One of the biggest<B> </B>things that keeps people from running
right out and programming their own CGI application is that they
think it'll be difficult. In some cases, they're right; but in
most cases, the amount of difficulty is exaggerated. Sure, the
first time out you won't want to try to create a topic relevance-based
search engine, or something else requiring a lot of programming
knowledge, but the point isn't to frustrate yourself when you're
just getting started. Take your time to become familiar with how
CGI works and what things you want to do with it, and many things
will start to come naturally.
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A>
</H2>
<P>
CGI programs are everywhere because they help people do things
they couldn't previously do with their Web server. Having seen
how servers can be taught to get help when they need it, you're
now ready for the higher mysteries of CGI. Remember, though, that
no matter how complex CGI may seem at times, it's just a matter
of establishing a conversation between the server (who's been
contacted by someone who needs something done) and the CGI program
(who's going to do something about it). There's always a real,
basic reason that something works the way it does and something
familiar you can relate it to. You just have to find the connection.
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch2.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch4.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

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