Files
kryon/tests/http_transfer_server.py
T
waozi c2755c9d14 Add HTTP transfers and raylib secondary windows
http_transfer runs HTTP requests while the application keeps drawing:
curl on a detached worker thread on desktops, the activity's transport on
Android, and fetch through the web bridge in the browser. A cancelled
transfer never writes into the caller's buffer, answers that do not fit
fail instead of arriving cut, and only http and https URLs are accepted.
Native and headless-browser tests run against a local server.

raylib_secondary_window gives a raylib application on SDL up to three
extra windows, each drawing its own Kryon tree into a render texture that
is copied to the window, so the main OpenGL context stays current. Its
probe runs on a private Xvfb display.

Both come from Inner Breeze.
2026-10-11 10:11:17 -03:00

47 lines
1.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 == '/big':
self.answer(200, b'x' * 100000)
elif self.path == '/slow':
time.sleep(3)
self.answer(200, b'late answer')
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()