2

I'm getting an Internal Server error when printing this:

enter image description here

This is my code (python):

print "HTTP/1.0 200 OK\n\r",
print "Content-Type: text/html\n\r",
print "\n\r", 

The error log says:

malformed header from script 'bad_req.py': Bad header: HTTP/1.0 200 OK

I'm researched the issue and can't find any solutions. Any help would be really appreciated.

2 Answers2

2

Yes, wrong end of lines definitely.

You should have "\r\n", but also, you do not have to worry about that if you're using script as CGI.

You can use only "\n" and everything will work.

Also, I don't think Apache will let you choose protocol for it to use and thus you shouldn't use your first line:

print "HTTP/1.0 200 OK"
  1. 200 OK is a status that will be returned back on a success anyway

  2. If you really do need to change status, use Status HTTP header, and Apache will adapt to it:

    print "Status: 400 Forbidden"

Your script should look like:

# If you want status :D
print "Status: 200 Some nasty extra status"
print "Content-Type: text/html\n"
# End of headers
print "<h1>Some HTML here</h1>"

Note that I used "\n" only on last header, and nowhere else.

That is because print adds "\n" automatically unless you tell it otherwise, but last header have to be separated from the body of document with two new lines "\r\n\r\n", that is why you should use it only on last header. It indicates end of headers.

Or you can do:

print "Content-Type: text/html"
print

Print with no args will print just "\n" and, as I said before, Apache will interpret them correctly as "\r\n"

In your case your headers looked like this repr:

"""
HTTP/1.0 200 OK

\rContent-Type: text/html

\r
\r

"""

Which is obviously a wrong header. :D

If you aren't using script as CGI rules may differ but only slightly.

If you have to enforce HTTP/1.0 behaviour then use header Connection header:

print "Connection: close"

although whether a connection will be closed remains upon the client and Apache's timeout. I am not sure whether the protocol will be changed to HTTP 1.0. Default is 1.1

Dalen
  • 4,128
  • 1
  • 17
  • 35
  • The 'status: 200 OK' is exactly what I needed, thanks! By the way ',' stops the newline from printing and unix ignores '\r' so it works, but your right, python adds a newline so both characters are redundant. –  Nov 25 '15 at 00:16
  • "," does this, but adds a " " space at the end instead. If you really want to control EOL yourself in Python 2.x, then use sys.stdout.write("something"). That won't introduce anything in your stream. Some terminals might show you \r, and you would be able to see it in editors if you save it to file. So, no, Unix doesn't ignore it, only some apps. And redundancy or not, as I said two new lines between headers will make only your first line being included as header. So use only print and \n to finish headers section. And everything will be cool. – Dalen Nov 25 '15 at 01:02
  • I didn't realise how ignorant I was was on the subject. Thanks. –  Nov 25 '15 at 03:18
1

You are ending lines in \n\r. (LF + CR).

According to HTTP1.0 page (Section 2.2):

HTTP/1.0 defines the octet sequence CR LF as the end-of-line marker for all protocol elements except the Entity-Body (see Appendix B for tolerant applications).

So you should end your lines in \r\n.

CristiFati
  • 38,250
  • 9
  • 50
  • 87