Module crary

This module provides the core crary apis.

Copyright © 2007, 2008 Scott Parish <srp@srparish.net>

Authors: Scott Parish (srp@srparish.net).

Description

This module provides the core crary apis.

Data Types

code()

code() = integer() | atom() | binary()

For example: 404 or not_found or <<"404 Not Found">>

crary_req()

crary_req() = record()

This is a record that represents the specifics of the request currently being handled. It has the following fields:

vsn::vsn()

The client HTTP Version.

Example: {1, 1}

method::string()

The request method.

Example: "GET"

uri::string()

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/"

headers::crary_headers:headers()

The request headers. Use crary_headers to access the contents of this data structure

sock::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.

opts::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.

Please -include("crary.hrl"). in your source files that need to interact with this record.

mfa()

mfa() = {Module::atom(), Function::atom(), Args::list()}

proplist()

proplist() = [Key::atom() | {Key::atom(), Value::term}]

vsn()

vsn() = {Major, Minor}

Function Index

bad_request/1This is a short cut for sending 400, Bad Request, error responses with the body already filled in.
code_to_binary/1Given a number or atom of a standard HTTP response code, return a binary (string) of the number and name.
error/3
forbidden/1This is a short cut for sending 403, Forbidden error responses with the body already filled in.
ident/0Return an iolist of the short ident string such as: crary/1.0.5.
ident/1Return an iolist of the ident, using Opts to determin if the long or short ident string should be used.
internal_server_error/4This is a short cut for sending 500, Internal Server Error, error responses with the body already filled in.
internal_server_error_html/4This generates the HTML that is used for 500, Internal Server Error.
list_to_vsn/1Parse the HTTP version string into a version tuple of {Maj, Min}.
long_ident/0Return 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/1This is a short cut for sending 404, Not Found error responses with the body already filled in.
not_implemented/1This is a short cut for sending 501, Not Implemented, error responses with the body already filled in.
pp/1Pretty 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/3Write a response line and response headers to socket, does not write the body, nor does it call crary_sock:done_writing/1.
r/4Write 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/3Write a response for errors, this includes the standard error header and footer html.
resp/3Alias for r/3
resp/4alias for r/4
servers/0Return a list of crary servers (as ports) currently running.
start/2Start a crary server listening on TcpPort.
start/3Start a crary server listening on TcpPort.
stop/1Stop the crary server that's running on TcpPort.
vsn_to_iolist/1Convert a vsn() tuple to a string.

Function Details

bad_request/1

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/1

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/3

error() -> term()

forbidden/1

forbidden(Crary_req::crary_req()) -> ok

This is a short cut for sending 403, Forbidden error responses with the body already filled in.

ident/0

ident() -> iolist()

Return an iolist of the short ident string such as: crary/1.0.5.

ident/1

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/4

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/4

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/1

list_to_vsn(VsnStr::string()) -> vsn()

Parse the HTTP version string into a version tuple of {Maj, Min}.

long_ident/0

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/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.

Example:
     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/1

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.

Example:
     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/1

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/3

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).

Example:
     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/4

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.

Static Example:
     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/3

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.

Example:
     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/3

resp() -> term()

Alias for r/3

See also: r/3.

resp/4

resp() -> term()

alias for r/4

See also: r/4.

servers/0

servers() -> [TcpPort::integer()]

Return a list of crary servers (as ports) currently running.

start/2

start(TcpPort::integer(), Handler::mfa() | {function(), Args}) -> pid()

Start a crary server listening on TcpPort.

See also: start/3.

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/1

stop(TcpPort::integer()) -> ok

Stop the crary server that's running on TcpPort.

vsn_to_iolist/1

vsn_to_iolist(Crary_req::vsn()) -> string()

Convert a vsn() tuple to a string.


Generated by EDoc, Mar 14 2008, 22:35:53.