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

Make use of macros for namespaces. Some code cleanup. (thanks to Stefan Strigler)

SVN Revision: 2279
This commit is contained in:
Badlop 2009-06-16 18:24:54 +00:00
parent b79473b2f5
commit 702978fa4b

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 243 2007-08-03 13:48:49Z sstrigler $ %%% Id : $Id: ejabberd_http_bind.erl 272 2007-08-15 12:11:06Z sstrigler $
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(ejabberd_http_bind). -module(ejabberd_http_bind).
-author('steve@zeank.in-berlin.de'). -author('steve@zeank.in-berlin.de').
-vsn('$Rev: 243 $'). -vsn('$Rev: 272 $').
-behaviour(gen_fsm). -behaviour(gen_fsm).
@ -65,6 +65,9 @@
-endif. -endif.
-define(BOSH_VERSION, "1.6"). -define(BOSH_VERSION, "1.6").
-define(NS_CLIENT, "jabber:client").
-define(NS_BOSH, "urn:xmpp:xbosh").
-define(NS_HTTP_BIND, "http://jabber.org/protocol/httpbind").
-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 -define(MIN_POLLING, "2"). % don't poll faster than that or we will
@ -118,7 +121,7 @@ process_request(Data) ->
"" -> "" ->
{200, ?HEADER, "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='improper-addressing' " "condition='improper-addressing' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='" ++ ?NS_HTTP_BIND ++ "'/>"};
XmppDomain -> XmppDomain ->
%% create new session %% create new session
Sid = sha:sha(term_to_binary({now(), make_ref()})), Sid = sha:sha(term_to_binary({now(), make_ref()})),
@ -156,11 +159,13 @@ process_request(Data) ->
fun() -> fun() ->
mnesia:write(#http_bind{id = Sid, 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),
handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, Packet, true) handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold,
Attrs, Packet, true)
end; end;
{ok, {Sid, Rid, Key, NewKey, Attrs, Packet}} -> {ok, {Sid, Rid, Key, NewKey, Attrs, Packet}} ->
%% old session %% old session
@ -180,12 +185,14 @@ process_request(Data) ->
_ -> _ ->
Packet Packet
end, end,
handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, InPacket, StreamStart); handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs,
InPacket, StreamStart);
_ -> _ ->
{400, ?HEADER, ""} {400, ?HEADER, ""}
end. end.
handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs, Packet, StreamStart) -> handle_http_put(Sid, Rid, Key, NewKey, Wait, Hold, Attrs,
Packet, StreamStart) ->
case http_put(Sid, Rid, Key, NewKey, Hold, Packet, StreamStart) of 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]),
@ -228,7 +235,7 @@ prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
{error, not_exists} -> {error, not_exists} ->
case xml:get_attr_s("type", Attrs) of case xml:get_attr_s("type", Attrs) of
"terminate" -> "terminate" ->
{200, ?HEADER, "<body xmlns='http://jabber.org/protocol/httpbind'/>"}; {200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND++"'/>"};
_ -> _ ->
?DEBUG("no session associated with sid: ~s", [Sid]), ?DEBUG("no session associated with sid: ~s", [Sid]),
{404, ?HEADER, ""} {404, ?HEADER, ""}
@ -239,47 +246,56 @@ prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
%% 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, ?HEADER, ""}; {200, ?HEADER, "<body type='error' xmlns='"++?NS_HTTP_BIND++"'/>"};
{ok, OutPacket} -> {ok, OutPacket} ->
?DEBUG("OutPacket: ~s", [OutPacket]), ?DEBUG("OutPacket: ~s", [OutPacket]),
case StreamStart of case StreamStart of
false -> false ->
send_outpacket(Sid, OutPacket); send_outpacket(Sid, OutPacket);
true -> true ->
OutEls = case xml_stream:parse_element( OutEls =
OutPacket++"</stream:stream>") of case xml_stream:parse_element(
El when element(1, El) == xmlelement -> OutPacket++"</stream:stream>") of
?DEBUG("~p", [El]), El when element(1, El) == xmlelement ->
{xmlelement, _, OutAttrs, Els} = El, ?DEBUG("~p", [El]),
AuthID = xml:get_attr_s("id", OutAttrs), {xmlelement, _, OutAttrs, Els} = El,
From = xml:get_attr_s("from", OutAttrs), AuthID = xml:get_attr_s("id", OutAttrs),
Version = xml:get_attr_s("version", OutAttrs), From = xml:get_attr_s("from", OutAttrs),
StreamError = false, Version = xml:get_attr_s("version", OutAttrs),
case Els of StreamError = false,
[] -> case Els of
[]; [] ->
[{xmlelement, "stream:features", StreamAttribs, StreamEls} | StreamTail] -> [];
[{xmlelement, "stream:features", [{"xmlns:stream","http://etherx.jabber.org/streams"}] ++ StreamAttribs, StreamEls}] ++ StreamTail; [{xmlelement, "stream:features",
Xml -> StreamAttribs, StreamEls}
Xml | StreamTail] ->
end; [{xmlelement, "stream:features",
{error, _} -> [{"xmlns:stream",
AuthID = "", ?NS_STREAM}
From = "", ]
Version = "", ++ StreamAttribs,
StreamError = true, StreamEls
[] }] ++ StreamTail;
end, Xml ->
Xml
end;
{error, _} ->
AuthID = "",
From = "",
Version = "",
StreamError = true,
[]
end,
if if
StreamError == true -> StreamError == true ->
{200, ?HEADER, "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
"condition='host-unknown' " "condition='host-unknown' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='"++?NS_HTTP_BIND++"'/>"};
true -> true ->
BOSH_attribs = BOSH_attribs =
[{"authid", AuthID}, [{"authid", AuthID},
{"xmlns:xmpp", "urn:xmpp:xbosh"}, {"xmlns:xmpp", ?NS_BOSH},
{"xmlns:stream","http://etherx.jabber.org/streams"}] ++ {"xmlns:stream", ?NS_STREAM}] ++
case OutEls of case OutEls of
[] -> [] ->
[]; [];
@ -290,7 +306,7 @@ prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
"http://jabber.org/protocol/httpbind"}, ?NS_HTTP_BIND},
{"sid",Sid}, {"sid",Sid},
{"wait", integer_to_list(Wait)}, {"wait", integer_to_list(Wait)},
{"requests", integer_to_list(Hold+1)}, {"requests", integer_to_list(Hold+1)},
@ -308,13 +324,13 @@ prepare_response(Sid, Rid, Wait, Hold, Attrs, StreamStart) ->
send_outpacket(Sid, OutPacket) -> send_outpacket(Sid, OutPacket) ->
case OutPacket of case OutPacket of
"" -> "" ->
{200, ?HEADER, "<body xmlns='http://jabber.org/protocol/httpbind'/>"}; {200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND++"'/>"};
"</stream:stream>" -> "</stream:stream>" ->
case mnesia:dirty_read({http_bind, Sid}) of case mnesia:dirty_read({http_bind, Sid}) 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, ?HEADER, "<body xmlns='http://jabber.org/protocol/httpbind'/>"}; {200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND++"'/>"};
_ -> _ ->
case xml_stream:parse_element("<body>" case xml_stream:parse_element("<body>"
++ OutPacket ++ OutPacket
@ -323,20 +339,20 @@ send_outpacket(Sid, OutPacket) ->
El when element(1, El) == xmlelement -> El when element(1, El) == xmlelement ->
{xmlelement, _, _, OEls} = El, {xmlelement, _, _, OEls} = El,
TypedEls = [xml:replace_tag_attr("xmlns", TypedEls = [xml:replace_tag_attr("xmlns",
"jabber:client",OEl) || ?NS_CLIENT,OEl) ||
OEl <- OEls], OEl <- OEls],
?DEBUG(" --- outgoing data --- ~n~s~n --- END --- ~n", ?DEBUG(" --- outgoing data --- ~n~s~n --- END --- ~n",
[xml:element_to_string( [xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
"http://jabber.org/protocol/httpbind"}], ?NS_HTTP_BIND}],
TypedEls})] TypedEls})]
), ),
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
"http://jabber.org/protocol/httpbind"}], ?NS_HTTP_BIND}],
TypedEls})}; TypedEls})};
{error, _E} -> {error, _E} ->
OutEls = case xml_stream:parse_element( OutEls = case xml_stream:parse_element(
@ -347,11 +363,21 @@ send_outpacket(Sid, OutPacket) ->
case SEls of case SEls of
[] -> [] ->
[]; [];
[{xmlelement, "stream:features", StreamAttribs, StreamEls} | StreamTail] -> [{xmlelement,
TypedTail = [xml:replace_tag_attr("xmlns", "stream:features",
"jabber:client",OEl) || StreamAttribs, StreamEls} |
StreamTail] ->
TypedTail =
[xml:replace_tag_attr(
"xmlns",
?NS_CLIENT,OEl) ||
OEl <- StreamTail], OEl <- StreamTail],
[{xmlelement, "stream:features", [{"xmlns:stream","http://etherx.jabber.org/streams"}] ++ StreamAttribs, StreamEls}] ++ TypedTail; [{xmlelement,
"stream:features",
[{"xmlns:stream",
?NS_STREAM}] ++
StreamAttribs, StreamEls}] ++
TypedTail;
Xml -> Xml ->
Xml Xml
end; end;
@ -361,23 +387,25 @@ send_outpacket(Sid, OutPacket) ->
end, end,
if if
StreamError -> StreamError ->
StreamErrCond = case xml_stream:parse_element( StreamErrCond =
"<stream:stream>"++OutPacket) of case xml_stream:parse_element(
El when element(1, El) == xmlelement -> "<stream:stream>"++OutPacket) of
{xmlelement, _Tag, _Attr, Els} = El, El when element(1, El) == xmlelement ->
[{xmlelement, SE, _, Cond} | _] = Els, {xmlelement, _Tag, _Attr, Els} = El,
if [{xmlelement, SE, _, Cond} | _] = Els,
SE == "stream:error" -> if
Cond; SE == "stream:error" ->
true -> Cond;
null true ->
end; null
{error, _E} -> end;
null {error, _E} ->
end, null
end,
case mnesia:dirty_read({http_bind, Sid}) of case mnesia:dirty_read({http_bind, Sid}) 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);
_ -> _ ->
err %% hu? err %% hu?
end, end,
@ -386,12 +414,12 @@ send_outpacket(Sid, OutPacket) ->
{200, ?HEADER, {200, ?HEADER,
"<body type='terminate' " "<body type='terminate' "
"condition='internal-server-error' " "condition='internal-server-error' "
"xmlns='http://jabber.org/protocol/httpbind'/>"}; "xmlns='"++?NS_HTTP_BIND++"'/>"};
_ -> _ ->
{200, ?HEADER, {200, ?HEADER,
"<body type='terminate' " "<body type='terminate' "
"condition='remote-stream-error' " "condition='remote-stream-error' "
"xmlns='http://jabber.org/protocol/httpbind'>" ++ "xmlns='"++?NS_HTTP_BIND++"'>" ++
elements_to_string(StreamErrCond) ++ elements_to_string(StreamErrCond) ++
"</body>"} "</body>"}
end; end;
@ -400,7 +428,7 @@ send_outpacket(Sid, OutPacket) ->
xml:element_to_string( xml:element_to_string(
{xmlelement,"body", {xmlelement,"body",
[{"xmlns", [{"xmlns",
"http://jabber.org/protocol/httpbind"}], ?NS_HTTP_BIND}],
OutEls})} OutEls})}
end end
end end
@ -615,13 +643,15 @@ handle_sync_event({http_put, Rid, Key, NewKey, Hold, Packet, StreamTo},
case StreamTo of case StreamTo of
{To, ""} -> {To, ""} ->
["<stream:stream to='", To, "' " ["<stream:stream to='", To, "' "
"xmlns='jabber:client' " "xmlns='"++?NS_CLIENT++"' "
"xmlns:stream='http://etherx.jabber.org/streams'>"] ++ Packet; "xmlns:stream='"++?NS_STREAM++"'>"]
++ Packet;
{To, Version} -> {To, Version} ->
["<stream:stream to='", To, "' " ["<stream:stream to='", To, "' "
"xmlns='jabber:client' " "xmlns='"++?NS_CLIENT++"' "
"version='", Version, "' " "version='", Version, "' "
"xmlns:stream='http://etherx.jabber.org/streams'>"] ++ Packet; "xmlns:stream='"++?NS_STREAM++"'>"]
++ Packet;
_ -> _ ->
Packet Packet
end, end,
@ -791,7 +821,7 @@ parse_request(Data) ->
lists:map(fun(E) -> lists:map(fun(E) ->
EXmlns = xml:get_tag_attr_s("xmlns",E), EXmlns = xml:get_tag_attr_s("xmlns",E),
if if
EXmlns == "jabber:client" -> EXmlns == ?NS_CLIENT ->
remove_tag_attr("xmlns",E); remove_tag_attr("xmlns",E);
true -> true ->
ok ok
@ -801,7 +831,7 @@ parse_request(Data) ->
if if
Name /= "body" -> Name /= "body" ->
{error, bad_request}; {error, bad_request};
Xmlns /= "http://jabber.org/protocol/httpbind" -> Xmlns /= ?NS_HTTP_BIND ->
{error, bad_request}; {error, bad_request};
true -> true ->
{ok, {Sid, Rid, Key, NewKey, Attrs, Packet}} {ok, {Sid, Rid, Key, NewKey, Attrs, Packet}}