Files
kryon/tests/http_transfer_server.py
T
waozi f12a5f1d65 Report the HTTP status of a failed download, also after a redirect
A download that failed on an HTTP error said only "download failed".
The curl header reader now parses each response's status and keeps the
final one: a curl --fail exit (22) records a 4xx or 5xx status in
http_status, including a 404 reached through a redirect. Only a 2xx
response with an unambiguous Content-Length supplies a total, as before.
The test server gains /redirect-missing, the header table checks each
status, and the test works in build/scratch instead of the system temp.

Local tests: file_download.
2026-11-23 04:23:10 -03:00

71 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""A local HTTP server for the http_transfer tests; prints its port."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import sys
import time
class Handler(BaseHTTPRequestHandler):
def log_message(self, *arguments):
pass
def answer(self, status, body):
self.send_response(status)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', str(len(body)))
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Headers', 'Authorization, X-Test, Content-Type')
self.end_headers()
self.wfile.write(body)
def do_OPTIONS(self):
self.answer(204, b'')
def do_GET(self):
if self.path == '/hello':
self.answer(200, b'hello world')
elif self.path == '/binary':
self.answer(200, b'\x00\x01\xff\x00z')
elif self.path == '/big':
self.answer(200, b'x' * 100000)
elif self.path == '/slow':
time.sleep(3)
self.answer(200, b'late answer')
elif self.path == '/redirect-missing':
self.send_response(302)
self.send_header('Location', '/missing')
self.send_header('Content-Length', '0')
self.end_headers()
elif self.path == '/redirect-stream':
self.send_response(302)
self.send_header('Location', '/stream')
self.send_header('Content-Length', '3')
self.end_headers()
self.wfile.write(b'old')
elif self.path == '/stream':
self.send_response(200)
self.send_header('Content-Length', '100000')
self.end_headers()
try:
for _ in range(100):
self.wfile.write(b'x' * 1000)
self.wfile.flush()
time.sleep(.01)
except (BrokenPipeError, ConnectionResetError):
pass
else:
self.answer(404, b'missing')
def do_POST(self):
length = int(self.headers.get('Content-Length') or 0)
body = self.rfile.read(length)
reply = '%s|%s|%s|%s' % (self.headers.get('Authorization'), self.headers.get('X-Test'),
self.headers.get('Content-Type'), body.decode())
self.answer(201, reply.encode())
server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
print(server.server_address[1], flush=True)
sys.stdout.close()
server.serve_forever()