25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Make use of new api for plugable http based services (thanks to Stefan Strigler)

SVN Revision: 2262
This commit is contained in:
Badlop 2009-06-16 18:23:45 +00:00
parent dd8e4c620b
commit 7d62dff7e5
2 changed files with 71 additions and 73 deletions

View File

@ -26,7 +26,7 @@
close/1, close/1,
process_request/1]). process_request/1]).
%%-define(ejabberd_debug, true). -define(ejabberd_debug, true).
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
@ -54,7 +54,7 @@
}). }).
%-define(DBGFSM, true). %%-define(DBGFSM, true).
-ifdef(DBGFSM). -ifdef(DBGFSM).
-define(FSMOPTS, [{debug, [trace]}]). -define(FSMOPTS, [{debug, [trace]}]).
@ -64,10 +64,10 @@
-define(MAX_REQUESTS, 2). % number of simultaneous requests -define(MAX_REQUESTS, 2). % number of simultaneous requests
-define(MIN_POLLING, "2"). % don't poll faster than that or we will shoot you -define(MIN_POLLING, "2"). % don't poll faster than that or we will shoot you
-define(MAX_WAIT, 60). % max num of secs to keep a request on hold -define(MAX_WAIT, 3600). % max num of secs to keep a request on hold
-define(MAX_INACTIVITY, 30000). % msecs to wait before terminating idle sessions -define(MAX_INACTIVITY, 30000). % msecs to wait before terminating idle sessions
-define(CT, {"Content-Type", "text/xml; charset=utf-8"}). -define(CT, {"Content-Type", "text/xml; charset=utf-8"}).
-define(BAD_REQUEST, ?CT). -define(HEADER, [?CT,{"X-Sponsored-By", "http://mabber.com"}]).
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
@ -88,7 +88,7 @@ send({http_bind, FsmRef}, Packet) ->
setopts({http_bind, FsmRef}, Opts) -> setopts({http_bind, FsmRef}, Opts) ->
case lists:member({active, once}, Opts) of case lists:member({active, once}, Opts) of
true -> true ->
gen_fsm:sync_send_all_state_event(FsmRef, activate); gen_fsm:send_all_state_event(FsmRef, {activate, self()});
_ -> _ ->
ok ok
end. end.
@ -100,14 +100,13 @@ close({http_bind, FsmRef}) ->
catch gen_fsm:sync_send_all_state_event(FsmRef, close). catch gen_fsm:sync_send_all_state_event(FsmRef, close).
process_request(#request{path = [], process_request(Data) ->
data = Data}) ->
case catch parse_request(Data) of case catch parse_request(Data) of
{ok, ID1, RID, Key, NewKey, Attrs, Packet} -> {ok, ID1, RID, Key, NewKey, Attrs, Packet} ->
XmppDomain = xml:get_attr_s("to",Attrs), XmppDomain = xml:get_attr_s("to",Attrs),
if if
(ID1 == "") and (XmppDomain == "") -> (ID1 == "") and (XmppDomain == "") ->
{200, [?CT], "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='improper-addressing' " "condition='improper-addressing' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='http://jabber.org/protocol/httpbind'/>"};
true -> true ->
@ -116,6 +115,7 @@ process_request(#request{path = [],
%% create new session %% create new session
NewID = sha:sha(term_to_binary({now(), make_ref()})), NewID = sha:sha(term_to_binary({now(), make_ref()})),
{ok, Pid} = start(NewID, Key), {ok, Pid} = start(NewID, Key),
?DEBUG("got pid: ~p", [Pid]),
Wait = case Wait = case
string:to_integer(xml:get_attr_s("wait",Attrs)) string:to_integer(xml:get_attr_s("wait",Attrs))
of of
@ -184,24 +184,24 @@ process_request(#request{path = [],
case http_put(ID, RID, Key, NewKey, Hold, InPacket, StreamStart) of case http_put(ID, RID, Key, NewKey, Hold, InPacket, StreamStart) of
{error, not_exists} -> {error, not_exists} ->
?DEBUG("no session associated with sid: ~p", [ID]), ?DEBUG("no session associated with sid: ~p", [ID]),
{404, [?BAD_REQUEST], ""}; {404, ?HEADER, ""};
{error, bad_key} -> {error, bad_key} ->
?DEBUG("bad key: ~s", [Key]), ?DEBUG("bad key: ~s", [Key]),
case mnesia:dirty_read({http_bind, ID}) of case mnesia:dirty_read({http_bind, ID}) of
[] -> [] ->
{404, [?BAD_REQUEST], ""}; {404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] -> [#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop), gen_fsm:sync_send_all_state_event(FsmRef,stop),
{404, [?BAD_REQUEST], ""} {404, ?HEADER, ""}
end; end;
{error, polling_too_frequently} -> {error, polling_too_frequently} ->
?DEBUG("polling too frequently: ~p", [ID]), ?DEBUG("polling too frequently: ~p", [ID]),
case mnesia:dirty_read({http_bind, ID}) of case mnesia:dirty_read({http_bind, ID}) of
[] -> %% unlikely! (?) [] -> %% unlikely! (?)
{404, [?BAD_REQUEST], ""}; {404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] -> [#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop), gen_fsm:sync_send_all_state_event(FsmRef,stop),
{403, [?BAD_REQUEST], ""} {403, ?HEADER, ""}
end; end;
{repeat, OutPacket} -> {repeat, OutPacket} ->
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p", ?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p",
@ -212,11 +212,8 @@ process_request(#request{path = [],
end end
end; end;
_ -> _ ->
{400, [?BAD_REQUEST], ""} {400, ?HEADER, ""}
end; end.
process_request(_Request) ->
{400, [], {xmlelement, "h1", [],
[{xmlcdata, "400 Bad Request"}]}}.
receive_loop(ID,ID1,RID,Wait,Hold,Attrs) -> receive_loop(ID,ID1,RID,Wait,Hold,Attrs) ->
receive receive
@ -228,14 +225,14 @@ prepare_response(ID,ID1,RID,Wait,Hold,Attrs) ->
case http_get(ID,RID) of case http_get(ID,RID) of
{error, not_exists} -> {error, not_exists} ->
?DEBUG("no session associated with sid: ~s", ID), ?DEBUG("no session associated with sid: ~s", ID),
{404, [?BAD_REQUEST], ""}; {404, ?HEADER, ""};
{ok, keep_on_hold} -> {ok, keep_on_hold} ->
receive_loop(ID,ID1,RID,Wait,Hold,Attrs); receive_loop(ID,ID1,RID,Wait,Hold,Attrs);
{ok, cancel} -> {ok, cancel} ->
%% actually it would be better if we could completely %% actually it would be better if we could completely
%% cancel this request, but then we would have to hack %% cancel this request, but then we would have to hack
%% ejabberd_http and I'm too lazy now %% ejabberd_http and I'm too lazy now
{404, [?BAD_REQUEST], ""}; {404, ?HEADER, ""};
{ok, OutPacket} -> {ok, OutPacket} ->
?DEBUG("OutPacket: ~s", [OutPacket]), ?DEBUG("OutPacket: ~s", [OutPacket]),
if if
@ -264,15 +261,15 @@ prepare_response(ID,ID1,RID,Wait,Hold,Attrs) ->
end, end,
if if
To == "" -> To == "" ->
{200, [?CT], "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='improper-addressing' " "condition='improper-addressing' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='http://jabber.org/protocol/httpbind'/>"};
StreamError == true -> StreamError == true ->
{200, [?CT], "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='host-unknown' " "condition='host-unknown' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='http://jabber.org/protocol/httpbind'/>"};
true -> true ->
{200, [?CT], {200, ?HEADER,
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
@ -292,13 +289,13 @@ prepare_response(ID,ID1,RID,Wait,Hold,Attrs) ->
send_outpacket(ID, OutPacket) -> send_outpacket(ID, OutPacket) ->
case OutPacket of case OutPacket of
"" -> "" ->
{200, [?CT], "<body xmlns='http://jabber.org/protocol/httpbind'/>"}; {200, ?HEADER, "<body xmlns='http://jabber.org/protocol/httpbind'/>"};
"</stream:stream>" -> "</stream:stream>" ->
case mnesia:dirty_read({http_bind, ID}) of case mnesia:dirty_read({http_bind, ID}) of
[#http_bind{pid = FsmRef}] -> [#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop) gen_fsm:sync_send_all_state_event(FsmRef,stop)
end, end,
{200, [?CT], "<body xmlns='http://jabber.org/protocol/httpbind'/>"}; {200, ?HEADER, "<body xmlns='http://jabber.org/protocol/httpbind'/>"};
_ -> _ ->
case xml_stream:parse_element("<body>" case xml_stream:parse_element("<body>"
++ OutPacket ++ OutPacket
@ -316,7 +313,7 @@ send_outpacket(ID, OutPacket) ->
"http://jabber.org/protocol/httpbind"}], "http://jabber.org/protocol/httpbind"}],
TypedEls})] TypedEls})]
), ),
{200, [?CT], {200, ?HEADER,
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
@ -367,12 +364,12 @@ send_outpacket(ID, OutPacket) ->
end, end,
case StreamErrCond of case StreamErrCond of
null -> null ->
{200, [?CT], {200, ?HEADER,
"<body type='terminate' " "<body type='terminate' "
"condition='internal-server-error' " "condition='internal-server-error' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='http://jabber.org/protocol/httpbind'/>"};
_ -> _ ->
{200, [?CT], {200, ?HEADER,
"<body type='terminate' " "<body type='terminate' "
"condition='remote-stream-error' " "condition='remote-stream-error' "
"xmlns='http://jabber.org/protocol/httpbind'>" ++ "xmlns='http://jabber.org/protocol/httpbind'>" ++
@ -380,7 +377,7 @@ send_outpacket(ID, OutPacket) ->
"</body>"} "</body>"}
end; end;
true -> true ->
{200, [?CT], {200, ?HEADER,
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
@ -404,8 +401,9 @@ send_outpacket(ID, OutPacket) ->
init([ID, Key]) -> init([ID, Key]) ->
?INFO_MSG("started: ~p", [{ID, Key}]), ?INFO_MSG("started: ~p", [{ID, Key}]),
Opts = [], % TODO Opts = [], % TODO
{ok, C2SPid} = ejabberd_c2s:start({?MODULE, {http_bind, self()}}, Opts), ejabberd_socket:start(ejabberd_c2s, ?MODULE, {http_bind, self()}, Opts),
ejabberd_c2s:become_controller(C2SPid), % {ok, C2SPid} = ejabberd_c2s:start({?MODULE, {http_bind, self()}}, Opts),
% ejabberd_c2s:become_controller(C2SPid),
Timer = erlang:start_timer(?MAX_INACTIVITY, self(), []), Timer = erlang:start_timer(?MAX_INACTIVITY, self(), []),
{ok, loop, #state{id = ID, {ok, loop, #state{id = ID,
key = Key, key = Key,
@ -438,6 +436,20 @@ init([ID, Key]) ->
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_event({activate, From}, StateName, StateData) ->
case StateData#state.input of
"" ->
{next_state, StateName, StateData#state{
waiting_input = {From, ok}}};
Input ->
Receiver = From,
Receiver ! {tcp, {http_bind, self()}, list_to_binary(Input)},
{next_state, StateName, StateData#state{
input = "",
waiting_input = false,
last_receiver = Receiver}}
end;
handle_event(_Event, StateName, StateData) -> handle_event(_Event, StateName, StateData) ->
{next_state, StateName, StateData}. {next_state, StateName, StateData}.
@ -455,19 +467,6 @@ handle_sync_event({send, Packet}, _From, StateName, StateData) ->
Reply = ok, Reply = ok,
{reply, Reply, StateName, StateData#state{output = Output}}; {reply, Reply, StateName, StateData#state{output = Output}};
handle_sync_event(activate, From, StateName, StateData) ->
case StateData#state.input of
"" ->
{reply, ok, StateName, StateData#state{
waiting_input = From}};
Input ->
From ! {tcp, {http_bind, self()}, list_to_binary(Input)},
{reply, ok, StateName, StateData#state{
input = "",
waiting_input = false,
last_receiver = From}}
end;
handle_sync_event(stop, _From, _StateName, StateData) -> handle_sync_event(stop, _From, _StateName, StateData) ->
Reply = ok, Reply = ok,
{stop, normal, Reply, StateData}; {stop, normal, Reply, StateData};
@ -751,6 +750,16 @@ parse_request(Data) ->
case xml_stream:parse_element(Data) of case xml_stream:parse_element(Data) of
El when element(1, El) == xmlelement -> El when element(1, El) == xmlelement ->
{xmlelement, Name, Attrs, Els} = El, {xmlelement, Name, Attrs, Els} = El,
FixedEls =
lists:filter(
fun(I) ->
case I of
{xmlelement, _, _, _} ->
true;
_ ->
false
end
end, Els),
ID = xml:get_attr_s("sid",Attrs), ID = xml:get_attr_s("sid",Attrs),
{RID,_X} = string:to_integer(xml:get_attr_s("rid",Attrs)), {RID,_X} = string:to_integer(xml:get_attr_s("rid",Attrs)),
Key = xml:get_attr_s("key",Attrs), Key = xml:get_attr_s("key",Attrs),
@ -760,13 +769,12 @@ parse_request(Data) ->
EXmlns = xml:get_tag_attr_s("xmlns",E), EXmlns = xml:get_tag_attr_s("xmlns",E),
if if
EXmlns == "jabber:client" -> EXmlns == "jabber:client" ->
xml:remove_tag_attr("xmlns",E); remove_tag_attr("xmlns",E);
true -> true ->
ok ok
end end
end, Els), end, FixedEls),
Packet = [xml:element_to_string(E) || E <- Els], Packet = [xml:element_to_string(E) || E <- FixedEls],
?DEBUG("ns fixed packet(s): ~s", [Packet]),
if if
Name /= "body" -> Name /= "body" ->
{error, bad_request}; {error, bad_request};
@ -802,3 +810,13 @@ elements_to_string([]) ->
[]; [];
elements_to_string([El | Els]) -> elements_to_string([El | Els]) ->
xml:element_to_string(El) ++ elements_to_string(Els). xml:element_to_string(El) ++ elements_to_string(Els).
remove_tag_attr(Attr, El) ->
case El of
{xmlelement, Name, Attrs, Els} ->
Attrs1 = lists:keydelete(Attr, 1, Attrs),
{xmlelement, Name, Attrs1, Els};
_ ->
El
end.

View File

@ -17,8 +17,7 @@
-define(MOD_HTTP_BIND_VERSION, "1.0"). -define(MOD_HTTP_BIND_VERSION, "1.0").
-vsn(?MOD_HTTP_BIND_VERSION). -vsn(?MOD_HTTP_BIND_VERSION).
-define(ejabberd_debug, true).
%%-define(ejabberd_debug, true).
-behaviour(gen_mod). -behaviour(gen_mod).
@ -44,7 +43,7 @@ process([], #request{method = 'POST',
[{xmlcdata, "400 Bad Request"}]}}; [{xmlcdata, "400 Bad Request"}]}};
process([], #request{method = 'POST', process([], #request{method = 'POST',
data = Data}) -> data = Data}) ->
?DEBUG("Incoming data: ~s", [Data]), ?DEBUG("Data: '~p'", [Data]),
ejabberd_http_bind:process_request(Data); ejabberd_http_bind:process_request(Data);
process([], #request{method = 'GET', process([], #request{method = 'GET',
data = []}) -> data = []}) ->
@ -75,28 +74,9 @@ process(_Path, _Request) ->
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% BEHAVIOUR CALLBACKS %%% BEHAVIOUR CALLBACKS
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
start(_Host, _Opts) -> start(_Host, _Opts) ->
HTTPBindSupervisor = ok.
{ejabberd_http_bind_sup,
{ejabberd_tmp_sup, start_link,
[ejabberd_http_bind_sup, ejabberd_http_bind]},
permanent,
infinity,
supervisor,
[ejabberd_tmp_sup]},
case supervisor:start_child(ejabberd_sup, HTTPBindSupervisor) of
{ok, _Pid} ->
ok;
{ok, _Pid, _Info} ->
ok;
{error, Error} ->
{'EXIT', {start_child_error, Error}}
end.
stop(_Host) -> stop(_Host) ->
case supervisor:terminate_child(ejabberd_sup, ejabberd_http_bind_sup) of ok.
ok ->
ok;
{error, Error} ->
{'EXIT', {terminate_child_error, Error}}
end.