Chapter 5: Introducing Perl

Providing some Feedback


[ Comments ] [ Copyright ] [ Chapter contents ] [ Book contents ]


Now we've figured out how to get query information into a Perl script, it's time to consider how to deliver the output of the script back to the server.

CGI scripts simply print their output to the standard output; the HTTP server reads the output and sends it back down to the client in the form of an HTTP body. First, it writes a brief header -- usually not a full HTTP header. The header consists of a line containing some server directives, or a full HTTP response followed by a blank line. (Server directives are interpreted by the server, which generates an appropriate HTTP header.) Next, the script writes to standard output whatever text needs to be returned to the client. The server digests the standard output from the CGI script and sends the appropriate portion of it back to the client. For example:

Content-type:
text/html
<HTML> 
<!-- stuff goes here --> 
</HTML>

This tells the server to send the script output as HTML, then sends the output from the script. (The HTML is presumably generated by print statements or some equivalent mechanism.)

Note the blank line between "Content-type" and "<HTML>". If there isn't one, you'll get a 500 Server Error message, and the server error logfile will contain a message like "Malformed header from script". This is because your neatly printed HTML will be mistaken by the server for part of the HTTP response header, rather than the body of the message.

Alternatively, the client can return a pointer to some other document. Instead of the normal header (containing a Content-type: line, and optional additional information), it contains a reference:

Location: /pub/myfile.txt

The Location: header is sent to the client, which then is tells the server to send /pub/myfile.txt instead. The location can also be a full URL; for example:

Location: http://www.antipope.org/charlie/index.html

Tells the client to go look at that file instead of the URL that was initially looked at.


[ Comments ] [ Copyright ] [ Chapter contents ] [ Book contents ]