📄 cgihtml.txt
字号:
cgihtml Documentation Eugene Eric Kim, eekim@eekim.com v1.69, March 18, 1998 Documentation for cgihtml, a set of CGI and HTML routines written in C. ______________________________________________________________________ Table of Contents 1. Introduction 1.1 Where to Find cgihtml 2. What is cgihtml? 2.1 What are the advantages of cgihtml? 2.2 Why C? 2.3 Latest changes 2.4 Files included in this package 3. Installation 3.1 Requirements 3.2 Obtaining and unpacking the distribution 3.3 Compiling the library 3.3.1 Makefile variables 3.3.2 Compiling for Win32 3.3.3 Configuring File Uploadn 3.3.4 Compiling and Installing 3.4 Porting 4. Using cgihtml 4.1 Basic programming structure 4.2 Compiling your program 5. Routines 5.1 cgi-lib.h 5.1.1 Library Variables 5.1.2 Library functions 5.2 html-lib.h 5.2.1 Library functions 5.3 cgi-llist.h 5.3.1 Library variables 5.3.2 Library functions 5.4 string-lib.h 5.4.1 Library functions 6. Example programs 6.1 test.cgi 6.2 query-results 6.3 mail.cgi 6.4 index-sample.cgi 6.5 ignore.cgi 7. Miscellaneous 7.1 Release notes 7.2 Future releases 7.3 Credits ______________________________________________________________________ 11.. IInnttrroodduuccttiioonn This documentation is also available at <http://www.eekim.com/software/cgihtml/cgihtml.html> Documentation last updated: March 18, 1998 11..11.. WWhheerree ttoo FFiinndd ccggiihhttmmll You can download cgihtml (gzipped, UNIX compressed, or PKZipped) from one of the following sites: eeeekkiimm..ccoomm <ftp://ftp.eekim.com/users/eekim/cgihtml/> HHaarrvvaarrdd CCoommppuutteerr SSoocciieettyy <ftp://hcs.harvard.edu/pub/web/tools/cgihtml/> KKeeiitthh BBuunnggee''ss IIoowwaa SSttaattee mmiirrrroorr ssiittee <ftp://aged.brenton.iastate.edu/> Useful information about cgihtml is available at the cgihtml home page <http://www.eekim.com/software/cgihtml/>, located at <http://www.eekim.com/software/cgihtml/>. 22.. WWhhaatt iiss ccggiihhttmmll?? cgihtml is a collection of routines for parsing World Wide Web (WWW) Common Gateway Interface (CGI) input and outputting HyperText Markup Language (HTML). 22..11.. WWhhaatt aarree tthhee aaddvvaannttaaggeess ooff ccggiihhttmmll?? cgihtml simplifies the task of parsing CGI input and producing HTML output. Tasks which would normally require several lines of C can be reduced to just a few. Additionally, I have attempted to include general routines which CGI programmers often find themselves using. Consequently, some of the complexities of CGI programming are hidden. On the other hand, if you want to know what's going on, source is included. 22..22.. WWhhyy CC?? The purpose of CGI programs is to take data and manipulate it as the web programmer desires. Since CGI programs are often dealing with text manipulation, Perl or other scripting languages is an ideal way of producing CGI scripts. (Perl programmers should check out Lincoln Stein's CGI.pm <http://www- genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html> or Steve Brenner's cgi-lib.pl <http://www.bio.cam.ac.uk/web/form.html>.) However, interpreted scripting languages tend to be relatively large. This rarely has a major effect on the performance of your server (unless you are a high-traffic site). However, if this is a concern of your's, a program written in C is often several times smaller than the equivalent program written in Perl. There's definitely a performance improvement when using CGI programs written in C, although the performance is not always noticeable. Additionally, some servers (notably Netsite and Apache) have APIs so that CGI programs can be written as extensions to the server, rather than as separate programs. This greatly improves performance, especially on high-traffic sites. The best way to take advantage of these APIs is to write your programs in C. Or, you might fall under one of the following categories: +o You don't know Perl +o You don't like Perl +o You like C In which case, you will hopefully find cgihtml useful. 22..33.. LLaatteesstt cchhaannggeess See the CHANGES file, included in the distribution. 22..44.. FFiilleess iinncclluuddeedd iinn tthhiiss ppaacckkaaggee The following files are included in this package: 33.. IInnssttaallllaattiioonn 33..11.. RReeqquuiirreemmeennttss cgihtml was written for Unix machines in C, although it has been successfully ported to Windows 95 and NT, VMS, OS-9, and other operating systems. All you need is a C compiler, and you should be set. By default, cgihtml assumes that the CGI source code goes in the cgi- src directory and the binaries in the cgi-bin directory. 33..22.. OObbttaaiinniinngg aanndd uunnppaacckkiinngg tthhee ddiissttrriibbuuttiioonn You may find cgihtml.tar.gz at <ftp://ftp.eekim.com/users/eekim/cgihtml/> To unpack the distribution, you must first gunzip it (using the GNU gzip utility) and then untar it. Copy the distribution into your CGI source directory, and try the following command: % gzip -dc cgihtml.tar.gz | tar xvf - cgihtml is also available in UNIX compressed (.Z) and PKZipped (.zip) format. 33..33.. CCoommppiilliinngg tthhee lliibbrraarryy To compile the library, examine the Makefiles in the cgihtml and examples directories, and make sure you are satisfied with the variables. 33..33..11.. MMaakkeeffiillee vvaarriiaabblleess INSTALLDIR in cgihtml's Makefile should point to your CGI source directory, while INSTALLDIR in your examples directory should point to your server's CGI binary directory. 33..33..22.. CCoommppiilliinngg ffoorr WWiinn3322 If you're compiling for Win32 (ie. Windows 95/NT), make sure to uncomment the line with -DWINDOWS. 33..33..33.. CCoonnffiigguurriinngg FFiillee UUppllooaaddnn By default, the file upload directory is set to /tmp. To change this value, uncomment #-DUPLOADDIR='"/tmp"' in the Makefile and replace /tmp with the directory of your choice. Make sure that whichever directory you choose is surrounded by both the single and double quotes, ie: '"/foo/bar"'. 33..33..44.. CCoommppiilliinngg aanndd IInnssttaalllliinngg When you are satisfied with the Makefiles, type: % make cgihtml.a This will produce the file cgihtml.a. To compile the library as well as all of the example programs, type: % make all To install the library and examples, type: % make install If you want to compile and/or install the example programs separately, change to the examples subdirectory and use make there. 33..44.. PPoorrttiinngg While compiling the libraries on various Unix machines, you may have trouble with the "ranlib" command. If you system doesn't seem to have this command, you most likely don't need. Set the RANLIB variable in the Makefile to "true". If you're compiling for Win32, make sure you use the -DWINDOWS directive when compiling. If you are compiling for DOS/16-bit Windows, VMS, or OS-9, you will need to change the filenames to support your OS. 44.. UUssiinngg ccggiihhttmmll 44..11.. BBaassiicc pprrooggrraammmmiinngg ssttrruuccttuurree There are standard initialization things that you will want to do when using this library. The following template should give you some idea of how to properly use this library. ______________________________________________________________________ /* template using cgihtml.a library */ #include <stdio.h> /* standard io functions */ #include "cgi-lib.h" /* CGI-related routines */ #include "html-lib.h" /* HTML-related routines */ int main() { llist entries; /* define a linked list; this is where the entries */ /* are stored. */ /* parse the form data and add it to the list */ read_cgi_input(&entries); /* The data is now in a very usable form. To search the list entries */ /* by name, call the function: */ /* cgi_val(entries, "nameofentry") */ /* which returns a pointer to the value associated with "nameofentry". */ html_header(); /* print HTML MIME header */ html_begin("Output"); /* send appropriate HTML headers with title */ /* <title>Output</title> */ /* display whatever data you wish here, probably with printf() */ html_end(); /* send appropriate HTML end footers (</body> </html>) */ list_clear(&entries); /* free up the pointers in the linked list */ return 0; } ______________________________________________________________________ 44..22.. CCoommppiilliinngg yyoouurr pprrooggrraamm To compile your program with the library, include the file cgihtml.a when linking your object files. For example, if your main object file is program.cgi.o, the following should work successfully: cc -o program.cgi program.cgi.o cgihtml.a 55.. RRoouuttiinneess 55..11.. ccggii--lliibb..hh 55..11..11.. LLiibbrraarryy VVaarriiaabblleess cgi-lib.h defines constants for the standard CGI environment variables. For instance, the value of the environment variable QUERY_STRING is stored in the constant QUERY_STRING in cgi-lib.h. Here is a list of the constants: +o SERVER_SOFTWARE +o SERVER_NAME +o GATEWAY_INTERFACE +o SERVER_PROTOCOL +o SERVER_PORT +o REQUEST_METHOD +o PATH_INFO +o PATH_TRANSLATED +o SCRIPT_NAME +o QUERY_STRING +o REMOTE_HOST +o REMOTE_ADDR +o AUTH_TYPE +o REMOTE_USER +o REMOTE_IDENT +o CONTENT_TYPE +o CONTENT_LENGTH +o HTTP_USER_AGENT 55..11..22.. LLiibbrraarryy ffuunnccttiioonnss sshhoorrtt aacccceepptt__iimmaaggee(());; accept_image() determines whether the browser will accept pictures. It does so by checking the HTTP_ACCEPT environment variable for an image MIME type. It returns a 1 if the browser will accept graphics, a 0 otherwise. vvooiidd uunneessccaappee__uurrll(());; unescape_url() converts escaped URI values into their character form. read_cgi_input() calls this function. You will rarely if ever need to access this function directly but it is made available in case you do. iinntt rreeaadd__ccggii__iinnppuutt((lllliisstt **eennttrriieess));; This routine parses the raw CGI data passed from the browser to the server and adds each associated name and value to the linked list entries. It will parse information transmitted using both the GET and POST method. If it receives no information, it will return a 0, otherwise it returns the number of entries returned. If it receives a badly encoded string, it will return -1. If you run your CGI program that calls read_cgi_input() from the command line, this function will start an interactive mode so you can directly input the CGI input string. Note that this string must be properly encoded. read_cgi_input() also handles HTTP file upload correctly. The file will be uploaded to the directory defined by UPLOADDIR in cgi-lib.h (/tmp by default). cchhaarr** ccggii__vvaall((lllliisstt ll,, cchhaarr **nnaammee));; cgi_val() searches the linked list for the value of the entry named name and returns the value if it finds it. If it cannot find an entry with name name, it returns NULL. cchhaarr**** ccggii__vvaall__mmuullttii((lllliisstt ll,, cchhaarr **nnaammee));; Same as cgi_val() except will return multiple values with the same name to an array of strings. Will return NULL if it cannot find an entry with name name cchhaarr** ccggii__nnaammee((lllliisstt ll,, cchhaarr **vvaalluuee));; Same as cgi_val() except searches for value with specified name. cchhaarr**** ccggii__nnaammee__mmuullttii((lllliisstt ll,, cchhaarr **vvaalluuee));; Analogous to cgi_multi_val(). iinntt ppaarrssee__ccooookkiieess((lllliisstt **eennttrriieess));; Parses the environment variable HTTP_COOKIE for cookies. Returns the number of cookies parsed, zero if there are none. vvooiidd pprriinntt__ccggii__eennvv(());; Pretty prints the environment variables defined in cgi-lib.h. Prints "(null)" if the variables are empty. vvooiidd pprriinntt__eennttrriieess((lllliisstt ll));; This is a generic routine which will iterate through the linked list and print each name and associated value in HTML form. It uses the <dl> list format to display the list. cchhaarr** eessccaappee__iinnppuutt((cchhaarr **ssttrr));; escape_input() "escapes" shell metacharacters in the string. It precedes all non-alphanumeric characters with a backslash. C routines including system() and popen() open up a Bourne shell process before running. If you do not escape shell metacharacters in the input (prefix metacharacters with a backslash), then malicious users may be able to take advantage of your system. sshhoorrtt iiss__ffoorrmm__eemmppttyy((lllliisstt ll));; is_form_empty() checks to see whether no names or values were submitted. Note that this is different from submitting a blank form. sshhoorrtt iiss__ffiieelldd__eexxiissttss((lllliisstt ll,,cchhaarr **ssttrr));; Checks to see whether a field actually exists. Equivalent to checking whether cgi_val() returns "" or NULL. If it returns "", the field exists but is empty; if it returns NULL, the field does not exist. sshhoorrtt iiss__ffiieelldd__eemmppttyy((lllliisstt ll,,cchhaarr **ssttrr));; Returns 1 (true) if either the field does not exist or the field does exist but is empty. 55..22.. hhttmmll--lliibb..hh 55..22..11.. LLiibbrraarryy ffuunnccttiioonnss vvooiidd hhttmmll__hheeaaddeerr(());; html_header() prints a MIME compliant header which should precede the output of any HTML document from a CGI script. It simply prints to STDOUT: Content-Type: text/html
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -