R – How to get a header that will work with IIS 6, 7 and Apache

apachecgiiis-6iis-7perl

I am trying to get a header that will work with Apache, IIS 6, and IIS 7. I won't go into the reason for that here. Let's just say that it's not as easy as I thought it would be 🙂

Anyway, the problem has something to do with NPH. In our code (originally written for IIS 6) we have

use CGI qw(:standard);

print "HTTP/1.0 200 OK\n";

print header;

at the top of every cgi script; I read that this is how you tell IIS that you want NPH.

Apache uses the filename to see if the output is nph (nph- must be the beginning of the filename) so what I did (which works in both IIS 6 and Apache) is the following:

use CGI qw(:standard);
print header('text/html', '200 OK');

IIS 7, interestingly, seems to require NPH, so if I don't either do

use CGI qw(:standard -nph);

or

print "HTTP/1.0 200 OK\n";

print header('text/html', '200 OK'); #parameters are apparently optional 

the browser tries to do something weird with the file, since it doesn't get the mimetype.

Also note: IIS 6 and 7 are ok without printing any header at all, but Apache doesn't like that.

Anyway, the best thing right now would be to make

use CGI qw(:standard);
print header('text/html', '200 OK');

somehow work in IIS 7. Does anyone know how I can do that? I don't know all the details for our server configuration, but if you tell me how to get any details you might need, I can do that.

Thanks either way!

Best Answer

I'd just create a subroutine that does the right thing depending on the server. You know what you have to do in each case, so just do that in that case.

The other option is to patch CGI.pm to set its $CGI::NPH variable correctly by looking at the server type. CGI.pm already has the basics there. Once you fix that, submit a patch.

Good luck :)