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

📄 ch16.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML>

<HEAD>
   <TITLE>Chapter 16 -- DOS CGI: The Basics</TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 16</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>DOS CGI: The Basics</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>


<UL>
<LI><A HREF="#TheBasics" >The Basics</A>
<UL>
<LI><A HREF="#CONTENT_FILE" >CONTENT_FILE</A>
<LI><A HREF="#OUTPUT_FILE" >OUTPUT_FILE</A>
<LI><A HREF="#OtherEnvironmentVariables" >Other Environment Variables</A>
</UL>
<LI><A HREF="#Languages" >Languages</A>
<UL>
<LI><A HREF="#BATandCMDFiles" >BAT and CMD Files</A>
<LI><A HREF="#BASICandItsCousins" >BASIC and Its Cousins</A>
<LI><A HREF="#PerlforDOS" >Perl for DOS</A>
<LI><A HREF="#cc" >C/C++</A>
</UL>
<LI><A HREF="#Limitations" >Limitations</A>
<LI><A HREF="#Resources" >Resources</A>
<UL>
<LI><A HREF="#ConvertingOtherCGIProgramsandInform" >Converting Other CGI Programs and Information</A>
<LI><A HREF="#WebServerSoftware" >Web Server Software</A>
<LI><A HREF="#DOSProgrammingBooks" >DOS Programming Books</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
DOS CGI has one advantage over other CGI implementations: it's
normally easier. You can perform some basic operations that take
no programming skills whatsoever and no time at all to create.
Say you have a form in which you want people to enter data, and
you want to save all the forms you get into one big file that
you can print out and review later. All you need to do is create
two lines in a text file and an HTML page that you can send back
to people after they submit the form. Using CGI is that easy,
and you'll see just how to use it in this chapter.
<P>
When you move from the basic functions to the more advanced, a
good quality about DOS CGI is that the methodology used isn't
much different from any other platform on which you can use CGI.
On the downside, DOS CGI is not without severe limitations in
performance or flexibility because it's still constrained by how
DOS deals with everything. Don't worry too much, though-programming
CGI for DOS isn't everyone's first choice, but if it's what you
need to work with, you don't need to suffer.
<P>
In covering DOS CGI's general use, you look at the following:
<UL>
<LI><FONT COLOR=#000000>The basics</FONT>
<LI><FONT COLOR=#000000>BAT files</FONT>
<LI><FONT COLOR=#000000>Other available languages</FONT>
<LI><FONT COLOR=#000000>Limitations of DOS CGI</FONT>
<LI><FONT COLOR=#000000>Resources</FONT>
</UL>
<P>
As I run through the examples  here, you should note the development
and server platform. All the DOS CGI examples shown in this chapter
are run on a generic Intel-based machine, running Windows 3.1
and using Robert Denny's WinHTTPD version 1.4c. If you're using
a different server package or running on a simulated version of
DOS on a different type of machine, you should be very careful.
Make sure that differences between what I use here and what you
have available won't cause a conflict. The best place to check
is at the command line (for basic functions), or you can check
the server documentation.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Caution</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Not all servers support the DOS CGI interface. It was originally designed as a makeshift solution for servers that, due to the platform they were running on, could not perform the more advanced functions. Check your server documentation to ensure that DOS 
CGI is supported before you go ahead and try the examples shown.</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<H2><A NAME="TheBasics"><FONT SIZE=5 COLOR=#FF0000>The Basics</FONT></A>
</H2>
<P>
DOS CGI differs from conventional CGI in that DOS doesn't let
the Web server store information into a large Standard Input (STDIN)
buffer, like most other environments. This restriction creates
a little problem. You can send information into the program through
the <TT><FONT FACE="Courier">Get</FONT></TT> method and make all
the information part of the <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>
environment variable, but doing so limits how much information
you can play with at any given time.
<P>
What DOS CGI does instead is make use of two special environment
variables: <TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT> and
<TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>. They allow CGI
to create its own special buffers for information; but instead
of holding the information in memory, these variables write it
directly to a file.
<H3><A NAME="CONTENT_FILE"><TT><FONT SIZE=4 FACE="Courier">CONTENT_FILE</FONT></TT></A>
</H3>
<P>
Every piece of incoming data that's not an environment variable
gets tossed into a temporary file, located somewhere on your machine.
The <TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT> environment
variable is the answer to the question &quot;Now where is it?&quot;
because the system could have given the file any name it feels
like. You don't have to worry about how the system generates the
name for the file or where it decides to toss it-whatever the
result, <TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT> tells
you.
<P>
The Content File contains nothing more than the data from the
<TT><FONT FACE="Courier">POST</FONT></TT> operation, URL-encoded
as one long string. For instance, the form Generic.htm, containing
First Name, Last Name, and E-mail fields, produces a Content File
that contains <TT><FONT FACE="Courier">first=Bill&amp;last=Schongar&amp;email=bills@aimtech.com</FONT></TT>
when run using a BAT file that does nothing more than print the
Content File back to the screen. Listing 16.1 shows the source
code for Generic.htm, and the resulting form is shown in Figure
16.1.
<P>
<A HREF="f16-1.gif" ><B>Figure 16.1 : </B><I>Data entered in the generic form.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 16.1. HTML source for a generic three-field form.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">&lt;title&gt;Generic Forms Test - DOS
CGI&lt;/title&gt;<BR>
<BR>
&lt;h1&gt;Generic Form&lt;/h1&gt;<BR>
<BR>
&lt;form method=post action=/cgi-dos/generic.bat&gt;<BR>
First Name: &lt;input name=first&gt; &lt;br&gt;<BR>
Last Name: &lt;input name=last&gt; &lt;br&gt;<BR>
Email: &lt;input name=email&gt; &lt;br&gt;<BR>
&lt;input type=submit value=&quot;Go&quot;&gt;<BR>
&lt;/form&gt;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
As you can see, all the information entered in the form has been
neatly bundled up, just as you would expect with any other CGI
implementation. Later in this chapter, you look at just what you
can do with this information, because DOS itself often seems to
lack built-in tools to handle it. Depending on what tools you
use, you can do a great deal before you send anything back to
the person waiting for it.
<H3><A NAME="OUTPUT_FILE"><TT><FONT SIZE=4 FACE="Courier">OUTPUT_FILE</FONT></TT></A>
</H3>
<P>
When you're ready to send something back to the user, you can
use the <TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT> environment
variable. Just like Standard Output on other systems, the Output
File is the place where the server expects to read data that comes
back from the application. In the background, the server is twiddling
its thumbs and occasionally checking the location it specifies
in the <TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT> environment
variable. When something appears there, the server starts reading
it in, line by line, and processing the data it contains.
<P>
Note that the server doesn't care how much you add to the file;
it just cares when you stop. If you've got processes going on
in the background that take awhile, you may want to think about
sending information to the client just to get the user started.
If you wait too long before sending anything back, the server
and the client may think that the connection's been abandoned
and therefore terminate the process with an error. You don't want
that to happen.
<P>
Information you send to the Output File follows a basic format.
You start with a header, to let the server and browser know what's
coming, and then follow it up with your information. You can send
back any type of header you want. The most common header to send
back is either a Content-type header or a Location header for
a file.
<H3><A NAME="OtherEnvironmentVariables">Other Environment Variables</A>
</H3>
<P>
You don't have to be content with just what's available in the
Content File. DOS CGI has a whole other range of environment variables
available at its beck and call, including <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>
and <TT><FONT FACE="Courier">REQUEST_METHOD</FONT></TT>.
<H2><A NAME="Languages"><FONT SIZE=5 COLOR=#FF0000>Languages</FONT></A>
</H2>
<P>
DOS CGI programs can be created in a number of different programming
languages. From the built-in system command languages to interpreted
and compiled languages, a number of tools exist to help you get
the job done. We'll look at the languages you're more likely to
encounter, and see how and why they can be used.
<H3><A NAME="BATandCMDFiles">BAT and CMD Files</A></H3>
<P>
Using batch (.BAT) and command (.CMD) files is the most basic
way to implement DOS CGI programs. They're built into the DOS
environment, and you can use them to access a variety of basic
built-in functions and to call more advanced external functions,
like another program. Their primary benefit is also their primary
drawback-they're simple and you can't do much inside them. I'm
not saying that they can't get the job done; it's all just a question
of how many hoops you're willing to jump through to make them
all work.
<H4>Commands and Syntax</H4>
<P>
To do anything with a BAT or CMD file, you need to know a little
bit about what makes them tick. As I've said all along, there's
not much to them, and you need to know just few basic commands
and concepts to get going.
<H5>echo</H5>
<BLOCKQUOTE>
Try this experiment. Go to a DOS prompt and type the following
on the command line:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">echo Hi, how are ya?</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
Then press Enter. What happens? You get back all the text that
follows the <TT><FONT FACE="Courier">echo</FONT></TT> command.
It simply repeats what you tell it to by printing the text right
back to the screen. You use this simple DOS command for printing
text, like <TT><FONT FACE="Courier">print</FONT></TT> in Perl
or <TT><FONT FACE="Courier">printf</FONT></TT> in C; it's the
basis for almost all output of data in a BAT or CMD file.
</BLOCKQUOTE>
<BLOCKQUOTE>
Blank lines also come in handy in CGI programs. Whether you use
them to make code in a file easier to read or to serve as a break
so that the server knows to do something different, you need blank
lines. The easy way to create a blank line with the <TT><FONT FACE="Courier">echo</FONT></TT>
command is just to put a period (.) right after it-no spaces,
no other characters, just a period, like this:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">echo.</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
If you put more characters after <TT><FONT FACE="Courier">echo</FONT></TT>,
it doesn't create a new line; it just functions like a normal
<TT><FONT FACE="Courier">echo</FONT></TT> command. With just that
period following it, it does what you want it to do.
</BLOCKQUOTE>
<H5>Environment Variables</H5>
<BLOCKQUOTE>
In the DOS world, environment variables such as <TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT>,
<TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>, and <TT><FONT FACE="Courier">REQUEST_METHOD</FONT></TT>
are marked in a special manner: they're surrounded by percent
signs (<TT><FONT FACE="Courier">%</FONT></TT>). So, if you want
to look at the content of the <TT><FONT FACE="Courier">PATH</FONT></TT>
environment variable on your system, from inside a BAT file, you
use the following command:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">echo %PATH%</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
When you run the BAT file from the DOS prompt, you get two lines:
one is the command being entered by the BAT file, just as you
would type it, and one is the result. This doesn't really matter
when you're running a DOS CGI program-the server is interested
in only what ends up in <TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>,
or %<TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>% as you refer
to it in your BAT file.
</BLOCKQUOTE>
<H5>type</H5>
<BLOCKQUOTE>
If you want to view a file or print several lines to the screen,
you use the <TT><FONT FACE="Courier">type</FONT></TT> command.
It echoes plain text back to the screen until it reaches the end
of the file-kind of an echo in an endless loop. If you send the
server and browser a header that says plain text is coming and
then use the <TT><FONT FACE="Courier">type</FONT></TT> command
to print a file to the Output File, everything that gets sent
to the Output File appears line by line on the end user's screen.
</BLOCKQUOTE>
<H5>Directing Output Using <FONT FACE="MCPdigital-B">&gt;</FONT><FONT SIZE=2>
and </FONT><FONT FACE="MCPdigital-B">&gt;&gt;</FONT></H5>
<BLOCKQUOTE>
When you want to send information somewhere else, that's called
<I>redirection</I>. To tell a BAT file that you want to redirect
information somewhere else, you have to use the symbols it understands.
These two symbols are the greater-than sign (<TT><FONT FACE="Courier">&gt;</FONT></TT>)
and two greater-than signs (<TT><FONT FACE="Courier">&gt;&gt;</FONT></TT>)
for most of the operations you need to worry about.
</BLOCKQUOTE>
<BLOCKQUOTE>
A greater-than symbol by itself (<TT><FONT FACE="Courier">&gt;</FONT></TT>)
means &quot;put everything on the left into the place on the right.&quot;
So, in real life you might have &quot;Orange Juice &gt; Cup,&quot;
meaning that you should take the orange juice and put it into
the cup. This process assumes that you're going to get a new cup
for the orange juice or that you're first going to dump out whatever
is in the cup already. When you're dealing with BAT files, the
same principle holds true-the command <TT><FONT FACE="Courier">echo
Hi, how are ya? &gt; myfile.txt</FONT></TT> first empties the
container (<TT><FONT FACE="Courier">myfile.txt</FONT></TT>) and
then shoves the text <TT><FONT FACE="Courier">Hi, how are ya?</FONT></TT>
into it. If you leave off the redirection, the text goes to the
screen by default.
</BLOCKQUOTE>
<BLOCKQUOTE>
Two greater-than symbols (<TT><FONT FACE="Courier">&gt;&gt;</FONT></TT>)

⌨️ 快捷键说明

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