25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +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,20 +113,16 @@ 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 =
if
(ParsedSid == "") ->
%% create new session %% create new session
NewSid = sha:sha(term_to_binary({now(), make_ref()})), Sid = sha:sha(term_to_binary({now(), make_ref()})),
{ok, Pid} = start(NewSid, Key), {ok, Pid} = start(Sid, Key),
?DEBUG("got pid: ~p", [Pid]), ?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))
@ -158,23 +154,16 @@ process_request(Data) ->
XmppVersion = xml:get_attr_s("xmpp:version", Attrs), XmppVersion = xml:get_attr_s("xmpp:version", Attrs),
mnesia:transaction( mnesia:transaction(
fun() -> fun() ->
mnesia:write(#http_bind{id = NewSid, mnesia:write(#http_bind{id = Sid,
pid = Pid, pid = Pid,
to = {XmppDomain, XmppVersion}, to = {XmppDomain, XmppVersion},
wait = Wait, wait = Wait,
hold = Hold}) hold = Hold})
end), end),
StreamStart = if handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, Packet, true)
(XmppDomain /= "") -> end;
true; {ok, {Sid, Rid, Key, NewKey, Attrs, Packet}} ->
true ->
false
end,
InPacket = Packet,
NewSid;
true ->
%% old session %% old session
Type = xml:get_attr_s("type",Attrs),
StreamStart = StreamStart =
case xml:get_attr_s("xmpp:restart",Attrs) of case xml:get_attr_s("xmpp:restart",Attrs) of
"true" -> "true" ->
@ -184,17 +173,20 @@ process_request(Data) ->
end, end,
Wait = ?MAX_WAIT, Wait = ?MAX_WAIT,
Hold = (?MAX_REQUESTS - 1), Hold = (?MAX_REQUESTS - 1),
if InPacket = case xml:get_attr_s("type",Attrs) of
(Type == "terminate") -> "terminate" ->
%% terminate session %% close stream
InPacket = Packet ++ "</stream:stream>"; Packet ++ "</stream:stream>";
true -> _ ->
InPacket = Packet Packet
end, end,
ParsedSid handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, InPacket, StreamStart);
end, _ ->
%% ?DEBUG("~n InPacket: ~s ~n", [InPacket]), {400, ?HEADER, ""}
case http_put(Sid, Rid, Key, NewKey, Hold, InPacket, StreamStart) of end.
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} -> {error, not_exists} ->
?DEBUG("no session associated with sid: ~p", [Sid]), ?DEBUG("no session associated with sid: ~p", [Sid]),
{404, ?HEADER, ""}; {404, ?HEADER, ""};
@ -221,26 +213,23 @@ process_request(Data) ->
[OutPacket]), [OutPacket]),
send_outpacket(Sid, OutPacket); send_outpacket(Sid, OutPacket);
ok -> ok ->
receive_loop(Sid,ParsedSid,Rid,Wait,Hold,Attrs) receive_loop(Sid, Rid, Wait, Hold, Attrs, StreamStart)
end
end;
_ ->
{400, ?HEADER, ""}
end. end.
receive_loop(Sid,ParsedSid,Rid,Wait,Hold,Attrs) ->
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, ""})