sigildocs

(sigil http request)

(sigil http request) - HTTP Request Types and Parsing

Provides the <http-request> record and functions for parsing HTTP/1.1 requests from sockets.

Exports

http-requestprocedure

HTTP Request Record Uses define-struct for immutability and functional updates via inherit

http-request?procedure

Test if a value is a http-request struct.

Get the method field of a http-request struct.

Get the path field of a http-request struct.

Get the query field of a http-request struct.

Get the version field of a http-request struct.

Get the headers field of a http-request struct.

Get the body field of a http-request struct.

Get the context field of a http-request struct.

Look up a header value by name (case-insensitive).

Returns the header value as a string, or #f if not present.

(http-request-header req "Content-Type")  ; => "text/html"
(http-request-header req "X-Custom")      ; => #f

Get the full URI (path + query string).

(http-request-uri req)  ; => "/users?page=2"

Get Content-Length as an integer, or #f if not present.

Get Content-Type header value, or #f if not present.

Get a value from the request context dict.

The key should be a keyword. Returns #f if not found and no default given.

(http-request-context-ref req user-id:)          ; => 42
(http-request-context-ref req missing: "none")   ; => "none"

Create a new request with an added context entry (functional update).

(http-request-with-context req user-id: 42)
parse-methodprocedure

Parse HTTP method string to symbol

Parse an HTTP request line like "GET /path?query HTTP/1.1".

Returns a list (method path query version) or #f on error.

parse-headersprocedure

Parse headers from list of lines Returns dict with lowercase keyword keys

Read a line from socket (up to CRLF or LF) Returns line without line ending, or #f on error, or 'eof

Read an HTTP request from a socket.

Takes a socket, a line-reading function, and a byte-reading function. Returns an http-request record, or #f on error.

Decode a single hex character to its numeric value

url-decodeprocedure

Decode a URL-encoded (percent-encoded) string.

Handles %XX sequences and + for spaces.

(url-decode "hello%20world")  ; => "hello world"
(url-decode "a+b")           ; => "a b"

Parse URL-encoded form data (application/x-www-form-urlencoded).

Returns an alist with symbol keys.

(parse-form-urlencoded "name=Alice&age=30")
; => ((name . "Alice") (age . "30"))

Extract boundary from a Content-Type header.

Takes a Content-Type string like "multipart/form-data; boundary=----WebKitFormBoundary...". Returns the boundary string, or #f if not found.

Parse multipart/form-data body.

Takes the request body and boundary string. Returns alist with symbol keys, like parse-form-urlencoded. For file uploads, the value is a dict with filename:, content-type:, content:.

Example:

(let* ((boundary (extract-multipart-boundary (http-request-content-type req)))
       (params (parse-multipart-form-data (http-request-body req) boundary)))
  (assoc-ref 'name params))

Parse form data from request, auto-detecting format.

Handles both application/x-www-form-urlencoded and multipart/form-data. Returns alist with symbol keys.

Example:

(let ((params (parse-form-data req)))
  (assoc-ref 'username params))