thrift_runtime_http (thrift_runtime v0.7.0)

View Source

The bytes-level HTTP/1.1 subset the http transport needs.

A Thrift message rides as an HTTP request/response body: the client wraps one encoded message in a POST per call, the server answers one response per request, and the connection stays open for the next exchange (HTTP/1.1 keep-alive). Only the pieces this codebase speaks are implemented — Content-Length framing; no chunked transfer, no header continuations, no trailer sections.

The parse side follows the streaming contract of the protocol codecs: more while the bytes are a valid prefix, a tagged error once they cannot become a valid exchange. Header names are compared case-insensitively (stored lowercase); a header block is capped at 16 KiB, the body at the caller's size limit (max_frame_size plays that role on both ends).

Summary

Functions

Splits one HTTP request off the front of Buffer.

Splits one HTTP response off the front of Buffer.

Builds the client's POST for one message body.

Builds a server response with the given status code.

Types

headers()

-type headers() :: [{binary(), binary()}].

request()

-type request() :: #{method := binary(), path := binary(), headers := headers(), body := binary()}.

response()

-type response() :: {StatusCode :: 100..599, headers(), Body :: binary()}.

Functions

extract_request(Buffer, MaxBody)

-spec extract_request(binary(), MaxBodySize :: pos_integer()) ->
                         {ok, request(), Leftover :: binary()} | more | {error, term()}.

Splits one HTTP request off the front of Buffer.

Answers the parsed request with the bytes after it. Anything but POST answers {error, not_post} — the HTTP server replies 405 to that; a body longer than MaxBodySize is refused without waiting for it.

extract_response(Buffer, MaxBody)

-spec extract_response(binary(), MaxBodySize :: pos_integer()) ->
                          {ok, response(), Leftover :: binary()} | more | {error, term()}.

Splits one HTTP response off the front of Buffer.

Answers {StatusCode, Headers, Body} with the bytes after it.

request(Host, Path, ExtraHeaders, Body)

-spec request(binary(), binary(), headers(), iodata()) -> iodata().

Builds the client's POST for one message body.

Host is the Host: header value; ExtraHeaders land after the built-in Content-Type.

response(Code, ExtraHeaders, Body)

-spec response(100..599, headers(), iodata()) -> iodata().

Builds a server response with the given status code.

Content-Type is application/x-thrift; ExtraHeaders land between it and the built-in Content-Length.