CGI (Common Gate Interface)
Doc generation
Two lines at least must be sent to the client:
- First line
- a full doc with MIME type Content-type: text/html for HTML doc,
or Content-type: text/plain for ASCII text
- a reference Location: /dir1/dir2/myfile.html, or Location:
gopher://httprules.foobar.org/
- Second line - a blank line
Example
#include <iostream.h>
main()
{
cout << "Content-type: text/html\n\n"; // 2nd \n is for the 2nd line
cout << "Hello, World!";
return(0);
}
test
Communication btwn Doc and CGI Script
CGI uses environment variables to send your script its parameters:
- QUERY_STRING
-
- in URL format: space change to '+'
- special characters in %xx hexadecimal encoding
- PATH_INFO
- <a href="/cgi/foobar/lang=eng">foobar</a>
Methods to Access Form Inputs
<FORM ACTION="http://www.csie.ntu.edu.tw/~your_hd/hello.exe" METHOD=GET>
| |
your ip address your script
- form fields are encoded as name/value pairs, separated by
"&"
- all spaces are encoded as "+"
- GET
-
- encoded input in QUERY_STRING
- form fields are encoded as name/value pairs:
- name1~value1&name2~value2 ...
- POST
-
- encoded input in stdin (standard input)
- should use CONTENT_LENGTH to determine data length
- form fields are encoded as name/value pairs:
- name1=value1&name2=value2 ...
Resources