(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-requestprocedureHTTP Request Record Uses define-struct for immutability and functional updates via inherit
http-request?procedureTest if a value is a http-request struct.
http-request-methodprocedureGet the method field of a http-request struct.
http-request-pathprocedureGet the path field of a http-request struct.
http-request-queryprocedureGet the query field of a http-request struct.
http-request-versionprocedureGet the version field of a http-request struct.
http-request-headersprocedureGet the headers field of a http-request struct.
http-request-bodyprocedureGet the body field of a http-request struct.
http-request-contextprocedureGet the context field of a http-request struct.
http-request-headerprocedureCase-insensitive header lookup Headers are stored with lowercase keyword keys, so we normalize the lookup key
http-request-uriprocedureGet full URI (path + query)
http-request-content-lengthprocedureGet Content-Length as integer or #f
http-request-content-typeprocedureGet Content-Type or #f
http-request-context-refprocedureGet value from context dict Key should be a keyword
http-request-with-contextprocedureCreate new request with added context (functional update) Key should be a keyword
parse-methodprocedureParse HTTP method string to symbol
parse-request-lineprocedureParse request line: "GET /path?query HTTP/1.1" Returns (method path query version) or #f on error
parse-headersprocedureParse headers from list of lines Returns dict with lowercase keyword keys
read-line-from-socketprocedureRead a line from socket (up to CRLF or LF) Returns line without line ending, or #f on error, or 'eof
read-http-requestprocedureRead 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
hex-char-valueprocedureDecode a single hex character to its numeric value
url-decodeprocedureDecode a URL-encoded (percent-encoded) string Handles %XX sequences and + for spaces
parse-form-urlencodedprocedureParse URL-encoded form data (application/x-www-form-urlencoded) Returns alist with symbol keys: ((name . "value") (other . "value2"))
extract-multipart-boundaryprocedureExtract 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-dataprocedureParse 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-dataprocedureParse 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))