Net.HTTP.Request

lib/Net/HTTP/Request.gob

Requests, as a server receives them and as a client sends them: a method, which carries the body, a target, and header fields.

Two types. A server's Request holds what arrived: the path and query it was sent to. A client's ClientRequest holds where to send it: a URL. They share Method and Headers, and HasHeaders builds either, but their targets differ, and a received request will carry what only a connection has, which a request being built has no value for.

Records. Both constructors are exported, and their fields are read and built directly, because each field's type holds its own invariant: a Method cannot put a body on a GET, and Headers cannot hold a field the library writes itself. No combination of valid fields is an invalid request.

type Request b = Request { method : Method b, target : RequestTarget, headers : Headers, control : Control, connection : ConnectionInfo }

A request a server received, routed by matching on it:

match (req.method, req.target.path) {
    (GET, "/health") -> ...
    (POST(body), "/orders") -> ...
    _ -> ...
}

control holds the values of the fields the library reads itself; the functions below read it. connection is what the server knows of the connection the request came on: its addresses, and over TLS the name the client asked for and the protocol chosen.

type Control = Control { contentLength : Option Int, upgrade : Array Token, trailers : Bool }

The values of a request's reserved fields, which headers cannot hold:

  • contentLength: the body's declared length, None for a chunked body or none.
  • upgrade: the protocols Upgrade offered, in order. An offer with a version (HTTP/2.0) is not a token, and is left out.
  • trailers: whether TE lists trailers, so the client reads trailers after a chunked response.
fun contentLength : fun(Request b) -> Option Int

Returns the length the request's body declared, if it declared one.

fun host : fun(Request b) -> Option String

Returns the host and port the request was sent to, from Host or from an absolute-form target.

fun upgradeRequested : fun(Request b) -> Array Token

Returns the protocols the client offered to switch to, in its order of preference, or none.

fun acceptsTrailers : fun(Request b) -> Bool

Returns whether the client reads trailers after a chunked response.

type ClientRequest b = ClientRequest { method : Method b, url : Either URLError HTTPURL, headers : Headers }

A request a client is to send, built with request and withHeaders:

withHeaders(request(GET, "https://api.example.com/users/1"),
            field(Accept, MediaType.json))

url is the target already parsed. A Left is not sent: send reports it as an error, so request itself never fails, and a URL written as a literal needs no ?.

class Target t

What a client can send a request to: an HTTPURL, or a String that writes one.

fun httpURL : [Target t] fun(t) -> Either URLError HTTPURL
fun request : [Target t] fun(Method b, t) -> ClientRequest b

Returns a request to t with no headers.

type RequestTarget = RequestTarget { authority : Option String, path : String, query : Option String }

The target of a request a server received, as the codec read it from the request line and the Host field.

path is encoded, exactly as it arrived (* for OPTIONS *); segments decodes it. authority is the host and port the request was sent to, from Host or from an absolute-form target, which must agree. The strings are the codec's checked input: nothing sends a RequestTarget, so a program that builds one, to test a handler, cannot put it on the wire.

fun segments : fun(RequestTarget) -> Option (Array String)

Returns the path's segments, decoded: /a%20b/c is ["a b", "c"]. Returns None if a segment is not UTF-8 once decoded.

instance HasHeaders (Request b)
instance HasHeaders (ClientRequest b)
instance Target HTTPURL
instance Target String