mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-16 17:15:55 +01:00
Remove useless loop and clean prepare_response (thanks to Christopher tofu Zorn)
1. Remove the silly loop that isnt needed, this speeds up all requests by 100 milliseconds. 2. Clean up prepare response function and add two new functions to handle out going payloads based of whether its a new session or not.
This commit is contained in:
parent
3b963c4f18
commit
bd69e3ad6c
@ -783,7 +783,7 @@ handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
|
||||
{buffered, _Sess} ->
|
||||
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
||||
{ok, Sess} ->
|
||||
prepare_response(Sess, Rid, Attrs, StreamStart)
|
||||
prepare_response(Sess, Rid, [], StreamStart)
|
||||
end.
|
||||
|
||||
http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
|
||||
@ -912,9 +912,7 @@ update_shaper(ShaperState, PayloadSize) ->
|
||||
{NewShaperState, undefined}
|
||||
end.
|
||||
|
||||
prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
Rid, _, StreamStart) ->
|
||||
receive after 100 -> ok end, %% TODO: Why is this needed. Argh. Bad programming practice.
|
||||
prepare_response(Sess, Rid, OutputEls, StreamStart) ->
|
||||
case catch http_get(Sess, Rid) of
|
||||
{ok, cancel} ->
|
||||
%% actually it would be better if we could completely
|
||||
@ -927,9 +925,16 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
||||
{ok, ROutPacket} ->
|
||||
OutPacket = lists:reverse(ROutPacket),
|
||||
?DEBUG("OutPacket: ~p", [OutPacket]),
|
||||
case StreamStart of
|
||||
false ->
|
||||
?DEBUG("OutPacket: ~p", [OutputEls++OutPacket]),
|
||||
prepare_outpacket_response(Sess, Rid, OutputEls++OutPacket, StreamStart);
|
||||
{'EXIT', {shutdown, _}} ->
|
||||
{200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
||||
{'EXIT', _Reason} ->
|
||||
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"}
|
||||
end.
|
||||
|
||||
%% Send output payloads on establised sessions
|
||||
prepare_outpacket_response(Sess, _Rid, OutPacket, false) ->
|
||||
case catch send_outpacket(Sess, OutPacket) of
|
||||
{'EXIT', _Reason} ->
|
||||
{200, ?HEADER,
|
||||
@ -938,7 +943,10 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
SendRes ->
|
||||
SendRes
|
||||
end;
|
||||
true ->
|
||||
%% Handle a new session along with its output payload
|
||||
prepare_outpacket_response(#http_bind{id=Sid, wait=Wait,
|
||||
hold=Hold, to=To}=Sess,
|
||||
Rid, OutPacket, true) ->
|
||||
case OutPacket of
|
||||
[{xmlstreamstart, _, OutAttrs} | Els] ->
|
||||
AuthID = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, "id", ""),
|
||||
@ -954,9 +962,9 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
attrs = StreamAttribs,
|
||||
children = StreamEls}}
|
||||
| StreamTail] ->
|
||||
% {xmlelement, "stream:features",
|
||||
% StreamAttribs, StreamEls}}
|
||||
% | StreamTail] ->
|
||||
%% {xmlelement, "stream:features",
|
||||
%% StreamAttribs, StreamEls}}
|
||||
%% | StreamTail] ->
|
||||
TypedTail =
|
||||
[check_default_xmlns(OEl) ||
|
||||
{xmlstreamelement, OEl} <-
|
||||
@ -965,22 +973,30 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
declared_ns = [{?NS_XMPP_s, ?NS_XMPP_pfx}],
|
||||
attrs = StreamAttribs,
|
||||
children = StreamEls}] ++
|
||||
% [{xmlelement, "stream:features",
|
||||
% [{"xmlns:stream",
|
||||
% ?NS_XMPP_s}] ++
|
||||
% StreamAttribs, StreamEls}] ++
|
||||
%% [{xmlelement, "stream:features",
|
||||
%% [{"xmlns:stream",
|
||||
%% ?NS_XMPP_s}] ++
|
||||
%% StreamAttribs, StreamEls}] ++
|
||||
TypedTail;
|
||||
StreamTail ->
|
||||
[check_default_xmlns(OEl) ||
|
||||
{xmlstreamelement, OEl} <-
|
||||
StreamTail]
|
||||
end,
|
||||
case OutEls of
|
||||
[] ->
|
||||
prepare_response(Sess, Rid, OutPacket, true);
|
||||
[#xmlel{name = 'stream:error'}] ->
|
||||
{200, ?HEADER, "<body type='terminate' "
|
||||
"condition='host-unknown' "
|
||||
"xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
||||
_ ->
|
||||
BOSH_attribs =
|
||||
[#xmlattr{name = 'authid', value = list_to_binary(AuthID)},
|
||||
#xmlattr{name = 'xmlns:stream', value = ?NS_XMPP_b}] ++
|
||||
case OutEls of
|
||||
[] ->
|
||||
[];
|
||||
prepare_response(Sess, Rid, OutPacket, true);
|
||||
_ ->
|
||||
[#xmlattr{name = 'version', ns = ?NS_BOSH_s, value = list_to_binary(Version)}]
|
||||
end,
|
||||
@ -1020,7 +1036,7 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
value = <<"true">>
|
||||
}
|
||||
] ++ BOSH_attribs,
|
||||
children = OutEls})};
|
||||
children = OutEls})}
|
||||
% {xmlelement,"body",
|
||||
% [{"xmlns",
|
||||
% ?NS_HTTP_BIND_s},
|
||||
@ -1039,18 +1055,14 @@ prepare_response(#http_bind{id=Sid, wait=Wait, hold=Hold, to=To}=Sess,
|
||||
% {"from", From},
|
||||
% {"secure", "true"} %% we're always being secure
|
||||
% ] ++ BOSH_attribs,OutEls})};
|
||||
{error, _} ->
|
||||
{200, ?HEADER, "<body type='terminate' "
|
||||
"condition='host-unknown' "
|
||||
"xmlns='"++?NS_HTTP_BIND_s++"'/>"}
|
||||
end
|
||||
end;
|
||||
{'EXIT', {shutdown, _}} ->
|
||||
{200, ?HEADER, "<body type='terminate' condition='system-shutdown' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
||||
{'EXIT', _Reason} ->
|
||||
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"}
|
||||
_ ->
|
||||
{200, ?HEADER, "<body type='terminate' "
|
||||
"condition='internal-server-error' "
|
||||
"xmlns='"++?NS_HTTP_BIND_s++"'/>"}
|
||||
end.
|
||||
|
||||
|
||||
http_get(#http_bind{pid = FsmRef, wait = Wait, hold = Hold}, Rid) ->
|
||||
gen_fsm:sync_send_all_state_event(
|
||||
FsmRef, {http_get, Rid, Wait, Hold}, 2 * ?MAX_WAIT * 1000).
|
||||
|
Loading…
Reference in New Issue
Block a user