📄 ch14.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 14 -- WinCGI Basics </TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 14</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>WinCGI 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="#YourVeryFirstWinCGIProgram" >Your Very First WinCGI Program</A>
<LI><A HREF="#Authentication" >Authentication</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
WinCGI is such an easy method to program CGI apps in because you
write the CGI application in Visual Basic, as opposed to C or
C++. This means, instead of having thousands of lines of code
just to do anything useful, you can write a fully functioning
WinCGI app in about 10 lines. And WinCGI apps that do advanced
things, like database access, don't take many more lines than
that because VB makes it very easy to access databases. But first,
let me start with the basics: environment variables and authentication.
<H2><A NAME="YourVeryFirstWinCGIProgram"><FONT SIZE=5 COLOR=#FF0000>Your
Very First WinCGI Program</FONT></A></H2>
<P>
For starters, I'll get some required knowledge out of the way.
First, if you haven't read the latest WinCGI spec (1.3a at this
time), or CGI(32).BAS, it's important that you read it now. CGI32.BAS
is at the end of the chapter, while the WinCGI spec can be found
online. Second, I am using WebSite as the server to back end my
WinCGI programs in these examples. Anywhere something is specific
to WebSite, I'll point it out. Lastly, there are certain options
that must be set in VB (any version) so that your program will
work. The steps you need to take are as follows:
<OL>
<LI>Set the startup type to Sub Main.
<BR>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
WinCGI apps don't use forms to interact with the user.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<BR>
<LI>Include CGI.BAS or CGI32.BAS, depending on if you are using
a 16-bit or 32-bit environment.
</OL>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The references in this chapter to the term WinCGI Procedure refer to procedures that occur in CGI/CGI32.BAS.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
To set up the sample programs in this chapter, you need to do
the following:
<OL>
<LI>Save the text of the listing in a .BAS file.
<LI>Go into VB and start a new project.
<LI>Add the .BAS file you just saved, and CGI(32).BAS to the project.
<LI>Set the start up to Sub Main
</OL>
<P>
Before you start coding programs, take a look at the procedures
and functions that reside in CGI/CGI32.BAS:
<OL>
<LI><TT><FONT FACE="Courier">FieldPresent</FONT></TT>-This function
returns True or False, depending on whether or not the field is
present.
<LI><TT><FONT FACE="Courier">ErrorHandler</FONT></TT>-This procedure
is used as a global error handling routine. When called, it returns
an HTML page to the browser that contains information about the
error.
<LI><TT><FONT FACE="Courier">GetAcceptTypes</FONT></TT>-This procedure
is used internally by the CGI/CGI32.BAS file. It creates an array
that contains the data from the <TT><FONT FACE="Courier">[Accept]</FONT></TT>
section.
<LI><TT><FONT FACE="Courier">GetArgs</FONT></TT>-This function
is used internally by the CGI/CGI32.BAS file. It splits the command
line into separate arguments, and also returns the number of arguments.
<LI><TT><FONT FACE="Courier">GetExtraHeaders</FONT></TT>-This
procedure is used internally by the CGI/CGI32.BAS file. It creates
an array that contains data from the <TT><FONT FACE="Courier">[Extra
Headers]</FONT></TT> section.
<LI><TT><FONT FACE="Courier">GetFormTuples</FONT></TT>-This procedure
is used internally by the CGI/CGI32.BAS file. It creates an array
that contains data from the form data.
<LI><TT><FONT FACE="Courier">GetProfile</FONT></TT>-This function
is used internally by the CGI/CGI32.BAS file. It grabs the data
from the profile so that other functions can process it.
<LI><TT><FONT FACE="Courier">GetSmallField</FONT></TT>-This function
returns the data from a form field.
<LI><TT><FONT FACE="Courier">InitializeCGI</FONT></TT>-This function
is used internally by the CGI/CGI32.BAS file. It initializes the
VB variables that represent CGI environment variables.
<LI><TT><FONT FACE="Courier">Main</FONT></TT>-This procedure is
used internally by the CGI/CGI32.BAS file. It handles the core
functionality of the CGI framework, and then passes control to
the users program.
<LI><TT><FONT FACE="Courier">Send</FONT></TT>-This procedure outputs
data to the browser.
<LI><TT><FONT FACE="Courier">SendNoOp</FONT></TT>-This procedure
tells the browser to do nothing at all.
<LI><TT><FONT FACE="Courier">WebDate</FONT></TT>-This function
converts time stored in a VB variant into a string that contains
HTTP 1.0 compliant time.
<LI><TT><FONT FACE="Courier">PlusToSpace</FONT></TT>-This procedure
converts pluses (+) that appear in HTTP encoded strings into spaces.
<LI><TT><FONT FACE="Courier">Unescape</FONT></TT>-This function
converts a string containing escaped characters into a string
where the characters are replaced with the unescaped form.
<LI><TT><FONT FACE="Courier">x2c</FONT></TT>-This function converts
an escaped character into an unescaped character.
</OL>
<P>
Now that you know what you have available in the way of procedures
and functions to help you, I'll show you how small and easy a
WinCGI app can be. I'll begin by having you write something simple,
like a hit counter.
<P>
I'm sure you've seen the simple 50 line Perl scripts or 100 line
C programs that do simple text counters.
<P>
Well, your VB app is very simple. In fact, it uses only one WinCGI
procedure (<TT><FONT FACE="Courier">Send</FONT></TT>).
<P>
The code is given in Listing 14.1.
<HR>
<BLOCKQUOTE>
<B>Listing 14.1. Simple CGI counter program.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">'CGI Counter Program.<BR>
'Copyright 1996 Daniel Berlin<BR>
'Very simple demonstration of how easy WinCGI in VB is.<BR>
Sub CGI_Main()<BR>
'Real Code<BR>
Dim FreeFileNumber As Integer 'Used to store the result of FreeFile
<BR>
Dim CounterString As String 'Used to store the counter read in
from the file<BR>
Dim CounterInteger As Integer 'Used to store real counter value
<BR>
<BR>
CounterInteger = 0 'Makes it easy to handle
the not exist error<BR>
FreeFileNumber = FreeFile<BR>
Open CurDir + "\counter.dat"
For Input As #FreeFileNumber 'Open it so we can <BR>
'get the current counter<BR>
Line Input #FreeFileNumber, CounterString
<BR>
Close #FreeFileNumber<BR>
<BR>
CounterInteger = Val(CounterString) 'Convert
string to integer<BR>
FreeFileNumber = FreeFile<BR>
<BR>
CounterInteger = CounterInteger + 1<BR>
Open CurDir + "\Counter.dat"
For Output As #FreeFileNumber<BR>
Print #FreeFileNumber, CounterInteger
<BR>
Close #FreeFileNumber<BR>
<BR>
Send ("Content-type: text/html")
'Standard Mime Header for HTML<BR>
Send ("")<BR>
Send (CounterInteger)<BR>
End Sub<BR>
<BR>
Sub Inter_Main()<BR>
'Handles interactive (startup without a commandline) startup<BR>
MsgBox "This is a Windows CGI Program._
<BR>
It
should only be run by the server"<BR>
Exit sub<BR>
End Sub</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
As you can see, more code is spent opening and closing files than
doing actual work. It is also not the most robust implementation
of a counter. If the counter file doesn't exist, it fails with
an error. But, it does work otherwise. It contains all the things
every CGI program has, which are
<UL>
<LI><FONT COLOR=#000000>A </FONT><TT><FONT FACE="Courier">Inter_Main</FONT></TT>
procedure that handles interactive startup (means no command line)
<LI><FONT COLOR=#000000>A </FONT><TT><FONT FACE="Courier">CGI_Main</FONT></TT>
procedure, which is the real main part of your program.
<LI><FONT COLOR=#000000>CGI.BAS or CGI32.BAS included in the project</FONT>
</UL>
<P>
As I have already mentioned, the only WinCGI procedure used in
it is <TT><FONT FACE="Courier">Send</FONT></TT>. This procedure
simply writes to the output file that is returned to the browser.
Because it is such a simple program, there is no need to dissect
it, so I'll move on to something just as easy: authentication
in CGI programs.
<H2><A NAME="Authentication"><FONT SIZE=5 COLOR=#FF0000>Authentication</FONT></A>
</H2>
<P>
There are WinCGI applications that need user level security. To
provide this, you use basic authentication.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
WebSite requires that the program name begin with a dollar sign ($), or else it won't pass the password to it.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The header to tell a browser you want authentication looks something
like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">HTTP/1.0 401 Unauthorized<BR>
Server: Website 1.1e<BR>
Date: 06/29/96<BR>
WWW-Authenticate: Basic realm="AuthDemo"<BR>
Content-type: text/html<BR>
</FONT></TT>
</BLOCKQUOTE>
<P>
When you authenticate, the server calls the program again (and,
this time, passes the authentication information). You need to
check for the username before outputting the header to authenticate.
<P>
So far, in VB, the code would look like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">If CGI_AuthUser = "" Then 'If
they haven't authenticated, do it<BR>
<BR>
Send "HTTP/1.0
401 Unauthorized"<BR>
Send ("Server:
" + CGI_ServerSoftware)<BR>
Send ("Date:
" + WebDate(Now))<BR>
Send ("WWW-Authenticate:
Basic realm=""AuthDemo""")<BR>
Send ("Content-type:
text/html")<BR>
Send ("")
<BR>
'If we get to this, the user clicked cancel<BR>
End If</FONT></TT>
</BLOCKQUOTE>
<P>
This, if compiled, will loop until the user enters a name or clicks
cancel.
<P>
The only way to get to the line <TT><FONT FACE="Courier">If we
get...</FONT></TT> is if you click cancel. If the user had not,
it would have called the program again in an attempt to authenticate.
Therefore, the code to handle what happens if the user clicks
cancel goes right after the same line.
<P>
So far, you have a program that will loop until either a name
is entered or cancel is clicked.
<P>
I'll expand it a little bit by writing the other half of the <TT><FONT FACE="Courier">if</FONT></TT>
statement (what happens if a name is entered).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -