Martin Panter added the comment:

Okay, now I understand the problem. There is a quirky header line with a space 
in the field name “P3P ”. The HTTP client’s parse_headers() treats this as any 
other header line, but then it passes the header to the email package, which 
interprets this line as being invalid and marking the end of the header. 
Therefore subsequent important header fields are missed, including Set-Cookie 
and Content-Length.

According to <https://tools.ietf.org/html/rfc7230#section-3.2>, that P3P line 
is technically not valid. Here is a self-contained demo or test case:

from socket import socket
from threading import Thread
from http.client import HTTPConnection

def serve():
    [client, _] = server.accept()
    with client, client.makefile("rb") as reader:
        while reader.readline().rstrip(b"\r\n"):
            pass
        client.sendall(
            b"HTTP/1.1 200 OK\r\n"
            b"Content-Length: 0\r\n"
            b"Extra-Space : invalid\r\n"
            b"Set-Cookie: name=value\r\n"
            b"\r\n"
        )

with socket() as server:
    server.bind(("localhost", 0))
    server.listen()
    background = Thread(target=serve)
    background.start()
    http = HTTPConnection(*server.getsockname())
    http.request("GET", "/")
    response = http.getresponse()
    print(response.msg.items())  # Set-Cookie is missing
    http.close()
    background.join()

The question is, should Python go out of its way to handle this server bug? It 
would probably require implementing a more permissive version of the header 
parser in the HTTP client, rather than reusing the stricter “email” module’s 
parser.

----------
versions: +Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25539>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to