简体   繁体   中英

printing multiple HTTP headers in Perl

I am trying to run a legacy Perl web application based on TetraBB under Apache2 on Debian.

This application uses print to output HTTP headers, eg print "Content-type: text/html\\n"; .

However, it seems that after the first print , no further headers are accepted. In this example: print "A\\n"; print "B\\n"; print "\\n"; print "A\\n"; print "B\\n"; print "\\n"; , header A is recognized by the browser as a response header, while B ends up at the top of the response HTML.

Writing print "A\\nB\\n\\n"; works.

Also, if the first print does not contain \\n\\n , a 500 Internal Server Error is generated.

What is causing this behavior and what can I do to make this work?

There is no difference between

print "Content-Type: text/html\n";
print "Set-Cookie: ...\n";
print "\n";

and

print "Content-Type: text/html\nSet-Cookie: ...\n\n";

unless you changed something that specifically makes them different (eg $\\ ). In fact, if you haven't enabled auto-flushing for STDOUT, the output will accumulate in a buffer to be sent in 4 or 8 KiB chunks to the pipe. This means there's absolutely no detectable difference to the other end of the pipe either.

You're actually outputting CGI headers , which the server then translates into response headers with the correct line endings. The CGI headers stop after the first double newline. Don't print two consecutive newlines until you are ready to terminate the headers, and do print them when you need to end the headers:

print <<HEADERS;
Content-type: text/plain
X-Some-Other: header

This is the body
HEADERS

Some servers have limits on the number of headers and on the length of header lines. Your error log may be able to say more about that.

To be any more useful than that, we need to see some actual code. A short program that demonstrates the problem would probably help you figure this out.

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