简体   繁体   中英

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?

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). . It appears you should implement a Response Filter. 看来您应该实现一个响应过滤器。 Apache2::Filter::HTTPHeadersFixup is one that does exactly what you asked for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM