Copyright © 2007, 2008 Scott Parish <srp@srparish.net>
Authors: Scott Parish (srp@srparish.net).
code() = integer() | atom() | binary()
For example: 404 or not_found or <<"404 Not Found">>
crary_req() = record()
This is a record that represents the specifics of the request currently being handled. It has the following fields:
vsn()The client HTTP Version.
Example: {1, 1}
The request method.
Example: "GET"
The request uri. Crary does not decode these, nor does it intend to ever do so; it may be only the path/query parts or it may be the absolute URI (see rfc2616, section 5.1.2).
Examples: "/index.html?sort=date",
"http://myhost.com:80/path/"
crary_headers:headers()The request headers. Use crary_headers to access the
contents of this data structure
crary_sock:sock()A descriptor for reading the body or writing the response via
crary_sock. Most of the crary_sock functions
accept this crary_req() record, so it should be rarely
necessary to access this directly.
proplist()The options that this server (the one for this port)
was started with. Use the erlang proplists module to
access values. Feel free to add whatever options your
handler needs to this data structure. Please name your
options so that they won't conflict with other handler's
options.
-include("crary.hrl"). in your source files that
need to interact with this record.
mfa() = {Module::atom(), Function::atom(), Args::list()}
proplist() = [Key::atom() | {Key::atom(), Value::term}]
vsn() = {Major, Minor}
| bad_request/1 | This is a short cut for sending 400, Bad Request, error responses
with the body already filled in. |
| code_to_binary/1 | Given a number or atom of a standard HTTP response code, return a binary (string) of the number and name. |
| error/3 | |
| forbidden/1 | This is a short cut for sending 403, Forbidden error
responses with the body already filled in. |
| ident/0 | Return an iolist of the short ident string such as: crary/1.0.5. |
| ident/1 | Return an iolist of the ident, using Opts to determin if the long
or short ident string should be used. |
| internal_server_error/4 | This is a short cut for sending 500, Internal Server Error,
error responses with the body already filled in. |
| internal_server_error_html/4 | This generates the HTML that is used for 500, Internal Server
Error. |
| list_to_vsn/1 | Parse the HTTP version string into a version tuple of {Maj, Min}. |
| long_ident/0 | Return an iolist of the longer ident string, which will list
all the loaded applications, for instance:
crary/1.0.5 kernel/2.11.5 stdlib/1.14.5 sasl/2.1.5.1 |
| not_found/1 | This is a short cut for sending 404, Not Found error
responses with the body already filled in. |
| not_implemented/1 | This is a short cut for sending 501, Not Implemented, error
responses with the body already filled in. |
| pp/1 | Pretty print the request: return a tuple list representing
the crary_req structure in a form that will print nicely via
io:format/2 ~p or error_logger:error_report/1. |
| r/3 | Write a response line and response headers to socket, does not
write the body, nor does it call crary_sock:done_writing/1. |
| r/4 | Write a response line and response headers to socket and
either write the body, or start a streamed body call F(Writer) to
generate the body. |
| r_error/3 | Write a response for errors, this includes the standard error header and footer html. |
| resp/3 | Alias for r/3 |
| resp/4 | alias for r/4 |
| servers/0 | Return a list of crary servers (as ports) currently running. |
| start/2 | Start a crary server listening on TcpPort. |
| start/3 | Start a crary server listening on TcpPort. |
| stop/1 | Stop the crary server that's running on TcpPort. |
| vsn_to_iolist/1 | Convert a vsn() tuple to a string. |
bad_request(Req::crary_req()) -> ok
This is a short cut for sending 400, Bad Request, error responses
with the body already filled in.
code_to_binary(Code::integer() | atom() | binary()) -> binary()
Given a number or atom of a standard HTTP response code, return a binary (string) of the number and name.
Example: code_to_binary(404) => <<"404 Not Found">>
code_to_binary(not_found) => <<"404 Not Found">>
This function is used by functions such as r/4, with_chunked_resp/4, and r_error/3 so that the Code
argument can be minimally specified.
error() -> term()
forbidden(Crary_req::crary_req()) -> ok
This is a short cut for sending 403, Forbidden error
responses with the body already filled in.
ident() -> iolist()
Return an iolist of the short ident string such as: crary/1.0.5.
ident(Crary_req::proplist()) -> iolist()
Return an iolist of the ident, using Opts to determin if the long
or short ident string should be used.
See also: ident/0, long_ident/0.
internal_server_error(Req::crary_req(), Class::atom(), Reason::term(), Stack::list()) -> ok
This is a short cut for sending 500, Internal Server Error,
error responses with the body already filled in.
crary_ctrl before calling the handler, places a try/catch
on the stack that will automatically call this function if there is
an uncaught exception, so you may not need to use this directly.
internal_server_error_html(Crary_req::crary_req(), Class::atom(), Reason::term(), Stack::list()) -> iolist()
This generates the HTML that is used for 500, Internal Server
Error. crary_body:with_writer/2 uses this to display error
messages; it can't call internal_server_error/4 as the response
headers will have already been sent.
list_to_vsn(VsnStr::string()) -> vsn()
Parse the HTTP version string into a version tuple of {Maj, Min}.
long_ident() -> iolist()
Return an iolist of the longer ident string, which will list
all the loaded applications, for instance:
crary/1.0.5 kernel/2.11.5 stdlib/1.14.5 sasl/2.1.5.1
not_found(Crary_req::crary_req()) -> ok
This is a short cut for sending 404, Not Found error
responses with the body already filled in.
handler(crary_req{uri = '/'} = Req) ->
crary:r(Req, ok, ["content-type", "text/plain"], "Hello World!");
handler(Req) -> % unknown uri
crary:not_found(Uri).
not_implemented(Crary_req::crary_req()) -> ok
This is a short cut for sending 501, Not Implemented, error
responses with the body already filled in.
ro_handler(crary_req{method = 'GET'} = Req) ->
crary:r(Req, ok, ["content-type", "text/plain"], "Hello World!");
ro_handler(Req) -> % method =/= 'GET'
crary:not_implemented(Req).
pp(Crary_req::crary_req()) -> list()
Pretty print the request: return a tuple list representing
the crary_req structure in a form that will print nicely via
io:format/2 ~p or error_logger:error_report/1.
r(Req::aitch_req(), Code::code(), Headers::crary_headers:headers()) -> ok
Write a response line and response headers to socket, does not
write the body, nor does it call crary_sock:done_writing/1. It
adds Server and Date headers to the response. Be sure to set the
headers appropriately for the form of the body (ei Content-Length
or Transfer-Encoding).
hello_handler(Req) ->
Body = "<html><body>Hello World!</body></html>",
BodyLenStr = integer_to_list(iolist_size(Body)),
crary:r(Req, ok, [{"content-type", "text/html"},
{<<"content-length">>, BodyLenStr}],
crary_sock:write(Req, Body),
crary_sock:done_writing(Req).
If you need to write a body or do body streaming, r/4 may
be a more convient function then this one.
r(Req::crary_req(), Code::code(), Headers::crary_headers:headers(), F::BodyOrF) -> ok
Write a response line and response headers to socket and
either write the body, or start a streamed body call F(Writer) to
generate the body. In both cases crary_sock:done_writing/1
is called before returning.
hello_handler(Req) ->
crary:r(Req, ok, [{"content-type", "text/html"}],
<<"<html><body>Hello World!</body></html>">>).
Streamed Example:
hello_handler(Req) ->
crary:r(Req, ok, [{"content-type", "text/html"}],
fun (W) ->
aitch_body:write(W, "<html><body>"),
aitch_body:write(W, "Hello World!"),
aitch_body:write(W, "</body></html>"),
end).
See also: crary_body:with_writer/2.
r_error(Req::crary_req(), Code::code(), Msg::iolist()) -> ok
Write a response for errors, this includes the standard error
header and footer html. The title and h1 are generated from the
Code. Msg should be verbage describing the problem.
ro_handler(crary_req{method = 'GET'} = Req) ->
crary:r(Req, ok, ["content-type", "text/plain"],
"Hello World!");
ro_handler(crary_req{method = Method} = Req) when Method =/= 'GET' ->
crary:r_error(Req, not_implemented,
["The method `", Method,
"' is not supported by this server.",
"Please only use 'GET' with this server"]).
resp() -> term()
Alias for r/3
See also: r/3.
resp() -> term()
alias for r/4
See also: r/4.
servers() -> [TcpPort::integer()]
Return a list of crary servers (as ports) currently running.
start(TcpPort::integer(), Handler::mfa() | {function(), Args}) -> pid()
Start a crary server listening on TcpPort.
See also: start/3.
start(TcpPort::integer(), Handler::mfa() | {function(), Args}, Options::proplist()) -> pid()
Start a crary server listening on TcpPort. Handler will be
called as apply(M, F, [Req | Args]) for each request.
stop(TcpPort::integer()) -> ok
Stop the crary server that's running on TcpPort.
vsn_to_iolist(Crary_req::vsn()) -> string()
Convert a vsn() tuple to a string.
Generated by EDoc, Mar 14 2008, 22:35:53.