Skip to content

std.http

http/1.1 client and server bits over std.net; no tls; one request per connection

pub HttpError :: errors {
bad_url,
tls_unsupported,
unknown_host,
connect_failed,
send_failed,
recv_failed,
bad_response,
bad_request,
too_many_redirects,
}
  • bad_url: not an http:// URL with a host
  • tls_unsupported: https://
  • unknown_host: the name has no address
  • recv_failed: the connection failed, or closed in the middle
  • bad_response: what came back is not HTTP/1.x
  • bad_request: what came in is not an HTTP request
  • too_many_redirects: more than 5
pub Header :: struct {
name: []const u8,
value: []const u8,
}

name case kept as written; lookups ignore case

pub Response :: struct {
status: u16,
reason: []const u8,
headers: []const Header,
body: []const u8,
}
pub header :: fn(self: *Self, name: []const u8) -> ?[]const u8

case-insensitive, first match; null if none

pub Request :: struct {
method: []const u8,
target: []const u8,
headers: []const Header,
body: []const u8,
}
  • target: the path and the query: “/search?q=x”
pub header :: fn(self: *Self, name: []const u8) -> ?[]const u8
pub path :: fn(self: *Self) -> []const u8

target without the query

pub query :: fn(self: *Self) -> []const u8

after the ?, “” if none; parse with url::query_pairs

pub get :: fn(a: *Arena, link: []const u8) -> Response ! HttpError

follows redirects

pub request :: fn(a: *Arena, method: []const u8, link: []const u8, headers: []const Header, body: []const u8) -> Response ! HttpError

in a; up to 5 redirects (303, POST 301/302 become GET); unpacks gzip/deflate

pub read_request :: fn(a: *Arena, conn: *net::TcpStream) -> Request ! HttpError

request line, headers, body (by length or chunked), in a

pub reason_phrase :: fn(status: u16) -> []const u8

standard reason phrase; “Status” if unknown

pub respond :: fn(conn: *net::TcpStream, status: u16, content_type: []const u8, body: []const u8) -> void ! HttpError

whole response: status, content type, length, Connection: close, body

pub respond_with :: fn(conn: *net::TcpStream, status: u16, content_type: []const u8, headers: []const Header, body: []const u8) -> void ! HttpError

respond plus your own headers; .send_failed if the head exceeds 1024 bytes