HTTP/1.0 200 OK
Content-Type: text/plain
After adding a newline, you can start printing the content. A more
complete response includes the date timestamp and server type. For
example:
HTTP/1.0 200 OK
Date: Tue, 10 Apr 2001 03:01:36 GMT
Server: Apache/1.3.19 (Unix) mod_perl/1.25
Content-Type: text/plain
To notify clients that the server is configured with
KeepAlive Off, clients must be told that the
connection will be closed after the content has been delivered:
Connection: close
$r->content_type('text/plain');
is the same as:
$r->header_out("Content-Type" => "text/plain");
but additionally sets some internal flags used by Apache. Whenever
special-purpose methods are available, you should use those instead
of setting the header directly.
A typical handler looks like this:
use Apache::Constants qw(OK);
$r->content_type('text/plain');
$r->send_http_header;
return OK if $r->header_only;
To be compliant with the HTTP protocol, if the client issues an HTTP
HEAD request rather than the usual
GET, we should send only the HTTP header, the
document body. When Apache receives a HEAD
request, header_only( ) returns true. Therefore,
in our example the handler returns immediately after sending the
headers.
In some cases, you can skip the explicit content-type setting if
Apache figures out the right MIME type based on the request. For
example, if the request is for an HTML file, the default
text/html will be used as the content type of
the response. Apache looks up the MIME type in the
mime.types file. You can always override the
default content type.
There is no free lunch—you get the mod_cgi behavior at the
expense of the small but finite overhead of parsing the text that is
sent. Note that mod_perl makes the assumption that individual headers
are not split across print( )statements.
The Apache::print( ) routine must gather up the
headers that your script outputs in order to pass them to
$r->send_http_header. This happens in
src/modules/perl/Apache.xs (print(
)) and Apache/Apache.pm
(send_cgi_header( )). There is a shortcut in
there—namely, the assumption that each print(
)statement contains one or more complete headers. If, for
example, you generate a Set-Cookie header using
multiple print( )statements, like this:
print "Content-type: text/plain\n";
print "Set-Cookie: iscookietext\; ";
print "expires=Wednesday, 09-Nov-1999 00:00:00 GMT\; ";
print "path=\/\; ";
print "domain=\.mmyserver.com\; ";
print "\n\n";
print "Hello";
the generated Set-Cookie header is split over a
number of print( )statements and gets lost. The
above example won't work! Try this instead:
my $cookie = "Set-Cookie: iscookietext\; ";
$cookie .= "expires=Wednesday, 09-Nov-1999 00:00:00 GMT\; ";
$cookie .= "path=\/\; ";
$cookie .= "domain=\.mmyserver.com\; ";
print "Content-type: text/plain\n",
print "$cookie\n\n";
print "Hello";
Using special-purpose cookie generator modules (for example,
Apache::Cookie or CGI::Cookie)
is an even cleaner solution.
Sometimes when you call a script you see an ugly
"Content-Type: text/html" displayed
at the top of the page, and often the HTML content
isn't rendered correctly by the browser. As you have
seen above, this generally happens when your code sends the headers
twice.