missing http headers in web server

Question:

Am using BaseHTTPRequestHandler http server and copy/pasted the code from the interwebs.
Here’s the part where the response/header is set

class S(BaseHTTPRequestHandler):    
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

But when calling the server with curl the response is:

curl: (1) Received HTTP/0.9 when not allowed

When calling through browser:

ERR_INVALID_HTTP_RESPONSE

protocol_version is http/1.0

The web server is called through nginx reverse proxy, which just does

   location / {
       proxy_http_version 1.1;
       proxy_pass  http://${NODE_NAME}:9000/;
   }

Are there more headers needed for this? How do we set correct http headers in BaseHTTPRequestHandler or nginx?

Asked By: Serve Laurijssen

||

Answers:

I am not familiar with BaseHTTPRequestHandler but I will try to help with the curl response as I had pretty similar issue few days ago.

Did you try to run curl with http flag set to 0.9?

curl 'http://your-domain.com' --http0.9

Maybe your server does respond with HTTP/0.9. Since curl 7.66.0, HTTP/0.9 is disabled by default so it could be a reason of "Received HTTP/0.9 when not allowed" response.

Answered By: Przemek Nowicki

Just sharing as a solution. I ran into the same issue, and ignoring this warning will result in some probe requestors not to run correctly. After some research, I found that the following order of response formation will fix the issue:

self.protocol_version = 'HTTP/1.0'
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-length", body_size)
self.end_headers()
self.wfile.write(bytes(item, "utf-8"))

The important part is to specify the body size in bytes – it is a protocol requirement from 1.0 on. After this error disappears and code starts to function as it should.

Answered By: Blade1024
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.