sockets + portable HTTP server — 6 primitives, same server runs in all 3
Added tcp-listen/accept/connect/recv/send/close to Python, C, and asm. One examples/http-server.lsp runs identically in all three impls and serves HTTP/1.0 with routing, content-type, and content-length headers. asm additions: - SYS_SOCKET/BIND/LISTEN/ACCEPT/CONNECT/SETSOCKOPT syscalls - 6 tcp-* builtins using the existing port encoding (SPECIAL ≥ 1000) - bi_tcp_connect: dotted-quad IPv4 parser, no DNS dependency Defects fixed along the way (surfaced by the HTTP server): - string-append: was 2-arg only; now variadic (walks arg list twice) - number->string: was stubbed to VAL_VOID; now correctly writes digits into a heap-allocated string (incl. negative handling) - String-literal reader: \r and \0 escape sequences now handled (was silently dropping backslash, treating them as literal 'r' / '0') - tcp_accept: sockaddr buffer was 8 bytes, now 16 (was corrupting caller's stack when accept wrote full struct sockaddr_in) Pinocchio benchmark (tests/web-benchmark.sh): At concurrency=20, 1000 requests, serving a 1KB body: uncommonlisp Python 373 req/s uncommonlisp C 370 req/s uncommonlisp asm 370 req/s python3 http.server 381 req/s (stdlib reference) busybox httpd 382 req/s (production reference) All five converge within 3% — the client (curl fork/exec) is the bottleneck, not the server. Our single-threaded blocking servers are indistinguishable from battle-tested ones at this load. Binary sizes: uncommonlisp asm 45 KB (HTTP + everything else) busybox httpd 2.1 MB (multi-call binary) python3 8 MB (interpreter) The asm HTTP server is 46× smaller than busybox and 176× smaller than Python, serves from 7 Linux syscalls, and the entire protocol handler is 70 lines of portable Scheme. Test counts: 132 asm (up 1), rest unchanged. All green.
This commit is contained in:
parent
5ce42a7089
commit
bfd4ec7ec8
8 changed files with 762 additions and 89 deletions
90
examples/http-server.lsp
Normal file
90
examples/http-server.lsp
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
;;; http-server.lsp — portable HTTP/1.0 server in pure Scheme
|
||||
;;;
|
||||
;;; Runs identically in Python, C, and asm. The only primitives used are
|
||||
;;; the six socket builtins + display/string-append/substring.
|
||||
;;;
|
||||
;;; python3 uncommonlisp.py --fast examples/http-server.lsp
|
||||
;;; ./c/uncommonlisp examples/http-server.lsp
|
||||
;;; ./asm/uncommonlisp < examples/http-server.lsp
|
||||
;;;
|
||||
;;; Default port 8080. First-line dispatch: GET / → greeting page.
|
||||
;;; GET /bench → 1 KB body for throughput benchmarks.
|
||||
;;; Other paths → 404.
|
||||
|
||||
(define *port* 8080)
|
||||
(define *crlf* "\r\n")
|
||||
(define *crlf-crlf* "\r\n\r\n")
|
||||
|
||||
;;; ── HTTP helpers ─────────────────────────────────────────────
|
||||
|
||||
(define (http-response status ctype body)
|
||||
(string-append
|
||||
"HTTP/1.0 " status *crlf*
|
||||
"Content-Type: " ctype *crlf*
|
||||
"Content-Length: " (number->string (string-length body)) *crlf*
|
||||
"Connection: close" *crlf-crlf*
|
||||
body))
|
||||
|
||||
;; Compare via char->integer so we don't need char=? (asm lacks it).
|
||||
(define SPACE 32)
|
||||
|
||||
(define (char-at s i) (char->integer (string-ref s i)))
|
||||
|
||||
(define (first-token s)
|
||||
(let ((len (string-length s)))
|
||||
(let loop ((i 0))
|
||||
(cond
|
||||
((= i len) s)
|
||||
((= (char-at s i) SPACE) (substring s 0 i))
|
||||
(else (loop (+ i 1)))))))
|
||||
|
||||
(define (second-token s)
|
||||
(let ((len (string-length s)))
|
||||
(let loop1 ((i 0))
|
||||
(cond
|
||||
((= i len) "")
|
||||
((= (char-at s i) SPACE)
|
||||
(let loop2 ((j (+ i 1)))
|
||||
(cond
|
||||
((= j len) (substring s (+ i 1) len))
|
||||
((= (char-at s j) SPACE) (substring s (+ i 1) j))
|
||||
(else (loop2 (+ j 1))))))
|
||||
(else (loop1 (+ i 1)))))))
|
||||
|
||||
;;; ── Request handler ─────────────────────────────────────────
|
||||
|
||||
(define *bench-body*
|
||||
;; ~1 KB payload so clients have something to measure throughput on.
|
||||
(let loop ((s "") (i 0))
|
||||
(if (= i 32) s
|
||||
(loop (string-append s "0123456789abcdef0123456789abcdef") (+ i 1)))))
|
||||
|
||||
(define (handle-request req)
|
||||
(let ((path (second-token req)))
|
||||
(cond
|
||||
((string=? path "/")
|
||||
(http-response "200 OK" "text/html"
|
||||
"<!doctype html><title>uncommonlisp</title><h1>feedback is all you need</h1><p>portable HTTP in Scheme.</p>"))
|
||||
((string=? path "/bench")
|
||||
(http-response "200 OK" "text/plain" *bench-body*))
|
||||
((string=? path "/hello")
|
||||
(http-response "200 OK" "text/plain" "hello world\n"))
|
||||
(else
|
||||
(http-response "404 Not Found" "text/plain"
|
||||
(string-append "not found: " path "\n"))))))
|
||||
|
||||
;;; ── Main loop ───────────────────────────────────────────────
|
||||
|
||||
(define (serve)
|
||||
(let ((server (tcp-listen *port*)))
|
||||
(display "uncommonlisp http server on :") (display *port*) (newline)
|
||||
(let loop ()
|
||||
(let ((client (tcp-accept server)))
|
||||
(let ((req (tcp-recv client 4096)))
|
||||
(if (and req (> (string-length req) 0))
|
||||
(tcp-send client (handle-request req))
|
||||
#f))
|
||||
(tcp-close client))
|
||||
(loop))))
|
||||
|
||||
(serve)
|
||||
Loading…
Add table
Add a link
Reference in a new issue