plasticinsect
plasticinsect

Reputation: 1752

Force HTTP response with no Content-type?

Is there some way to deliberately force a Perl CGI script to return an HTTP response with no Content-type header? This is for testing a diagnostic crawler that looks for various malformed headers.

The closest I have gotten is this:

#!/usr/bin/perl
print "Status: 200 OK\r\n";
print "Content-type:\r\n";
print "\r\n";
print "Message body goes here.\r\n";

This generates the following response:

HTTP/1.1 200 OK
Date: Mon, 13 Jan 2014 23:17:23 GMT
Server: Apache/2.2.14 (Ubuntu)
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: 
Content-Length: 25

Message body goes here.

But the content-type is still defined, it just has a value of "". I really need to generate a response that has no content-type at all.

When I remove the line that prints the content-type:

#!/usr/bin/perl
print "Status: 200 OK\r\n";
print "\r\n";
print "Message body goes here.\r\n";

Then the header gets added automatically:

HTTP/1.1 200 OK
Date: Mon, 13 Jan 2014 23:19:00 GMT
Server: Apache/2.2.14 (Ubuntu)
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/plain
Content-Length: 25

Message body goes here.

Is there some way to bypass this behavior? Ideally I would like to do this as a Perl script running on a normally-configured Apache server. If that isn't possible, is there some other way, maybe using a different language/server/configuration?

Upvotes: 2

Views: 977

Answers (1)

David-SkyMesh
David-SkyMesh

Reputation: 5171

No, the CGI interface requires that the Content-type header is set, so any web server software that implements CGI correctly will give a 500 - Internal Server Error.

You'll actually have to implement your own HTTP server (or modify one) to get what you want.

That might be possible within Apache by implementing some mod_perl handler that runs after the Apache Response Handler (not sure one exists, or possibly by stacking handlers).. edit: It appears you should implement a Response Filter. Apache2::Filter::HTTPHeadersFixup is one that does exactly what you asked for.

Upvotes: 2

Related Questions