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.

Case-insensitive header lookup Headers are stored with lowercase keyword keys, so we normalize the lookup key

Get full URI (path + query)

Get Content-Length as integer or #f

Get Content-Type or #f

Get value from context dict Key should be a keyword

Create new request with added context (functional update) Key should be a keyword

parse-methodprocedure

Parse HTTP method string to symbol

Parse request line: "GET /path?query HTTP/1.1" Returns (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 HTTP request from socket read-line-fn: (socket) -> string or #f or eof-object read-bytes-fn: (socket count) -> string or bytevector or #f Returns <http-request> 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

Parse URL-encoded form data (application/x-www-form-urlencoded) Returns alist with symbol keys: ((name . "value") (other . "value2"))

Extract boundary from 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))