![]() |
Web Conference 2004Writing Perl/CGI Scripts for ITS/ASET Web servicesSpeaking CGI |
| <- Back - More on HTTP | | | Up | | | More on CGI - Next -> |
When an HTTP request calls a CGI program, the program is started by the Web server process and data is passed back and forth between the Web server process (Apache) and the CGI. This is local to the Web server computer and is in addition to the HTTP dialogue.
Here is what the conversation between the browser, HTTP server program (Apache) and CGI program (passingdata.cgi) look like.
The browser makes the following HTTP request to the server (*):
| Browser --HTTP==> Web Server |
|---|
GET /users/j/c/jcd/useful/webcon/2004/passingdata.cgi?color=blue HTTP/1.0 |
|
* - Most browsers are much chattier than this. My browser sent the following to the server: [top] |
GET /staff/j/c/jcd/useful/webcon/2004/passingdata.cgi?color=blue HTTP/1.1 Host: test.scripts.psu.edu User-Agent: Mozilla/5.0 Galeon/1.2.11 (X11; Linux i686; U;) Gecko/20030417 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Referer: http://www.personal.psu.edu/users/j/c/jcd/useful/webcon/2004/passingdata.html |
The HTTP server program (Apache) determines this request is for the CGI program called passingdata.cgi, runs it, and sets an environment variable called QUERY_STRING with the parameters inside:
| Web Server Program (Apache) --Environment==> passingdata.cgi | |
|---|---|
| QUERY_STRING | color=blue |
param('color') reads QUERY_STRING, sets $color to "blue", and the script prints the following message to the HTTP server via STDOUT (Standard Output):
| Web Server Program (Apache) <==STDOUT-- passingdata.cgi |
|---|
Content-Type: text/html <html> <body> <p>Your favorite color is blue.</p> </body> </html> |
Then the HTTP server adds its own lines to the beginning of the message, and finally sends the following to the browser:
| Browser <==HTTP-- Web Server |
|---|
HTTP/1.1 200 OK Date: Wed, 09 Jun 2004 18:59:41 GMT Server: Apache/1.3.12 (Unix) mod_ssl/2.6.2 OpenSSL/0.9.5a Connection: close Content-Type: text/html <html> <body> <p>Your favorite color is blue.</p> </body> </html> |
The first five lines of the reply back to the browser are the HTTP header, which indicate status of the request, and aspects of the requested page. The rest are the HTTP body which includes the Web page we see after we send the form.
| <- Back - More on HTTP | | | Up | | | More on CGI - Next -> |
If you have any questions, feel free to ask me - mailto:jcd@psu.edu
Content by: Jeff D'Angelo <jcd@psu.edu> © 2004
Last update on: Sunday, 13-Jun-2004 17:00:34 EDT