Skip to content

Instantly share code, notes, and snippets.

@wfrisch
Created January 24, 2024 11:26
Show Gist options
  • Save wfrisch/bc00bfa049f2aab76dbb73215b1f5bb5 to your computer and use it in GitHub Desktop.
Save wfrisch/bc00bfa049f2aab76dbb73215b1f5bb5 to your computer and use it in GitHub Desktop.
HTTP server that sends incomplete chunks, triggering ChunkedEncodingError in python-requests clients. Reproducer for https://github.com/thp/urlwatch/issues/725
#!/usr/bin/env python3
"""
Minimal HTTP server that sends incomplete chunks,
triggering ChunkedEncodingError in python-requests clients.
Reproducer for https://github.com/thp/urlwatch/issues/725
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Start sending a response with chunked encoding
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.send_header('Transfer-Encoding', 'chunked')
self.end_headers()
# Send a part of the response, but not the entire content
self.wfile.write(b"8\r\n")
self.wfile.write(b"This is a \r\n")
# Not sending the last chunk and closing the connection should
# trigger ChunkedEncodingError in the client
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler,
port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"Starting faulty HTTP server on port {port}...")
httpd.serve_forever()
if __name__ == '__main__':
run()
# vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment