|
Exports
Misultin modules list of exports.
1. misultinDescriptionThis module contains the necessary functionalities to manage the misultin HTTP server. Exportsstart_link(Options) -> ResultOptions = [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:
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() -> okStops the misultin gen_server. Example to stop the Misultin HTTP server: misultin:stop(). 2. Misultin RequestDescriptionThis 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. ExportsReq: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() -> ResultResult = [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() -> ResultResult = [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) -> ResourcesOptions = [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:
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 WebsocketsDescriptionThis 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. ExportsWs: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. |