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 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.

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 header lines into dict with lowercase keyword keys

Handle a complete request

send-responseprocedure

Send a response to socket

Send an error response

http-servevariable

Convenience function: create and start server in one call.

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