Module mochiweb_request

MochiWeb HTTP Request abstraction.

Copyright © 2007 Mochi Media, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Authors: Bob Ippolito (bob@mochimedia.com).

Description

MochiWeb HTTP Request abstraction.

Data Types

encoding()

encoding() = string()

field()

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

headers()

abstract datatype: headers()

A mochiweb_headers structure.

ioheaders()

ioheaders() = headers() | [{key(), value()}]

key()

key() = atom() | string() | binary()

request()

abstract datatype: request()

A mochiweb_request parameterized module instance.

response()

abstract datatype: response()

A mochiweb_response parameterized module instance.

value()

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

Function Index

accepted_content_types/2Filters which of the given media types this request accepts.
accepted_encodings/2Returns a list of encodings accepted by a request.
accepts_content_type/2Determines whether a request accepts a given media type by analyzing its "Accept" header.
cleanup/1Clean up any junk in the process dictionary, required before continuing a Keep-Alive request.
dump/1Dump the internal representation to a "human readable" set of terms for debugging/inspection purposes.
get/2Return the internal representation of the given field.
get_combined_header_value/2
get_cookie_value/2Get the value of the given cookie.
get_header_value/2Get the value of a given request header.
get_primary_header_value/2
new/5Create a new request instance.
new/6Create a new request instance.
not_found/1Alias for not_found([]).
not_found/2Alias for respond({404, [{"Content-Type", "text/plain"} | ExtraHeaders], <<"Not found.">>}).
ok/2respond({200, [{"Content-Type", ContentType} | Headers], Body}).
parse_cookie/1Parse the cookie header.
parse_post/1Parse an application/x-www-form-urlencoded form POST.
parse_qs/1Parse the query string of the URL.
recv/2Receive Length bytes from the client as a binary, with the default idle timeout.
recv/3Receive Length bytes from the client as a binary, with the given Timeout in msec.
recv_body/1Receive the body of the HTTP request (defined by Content-Length).
recv_body/2Receive the body of the HTTP request (defined by Content-Length).
respond/2Start the HTTP response with start_response, and send Body to the client (if the get(method) /= 'HEAD').
send/2Send data over the socket.
serve_file/3Serve a file relative to DocRoot.
serve_file/4Serve a file relative to DocRoot.
should_close/1Return true if the connection must be closed.
start_raw_response/2Start the HTTP response by sending the Code HTTP response and ResponseHeaders.
start_response/2Start the HTTP response by sending the Code HTTP response and ResponseHeaders.
start_response_length/2Start the HTTP response by sending the Code HTTP response and ResponseHeaders including a Content-Length of Length.
stream_body/4
stream_body/5

Function Details

accepted_content_types/2

accepted_content_types(Types1::[string() | binary()], THIS::request()) -> [string()] | bad_accept_header

Filters which of the given media types this request accepts. This filtering is performed by analyzing the "Accept" header. The returned list is sorted according to the preferences specified in the "Accept" header (higher Q values first). If two or more types have the same preference (Q value), they're order in the returned list is the same as they're order in the input list.

Examples

1) For a missing "Accept" header: accepted_content_types(["text/html", "application/json"]) -> ["text/html", "application/json"]

2) For an "Accept" header with value "text/html, application/*": accepted_content_types(["application/json", "text/html"]) -> ["application/json", "text/html"]

3) For an "Accept" header with value "text/html, */*; q=0.0": accepted_content_types(["text/html", "application/json"]) -> ["text/html"]

4) For an "Accept" header with value "text/html; q=0.5, */*; q=0.1": accepts_content_types(["application/json", "text/html"]) -> ["text/html", "application/json"]

accepted_encodings/2

accepted_encodings(SupportedEncodings::[encoding()], THIS::request()) -> [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"]

accepts_content_type/2

accepts_content_type(ContentType1::string() | binary(), THIS::request()) -> boolean() | bad_accept_header

Determines whether a request accepts a given media type by analyzing its "Accept" header.

Examples

1) For a missing "Accept" header: accepts_content_type("application/json") -> true

2) For an "Accept" header with value "text/plain, application/*": accepts_content_type("application/json") -> true

3) For an "Accept" header with value "text/plain, */*; q=0.0": accepts_content_type("application/json") -> false

4) For an "Accept" header with value "text/plain; q=0.5, */*; q=0.1": accepts_content_type("application/json") -> true

5) For an "Accept" header with value "text/*; q=0.0, */*": accepts_content_type("text/plain") -> false

cleanup/1

cleanup(X1::request()) -> ok

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

dump/1

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

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

get/2

get(X1::field(), THIS::request()) -> 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_combined_header_value/2

get_combined_header_value(K, X2) -> any()

get_cookie_value/2

get_cookie_value(Key::string, THIS::request()) -> string() | undefined

Get the value of the given cookie.

get_header_value/2

get_header_value(K, X2::request()) -> undefined | Value

Get the value of a given request header.

get_primary_header_value/2

get_primary_header_value(K, X2) -> any()

new/5

new(Socket, Method, RawPath, Version, Headers::headers()) -> request()

Create a new request instance.

new/6

new(Socket, Opts, Method, RawPath, Version, Headers::headers()) -> request()

Create a new request instance.

not_found/1

not_found(THIS::request()) -> response()

Alias for not_found([]).

not_found/2

not_found(ExtraHeaders, THIS::request()) -> response()

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

ok/2

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

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

parse_cookie/1

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

Parse the cookie header.

parse_post/1

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

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

parse_qs/1

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

Parse the query string of the URL.

recv/2

recv(Length::integer(), THIS::request()) -> binary()

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

recv/3

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

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

recv_body/1

recv_body(THIS::request()) -> 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/2

recv_body(MaxBody::integer(), THIS::request()) -> binary()

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

respond/2

respond(X1::{integer(), ioheaders(), iodata() | chunked | {file, IoDevice}}, THIS::request()) -> 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/2

send(Data::iodata(), X2::request()) -> ok

Send data over the socket.

serve_file/3

serve_file(Path, DocRoot, THIS::request()) -> Response

Serve a file relative to DocRoot.

serve_file/4

serve_file(Path, DocRoot, ExtraHeaders, THIS::request()) -> Response

Serve a file relative to DocRoot.

should_close/1

should_close(THIS::request()) -> bool()

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

start_raw_response/2

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

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

start_response/2

start_response(X1::{integer(), ioheaders()}, THIS::request()) -> 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/2

start_response_length(X1::{integer(), ioheaders(), integer()}, THIS::request()) -> 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/4

stream_body(MaxChunkSize, ChunkFun, FunState, THIS) -> any()

stream_body/5

stream_body(MaxChunkSize, ChunkFun, FunState, MaxBodyLength, THIS) -> any()


Generated by EDoc