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

Some code cleanup (thanks to Stefan Strigler)

SVN Revision: 2213
This commit is contained in:
Badlop 2009-06-16 17:44:47 +00:00
parent 100befc771
commit f248cfd18d

View File

@ -3,12 +3,12 @@
%%% Author : Stefan Strigler <steve@zeank.in-berlin.de> %%% Author : Stefan Strigler <steve@zeank.in-berlin.de>
%%% Purpose : HTTP Binding support (JEP-0124) %%% Purpose : HTTP Binding support (JEP-0124)
%%% Created : 21 Sep 2005 by Stefan Strigler <steve@zeank.in-berlin.de> %%% Created : 21 Sep 2005 by Stefan Strigler <steve@zeank.in-berlin.de>
%%% Id : $Id: ejabberd_http_bind.erl 239 2007-08-03 10:54:00Z sstrigler $ %%% Id : $Id: ejabberd_http_bind.erl 240 2007-08-03 12:05:14Z sstrigler $
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(ejabberd_http_bind). -module(ejabberd_http_bind).
-author('steve@zeank.in-berlin.de'). -author('steve@zeank.in-berlin.de').
-vsn('$Rev: 239 $'). -vsn('$Rev: 240 $').
-behaviour(gen_fsm). -behaviour(gen_fsm).
@ -28,7 +28,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").
@ -113,134 +113,123 @@ peername(_Socket) ->
process_request(Data) -> process_request(Data) ->
case catch parse_request(Data) of case catch parse_request(Data) of
{ok, {ParsedSid, Rid, Key, NewKey, Attrs, Packet}} -> {ok, {"", Rid, Key, NewKey, Attrs, Packet}} ->
XmppDomain = xml:get_attr_s("to",Attrs), case xml:get_attr_s("to",Attrs) of
if "" ->
(ParsedSid == "") and (XmppDomain == "") ->
{200, ?HEADER, "<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 -> XmppDomain ->
Sid = %% create new session
if Sid = sha:sha(term_to_binary({now(), make_ref()})),
(ParsedSid == "") -> {ok, Pid} = start(Sid, Key),
%% create new session ?DEBUG("got pid: ~p", [Pid]),
NewSid = sha:sha(term_to_binary({now(), make_ref()})), Wait = case
{ok, Pid} = start(NewSid, Key), string:to_integer(xml:get_attr_s("wait",Attrs))
?DEBUG("got pid: ~p", [Pid]), of
Wait = case {error, _} ->
string:to_integer(xml:get_attr_s("wait",Attrs)) ?MAX_WAIT;
of {CWait, _} ->
{error, _} -> if
?MAX_WAIT; (CWait > ?MAX_WAIT) ->
{CWait, _} -> ?MAX_WAIT;
if true ->
(CWait > ?MAX_WAIT) -> CWait
?MAX_WAIT; end
true -> end,
CWait Hold = case
end string:to_integer(
end, xml:get_attr_s("hold",Attrs))
Hold = case of
string:to_integer( {error, _} ->
xml:get_attr_s("hold",Attrs)) (?MAX_REQUESTS - 1);
of {CHold, _} ->
{error, _} -> if
(?MAX_REQUESTS - 1); (CHold > (?MAX_REQUESTS - 1)) ->
{CHold, _} -> (?MAX_REQUESTS - 1);
if true ->
(CHold > (?MAX_REQUESTS - 1)) -> CHold
(?MAX_REQUESTS - 1); end
true -> end,
CHold XmppVersion = xml:get_attr_s("xmpp:version", Attrs),
end mnesia:transaction(
end, fun() ->
XmppVersion = xml:get_attr_s("xmpp:version", Attrs), mnesia:write(#http_bind{id = Sid,
mnesia:transaction( pid = Pid,
fun() -> to = {XmppDomain, XmppVersion},
mnesia:write(#http_bind{id = NewSid, wait = Wait,
pid = Pid, hold = Hold})
to = {XmppDomain, XmppVersion}, end),
wait = Wait, handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, Packet, true)
hold = Hold}) end;
end), {ok, {Sid, Rid, Key, NewKey, Attrs, Packet}} ->
StreamStart = if %% old session
(XmppDomain /= "") -> StreamStart =
true; case xml:get_attr_s("xmpp:restart",Attrs) of
true -> "true" ->
false true;
end, _ ->
InPacket = Packet, false
NewSid; end,
true -> Wait = ?MAX_WAIT,
%% old session Hold = (?MAX_REQUESTS - 1),
Type = xml:get_attr_s("type",Attrs), InPacket = case xml:get_attr_s("type",Attrs) of
StreamStart = "terminate" ->
case xml:get_attr_s("xmpp:restart",Attrs) of %% close stream
"true" -> Packet ++ "</stream:stream>";
true; _ ->
_ -> Packet
false end,
end, handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, InPacket, StreamStart);
Wait = ?MAX_WAIT, _ ->
Hold = (?MAX_REQUESTS - 1), {400, ?HEADER, ""}
if
(Type == "terminate") ->
%% terminate session
InPacket = Packet ++ "</stream:stream>";
true ->
InPacket = Packet
end,
ParsedSid
end,
%% ?DEBUG("~n InPacket: ~s ~n", [InPacket]),
case http_put(Sid, Rid, Key, NewKey, Hold, InPacket, StreamStart) of
{error, not_exists} ->
?DEBUG("no session associated with sid: ~p", [Sid]),
{404, ?HEADER, ""};
{error, bad_key} ->
?DEBUG("bad key: ~s", [Key]),
case mnesia:dirty_read({http_bind, Sid}) of
[] ->
{404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop),
{404, ?HEADER, ""}
end;
{error, polling_too_frequently} ->
?DEBUG("polling too frequently: ~p", [Sid]),
case mnesia:dirty_read({http_bind, Sid}) of
[] -> %% unlikely! (?)
{404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop),
{403, ?HEADER, ""}
end;
{repeat, OutPacket} ->
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p",
[OutPacket]),
send_outpacket(Sid, OutPacket);
ok ->
receive_loop(Sid,ParsedSid,Rid,Wait,Hold,Attrs)
end
end;
_ ->
{400, ?HEADER, ""}
end. end.
receive_loop(Sid,ParsedSid,Rid,Wait,Hold,Attrs) -> handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, Packet, StreamStart) ->
case http_put(Sid, Rid, Key, NewKey, Hold, Packet, StreamStart) of
{error, not_exists} ->
?DEBUG("no session associated with sid: ~p", [Sid]),
{404, ?HEADER, ""};
{error, bad_key} ->
?DEBUG("bad key: ~s", [Key]),
case mnesia:dirty_read({http_bind, Sid}) of
[] ->
{404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop),
{404, ?HEADER, ""}
end;
{error, polling_too_frequently} ->
?DEBUG("polling too frequently: ~p", [Sid]),
case mnesia:dirty_read({http_bind, Sid}) of
[] -> %% unlikely! (?)
{404, ?HEADER, ""};
[#http_bind{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(FsmRef,stop),
{403, ?HEADER, ""}
end;
{repeat, OutPacket} ->
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p",
[OutPacket]),
send_outpacket(Sid, OutPacket);
ok ->
receive_loop(Sid, Rid, Wait, Hold, Attrs, StreamStart)
end.
receive_loop(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
receive receive
after 100 -> ok after 100 -> ok
end, end,
prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs). prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart).
prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs) -> prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
case http_get(Sid,Rid) of case http_get(Sid, Rid) of
{error, not_exists} -> {error, not_exists} ->
?DEBUG("no session associated with sid: ~s", [Sid]), ?DEBUG("no session associated with sid: ~s", [Sid]),
{404, ?HEADER, ""}; {404, ?HEADER, ""};
{ok, keep_on_hold} -> {ok, keep_on_hold} ->
receive_loop(Sid,ParsedSid,Rid,Wait,Hold,Attrs); receive_loop(Sid, Rid, Wait, Hold, Attrs, StreamStart);
{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
@ -248,16 +237,18 @@ prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs) ->
{404, ?HEADER, ""}; {404, ?HEADER, ""};
{ok, OutPacket} -> {ok, OutPacket} ->
?DEBUG("OutPacket: ~s", [OutPacket]), ?DEBUG("OutPacket: ~s", [OutPacket]),
if case StreamStart of
Sid == ParsedSid -> false ->
send_outpacket(Sid, OutPacket); send_outpacket(Sid, OutPacket);
true -> true ->
To = xml:get_attr_s("to",Attrs),
OutEls = case xml_stream:parse_element( OutEls = case xml_stream:parse_element(
OutPacket++"</stream:stream>") of OutPacket++"</stream:stream>") of
El when element(1, El) == xmlelement -> El when element(1, El) == xmlelement ->
?DEBUG("~p", [El]),
{xmlelement, _, OutAttrs, Els} = El, {xmlelement, _, OutAttrs, Els} = El,
AuthID = xml:get_attr_s("id", OutAttrs), AuthID = xml:get_attr_s("id", OutAttrs),
From = xml:get_attr_s("from", OutAttrs),
Version = xml:get_attr_s("version", OutAttrs),
StreamError = false, StreamError = false,
case Els of case Els of
[] -> [] ->
@ -269,14 +260,12 @@ prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs) ->
end; end;
{error, _} -> {error, _} ->
AuthID = "", AuthID = "",
From = "",
Version = "",
StreamError = true, StreamError = true,
[] []
end, end,
if if
To == "" ->
{200, ?HEADER, "<body type='terminate' "
"condition='improper-addressing' "
"xmlns='http://jabber.org/protocol/httpbind'/>"};
StreamError == true -> StreamError == true ->
{200, ?HEADER, "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='host-unknown' " "condition='host-unknown' "
@ -290,7 +279,7 @@ prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs) ->
[] -> [] ->
[]; [];
_ -> _ ->
[{"xmpp:version", "1.0"}] [{"xmpp:version", Version}]
end, end,
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( xml:element_to_string(
@ -304,7 +293,7 @@ prepare_response(Sid,ParsedSid,Rid,Wait,Hold,Attrs) ->
integer_to_list(trunc(?MAX_INACTIVITY/1000))}, integer_to_list(trunc(?MAX_INACTIVITY/1000))},
{"polling", ?MIN_POLLING}, {"polling", ?MIN_POLLING},
{"ver", ?BOSH_VERSION}, {"ver", ?BOSH_VERSION},
{"from", To}, {"from", From},
{"secure", "true"} %% we're always being secure {"secure", "true"} %% we're always being secure
] ++ BOSH_attribs,OutEls})} ] ++ BOSH_attribs,OutEls})}
end end
@ -745,18 +734,18 @@ terminate(_Reason, _StateName, StateData) ->
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
http_put(Sid, Rid, Key, NewKey, Hold, Packet, Restart) -> http_put(Sid, Rid, Key, NewKey, Hold, Packet, StreamStart) ->
?DEBUG("http-put",[]), ?DEBUG("http-put",[]),
case mnesia:dirty_read({http_bind, Sid}) of case mnesia:dirty_read({http_bind, Sid}) of
[] -> [] ->
?DEBUG("not found",[]), ?DEBUG("not found",[]),
{error, not_exists}; {error, not_exists};
[#http_bind{pid = FsmRef,to=StreamTo}] -> [#http_bind{pid = FsmRef,to={To, Version}}] ->
case Restart of case StreamStart of
true -> true ->
?DEBUG("restart requested for ~s", [To]), ?DEBUG("restart requested for ~s", [To]),
gen_fsm:sync_send_all_state_event( gen_fsm:sync_send_all_state_event(
FsmRef, {http_put, Rid, Key, NewKey, Hold, Packet, StreamTo}); FsmRef, {http_put, Rid, Key, NewKey, Hold, Packet, {To, Version}});
_ -> _ ->
gen_fsm:sync_send_all_state_event( gen_fsm:sync_send_all_state_event(
FsmRef, {http_put, Rid, Key, NewKey, Hold, Packet, ""}) FsmRef, {http_put, Rid, Key, NewKey, Hold, Packet, ""})