sigildocs

(sigil http server)

(sigil http server) - HTTP Server Implementation

Provides the HTTP server with support for:

  • Non-blocking I/O via coroutines
  • Multiple concurrent connections
  • Streaming responses

Exports

http-serverprocedure

HTTP Server Record

http-server?procedure

Test if a value is a http-server struct.

Get the handler field of a http-server struct.

Get the port field of a http-server struct.

Get the host field of a http-server struct.

Get the backlog field of a http-server struct.

Get the timeout field of a http-server struct.

Get the max-request-size field of a http-server struct.

Get the socket field of a http-server struct.

Get the running field of a http-server struct.

Get the clients field of a http-server struct.

Get the clients field of a http-server struct.

http-clientprocedure

Client connection state

http-client?procedure

Test if a value is a http-client struct.

Get the socket field of a http-client struct.

Get the buffer field of a http-client struct.

Get the headers-complete field of a http-client struct.

Get the content-length field of a http-client struct.

Get the body-bytes-read field of a http-client struct.

Get the request field of a http-client struct.

Get the response-started field of a http-client struct.

Get the created-at field of a http-client struct.

Create a new HTTP server.

The handler is called for each request and should return a response.

(define server
  (make-http-server
    (lambda (req) (response body: "Hello!"))
    port: 3000
    host: "127.0.0.1"))

(http-server-start server)

Options:

  • port: - Port to listen on (default: 8080)
  • host: - Address to bind to (default: "0.0.0.0")
  • backlog: - Listen queue size (default: 128)
  • timeout: - Request timeout in milliseconds (default: 30000)
  • max-request-size: - Maximum request body size in bytes (default: 10MB)

Check if server is currently running.

Returns #t if the server has been started and not stopped.

(define srv (make-http-server handler))
(http-server-running? srv)  ; => #f

Start the server (blocking - runs event loop).

Binds to the configured host and port, then enters the event loop. Does not return until the server is stopped.

An outer guard wraps server-loop so that any exception escaping the inner per-operation guards (e.g. from await-readable, scheduler tick internals, or other code paths not covered by process-connections guards) is caught, logged, and the loop is restarted with the current server state. This prevents a single runtime error in the loop machinery from killing the whole process — the cost is a log line and one restart tick rather than a crash + external supervisor respawn.

Stop the server gracefully.

Closes the listening socket and all active client connections.

Process one round of I/O (for custom event loops).

server-loopprocedure

Main server loop

When running inside a channel-run context, cooperates with other tasks via await-readable. Otherwise uses traditional socket-select.

Process connections using socket-select

Accept a new client connection

Process clients that have readable data

Handle data from a client Returns updated client or #f to remove

Try to parse request from buffer and handle it Returns updated client, or #f to remove client

Continue reading body data

Finalize and handle the complete request

Parse just the headers, returning (method path query version . headers-dict) or #f on error

Find end of headers (rnrn)

Parse a complete HTTP request buffer using the same byte-counted framing path as the live server. Returns #f for incomplete or invalid requests. This is intentionally small and useful for regression tests around Content-Length byte accounting.

Normalize request buffers to bytevectors so Content-Length is compared against bytes, not decoded string characters. HTTP headers are ASCII, so Latin-1 conversion preserves old string handler behavior.

Parse header lines into dict with lowercase keyword keys

Handle a complete request

send-responseprocedure

Send a response to socket.

Uses a write loop to handle partial writes from non-blocking sockets. socket-write may return fewer bytes than requested, so we loop until the full data is sent. Converts strings to bytevectors for correct byte-offset tracking (socket-write returns bytes written, not characters).

Send an error response

http-servevariable

Convenience function: create and start server in one call.

(http-serve (lambda (req) (response body: "Hello!")) port: 8080)