HTTP
Source: std/http.mw.
The client speaks HTTP/1.1 and HTTP over TLS (see tls.mw); the server side gives you request parsing and reply helpers to build a small web server on top of http_accept.
HTTP/1.1 client and server on top of std/net.mw.
Server loop:
var srv: i64 = http_listen(8080);while 1 == 1 { var conn: *HttpConn = http_accept(srv); var req: *HttpMessage = http_read_request(conn); http_reply_text(conn, 200, "hello\n"); http_conn_close(conn);}Client (http:// and https://; https verifies the certificate chain and the host name against the system trust store, see http_set_roots):
var res: *HttpMessage = http_get("https://example.com/");Messages, connections and URLs are heap allocated; use the *_free functions.
HttpConn
Section titled “HttpConn”struct HttpConn { fd: i64; buf: *u8; len: i64; pos: i64; keep_alive: i64; tls: *Tls;}HttpMessage
Section titled “HttpMessage”struct HttpMessage { method: *u8; // requests target: *u8; // requests: path and query, as sent version: *u8; status: i64; // responses reason: *u8; headers: *VecPtr; // name, value, name, value, ... body: *u8; // always NUL terminated body_len: i64;}struct Url { scheme: *u8; host: *u8; port: i64; path: *u8; // path and query, always starts with '/'}Functions
Section titled “Functions”http_conn_new
Section titled “http_conn_new”fn http_conn_new(fd: i64) -> *HttpConnhttp_conn_write
Section titled “http_conn_write”fn http_conn_write(c: *HttpConn, data: *u8, n: i64) -> i64Writes all bytes (through TLS when the connection has it); 0 on success.
http_conn_close
Section titled “http_conn_close”fn http_conn_close(c: *HttpConn)http_conn_fill
Section titled “http_conn_fill”fn http_conn_fill(c: *HttpConn) -> boolRefills the buffer; false at end of stream or error.
http_conn_read_line
Section titled “http_conn_read_line”fn http_conn_read_line(c: *HttpConn, out: *String, limit: i64) -> boolReads one line (without the line terminator) into out. False if the stream ended first or the line is longer than limit.
http_conn_read_exact
Section titled “http_conn_read_exact”fn http_conn_read_exact(c: *HttpConn, dst: *u8, n: i64) -> boolCopies exactly n bytes (from the buffer, then the socket) into dst.
http_message_new
Section titled “http_message_new”fn http_message_new() -> *HttpMessagehttp_message_free
Section titled “http_message_free”fn http_message_free(m: *HttpMessage)http_header_add
Section titled “http_header_add”fn http_header_add(m: *HttpMessage, name: *u8, value: *u8)http_header_get
Section titled “http_header_get”fn http_header_get(m: *HttpMessage, name: *u8) -> *u8First header called name (case-insensitive), or null.
http_header_has_token
Section titled “http_header_has_token”fn http_header_has_token(m: *HttpMessage, name: *u8, token: *u8) -> boolhttp_set_body
Section titled “http_set_body”fn http_set_body(m: *HttpMessage, data: *u8, len: i64)http_read_headers
Section titled “http_read_headers”fn http_read_headers(c: *HttpConn, m: *HttpMessage) -> boolReads “Name: value” lines until the blank line.
http_read_chunked
Section titled “http_read_chunked”fn http_read_chunked(c: *HttpConn, m: *HttpMessage, max_body: i64) -> boolhttp_read_body
Section titled “http_read_body”fn http_read_body(c: *HttpConn, m: *HttpMessage, until_close: bool, max_body: i64) -> boolReads the body announced by the headers. until_close allows bodies that end when the peer closes the connection (responses only).
http_max_body
Section titled “http_max_body”fn http_max_body() -> i64http_read_request
Section titled “http_read_request”fn http_read_request(c: *HttpConn) -> *HttpMessageReads one request. Null when the client closed the connection or sent something malformed.
http_read_response
Section titled “http_read_response”fn http_read_response(c: *HttpConn, head_only: bool) -> *HttpMessageReads one response. head_only is for replies to HEAD requests.
http_reason
Section titled “http_reason”fn http_reason(status: i64) -> *u8http_reply
Section titled “http_reply”fn http_reply(c: *HttpConn, status: i64, content_type: *u8, extra_headers: *u8, body: *u8, body_len: i64) -> i64Sends a full response. extra_headers is raw text such as “Cache-Control: no-store\r\n” (or null). Content-Length and Connection are added automatically.
http_reply_text
Section titled “http_reply_text”fn http_reply_text(c: *HttpConn, status: i64, text: *u8) -> i64http_reply_html
Section titled “http_reply_html”fn http_reply_html(c: *HttpConn, status: i64, html: *u8) -> i64http_reply_json
Section titled “http_reply_json”fn http_reply_json(c: *HttpConn, status: i64, json: *u8) -> i64http_reply_redirect
Section titled “http_reply_redirect”fn http_reply_redirect(c: *HttpConn, status: i64, location: *u8) -> i64url_encode
Section titled “url_encode”fn url_encode(s: *u8) -> *u8Percent-encodes everything except unreserved characters (RFC 3986).
url_decode
Section titled “url_decode”fn url_decode(s: *u8, plus_is_space: bool) -> *u8Decodes %XX sequences; plus_is_space is for query strings and forms.
http_path
Section titled “http_path”fn http_path(target: *u8) -> *u8The path part of a request target (before ?), as a new string.
http_query_param
Section titled “http_query_param”fn http_query_param(target: *u8, name: *u8) -> *u8Value of query parameter name (decoded, new string) or null.
url_free
Section titled “url_free”fn url_free(u: *Url)url_parse
Section titled “url_parse”fn url_parse(url: *u8) -> *UrlParses http(s)://host[:port][/path?query]. Null if malformed.
http_listen
Section titled “http_listen”fn http_listen(port: i64) -> i64Listens on every interface.
http_accept
Section titled “http_accept”fn http_accept(listener: i64) -> *HttpConnWaits for a client; null on error. Reads time out after 15 seconds.
http_mime_for
Section titled “http_mime_for”fn http_mime_for(path: *u8) -> *u8http_serve_file
Section titled “http_serve_file”fn http_serve_file(c: *HttpConn, root: *u8, target: *u8) -> boolServes root + the request path. Paths containing “..” are refused, and “/” maps to index.html. Returns true if a file was sent.
http_last_error
Section titled “http_last_error”fn http_last_error() -> *u8Reason for the last failed client call.
http_set_roots
Section titled “http_set_roots”fn http_set_roots(roots: *VecPtr)http_roots
Section titled “http_roots”fn http_roots() -> *VecPtrhttp_request
Section titled “http_request”fn http_request(method: *u8, url: *u8, extra_headers: *u8, body: *u8, body_len: i64) -> *HttpMessageSends one request and reads the response. extra_headers is raw header text ending in “\r\n” (or null); body may be null. Handles http:// and https:// URLs.
http_get
Section titled “http_get”fn http_get(url: *u8) -> *HttpMessagehttp_post
Section titled “http_post”fn http_post(url: *u8, content_type: *u8, body: *u8, body_len: i64) -> *HttpMessagehttp_get_follow
Section titled “http_get_follow”fn http_get_follow(url: *u8, max_redirects: i64) -> *HttpMessageGET that follows up to max_redirects redirections (301, 302, 303, 307, 308).