diff options
author | Christophe Romain <christophe.romain@process-one.net> | 2012-09-11 15:45:59 +0200 |
---|---|---|
committer | Christophe Romain <christophe.romain@process-one.net> | 2012-09-11 15:45:59 +0200 |
commit | 011535f0de1a14d6f5f411035bff9eeafec1c612 (patch) | |
tree | e60951904fbdc14dc126450c4d7515f51188d4b7 /src/web/mod_http_bind.erl | |
parent | Merge branch '2.1.x' into 2.2.x (diff) |
binary refactoring
Diffstat (limited to 'src/web/mod_http_bind.erl')
-rw-r--r-- | src/web/mod_http_bind.erl | 122 |
1 files changed, 59 insertions, 63 deletions
diff --git a/src/web/mod_http_bind.erl b/src/web/mod_http_bind.erl index 341e9fa7d..c7de9c329 100644 --- a/src/web/mod_http_bind.erl +++ b/src/web/mod_http_bind.erl @@ -31,88 +31,90 @@ %%%---------------------------------------------------------------------- -module(mod_http_bind). + -author('steve@zeank.in-berlin.de'). %%-define(ejabberd_debug, true). -behaviour(gen_mod). --export([ - start/2, - stop/1, - process/2 - ]). +-export([start/2, stop/1, process/2]). -include("ejabberd.hrl"). + -include("jlib.hrl"). + -include("ejabberd_http.hrl"). + -include("http_bind.hrl"). -define(PROCNAME_MHB, ejabberd_mod_http_bind). -%% Duplicated from ejabberd_http_bind. -%% TODO: move to hrl file. --record(http_bind, {id, pid, to, hold, wait, process_delay, version}). +-record(http_bind, + {id, pid, to, hold, wait, process_delay, version}). %%%---------------------------------------------------------------------- %%% API %%%---------------------------------------------------------------------- -process([], #request{method = 'POST', - data = []}) -> +process([], #request{method = 'POST', data = <<>>}) -> ?DEBUG("Bad Request: no data", []), - {400, ?HEADER, {xmlelement, "h1", [], - [{xmlcdata, "400 Bad Request"}]}}; -process([], #request{method = 'POST', - data = Data, - ip = IP}) -> + {400, ?HEADER, + #xmlel{name = <<"h1">>, children = [{xmlcdata, <<"400 Bad Request">>}]}}; +process([], + #request{method = 'POST', data = Data, ip = IP}) -> ?DEBUG("Incoming data: ~s", [Data]), ejabberd_http_bind:process_request(Data, IP); -process([], #request{method = 'GET', - data = []}) -> +process([], #request{method = 'GET', data = <<>>}) -> {200, ?HEADER, get_human_html_xmlel()}; -process([], #request{method = 'OPTIONS', - data = []}) -> - {200, ?OPTIONS_HEADER, []}; +process([], #request{method = 'OPTIONS', data = <<>>}) -> + {200, ?OPTIONS_HEADER, <<>>}; process([], #request{method = 'HEAD'}) -> - {200, ?HEADER, []}; + {200, ?HEADER, <<>>}; process(_Path, _Request) -> ?DEBUG("Bad Request: ~p", [_Request]), - {400, ?HEADER, {xmlelement, "h1", [], - [{xmlcdata, "400 Bad Request"}]}}. + {400, ?HEADER, + #xmlel{name = <<"h1">>, children = [{xmlcdata, <<"400 Bad Request">>}]}}. get_human_html_xmlel() -> - Heading = "ejabberd " ++ atom_to_list(?MODULE), - {xmlelement, "html", [{"xmlns", "http://www.w3.org/1999/xhtml"}], - [{xmlelement, "head", [], - [{xmlelement, "title", [], [{xmlcdata, Heading}]}]}, - {xmlelement, "body", [], - [{xmlelement, "h1", [], [{xmlcdata, Heading}]}, - {xmlelement, "p", [], - [{xmlcdata, "An implementation of "}, - {xmlelement, "a", - [{"href", "http://xmpp.org/extensions/xep-0206.html"}], - [{xmlcdata, "XMPP over BOSH (XEP-0206)"}]}]}, - {xmlelement, "p", [], - [{xmlcdata, "This web page is only informative. " - "To use HTTP-Bind you need a Jabber/XMPP client that supports it."} - ]} - ]}]}. + Heading = <<"ejabberd ", + (iolist_to_binary(atom_to_list(?MODULE)))/binary>>, + #xmlel{name = <<"html">>, + attrs = + [{<<"xmlns">>, <<"http://www.w3.org/1999/xhtml">>}], + children = + [#xmlel{name = <<"head">>, + children = + [#xmlel{name = <<"title">>, + children = [{xmlcdata, Heading}]}]}, + #xmlel{name = <<"body">>, + children = + [#xmlel{name = <<"h1">>, + children = [{xmlcdata, Heading}]}, + #xmlel{name = <<"p">>, + children = + [{xmlcdata, <<"An implementation of ">>}, + #xmlel{name = <<"a">>, + attrs = + [{<<"href">>, + <<"http://xmpp.org/extensions/xep-0206.html">>}], + children = + [{xmlcdata, + <<"XMPP over BOSH (XEP-0206)">>}]}]}, + #xmlel{name = <<"p">>, + children = + [{xmlcdata, + <<"This web page is only informative. To " + "use HTTP-Bind you need a Jabber/XMPP " + "client that supports it.">>}]}]}]}. -%%%---------------------------------------------------------------------- -%%% BEHAVIOUR CALLBACKS -%%%---------------------------------------------------------------------- start(Host, _Opts) -> setup_database(), Proc = gen_mod:get_module_proc(Host, ?PROCNAME_MHB), - ChildSpec = - {Proc, - {ejabberd_tmp_sup, start_link, - [Proc, ejabberd_http_bind]}, - permanent, - infinity, - supervisor, - [ejabberd_tmp_sup]}, + ChildSpec = {Proc, + {ejabberd_tmp_sup, start_link, + [Proc, ejabberd_http_bind]}, + permanent, infinity, supervisor, [ejabberd_tmp_sup]}, supervisor:start_child(ejabberd_sup, ChildSpec). stop(Host) -> @@ -123,23 +125,17 @@ stop(Host) -> setup_database() -> migrate_database(), mnesia:create_table(http_bind, - [{ram_copies, [node()]}, - {local_content, true}, + [{ram_copies, [node()]}, {local_content, true}, {attributes, record_info(fields, http_bind)}]), mnesia:add_table_copy(http_bind, node(), ram_copies). migrate_database() -> case catch mnesia:table_info(http_bind, attributes) of - [id, pid, to, hold, wait, process_delay, version] -> - ok; - _ -> - %% Since the stored information is not important, instead - %% of actually migrating data, let's just destroy the table - mnesia:delete_table(http_bind) + [id, pid, to, hold, wait, process_delay, version] -> ok; + _ -> mnesia:delete_table(http_bind) end, - case catch mnesia:table_info(http_bind, local_content) of - false -> - mnesia:delete_table(http_bind); - _ -> - ok + case catch mnesia:table_info(http_bind, local_content) + of + false -> mnesia:delete_table(http_bind); + _ -> ok end. |