No-Parse Headers/Web Developer® Vol. 2 No. 3

No-Parse Headers

No-Parse Headers (NPHs) enable a script to attach its output stream directly to the socket that leads to the user's browser. An NPH script must generate all appropriate HTTP headers in the transaction, but it gives you the opportunity to stick in any additional or modified headers you want. (A side benefit is that NPH scripts feed out live, so after every <P>, <BR>, or--when using <PRE> tags--hard return, the user's browser immediately updates.)

There's a nice and detailed list of all possible HTTP headers masked in a bit of technospeak in the current HTTP 1.1 draft spec.

The general form goes something like:

   HTTP/1.0 [return code] [reason]
   Header: Contents
   Header: Contents
   [blank line with hard return]
   Contents
Here's a more elegant way to do redirects, where you feed:
   HTTP/1.0 304 Redirect
   Content-Type: text/html
   Location: URL
   [blank line with hard return]
Or in Perl:
   print "HTTP/1.0 304 Redirect\nContent-Type: text/html\nLocation:
   http://whatever\n\n";
This works for a wider variety of browsers than the alternative of sticking in a header following the real headers.

In the case of sending cookies out, if your intent is just to pass existing cookies and generate new ones only for fresh users, here's a code fragment. (Note that most servers come with a utility that'll take some arguments and generate the headers you need without having to calculate them out individually. If you don't like the one that comes with your server, consider making a library with various arguments to pass, so you can avoid reproducing this code over and over again.)

--- perl code for feeding out a cookie with any random file ---
# assume all variables have been parsed into the associated array %in
# &ctime(date) feeds out a hard return of its own
require 'ctime.pl';
$cookie = $ENV{'HTTP_COOKIE'};
if (!$cookie or $cookie !~ /visit\=/) {
	$cookie = $ENV{'REMOTE_ADDR'] . "." . date;
}
$file = $in{'html'};
print << EOF
HTTP/1.0 200 OK
Content-Type: text/html
Set-Cookie: $cookie
Expires: &ctime(date)
EOF;
open (IN, "< $file");
while () { print; }
close IN;
--- end perl code fragment ---
NPH scripts are remarkably simple to install on most servers that support the standard Common Gateway Interface or variants of it. For Netscape, CERN, NCSA, and many others, any script whose name begins with "nph-" will be executed as an NPH script. Gosh, occasionally it's easy.

You can also pass limited header information in tags that Netscape supports, but since only certain versions of Netscape Navigator will read them, and since you still need a script to drop in the unique cookie, it's better to use NPH.
--G.F.