Web Perl   «Prev 

Mime Headers

The output of a CGI program must begin with a MIME header. MIME (Multipurpose Internet Mail Exchange) is a specification that was designed to add media type information to email messages on the Internet. HTTP uses it to specify what type of data is being returned to the client.
Some common MIME types that you will see on the Web are:

text/htmlAn HTML document
text/plainA plain text document
image/gifA GIF image
image/jpegA JPEG (or JPG) image

HTTP specification

The HTTP specification states that all headers must be terminated with both a carriage-return (hex 0D) and a linefeed (hex 0A) character (a CR/LF pair), and that the last header in a stream must be followed by a blank line, and also terminated with a CR/LF pair. The MIME header at the beginning of a CGI program usually has two CR/LF pairs at the end of it.
The MIME header that is sent by the CGI program will actually be combined with a set of headers that the HTTP server will send to the client before sending the body of the data.

mod_cgi vs. mod_perl

By default, the mod_cgi module is used to handle CGI content in Apache. The MIME type of the file, as well as its location, define whether or not mod_cgi will be invoked to handle the request. The location must fall within a directory defined as ScriptAlias. When mod_cgi executes a CGI script, Apache forks a new process containing the interpreter for the CGI program. Since this is a course on Perl, it means that the Perl interpreter is loaded each time a CGI program is executed. The CGI program itself is compiled on each execution as well. Any database connections and other necessities are also created for each execution. Naturally, this forking, loading the interpreter, and compiling the program have a cost. On busy systems, the resource cost of running a CGI program can make the application unacceptably slow. Where mod_cgi must load the Perl interpreter with each invocation of the CGI script, mod_perl embeds the Perl interpreter directly into the Apache child process. This means that the Perl interpreter is loaded only once for each child process, rather than once for each execution of the CGI program. mod_perl uses a couple of methods to execute Perl scripts. One of these methods compiles the program only once, when it is first used.
mod_perl is just one method for speeding up execution of CGI programs. Another method is by using the FastCGI module. To find out more about FastCGI, visit FastCGI.