📄 cgihtml.txt
字号:
and a blank line. vvooiidd mmiimmee__hheeaaddeerr((cchhaarr **mmiimmee));; Allows you to print any MIME header. For example, if you are about to send a GIF image to the standard output from your C CGI program, precede your program with: ______________________________________________________________________ mime_header("image/gif"); /* now you can send your GIF file to stdout */ ______________________________________________________________________ mime_header() simply prints Content-Type: followed by your specified MIME header and a blank line. vvooiidd nnpphh__hheeaaddeerr((cchhaarr **ssttaattuuss));; Sends a standard HTTP header for direct communication with the client using no parse header. status is the status code followed by the status message. For instance, to send a "No Content" header, you could use: ______________________________________________________________________ nph_header("204 No Content"); html_header(); ______________________________________________________________________ which would send: HTTP/1.0 204 No Content Server: CGI using cgihtml Content-Type: text/html nph_header() does not send a blank line after printing the headers, so you must follow it with either another header or a blank line. Also, scripts using this function must have "nph-" preceding their filenames. vvooiidd sshhooww__hhttmmll__ppaaggee((cchhaarr **lloocc));; Sends a "Location: " header. loc is the location of the HTML file you wish sent to the browser. For example, if you want to send the root index file from the CGI program: ______________________________________________________________________ show_html_page("/index.html"); ______________________________________________________________________ vvooiidd ssttaattuuss((cchhaarr **ssttaattuuss));; Sends an HTTP Status header. status is a status code followed by a status message. For instance, to send a status code of 302 (temporary redirection) followed by a location header: ______________________________________________________________________ status("302 Temporarily Moved"); show_html_page("http://hcs.harvard.edu/"); ______________________________________________________________________ status() does not print a blank line following the header, so you must follow it with either another function which does output a blank line or an explicit printf("\r\n");. vvooiidd pprraaggmmaa((cchhaarr **mmssgg));; Sends an HTTP Pragma header. Most commonly used to tell the browser not to cache the document, ie.: ______________________________________________________________________ pragma("No-cache"); html_header(); ______________________________________________________________________ As with status(), pragma() does not print a blank line folowing the header. vvooiidd sseett__ccooookkiiee((cchhaarr **nnaammee,, cchhaarr **vvaalluuee,, cchhaarr **eexxppiirreess,, cchhaarr **ppaatthh,, cchhaarr **ddoommaaiinn,, sshhoorrtt sseeccuurree));; Sets a cookie using the values given in the parameters. vvooiidd hhttmmll__bbeeggiinn((cchhaarr **ttiittllee));; html_begin() sends somewhat standard HTML tags which should generally be at the top of every HTML file. It will send: <html> <head> <title>title</title> </head> <body> vvooiidd hhttmmll__eenndd(());; html_end() is the complement to html_begin(), sending the following HTML: </body> </html> Note that neither html_begin() nor html_end() are necessary for your CGI scripts to output HTML, but they are good style, and I encourage use of these routines. vvooiidd hh11((cchhaarr **hheeaaddeerr));; Surrounds header with appropriate headline tags. Defined for h1() to h6(). vvooiidd hhiiddddeenn((cchhaarr **nnaammee,, cchhaarr **vvaalluuee));; Prints a hidden form field with name name and value value. 55..33.. ccggii--lllliisstt..hh For most scripts, with the exception of list_end(), you will most likely never have to use any of the link list routines available, since cgi-lib.h handles most common linked list manipulation almost transparently. However, you may sometimes want to manipulate the information directly or perform special functions on each entry, in which case these routines may be useful. 55..33..11.. LLiibbrraarryy vvaarriiaabblleess ______________________________________________________________________ typedef struct { char *name; char *value; } entrytype; typedef struct _node { entrytype entry; struct _node* next; } node; typedef struct { node* head; } llist; ______________________________________________________________________ 55..33..22.. LLiibbrraarryy ffuunnccttiioonnss vvooiidd lliisstt__ccrreeaattee((lllliisstt **ll));; list_create() creates and initializes the list, and it should be called at the beginning of every CGI script using this library. nnooddee** lliisstt__nneexxtt((nnooddee** ww));; list_next() returns the next node on the list. sshhoorrtt oonn__lliisstt((lllliisstt **ll,, nnooddee** ww));; on_list() returns a 1 if the node w is on the linked list l; otherwise, it returns a 0. sshhoorrtt oonn__lliisstt__ddeebbuugg((lllliisstt **ll,, nnooddee** ww));; The previous routine makes the assumption that my linked list routines are bug-free, a possibly bad assumption. If you are using linked list routines and on_list() isn't returning the correct value, try using on_list_debug() which makes no assumptions, is almost definitely reliable, but is a little slower than the other routine. vvooiidd lliisstt__ttrraavveerrssee((lllliisstt **ll,, vvooiidd ((**vviissiitt))((eennttrryyttyyppee iitteemm))));; list_traverse() lets you pass a pointer to a function which will manipulate each entry on the list. To use, you must create a function that will take as its argument a variable of type entrytype. For example, if you wanted to write your own print_entries() function, you could do the following: ______________________________________________________________________ void print_element(entrytype item); { printf("%s = %s\n",item.name,item.value); } void print_entries(llist entries); { list_traverse(&stuff, print_element); } ______________________________________________________________________ nnooddee** lliisstt__iinnssaafftteerr((lllliisstt** ll,, nnooddee** ww,, eennttrryyttyyppee iitteemm));; list_insafter() adds the entry item after the node w and returns the pointer to the newly created node. I didn't bother writing a function to insert before a node since my CGI functions don't need one. vvooiidd lliisstt__cclleeaarr((lllliisstt** ll));; This routine frees up the memory used by the linked list after you are finished with it. It is imperative that you call this function at the end of every program which calls read_cgi_input(). 55..44.. ssttrriinngg--lliibb..hh 55..44..11.. LLiibbrraarryy ffuunnccttiioonnss cchhaarr** nneewwssttrr((cchhaarr **ssttrr));; newstr() allocates memory and returns a copy of str. Use this function to correctly allocate memory and copy strings. cchhaarr** ssuubbssttrr((cchhaarr **ssttrr,, iinntt ooffffsseett,, iinntt lleenn));; Analogous to the Perl substr function. Finds the substring of str at an offset of offset and of length len. A negative offset will start the substring from the end of the string. cchhaarr** rreeppllaaccee__llttggtt((cchhaarr **ssttrr));; Replaces all instances of < and > in str with < and >. cchhaarr** lloowweerr__ccaassee((cchhaarr **bbuuffffeerr));; Converts a string from upper to lower case. 66.. EExxaammppllee pprrooggrraammss 66..11.. tteesstt..ccggii test.cgi is a simple test program. It will display the CGI environment, and if there is any input, it will parse and display those values as well. 66..22.. qquueerryy--rreessuullttss This is a generic forms parser which is useful for testing purposes. It will parse both GET and POST forms successfully. Simply call it as the form "action", and it will return all of the names and associated values entered by the user. query-results also works from the command line. For instance, if you run query-results from the command line, you will see: Content-type: text/html <html> <head> <title>Query Results</title> </head> <body> --- cgihtml Interactive Mode --- Enter CGI input string. Remember to encode appropriate characters. Press ENTER when done: Suppose you enter the input string: name=eugene&age=21 Then query-results will return: Input string: name=eugene&age=21 String length: 18 --- end cgihtml Interactive Mode --- <h1>Query results</h1> <dl> <dt> <b>name</b> <dd> eugene <dt> <b>age</b> <dd> 21 </dl> </body> </html> This feature is extremely useful if you are debugging code. query- results will also handle file upload properly and transparently. It will save the file to the directory defined by UPLOADDIR (/tmp by default). 66..33.. mmaaiill..ccggii This is a generic comments program which will parse the form, check to see if the intended recipient is a valid recipient, and send the e- mail if so. You will want to edit two things in the source file: WEBADMIN and AUTH. WEBADMIN is the complete e-mail address to which the comments should be sent by default. AUTH is the exact location of the authorization file. The authorization file is simple a text file with a list of valid e- mail recipients. Users will only be able to use this program to send e-mail to those listed in the authorization file. Your file might look like this: web@www.company.com jschmoe@www.company.com In the above case, you would only be able to send e-mail to web@www.company.com and jschmoe@www.company.com. Make sure you include the value of WEBADMIN in your authorization file. The following are valid variables in your form: +o to +o name +o email +o subject +o content If there is no to variable defined in the form, the mail will be sent to WEBADMIN by default. mail.cgi will reject empty forms. mail.cgi adds a "X-Sender:" header on each message so recipients know that the message was sent by this program and not by a regular mail client. 66..44.. iinnddeexx--ssaammppllee..ccggii Imagemaps have become increasingly popular to use on home pages. Unfortunately, imagemaps are not lynx friendly; if you forget to include some sort of text index as well, lynx users will not be able to access any of your subpages. You can circumvent this problem by using a CGI program as your home page rather than an HTML page (or by using server-side includes). This CGI program will determine whether your browser is a graphics or text-browser. If it is a text-browser, it will send a text HTML file, otherwise it will send a graphics HTML file. You will need to create two HTML files: a graphical and a text one. Place the names of these files in the macros: TEXT_PAGE and IMAGE_PAGE. 66..55.. iiggnnoorree..ccggii Sends a status code of 204, signifying no content. If you use imagemaps, you can set "default" to /cgi-bin/ignore.cgi. Whenever someone clicks on a part of the picture which is undefined, the server will just ignore the request. 77.. MMiisscceellllaanneeoouuss 77..11.. RReelleeaassee nnootteess I periodically enhance this library, and I welcome any comments or suggestions. Please e-mail them to eekim@eekim.com. This library is e-mail ware. Please send me e-mail if you use this library; I'd really like to hear your comments. Although I do not require it, I would appreciate attribution if you use my code. 77..22.. FFuuttuurree rreelleeaasseess This library is nearing its final release. I hope to include FastCGI support, and API support (for Apache, Netscape, and Microsoft servers). I may also attempt to port it to the Macintosh, and I want to improve generally portability. I will most likely rewrite the API in the next major release. Finally, I'd like to improve the robustness and fix all the bugs. If you have any suggestions, I'd like to hear them. Feel free to e- mail me at eekim@eekim.com 77..33.. CCrreeddiittss Thanks to the countless people who have sent me suggestions and comments. You may contact me via e-mail at eekim@eekim.com. My web page is located at <http://www.eekim.com/>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -