My favorites | Sign in
Project Home Downloads
Search
for
Exports  
Misultin modules list of exports.
Updated Mar 21, 2011 by ostine...@gmail.com

1. misultin

Description

This module contains the necessary functionalities to manage the misultin HTTP server.

Exports

start_link(Options) -> Result

Options = [Option]
 Option = {ip, string()} |
  {port, integer()} |
  {backlog, integer()} |
  {recv_timeout, integer()} |
  {max_connections, integer()} |
  {loop, fun()} |
  {autoexit, true|false} |
  {ws_loop, fun()|none} |
  {ws_autoexit, true|false} |
  {post_max_size, integer()} |
  {get_url_max_size, integer()} |
  {compress, true|false} |
  {ssl, SslOptions}
 SslOptions = [SslOption(§)]
Result = {ok, Pid} | ignore | {error, Error}
 Pid = pid()
 Error = {already_started,Pid} | term()

(§) SslOption is defined as a ssl_new option. Please note that Misultin implementation of SSL needs Erlang R13B or newer.

Starts the misultin gen_server. Options are:

  • ip: the IP address of the HTTP server, defaults to "0.0.0.0"
  • port: an integer specifying the port number where the HTTP server will run, defaults to 80
  • backlog: defines the maximum length that the queue of pending connections may grow to, defaults to 128
  • recv_timeout: defines the receive timeout on incoming TCP data, defaults to 30000 (30 seconds)
  • max_connections: sets the maximum number of concurrent connections that the server is willing to handle
  • loop: the callback function to be called on every incoming HTTP request
  • autoexit: if set to true, then the loop specified as loop will be killed automatically by the server when the client closes the socket; if set to false, an event message is sent to loop which then needs to be exited in the most appropriate way.
  • ws_loop: the loop of the listening process which handles a websocket. If a function is passed as ws_loop option then websockets requests will be treated, they will be ignored. The function passed as ws_loop is spawned by misultin to handle the connected websockets. Data coming from a browser will be sent to this process and will have the message format {browser, Data}, where Data is a string(). If you need to send data to the browser, you may do so by using the parametrized function Ws:send(Data), Data being a string() or an iolist() (see the Example Page). Defaults to none.
  • ws_autoexit: if set to true, then the loop specified as ws_loop will be killed automatically by the server when the socket closes; if set to false, an event message is sent to ws_loop which then needs to be exited in the most appropriate way. See the Misultin Websockets With Event example.
  • compress: set to true to send responses in compressed format, if available. Please note that files sent with Req:file/1,2 and stream with Req:stream/1,2,3 will not be compressed.
  • post_max_size: sets the maximum size in bytes of a POST request body. Defaults to 4MB.
  • get_url_max_size: sets the maximum length of the URI of a request. Defaults to 2000.
  • ssl: allows to start Misultin in SSL mode. Parameter is a list of ssl_new options.

The example here below starts a Misultin HTTP server on port 8080, and sets all incoming HTTP requests to be treated by a function handle_http/1:

misultin:start_link([{port, 8080}, {loop, fun(Req) -> handle_http(Req) end}]).

stop() -> ok

Stops the misultin gen_server.

Example to stop the Misultin HTTP server:

misultin:stop().

2. Misultin Request

Description

This module contains the necessary functionalities to manage the incoming HTTP requests and provide a response. This module uses Erlang parametrized modules, therefore the syntax is to be understood accordingly.

Exports

Req:chunk(head)

Same as Req:chunk(head, []).

Req:chunk(head, Headers)

Headers = {Header]
 Header = {HeaderName, HeaderContent}
  HeaderName = atom()
  HeaderContent = list()

Starts an HTTP response with chunked content.

HeaderName is the name of the HTTP header field; HeaderContent is the content of the header.

The functions chunk/1,2,3 are used when the HTTP response has to be given progressively using chunked transfer encoding, and not altogether as it happens in respond/2,3,4.

In order to provide a chunked HTTP response, these functions have to be used in a specific manner, as shown in this example:

% send headers
Req:chunk(head, [{"Content-Type", "text/html"}]),
% send chunk
Req:chunk("Sending CHUNK 1<br/>"),
timer:sleep(2000),
% send chunk
Req:chunk("Sending CHUNK 2<br/>"),
timer:sleep(2000),
% send chunk
Req:chunk("Sending CHUNK 3<br/>"),
% close
Req:chunk(done).

This will start a 200 HTTP response of text/plain Content-Type and send 3 chunks in sequence.

Req:chunk(Template)

Template = atom() | string() | binary() | iolist()

Sends data chunk into the open HTTP socket.

Req:chunk(Template, Vars)

Template = string()
Vars = [term()]

Sends data into the open HTTP socket.

Template and Vars build a character list which represents Vars formatted in accordance with Template. See io:format/3 for a detailed description of the available formatting options. A fault is generated if there is an error in the format string or argument list.

Req:chunk(done)

Ends the Chunked sequence.

Req:file(FilePath)

FilePath = list()

Sends a file to be visualized in a browser, for example:

Req:file("/usr/local/files/content.html").

Req:file(attachment, FilePath)

FilePath = list()

Sends a file as an attachment (so that a browser prompts for download), for example:

Req:file(attachment, "/usr/local/files/test.zip").

Req:get(ReqInfo) -> term()

ReqInfo = socket | socket_mode | peer_addr | peer_port | peer_cert | connection | content_length | vsn | method | uri | args | headers | body

Retrieves the Request information. See Req:raw/0 for additional information on the output format.

Req:ok(Template)

Same as Req:ok([], Template).

Req:ok(Headers, Template)

Same as Req:respond(200, Headers, Template).

Req:ok(Headers, Template, Vars)

Same as Req:respond(200, Headers, Template, Vars).

Req:parse_post() -> Result

Result = [Argument]
 Argument = {ArgumentName, ArgumentValue}
  ArgumentName = list()
  ArgumentValue = list()

Parses the arguments passed in the body of the Request, when method is 'POST' and the Request Content-Type header has been specified as application/x-www-form-urlencoded.

Req:parse_qs() -> Result

Result = [Argument]
 Argument = {ArgumentName, ArgumentValue}
  ArgumentName = list()
  ArgumentValue = list()

Parses the arguments passed in the querystring, when method is 'GET'.

Req:raw() -> record()

Retrieves the Request complete record, defined as follows:

-record(req, {
	socket,				% the socket handling the request
	socket_mode,			% http | ssl
	peer_addr,			% IP | undefined
	peer_port,			% Port | undefined
	peer_cert,			% undefined | the DER encoded peer certificate
	connection = keep_alive,	% keep_alive | close
	content_length,			% integer()
	vsn,				% {Maj, Min}
	method,				% 'GET' | 'POST'
	uri,				% {UriType, Uri}
	args = "",			% list()
	headers,			% [{Tag, Val}]
	body = <<>>			% bytes()
}).

To use this record in your own code, you will need to include the file misultin.hrl. Otherwise you'll be better off using the appropriate function Req:get/1.

Req:resource(Options) -> Resources

Options = [Option]
 Option = lowercase | urldecode
Resources = [Resource]
 Resource = list()

This functionality provides support for RESTful services built with Misultin. It will split the URI into the list of its resources (i.e. it will separate the URI with the / token).

It also allows to provide two options:

  • lowercase: convert all resources to lowercase;
  • urldecode: decode urlencoded characters.

See the ExamplesPage for a more detailed usage of this function.

Req:respond(HttpCode, Template)

Same as Req:respond(HttpCode, [], Template).

Req:respond(HttpCode, Headers, Template)

HttpCode = integer()
Headers = {Header]
 Header = {HeaderName, HeaderContent}
  HeaderName = atom()
  HeaderContent = list()
Template = atom() | string() | binary() | iolist()

Formats an HTTP response of code HttpCode.

HeaderName is the name of the HTTP header field; HeaderContent is the content of the header.

This example formats a static XML response:

Req:respond(200, [{"Content-Type", "text/xml"}], "<misultin_test>ok</misultin_test>").

This example formats an XML response with the content of a parameter Value, using an iolist():

Req:respond(200, [{"Content-Type", "text/xml"}], ["<misultin_test><value>", Value, "</value></misultin_test>"]).

Req:respond(HttpCode, Headers, Template, Vars)

HttpCode = integer()
Headers = {Header]
 Header = {HeaderName, HeaderContent}
  HeaderName = atom()
  HeaderContent = list()
Template = string()
Vars = [term()]

Formats an HTTP response of code HttpCode.

HeaderName is the name of the HTTP header field; HeaderContent is the content of the header.

Template and Vars build a character list which represents Vars formatted in accordance with Template. See io:format/3 for a detailed description of the available formatting options. A fault is generated if there is an error in the format string or argument list.

This example formats an XML response with the content of a parameter Value:

Req:respond(200, [{"Content-Type", "text/xml"}], "<misultin_test><value>~s</value></misultin_test>", [Value]).

Req:stream(head)

Same as Req:stream(head, 200, []).

Req:stream(head, Headers)

Same as Req:stream(head, 200, Headers).

Req:stream(head, HttpCode, Headers)

HttpCode = integer()
Headers = {Header]
 Header = {HeaderName, HeaderContent}
  HeaderName = atom()
  HeaderContent = list()

Starts an HTTP response of code HttpCode.

HeaderName is the name of the HTTP header field; HeaderContent is the content of the header.

The functions stream/1,2,3 are used when the HTTP response has to be given progressively, and not altogether as it happens in respond/2,3,4.

In order to provide a 'streamed' HTTP response, these functions have to be used in a specific manner, as shown in this example:

% send headers
Req:stream(head, [{"Content-Type", "text/plain"}]),
% send stream
Req:stream("1"),
timer:sleep(2000),
% send stream
Req:stream("2"),
timer:sleep(2000),
% send stream
Req:stream("3"),
% close socket
Req:stream(close).

This will start a 200 HTTP response of text/plain Content-Type and immediately send the character 1. It will then wait 2 seconds, before sending character 2. Finally, it will wait another 2 seconds before sending the last character 3 and close the socket.

Basically, the stream/1,2,3 functions work by providing all HTTP headers with the exception of Content-Length, which would otherwise allow the HTTP client to close the socket when all data is received. Therefore, the HTTP client keeps on listening since it doesn't know how much bytes it is supposed to receive, and thus it can be sent data progressively. Finally, however, since in this example no Content-Length header has been set, the closing of the socket needs to be done server-side, and thus the use of the function Req:stream(close).

Req:stream(Template)

Template = atom() | string() | binary() | iolist()

Sends data into the open HTTP socket.

Req:stream(Template, Vars)

Template = string()
Vars = [term()]

Sends data into the open HTTP socket.

Template and Vars build a character list which represents Vars formatted in accordance with Template. See io:format/3 for a detailed description of the available formatting options. A fault is generated if there is an error in the format string or argument list.

Req:stream(close)

Server-side closes the open socket. If Content-Length has NOT been specified, the socket must manually be closed by calling Req:stream(close).

Req:stream({error, Reason})

If an error occurred when dealing with streams, you may call this function to terminate the connection and log the error.

3. Misultin Websockets

Description

This module contains the necessary functionalities to manage websockets. For the draft websocket protocol, you may refer to http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-55. This module uses Erlang parametrized modules, therefore the syntax is to be understood accordingly.

Exports

Ws:get(WsInfo) -> term()

WsInfo = socket | socket_mode | peer_addr | peer_port | peer_cert | vsn | origin | host | path | headers

Retrieves the Websocket information. See Ws:raw/0 for additional information on the output format.

Ws:raw() -> record()

Retrieves the Websocket complete record, defined as follows:

-record(req, {
	socket,				% the socket handling the websocket
	socket_mode,		% http | ssl
	peer_addr,			% IP | undefined
	peer_port,			% Port | undefined
	peer_cert,			% undefined | the DER encoded peer certificate
	vsn,				% {Maj,Min} | {'draft-hixie', Ver}
	origin,				% the originator
	host,				% the host
	path,				% the websocket GET request path
	headers				% [{Tag, Val}]
}).

To use this record in your own code, you will need to include the file misultin.hrl. Otherwise you'll be better off using the appropriate function Ws:get/1.

Ws:send(Data)

Data = string() | iolist()

Sends data into the open Websocket to the recipient browser. For additional information, see the Example Page.

Powered by Google Project Hosting