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

Started code clean-up (EJAB-936)(thanks to Michael Remond)

SVN Revision: 2255
This commit is contained in:
Badlop 2009-06-16 17:48:13 +00:00
parent caf9c4f65e
commit ada531bd8d

View File

@ -4,7 +4,7 @@
%%% Purpose : Implements XMPP over BOSH (XEP-0205) (formerly known as
%%% HTTP Binding)
%%% Created : 21 Sep 2005 by Stefan Strigler <steve@zeank.in-berlin.de>
%%% Id : $Id: ejabberd_http_bind.erl 953 2009-05-07 10:40:40Z alexey $
%%% Id : $Id: ejabberd_http_bind.erl 958 2009-05-12 14:43:41Z mremond $
%%%----------------------------------------------------------------------
-module(ejabberd_http_bind).
@ -135,6 +135,7 @@ process_request(Data, IP) ->
{ok, {"", Rid, Attrs, Payload}} ->
case xml:get_attr_s("to",Attrs) of
"" ->
?ERROR_MSG("Session not created (Improper addressing)", []),
{200, ?HEADER, "<body type='terminate' "
"condition='improper-addressing' "
"xmlns='" ++ ?NS_HTTP_BIND ++ "'/>"};
@ -168,14 +169,13 @@ process_request(Data, IP) ->
end,
handle_http_put(Sid, Rid, Attrs, Payload2, StreamStart, IP);
_ ->
?ERROR_MSG("Received bad request: ~p", [Data]),
{400, ?HEADER, ""}
end.
handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs, Payload, IP) ->
?DEBUG("got pid: ~p", [Pid]),
Wait = case
string:to_integer(xml:get_attr_s("wait",Attrs))
of
Wait = case string:to_integer(xml:get_attr_s("wait",Attrs)) of
{error, _} ->
?MAX_WAIT;
{CWait, _} ->
@ -186,9 +186,7 @@ handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs, Payload, IP) ->
CWait
end
end,
Hold = case
string:to_integer(xml:get_attr_s("hold",Attrs))
of
Hold = case string:to_integer(xml:get_attr_s("hold",Attrs)) of
{error, _} ->
(?MAX_REQUESTS - 1);
{CHold, _} ->
@ -308,12 +306,7 @@ handle_sync_event({send, Packet}, _From, StateName, StateData) ->
Output = [StateData#state.output | Packet],
if
StateData#state.http_receiver /= undefined ->
if
StateData#state.timer /= undefined ->
cancel_timer(StateData#state.timer);
true ->
ok
end,
cancel_timer(StateData#state.timer),
Timer = if
StateData#state.pause > 0 ->
erlang:start_timer(
@ -329,12 +322,7 @@ handle_sync_event({send, Packet}, _From, StateName, StateData) ->
{ok, Output}
end,
gen_fsm:reply(StateData#state.http_receiver, HTTPReply),
if
StateData#state.wait_timer /= undefined ->
cancel_timer(StateData#state.wait_timer);
true ->
ok
end,
cancel_timer(StateData#state.wait_timer),
Reply = ok,
{reply, Reply, StateName,
StateData#state{output = [],
@ -409,12 +397,7 @@ handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
true ->
ok
end,
if
StateData#state.wait_timer /= undefined ->
cancel_timer(StateData#state.wait_timer);
true ->
ok
end,
cancel_timer(StateData#state.wait_timer),
{TMegSec, TSec, TMSec} = now(),
TNow = (TMegSec * 1000000 + TSec) * 1000000 + TMSec,
if
@ -430,12 +413,7 @@ handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
wait_timer = WaitTimer,
timer = undefined}};
(StateData#state.input == "cancel") ->
if
StateData#state.timer /= undefined ->
cancel_timer(StateData#state.timer);
true ->
ok
end,
cancel_timer(StateData#state.timer),
Timer = if
StateData#state.pause > 0 ->
erlang:start_timer(
@ -451,12 +429,7 @@ handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
wait_timer = undefined,
timer = Timer}};
true ->
if
StateData#state.timer /= undefined ->
cancel_timer(StateData#state.timer);
true ->
ok
end,
cancel_timer(StateData#state.timer),
Timer = if
StateData#state.pause > 0 ->
erlang:start_timer(
@ -508,19 +481,14 @@ code_change(_OldVsn, StateName, StateData, _Extra) ->
%%----------------------------------------------------------------------
handle_info({timeout, Timer, _}, _StateName,
#state{timer = Timer} = StateData) ->
?DEBUG("ding dong", []),
?DEBUG("Session timeout. Closing the HTTP bind Session.", []),
{stop, normal, StateData};
handle_info({timeout, WaitTimer, _}, StateName,
#state{wait_timer = WaitTimer} = StateData) ->
if
StateData#state.http_receiver /= undefined ->
if
StateData#state.timer /= undefined ->
cancel_timer(StateData#state.timer);
true ->
ok
end,
cancel_timer(StateData#state.timer),
Timer = if
StateData#state.pause > 0 ->
erlang:start_timer(
@ -655,12 +623,7 @@ process_http_put({http_put, Rid, Attrs, Payload, Hold, StreamTo, IP},
?DEBUG("reqlist: ~p", [ReqList]),
%% setup next timer
if
StateData#state.timer /= undefined ->
cancel_timer(StateData#state.timer);
true ->
ok
end,
cancel_timer(StateData#state.timer),
Timer = if
Pause > 0 ->
erlang:start_timer(
@ -743,13 +706,13 @@ process_buffered_request(Reply, StateName, StateData) ->
handle_http_put(Sid, Rid, Attrs, Payload, StreamStart, IP) ->
case http_put(Sid, Rid, Attrs, Payload, StreamStart, IP) of
{error, not_exists} ->
?DEBUG("no session associated with sid: ~p", [Sid]),
?ERROR_MSG("no session associated with sid: ~p", [Sid]),
{404, ?HEADER, ""};
{{error, Reason}, Sess} ->
?ERROR_MSG("Error on HTTP put. Reason: ~p", [Reason]),
handle_http_put_error(Reason, Sess);
{{repeat, OutPacket}, Sess} ->
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p",
[OutPacket]),
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p", [OutPacket]),
send_outpacket(Sess, OutPacket);
{ok, Sess} ->
prepare_response(Sess, Rid, Attrs, StreamStart)
@ -802,17 +765,19 @@ handle_http_put_error(Reason, #http_bind{pid=FsmRef}) ->
gen_fsm:sync_send_all_state_event(FsmRef,stop),
case Reason of
not_exists -> %% bad rid
?ERROR_MSG("Closing HTTP bind session (Bad rid).", []),
{404, ?HEADER, ""};
bad_key ->
?ERROR_MSG("Closing HTTP bind session (Bad key).", []),
{404, ?HEADER, ""};
polling_too_frequently ->
?ERROR_MSG("Closing HTTP bind session (User polling too frequently).", []),
{403, ?HEADER, ""}
end.
prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
Rid, _, StreamStart) ->
receive after 100 -> ok end,
receive after 100 -> ok end, %% TODO: Why is this needed. Argh. Bad programming practice.
case catch http_get(Sess, Rid) of
{ok, cancel} ->
%% actually it would be better if we could completely
@ -1013,8 +978,7 @@ send_outpacket(#http_bind{pid = FsmRef}, OutPacket) ->
end.
parse_request(Data) ->
?DEBUG("--- incoming data --- ~n~s~n --- END --- ",
[Data]),
?DEBUG("--- incoming data --- ~n~s~n --- END --- ", [Data]),
case xml_stream:parse_element(Data) of
El when element(1, El) == xmlelement ->
{xmlelement, Name, Attrs, Els} = El,
@ -1029,6 +993,7 @@ parse_request(Data) ->
{'EXIT', _} ->
{error, bad_request};
Rid ->
%% I guess this is to remove XMLCDATA:
FixedEls =
lists:filter(
fun(I) ->
@ -1039,19 +1004,21 @@ parse_request(Data) ->
false
end
end, Els),
lists:map(
fun(E) ->
EXmlns = xml:get_tag_attr_s("xmlns",E),
if
EXmlns == ?NS_CLIENT ->
remove_tag_attr("xmlns",E);
true ->
ok
end
end, FixedEls),
Payload = [xml:element_to_string(E) ||
E <- FixedEls],
%% MR: I commented this code, because it is not used.
%% lists:map(
%% fun(E) ->
%% EXmlns = xml:get_tag_attr_s("xmlns",E),
%% if
%% EXmlns == ?NS_CLIENT ->
%% remove_tag_attr("xmlns",E);
%% true ->
%% ok
%% end
%% end, FixedEls),
Payload = [xml:element_to_string(E) || E <- FixedEls],
Sid = xml:get_attr_s("sid",Attrs),
%% MR: I do not think we need to extract
%% Sid. We should have it somewhere else:
{ok, {Sid, Rid, Attrs, Payload}}
end
end;
@ -1059,6 +1026,9 @@ parse_request(Data) ->
{error, bad_request}
end.
%% Cancel timer and empty message queue.
cancel_timer(undefined) ->
ok;
cancel_timer(Timer) ->
erlang:cancel_timer(Timer),
receive
@ -1068,6 +1038,7 @@ cancel_timer(Timer) ->
ok
end.
%% TODO: Use tail recursion and list reverse ?
elements_to_string([]) ->
[];
elements_to_string([El | Els]) ->
@ -1085,24 +1056,20 @@ get_max_inactivity({Host, _}, Default) ->
get_max_inactivity(_, Default) ->
Default.
remove_tag_attr(Attr, El) ->
case El of
{xmlelement, Name, Attrs, Els} ->
remove_tag_attr(Attr, {xmlelement, Name, Attrs, Els}) ->
Attrs1 = lists:keydelete(Attr, 1, Attrs),
{xmlelement, Name, Attrs1, Els};
_ ->
El
end.
remove_tag_attr(Attr, El) ->
El.
check_default_xmlns({xmlelement, Name, Attrs, Els} = El) ->
EXmlns = xml:get_tag_attr_s("xmlns", El),
if
EXmlns == "" ->
{xmlelement, Name, [{"xmlns", ?NS_CLIENT} | Attrs], Els};
true ->
El
case xml:get_tag_attr_s("xmlns", El) of
"" -> {xmlelement, Name, [{"xmlns", ?NS_CLIENT} | Attrs], Els};
_ -> El
end.
%% Check that mod_http_bind has been defined in config file.
%% Print a warning in log file if this is not the case.
check_bind_module(XmppDomain) ->
case gen_mod:is_loaded(XmppDomain, mod_http_bind) of
true -> ok;