Skip to content

BaseHTTPRequestHandler incorrectly expects headers from HTTP/0.9 requests #139492

Closed as duplicate

Description

@Carmina16

Bug report

Bug description:

The HTTPServer class seems to have issues with pre-HTTP 1.0 type requests. The request is not passed to the GET handler until the client closes the connection.

Maybe the BaseHTTPRequestHandler tries to read non-existent headers or wants an empty line in the request?

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Activity

  1. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    Can you show the server and client code? We need a reproducer to understand your issue.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Oct 2, 2025
  3. Carmina16 commented on Oct 2, 2025

    @Carmina16
    Author
    from http.server import *
    from sys import stderr, stdout
    
    class MyHandler(BaseHTTPRequestHandler):
    	def do_GET(self):
    		print("Request:",repr(self.requestline),file=stderr)
    		return
    
    srv=HTTPServer(
    	RequestHandlerClass=MyHandler,
    	server_address=('',8080)
    	)
    srv.serve_forever()
  4. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    What is the request you are sending? Make sure that request is a well-formed HTTP/0.9 as well

  5. Carmina16 commented on Oct 2, 2025

    @Carmina16
    Author

    Sending GET /foo\r\n and not closing connection afterwards should work.

  6. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    HTTP/0.9 is a one-line protocol. The connection is terminated after the response has been sent. See https://www.w3.org/Protocols/HTTP/AsImplemented. So it's working as intended.

  7. removed
    type-bugAn unexpected behavior, bug, or error
    pendingThe issue will be closed if no feedback is provided
    on Oct 2, 2025
  8. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    Btw, if you need to keep alive your connection, you need to use HTTP/1.1.

  9. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    Sending GET /foo\r\n and not closing connection afterwards should work.

    I'm not sure whether you meant the connection on the client or the server, but note that it's possible to see a hang if you use echo. For instance echo "GET /index.html\r\n"| nc 127.0.0.1 8080 would make it hang but not with echo -e "GET /index.html\r\n" | nc 127.0.0.1 8080 and that's because the client request is not completed

  10. Carmina16 commented on Oct 2, 2025

    @Carmina16
    Author

    The connection is terminated after the response has been sent.

    But it is not terminated. The server hangs (probably misreading the request) until the client aborts the connection, then the request is finally processed but obviously there is nowhere to send the response.

  11. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    Ah! I see what you mean. Sorry for misunderstanding the issue. Indeed, it's stuck in http.client.parse_headers.

  12. reopened this on Oct 2, 2025
  13. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 2, 2025
  14. self-assigned this
    on Oct 2, 2025
  15. changed the title [-]HttpServer hangs on HTTP 0.9 requests[/-] [+]`BaseHTTPRequestHandler` incorrectly expects headers from HTTP/0.9 requests[/+] on Oct 2, 2025
  16. picnixz commented on Oct 2, 2025

    @picnixz
    Member

    Actually this is a duplicate of #70765. I'll just attach that issue instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions