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

Update some HTTP-Bind to work with exmpp; but doesn't work yet.

This commit is contained in:
Badlop 2010-04-14 16:50:32 +02:00
parent 58a4b594db
commit 3259c1c2ea

View File

@ -88,7 +88,7 @@
-define(FSMOPTS, []). -define(FSMOPTS, []).
-endif. -endif.
-define(BOSH_VERSION, "1.8"). -define(BOSH_VERSION_b, <<"1.8">>).
-define(MAX_REQUESTS, 2). % number of simultaneous requests -define(MAX_REQUESTS, 2). % number of simultaneous requests
-define(MIN_POLLING, 2000000). % don't poll faster than that or we will -define(MIN_POLLING, 2000000). % don't poll faster than that or we will
@ -171,7 +171,7 @@ process_request(Data, IP) ->
case catch parse_request(Data, PayloadSize, MaxStanzaSize) of case catch parse_request(Data, PayloadSize, MaxStanzaSize) of
%% No existing session: %% No existing session:
{ok, {"", Rid, Attrs, Payload}} -> {ok, {"", Rid, Attrs, Payload}} ->
case xml:get_attr_s("to",Attrs) of case exmpp_xml:get_attribute_from_list_as_list(Attrs, "to", "") of
"" -> "" ->
?DEBUG("Session not created (Improper addressing)", []), ?DEBUG("Session not created (Improper addressing)", []),
{200, ?HEADER, "<body type='terminate' " {200, ?HEADER, "<body type='terminate' "
@ -194,13 +194,13 @@ process_request(Data, IP) ->
%% Existing session %% Existing session
{ok, {Sid, Rid, Attrs, Payload1}} -> {ok, {Sid, Rid, Attrs, Payload1}} ->
StreamStart = StreamStart =
case xml:get_attr_s("xmpp:restart",Attrs) of case exmpp_xml:get_attribute_from_list_as_list(Attrs, "xmpp:restart", "") of
"true" -> "true" ->
true; true;
_ -> _ ->
false false
end, end,
Payload2 = case xml:get_attr_s("type",Attrs) of Payload2 = case exmpp_xml:get_attribute_from_list_as_list(Attrs, "type", "") of
"terminate" -> "terminate" ->
%% close stream %% close stream
Payload1 ++ [{xmlstreamend, "stream:stream"}]; Payload1 ++ [{xmlstreamend, "stream:stream"}];
@ -227,7 +227,7 @@ process_request(Data, IP) ->
handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs, handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
Payload, PayloadSize, IP) -> Payload, PayloadSize, IP) ->
?DEBUG("got pid: ~p", [Pid]), ?DEBUG("got pid: ~p", [Pid]),
Wait = case string:to_integer(xml:get_attr_s("wait",Attrs)) of Wait = case string:to_integer(exmpp_xml:get_attribute_from_list_as_list(Attrs, "wait", "")) of
{error, _} -> {error, _} ->
?MAX_WAIT; ?MAX_WAIT;
{CWait, _} -> {CWait, _} ->
@ -238,7 +238,7 @@ handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
CWait CWait
end end
end, end,
Hold = case string:to_integer(xml:get_attr_s("hold",Attrs)) of Hold = case string:to_integer(exmpp_xml:get_attribute_from_list_as_list(Attrs, "hold", "")) of
{error, _} -> {error, _} ->
(?MAX_REQUESTS - 1); (?MAX_REQUESTS - 1);
{CHold, _} -> {CHold, _} ->
@ -251,11 +251,11 @@ handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
end, end,
Version = Version =
case catch list_to_float( case catch list_to_float(
xml:get_attr_s("ver", Attrs)) of exmpp_xml:get_attribute_from_list_as_list(Attrs, "ver", "")) of
{'EXIT', _} -> 0.0; {'EXIT', _} -> 0.0;
V -> V V -> V
end, end,
XmppVersion = xml:get_attr_s("xmpp:version", Attrs), XmppVersion = exmpp_xml:get_attribute_from_list_as_list(Attrs, "xmpp:version", ""),
?DEBUG("Create session: ~p", [Sid]), ?DEBUG("Create session: ~p", [Sid]),
mnesia:transaction( mnesia:transaction(
fun() -> fun() ->
@ -581,8 +581,8 @@ process_http_put(#http_put{rid = Rid, attrs = Attrs, payload = Payload,
StateName, StateData, RidAllow) -> StateName, StateData, RidAllow) ->
?DEBUG("Actually processing request: ~p", [Request]), ?DEBUG("Actually processing request: ~p", [Request]),
%% Check if key valid %% Check if key valid
Key = xml:get_attr_s("key", Attrs), Key = exmpp_xml:get_attribute_from_list_as_list(Attrs, "key", ""),
NewKey = xml:get_attr_s("newkey", Attrs), NewKey = exmpp_xml:get_attribute_from_list_as_list(Attrs, "newkey", ""),
KeyAllow = KeyAllow =
case RidAllow of case RidAllow of
repeat -> repeat ->
@ -687,20 +687,22 @@ process_http_put(#http_put{rid = Rid, attrs = Attrs, payload = Payload,
C2SPid -> C2SPid ->
case StreamTo of case StreamTo of
{To, ""} -> {To, ""} ->
StreamAttrs = [#xmlattr{name = 'to', value = list_to_binary(To)},
#xmlattr{name = 'xmlns', value = ?NS_JABBER_CLIENT_b},
#xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}],
StreamEl = #xmlel{ns = 'stream:stream', attrs = StreamAttrs},
gen_fsm:send_event( gen_fsm:send_event(
C2SPid, C2SPid,
{xmlstreamstart, "stream:stream", {xmlstreamstart, StreamEl});
[{"to", To},
{"xmlns", ?NS_JABBER_CLIENT_s},
{"xmlns:stream", ?NS_XMPP_s}]});
{To, Version} -> {To, Version} ->
StreamAttrs = [#xmlattr{name = 'to', value = list_to_binary(To)},
#xmlattr{name = 'xmlns', value = ?NS_JABBER_CLIENT_b},
#xmlattr{name = 'version', value = list_to_binary(Version)},
#xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}],
StreamEl = #xmlel{ns = 'stream:stream', attrs = StreamAttrs},
gen_fsm:send_event( gen_fsm:send_event(
C2SPid, C2SPid,
{xmlstreamstart, "stream:stream", {xmlstreamstart, StreamEl});
[{"to", To},
{"xmlns", ?NS_JABBER_CLIENT_s},
{"version", Version},
{"xmlns:stream", ?NS_XMPP_s}]});
_ -> _ ->
ok ok
end, end,
@ -767,7 +769,7 @@ handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
?DEBUG("Trafic Shaper: Delaying request ~p", [Rid]), ?DEBUG("Trafic Shaper: Delaying request ~p", [Rid]),
timer:sleep(Pause), timer:sleep(Pause),
%{200, ?HEADER, %{200, ?HEADER,
% xml:element_to_string( % exmpp_xml:document_to_list(
% {xmlelement, "body", % {xmlelement, "body",
% [{"xmlns", ?NS_HTTP_BIND_s}, % [{"xmlns", ?NS_HTTP_BIND_s},
% {"type", "error"}], []})}; % {"type", "error"}], []})};
@ -804,7 +806,7 @@ handle_http_put_error(Reason, #http_bind{pid=FsmRef, version=Version})
case Reason of case Reason of
not_exists -> not_exists ->
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
attrs = [ attrs = [
@ -820,7 +822,7 @@ handle_http_put_error(Reason, #http_bind{pid=FsmRef, version=Version})
})}; })};
bad_key -> bad_key ->
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
attrs = [ attrs = [
@ -836,7 +838,7 @@ handle_http_put_error(Reason, #http_bind{pid=FsmRef, version=Version})
})}; })};
polling_too_frequently -> polling_too_frequently ->
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
attrs = [ attrs = [
@ -875,7 +877,7 @@ rid_allow(OldRid, NewRid, Attrs, Hold, MaxPause) ->
%% We did not miss any packet, we can process it immediately: %% We did not miss any packet, we can process it immediately:
NewRid == OldRid + 1 -> NewRid == OldRid + 1 ->
case catch list_to_integer( case catch list_to_integer(
xml:get_attr_s("pause", Attrs)) of exmpp_xml:get_attribute_from_list_as_list(Attrs, "pause", "")) of
{'EXIT', _} -> {'EXIT', _} ->
{true, 0}; {true, 0};
Pause1 when Pause1 =< MaxPause -> Pause1 when Pause1 =< MaxPause ->
@ -934,9 +936,9 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
true -> true ->
case OutPacket of case OutPacket of
[{xmlstreamstart, _, OutAttrs} | Els] -> [{xmlstreamstart, _, OutAttrs} | Els] ->
AuthID = xml:get_attr_s("id", OutAttrs), AuthID = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "id", ""),
From = xml:get_attr_s("from", OutAttrs), FromB = exmpp_xml:get_attribute_from_list_as_binary(OutAttrs, "from", ""),
Version = xml:get_attr_s("version", OutAttrs), Version = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "version", ""),
OutEls = OutEls =
case Els of case Els of
[] -> [] ->
@ -969,60 +971,59 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
StreamTail] StreamTail]
end, end,
BOSH_attribs = BOSH_attribs =
[{"authid", AuthID}, [#xmlattr{name = 'authid', value = list_to_binary(AuthID)},
{"xmlns:xmpp", ?NS_BOSH_s}, #xmlattr{name = 'xmlns:xmpp', value = ?NS_BOSH_b},
{"xmlns:stream", ?NS_XMPP_s}] ++ #xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}] ++
case OutEls of case OutEls of
[] -> [] ->
[]; [];
_ -> _ ->
[{"xmpp:version", Version}] [#xmlattr{name = 'xmpp:version', value = list_to_binary(Version)}]
end, end,
MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY), MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY),
MaxPause = get_max_pause(To), MaxPause = get_max_pause(To),
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
attrs = [ attrs = [
#xmlattr{name = 'sid', #xmlattr{name = 'sid',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = Sid value = list_to_binary(Sid)
}, },
#xmlattr{name = 'wait', #xmlattr{name = 'wait',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = integer_to_list(Wait) value = list_to_binary(integer_to_list(Wait))
}, },
#xmlattr{name = 'requests', #xmlattr{name = 'requests',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = integer_to_list(Hold+1) value = list_to_binary(integer_to_list(Hold+1))
}, },
#xmlattr{name = 'inactivity', #xmlattr{name = 'inactivity',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = integer_to_list(trunc(MaxInactivity/1000)) value = list_to_binary(integer_to_list(trunc(MaxInactivity/1000)))
}, },
#xmlattr{name = 'maxpause', #xmlattr{name = 'maxpause',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = integer_to_list(MaxPause) value = list_to_binary(integer_to_list(MaxPause))
}, },
#xmlattr{name = 'polling', #xmlattr{name = 'polling',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = integer_to_list(trunc(?MIN_POLLING/1000000)) value = list_to_binary(integer_to_list(trunc(?MIN_POLLING/1000000)))
}, },
#xmlattr{name = 'ver', #xmlattr{name = 'ver',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = ?BOSH_VERSION value = ?BOSH_VERSION_b
}, },
#xmlattr{name = 'from', #xmlattr{name = 'from',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = From value = FromB
}, },
#xmlattr{name = 'secure', #xmlattr{name = 'secure',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
value = "true" value = <<"true">>
}, }
BOSH_attribs ] ++ BOSH_attribs,
],
children = OutEls})}; children = OutEls})};
% {xmlelement,"body", % {xmlelement,"body",
% [{"xmlns", % [{"xmlns",
@ -1084,7 +1085,7 @@ send_outpacket(#http_bind{pid = FsmRef}, OutPacket) ->
true -> true ->
TypedEls = [check_default_xmlns(OEl) || TypedEls = [check_default_xmlns(OEl) ||
{xmlstreamelement, OEl} <- OutPacket], {xmlstreamelement, OEl} <- OutPacket],
Body = xml:element_to_string( Body = exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
children = TypedEls}), children = TypedEls}),
@ -1129,7 +1130,7 @@ send_outpacket(#http_bind{pid = FsmRef}, OutPacket) ->
StreamTail] StreamTail]
end, end,
{200, ?HEADER, {200, ?HEADER,
xml:element_to_string( exmpp_xml:document_to_list(
#xmlel{name = 'body', #xmlel{name = 'body',
ns = ?NS_HTTP_BIND_s, ns = ?NS_HTTP_BIND_s,
children = OutEls})}; children = OutEls})};
@ -1183,15 +1184,15 @@ parse_request(Data, PayloadSize, MaxStanzaSize) ->
?DEBUG("--- incoming data --- ~n~s~n --- END --- ", [Data]), ?DEBUG("--- incoming data --- ~n~s~n --- END --- ", [Data]),
%% MR: I do not think it works if put put several elements in the %% MR: I do not think it works if put put several elements in the
%% same body: %% same body:
case xml_stream:parse_element(Data) of case exmpp_xmlstream:parse_element(Data) of
#xmlel{name = 'body', [#xmlel{name = 'body',
ns = Xmlns, ns = Xmlns,
attrs = Attrs, attrs = Attrs,
children = Els} = Xml-> children = Els}] = [Xml] ->
% {xmlelement, "body", Attrs, Els} -> % {xmlelement, "body", Attrs, Els} ->
% Xmlns = xml:get_attr_s("xmlns",Attrs), % Xmlns = xml:get_attr_s("xmlns",Attrs),
if if
Xmlns /= ?NS_HTTP_BIND_s -> Xmlns /= ?NS_HTTP_BIND ->
{error, bad_request}; {error, bad_request};
true -> true ->
%case catch list_to_integer(xml:get_attr_s("rid", Attrs)) of %case catch list_to_integer(xml:get_attr_s("rid", Attrs)) of
@ -1212,8 +1213,7 @@ parse_request(Data, PayloadSize, MaxStanzaSize) ->
false false
end end
end, Els), end, Els),
% Suggestion : Sid = exmpp_xml:get_attribute_as_list(Xml, "sid", ""), Sid = exmpp_xml:get_attribute_as_list(Xml, "sid", ""),
Sid = xml:get_attr_s("sid",Attrs),
if if
PayloadSize =< MaxStanzaSize -> PayloadSize =< MaxStanzaSize ->
{ok, {Sid, Rid, Attrs, FixedEls}}; {ok, {Sid, Rid, Attrs, FixedEls}};
@ -1222,10 +1222,10 @@ parse_request(Data, PayloadSize, MaxStanzaSize) ->
end end
end end
end; end;
#xmlel{} -> [#xmlel{}] ->
% {xmlelement, _Name, _Attrs, _Els} -> % {xmlelement, _Name, _Attrs, _Els} ->
{error, bad_request}; {error, bad_request};
{error, _Reason} -> {error, _} ->
{error, bad_request} {error, bad_request}
end. end.
@ -1260,7 +1260,7 @@ set_inactivity_timer(_Pause, MaxInactivity) ->
elements_to_string([]) -> elements_to_string([]) ->
[]; [];
elements_to_string([El | Els]) -> elements_to_string([El | Els]) ->
xml:element_to_string(El) ++ elements_to_string(Els). exmpp_xml:document_to_list(El) ++ elements_to_string(Els).
%% @spec (To, Default::integer()) -> integer() %% @spec (To, Default::integer()) -> integer()
%% where To = [] | {Host::string(), Version::string()} %% where To = [] | {Host::string(), Version::string()}