mochiweb_request

MochiWeb HTTP Request abstraction.

MochiWeb HTTP Request abstraction.

DATA TYPES

encoding() = string()
field() = socket | scheme | method | raw_path | version | headers | peer | path | body_length | range
headers()

A mochiweb_headers structure.

iodata() = binary() | iolist()
ioheaders() = headers() | [{key(), value()}]
iolist() = [iolist() | binary() | char()]
key() = atom() | string() | binary()
response()

A mochiweb_response parameterized module instance.

value() = atom() | string() | binary() | integer()

Functions


accepted_encodings(SupportedEncodings::[encoding()]) -> [encoding()] | bad_accept_encoding_value

Returns a list of encodings accepted by a request. Encodings that are not supported by the server will not be included in the return list. This list is computed from the "Accept-Encoding" header and its elements are ordered, descendingly, according to their Q values.

Section 14.3 of the RFC 2616 (HTTP 1.1) describes the "Accept-Encoding" header and the process of determining which server supported encodings can be used for encoding the body for the request's response.

Examples

1) For a missing "Accept-Encoding" header: accepted_encodings(["gzip", "identity"]) -> ["identity"]

2) For an "Accept-Encoding" header with value "gzip, deflate": accepted_encodings(["gzip", "identity"]) -> ["gzip", "identity"]

3) For an "Accept-Encoding" header with value "gzip;q=0.5, deflate": accepted_encodings(["gzip", "deflate", "identity"]) -> ["deflate", "gzip", "identity"]

cleanup() -> ok

Clean up any junk in the process dictionary, required before continuing a Keep-Alive request.

dump() -> {mochiweb_request, [{atom(), term()}]}

Dump the internal representation to a "human readable" set of terms for debugging/inspection purposes.

get(X1::field()) -> term()

Return the internal representation of the given field. If socket is requested on a HTTPS connection, then an ssl socket will be returned as {ssl, SslSocket}. You can use SslSocket with the ssl application, eg: ssl:peercert(SslSocket).

Get the value of the given cookie.

get_header_value(K) -> undefined | Value

Get the value of a given request header.

get_primary_header_value(K) -> term()

not_found() -> response()

Alias for not_found([]).

not_found(ExtraHeaders) -> response()

Alias for respond({404, [{"Content-Type", "text/plain"} | ExtraHeaders], <<"Not found.">>}).

ok(X1::{value(), iodata()} | {value(), ioheaders(), iodata() | {file, IoDevice}}) -> response()

respond({200, [{"Content-Type", ContentType} | Headers], Body}).

parse_cookie() -> [{Key::string(), Value::string()}]

Parse the cookie header.

parse_post() -> [{Key::string(), Value::string()}]

Parse an application/x-www-form-urlencoded form POST. This has the side-effect of calling recv_body().

parse_qs() -> [{Key::string(), Value::string()}]

Parse the query string of the URL.

recv(Length::integer()) -> binary()

Receive Length bytes from the client as a binary, with the default idle timeout.

recv(Length::integer(), Timeout::integer()) -> binary()

Receive Length bytes from the client as a binary, with the given Timeout in msec.

recv_body() -> binary()

Receive the body of the HTTP request (defined by Content-Length). Will only receive up to the default max-body length of 1MB.

recv_body(MaxBody::integer()) -> binary()

Receive the body of the HTTP request (defined by Content-Length). Will receive up to MaxBody bytes.

respond(X1::{integer(), ioheaders(), iodata() | chunked | {file, IoDevice}}) -> response()

Start the HTTP response with start_response, and send Body to the client (if the get(method) /= 'HEAD'). The Content-Length header will be set by the Body length, and the server will insert header defaults.

send(Data::iodata()) -> ok

Send data over the socket.

serve_file(Path, DocRoot) -> Response

Serve a file relative to DocRoot.

serve_file(Path, DocRoot, ExtraHeaders) -> Response

Serve a file relative to DocRoot.

should_close() -> bool()

Return true if the connection must be closed. If false, using Keep-Alive should be safe.

start_raw_response(X1::{integer(), headers()}) -> response()

Start the HTTP response by sending the Code HTTP response and ResponseHeaders.

start_response(X1::{integer(), ioheaders()}) -> response()

Start the HTTP response by sending the Code HTTP response and ResponseHeaders. The server will set header defaults such as Server and Date if not present in ResponseHeaders.

start_response_length(X1::{integer(), ioheaders(), integer()}) -> response()

Start the HTTP response by sending the Code HTTP response and ResponseHeaders including a Content-Length of Length. The server will set header defaults such as Server and Date if not present in ResponseHeaders.

stream_body(MaxChunkSize, ChunkFun, FunState) -> term()

Bob Ippolito bob@mochimedia.com
View Functions