This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients martin.panter, vstinner, 신동원
Date 2015-11-04.02:24:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1446603856.11.0.338109393217.issue25539@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-11-04 02:24:16martin.pantersetrecipients: + martin.panter, vstinner, 신동원
2015-11-04 02:24:16martin.pantersetmessageid: <1446603856.11.0.338109393217.issue25539@psf.upfronthosting.co.za>
2015-11-04 02:24:15martin.panterlinkissue25539 messages
2015-11-04 02:24:14martin.pantercreate