2009-06-16 19:43:35 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_http_bind.erl
|
|
|
|
%%% Author : Stefan Strigler <steve@zeank.in-berlin.de>
|
2012-02-20 14:15:08 +01:00
|
|
|
%%% Purpose : Implements XMPP over BOSH (XEP-0206) (formerly known as
|
2009-06-16 19:45:34 +02:00
|
|
|
%%% HTTP Binding)
|
2009-06-16 19:43:35 +02:00
|
|
|
%%% Created : 21 Sep 2005 by Stefan Strigler <steve@zeank.in-berlin.de>
|
2009-08-31 20:37:52 +02:00
|
|
|
%%% Modified: may 2009 by Mickael Remond, Alexey Schepin
|
|
|
|
%%% Id : $Id: ejabberd_http_bind.erl 953 2009-05-07 10:40:40Z alexey $
|
2009-06-16 19:43:35 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_http_bind).
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2009-06-16 19:46:59 +02:00
|
|
|
-export([start_link/3,
|
2009-06-16 19:43:35 +02:00
|
|
|
init/1,
|
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
|
|
|
handle_info/3,
|
|
|
|
terminate/3,
|
|
|
|
send/2,
|
2009-08-31 20:37:52 +02:00
|
|
|
send_xml/2,
|
2009-06-16 19:48:13 +02:00
|
|
|
sockname/1,
|
2009-06-16 19:46:51 +02:00
|
|
|
peername/1,
|
2009-08-31 20:37:52 +02:00
|
|
|
setopts/2,
|
2009-06-16 19:43:35 +02:00
|
|
|
controlling_process/2,
|
2009-08-31 20:37:52 +02:00
|
|
|
become_controller/2,
|
|
|
|
custom_receiver/1,
|
|
|
|
reset_stream/1,
|
|
|
|
change_shaper/2,
|
|
|
|
monitor/1,
|
2009-06-16 19:43:35 +02:00
|
|
|
close/1,
|
2010-05-05 22:17:31 +02:00
|
|
|
start/4,
|
|
|
|
handle_session_start/8,
|
2010-05-05 22:27:58 +02:00
|
|
|
handle_http_put/7,
|
2010-05-05 22:17:31 +02:00
|
|
|
http_put/7,
|
|
|
|
http_get/2,
|
|
|
|
prepare_response/4,
|
2009-06-16 19:46:51 +02:00
|
|
|
process_request/2]).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-include("jlib.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-include("ejabberd_http.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-02-15 21:20:39 +01:00
|
|
|
-include("http_bind.hrl").
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(http_bind,
|
|
|
|
{id, pid, to, hold, wait, process_delay, version}).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-06-16 19:46:51 +02:00
|
|
|
-define(NULL_PEER, {{0, 0, 0, 0}, 0}).
|
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
%% http binding request
|
2009-06-16 19:44:13 +02:00
|
|
|
-record(hbr, {rid,
|
|
|
|
key,
|
|
|
|
out}).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
|
|
|
-record(state, {id,
|
2009-06-16 19:45:22 +02:00
|
|
|
rid = none,
|
2009-06-16 19:43:35 +02:00
|
|
|
key,
|
2009-06-16 19:46:59 +02:00
|
|
|
socket,
|
2009-06-16 19:43:35 +02:00
|
|
|
output = "",
|
2009-08-31 20:37:52 +02:00
|
|
|
input = queue:new(),
|
2009-06-16 19:43:35 +02:00
|
|
|
waiting_input = false,
|
2009-08-31 20:37:52 +02:00
|
|
|
shaper_state,
|
|
|
|
shaper_timer,
|
2009-06-16 19:43:35 +02:00
|
|
|
last_receiver,
|
|
|
|
last_poll,
|
2009-06-16 19:46:21 +02:00
|
|
|
http_receiver,
|
2012-04-27 13:19:49 +02:00
|
|
|
out_of_order_receiver = false,
|
2009-06-16 19:46:21 +02:00
|
|
|
wait_timer,
|
2009-06-16 19:43:35 +02:00
|
|
|
ctime = 0,
|
|
|
|
timer,
|
2009-06-16 19:46:51 +02:00
|
|
|
pause=0,
|
2009-08-31 20:37:52 +02:00
|
|
|
unprocessed_req_list = [], % list of request that have been delayed for proper reordering: {Request, PID}
|
2009-06-16 19:47:03 +02:00
|
|
|
req_list = [], % list of requests (cache)
|
2009-06-16 19:47:23 +02:00
|
|
|
max_inactivity,
|
2009-08-31 20:37:52 +02:00
|
|
|
max_pause,
|
2009-06-16 19:47:23 +02:00
|
|
|
ip = ?NULL_PEER
|
2009-06-16 19:43:35 +02:00
|
|
|
}).
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Internal request format:
|
|
|
|
-record(http_put, {rid,
|
|
|
|
attrs,
|
|
|
|
payload,
|
|
|
|
payload_size,
|
|
|
|
hold,
|
|
|
|
stream,
|
|
|
|
ip}).
|
2009-06-16 19:44:13 +02:00
|
|
|
|
|
|
|
%%-define(DBGFSM, true).
|
2009-06-16 19:43:35 +02:00
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
-endif.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Wait 100ms before continue processing, to allow the client provide more related stanzas.
|
|
|
|
-define(BOSH_VERSION, <<"1.8">>).
|
2009-06-16 19:44:42 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(NS_CLIENT, <<"jabber:client">>).
|
|
|
|
|
|
|
|
-define(NS_BOSH, <<"urn:xmpp:xbosh">>).
|
|
|
|
|
|
|
|
-define(NS_HTTP_BIND,
|
|
|
|
<<"http://jabber.org/protocol/httpbind">>).
|
|
|
|
|
|
|
|
-define(MAX_REQUESTS, 2).
|
|
|
|
|
|
|
|
-define(MIN_POLLING, 2000000).
|
|
|
|
|
|
|
|
-define(MAX_WAIT, 3600).
|
|
|
|
|
|
|
|
-define(MAX_INACTIVITY, 30000).
|
|
|
|
|
|
|
|
-define(MAX_PAUSE, 120).
|
2009-06-16 19:45:38 +02:00
|
|
|
|
2010-06-07 13:44:55 +02:00
|
|
|
-define(PROCESS_DELAY_DEFAULT, 100).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-06-07 13:44:55 +02:00
|
|
|
-define(PROCESS_DELAY_MIN, 0).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-06-07 13:44:55 +02:00
|
|
|
-define(PROCESS_DELAY_MAX, 1000).
|
|
|
|
|
2010-11-17 20:14:59 +01:00
|
|
|
%% Line copied from mod_http_bind.erl
|
|
|
|
-define(PROCNAME_MHB, ejabberd_mod_http_bind).
|
2009-06-16 19:44:13 +02:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-06-16 19:47:56 +02:00
|
|
|
%% TODO: If compile with no supervisor option, start the session without
|
|
|
|
%% supervisor
|
|
|
|
start(XMPPDomain, Sid, Key, IP) ->
|
|
|
|
?DEBUG("Starting session", []),
|
2010-11-17 20:14:59 +01:00
|
|
|
SupervisorProc = gen_mod:get_module_proc(XMPPDomain, ?PROCNAME_MHB),
|
|
|
|
case catch supervisor:start_child(SupervisorProc, [Sid, Key, IP]) of
|
2009-06-16 19:48:00 +02:00
|
|
|
{ok, Pid} -> {ok, Pid};
|
2009-06-16 19:47:56 +02:00
|
|
|
_ -> check_bind_module(XMPPDomain),
|
|
|
|
{error, "Cannot start HTTP bind session"}
|
|
|
|
end.
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-06-16 19:46:59 +02:00
|
|
|
start_link(Sid, Key, IP) ->
|
|
|
|
gen_fsm:start_link(?MODULE, [Sid, Key, IP], ?FSMOPTS).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-06-16 19:46:59 +02:00
|
|
|
send({http_bind, FsmRef, _IP}, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{send, Packet}).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
send_xml({http_bind, FsmRef, _IP}, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{send_xml, Packet}).
|
2009-08-31 20:37:52 +02:00
|
|
|
|
2009-06-16 19:46:59 +02:00
|
|
|
setopts({http_bind, FsmRef, _IP}, Opts) ->
|
2009-06-16 19:43:35 +02:00
|
|
|
case lists:member({active, once}, Opts) of
|
|
|
|
true ->
|
2009-06-16 19:43:39 +02:00
|
|
|
gen_fsm:send_all_state_event(FsmRef, {activate, self()});
|
2009-06-16 19:43:35 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
controlling_process(_Socket, _Pid) -> ok.
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
custom_receiver({http_bind, FsmRef, _IP}) ->
|
|
|
|
{receiver, ?MODULE, FsmRef}.
|
|
|
|
|
|
|
|
become_controller(FsmRef, C2SPid) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:send_all_state_event(FsmRef,
|
|
|
|
{become_controller, C2SPid}).
|
2009-08-31 20:37:52 +02:00
|
|
|
|
|
|
|
reset_stream({http_bind, _FsmRef, _IP}) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
change_shaper({http_bind, FsmRef, _IP}, Shaper) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:send_all_state_event(FsmRef,
|
|
|
|
{change_shaper, Shaper}).
|
2009-08-31 20:37:52 +02:00
|
|
|
|
|
|
|
monitor({http_bind, FsmRef, _IP}) ->
|
|
|
|
erlang:monitor(process, FsmRef).
|
|
|
|
|
2009-06-16 19:46:59 +02:00
|
|
|
close({http_bind, FsmRef, _IP}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop, close}).
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
sockname(_Socket) -> {ok, ?NULL_PEER}.
|
2009-06-16 19:46:51 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
peername({http_bind, _FsmRef, IP}) -> {ok, IP}.
|
2009-06-16 19:44:13 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
|
|
|
|
%% Entry point for data coming from client through ejabberd HTTP server:
|
2009-06-16 19:46:51 +02:00
|
|
|
process_request(Data, IP) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
Opts1 = ejabberd_c2s_config:get_c2s_limits(),
|
|
|
|
Opts = [{xml_socket, true} | Opts1],
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxStanzaSize = case lists:keysearch(max_stanza_size, 1,
|
|
|
|
Opts)
|
|
|
|
of
|
|
|
|
{value, {_, Size}} -> Size;
|
|
|
|
_ -> infinity
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
PayloadSize = iolist_size(Data),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch parse_request(Data, PayloadSize,
|
|
|
|
MaxStanzaSize)
|
|
|
|
of
|
|
|
|
%% No existing session:
|
|
|
|
{ok, {<<"">>, Rid, Attrs, Payload}} ->
|
|
|
|
case xml:get_attr_s(<<"to">>, Attrs) of
|
|
|
|
<<"">> ->
|
|
|
|
?DEBUG("Session not created (Improper addressing)", []),
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='improper-ad"
|
|
|
|
"dressing' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
XmppDomain ->
|
2013-06-20 10:40:44 +02:00
|
|
|
Sid = p1_sha:sha(term_to_binary({now(), make_ref()})),
|
2013-03-14 10:33:02 +01:00
|
|
|
case start(XmppDomain, Sid, <<"">>, IP) of
|
|
|
|
{error, _} ->
|
|
|
|
{500, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='internal-se"
|
|
|
|
"rver-error' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary,
|
|
|
|
"'>Internal Server Error</body>">>};
|
|
|
|
{ok, Pid} ->
|
|
|
|
handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
|
|
|
|
Payload, PayloadSize, IP)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
%% Existing session
|
|
|
|
{ok, {Sid, Rid, Attrs, Payload1}} ->
|
|
|
|
StreamStart = case xml:get_attr_s(<<"xmpp:restart">>,
|
|
|
|
Attrs)
|
|
|
|
of
|
|
|
|
<<"true">> -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
Payload2 = case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"terminate">> ->
|
|
|
|
Payload1 ++ [{xmlstreamend, <<"stream:stream">>}];
|
|
|
|
_ -> Payload1
|
|
|
|
end,
|
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload2, PayloadSize,
|
|
|
|
StreamStart, IP);
|
|
|
|
{size_limit, Sid} ->
|
|
|
|
case mnesia:dirty_read({http_bind, Sid}) of
|
|
|
|
{error, _} -> {404, ?HEADER, <<"">>};
|
|
|
|
{ok, #http_bind{pid = FsmRef}} ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop, close}),
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='undefined-c"
|
|
|
|
"ondition' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'>Request Too Large</body>">>}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
?DEBUG("Received bad request: ~p", [Data]),
|
|
|
|
{400, ?HEADER, <<"">>}
|
2009-06-16 19:44:47 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
|
|
|
|
Payload, PayloadSize, IP) ->
|
2009-06-16 19:47:56 +02:00
|
|
|
?DEBUG("got pid: ~p", [Pid]),
|
2013-03-14 10:33:02 +01:00
|
|
|
Wait = case str:to_integer(xml:get_attr_s(<<"wait">>,
|
|
|
|
Attrs))
|
|
|
|
of
|
|
|
|
{error, _} -> ?MAX_WAIT;
|
|
|
|
{CWait, _} ->
|
|
|
|
if CWait > (?MAX_WAIT) -> ?MAX_WAIT;
|
|
|
|
true -> CWait
|
|
|
|
end
|
2009-06-16 19:47:56 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Hold = case str:to_integer(xml:get_attr_s(<<"hold">>,
|
|
|
|
Attrs))
|
|
|
|
of
|
|
|
|
{error, _} -> (?MAX_REQUESTS) - 1;
|
|
|
|
{CHold, _} ->
|
|
|
|
if CHold > (?MAX_REQUESTS) - 1 -> (?MAX_REQUESTS) - 1;
|
|
|
|
true -> CHold
|
|
|
|
end
|
2009-06-16 19:47:56 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Pdelay = case
|
|
|
|
str:to_integer(xml:get_attr_s(<<"process-delay">>,
|
|
|
|
Attrs))
|
|
|
|
of
|
|
|
|
{error, _} -> ?PROCESS_DELAY_DEFAULT;
|
|
|
|
{CPdelay, _}
|
|
|
|
when ((?PROCESS_DELAY_MIN) =< CPdelay) and
|
|
|
|
(CPdelay =< (?PROCESS_DELAY_MAX)) ->
|
|
|
|
CPdelay;
|
|
|
|
{CPdelay, _} ->
|
|
|
|
lists:max([lists:min([CPdelay, ?PROCESS_DELAY_MAX]),
|
|
|
|
?PROCESS_DELAY_MIN])
|
2010-06-07 13:44:55 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Version = case catch
|
|
|
|
list_to_float(binary_to_list(xml:get_attr_s(<<"ver">>, Attrs)))
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> 0.0;
|
|
|
|
V -> V
|
|
|
|
end,
|
|
|
|
XmppVersion = xml:get_attr_s(<<"xmpp:version">>, Attrs),
|
2009-06-16 19:48:22 +02:00
|
|
|
?DEBUG("Create session: ~p", [Sid]),
|
2011-10-14 08:57:51 +02:00
|
|
|
mnesia:dirty_write(
|
|
|
|
#http_bind{id = Sid,
|
|
|
|
pid = Pid,
|
|
|
|
to = {XmppDomain,
|
|
|
|
XmppVersion},
|
|
|
|
hold = Hold,
|
|
|
|
wait = Wait,
|
|
|
|
process_delay = Pdelay,
|
|
|
|
version = Version
|
|
|
|
}),
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, true, IP).
|
2009-06-16 19:47:56 +02:00
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2009-06-16 19:48:13 +02:00
|
|
|
%% {stop, StopReason}
|
2009-06-16 19:43:35 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-06-16 19:46:59 +02:00
|
|
|
init([Sid, Key, IP]) ->
|
|
|
|
?DEBUG("started: ~p", [{Sid, Key, IP}]),
|
2009-08-31 20:37:52 +02:00
|
|
|
Opts1 = ejabberd_c2s_config:get_c2s_limits(),
|
|
|
|
Opts = [{xml_socket, true} | Opts1],
|
|
|
|
Shaper = none,
|
|
|
|
ShaperState = shaper:new(Shaper),
|
2009-06-16 19:46:59 +02:00
|
|
|
Socket = {http_bind, self(), IP},
|
|
|
|
ejabberd_socket:start(ejabberd_c2s, ?MODULE, Socket, Opts),
|
2009-06-16 19:43:35 +02:00
|
|
|
Timer = erlang:start_timer(?MAX_INACTIVITY, self(), []),
|
2009-06-16 19:43:47 +02:00
|
|
|
{ok, loop, #state{id = Sid,
|
2009-06-16 19:43:35 +02:00
|
|
|
key = Key,
|
2009-06-16 19:46:59 +02:00
|
|
|
socket = Socket,
|
2009-08-31 20:37:52 +02:00
|
|
|
shaper_state = ShaperState,
|
2009-06-16 19:47:23 +02:00
|
|
|
max_inactivity = ?MAX_INACTIVITY,
|
2009-08-31 20:37:52 +02:00
|
|
|
max_pause = ?MAX_PAUSE,
|
2009-06-16 19:43:35 +02:00
|
|
|
timer = Timer}}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-06-16 19:48:13 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2009-06-16 19:43:35 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_event({become_controller, C2SPid}, StateName, StateData) ->
|
2009-06-16 19:43:39 +02:00
|
|
|
case StateData#state.input of
|
2013-03-14 10:33:02 +01:00
|
|
|
cancel ->
|
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{waiting_input = C2SPid}};
|
|
|
|
Input ->
|
|
|
|
lists:foreach(fun (Event) -> C2SPid ! Event end,
|
|
|
|
queue:to_list(Input)),
|
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{input = queue:new(),
|
|
|
|
waiting_input = C2SPid}}
|
2009-06-16 19:43:39 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_event({change_shaper, Shaper}, StateName,
|
|
|
|
StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
NewShaperState = shaper:new(Shaper),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{shaper_state = NewShaperState}};
|
2009-06-16 19:43:35 +02:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2009-06-16 19:48:13 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2009-06-16 19:43:35 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_sync_event({send_xml, Packet}, _From, StateName,
|
|
|
|
#state{http_receiver = undefined} = StateData) ->
|
|
|
|
Output = [Packet | StateData#state.output],
|
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply, StateName,
|
|
|
|
StateData#state{output = Output}};
|
2012-04-27 13:19:49 +02:00
|
|
|
handle_sync_event({send_xml, Packet}, _From, StateName,
|
|
|
|
#state{out_of_order_receiver = true} = StateData) ->
|
|
|
|
Output = [Packet | StateData#state.output],
|
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply, StateName,
|
|
|
|
StateData#state{output = Output}};
|
|
|
|
handle_sync_event({send_xml, Packet}, _From, StateName,
|
|
|
|
StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
Output = [Packet | StateData#state.output],
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
HTTPReply = {ok, Output},
|
|
|
|
gen_fsm:reply(StateData#state.http_receiver, HTTPReply),
|
|
|
|
cancel_timer(StateData#state.wait_timer),
|
|
|
|
Rid = StateData#state.rid,
|
2013-03-14 10:33:02 +01:00
|
|
|
ReqList = [#hbr{rid = Rid, key = StateData#state.key,
|
|
|
|
out = Output}
|
|
|
|
| [El
|
|
|
|
|| El <- StateData#state.req_list, El#hbr.rid /= Rid]],
|
2009-08-31 20:37:52 +02:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state{output = [], http_receiver = undefined,
|
|
|
|
req_list = ReqList, wait_timer = undefined,
|
2009-08-31 20:37:52 +02:00
|
|
|
timer = Timer}};
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-06-16 19:48:22 +02:00
|
|
|
handle_sync_event({stop,close}, _From, _StateName, StateData) ->
|
2009-06-16 19:43:35 +02:00
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
2009-06-16 19:48:22 +02:00
|
|
|
handle_sync_event({stop,stream_closed}, _From, _StateName, StateData) ->
|
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
|
|
|
handle_sync_event({stop,Reason}, _From, _StateName, StateData) ->
|
2010-02-19 00:01:26 +01:00
|
|
|
?DEBUG("Closing bind session ~p - Reason: ~p", [StateData#state.id, Reason]),
|
2009-06-16 19:48:22 +02:00
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
2009-06-16 19:47:03 +02:00
|
|
|
%% HTTP PUT: Receive packets from the client
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(#http_put{rid = Rid}, _From,
|
|
|
|
StateName, StateData)
|
|
|
|
when StateData#state.shaper_timer /= undefined ->
|
|
|
|
Pause = case
|
|
|
|
erlang:read_timer(StateData#state.shaper_timer)
|
|
|
|
of
|
|
|
|
false -> 0;
|
|
|
|
P -> P
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
Reply = {wait, Pause},
|
|
|
|
?DEBUG("Shaper timer for RID ~p: ~p", [Rid, Reply]),
|
|
|
|
{reply, Reply, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(#http_put{payload_size =
|
|
|
|
PayloadSize} =
|
|
|
|
Request,
|
2009-09-18 15:29:08 +02:00
|
|
|
_From, StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("New request: ~p", [Request]),
|
2009-08-31 20:37:52 +02:00
|
|
|
{NewShaperState, NewShaperTimer} =
|
2013-03-14 10:33:02 +01:00
|
|
|
update_shaper(StateData#state.shaper_state,
|
|
|
|
PayloadSize),
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_http_put_event(Request, StateName,
|
|
|
|
StateData#state{shaper_state = NewShaperState,
|
|
|
|
shaper_timer = NewShaperTimer});
|
2009-06-16 19:47:03 +02:00
|
|
|
%% HTTP GET: send packets to the client
|
2009-06-16 19:46:21 +02:00
|
|
|
handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
|
2009-06-16 19:45:38 +02:00
|
|
|
%% setup timer
|
2009-08-31 20:37:52 +02:00
|
|
|
TNow = tnow(),
|
2009-06-16 19:48:13 +02:00
|
|
|
if
|
|
|
|
(Hold > 0) and
|
2012-04-27 13:19:49 +02:00
|
|
|
((StateData#state.output == []) or (StateData#state.rid < Rid)) and
|
2009-06-16 19:48:13 +02:00
|
|
|
((TNow - StateData#state.ctime) < (Wait*1000*1000)) and
|
2012-04-27 13:19:49 +02:00
|
|
|
(StateData#state.rid =< Rid) and
|
2012-05-02 20:59:09 +02:00
|
|
|
(StateData#state.pause == 0) ->
|
|
|
|
send_receiver_reply(StateData#state.http_receiver, {ok, empty}),
|
|
|
|
cancel_timer(StateData#state.wait_timer),
|
2009-06-16 19:46:21 +02:00
|
|
|
WaitTimer = erlang:start_timer(Wait * 1000, self(), []),
|
2009-08-31 20:37:52 +02:00
|
|
|
%% MR: Not sure we should cancel the state timer here.
|
2009-06-16 19:48:22 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-06-16 19:46:21 +02:00
|
|
|
{next_state, StateName, StateData#state{
|
|
|
|
http_receiver = From,
|
2012-04-27 13:19:49 +02:00
|
|
|
out_of_order_receiver = StateData#state.rid < Rid,
|
2009-06-16 19:46:21 +02:00
|
|
|
wait_timer = WaitTimer,
|
2009-06-16 19:48:00 +02:00
|
|
|
timer = undefined}};
|
2009-06-16 19:43:35 +02:00
|
|
|
true ->
|
2009-06-16 19:48:13 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-08-31 20:37:52 +02:00
|
|
|
Reply = {ok, StateData#state.output},
|
2009-06-16 19:43:35 +02:00
|
|
|
%% save request
|
2009-08-31 20:37:52 +02:00
|
|
|
ReqList = [#hbr{rid = Rid,
|
|
|
|
key = StateData#state.key,
|
|
|
|
out = StateData#state.output
|
2009-06-16 19:48:13 +02:00
|
|
|
} |
|
|
|
|
[El || El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid /= Rid ]
|
2009-06-16 19:43:35 +02:00
|
|
|
],
|
2012-05-02 20:59:09 +02:00
|
|
|
if
|
|
|
|
(StateData#state.http_receiver /= undefined) and
|
|
|
|
StateData#state.out_of_order_receiver ->
|
|
|
|
{reply, Reply, StateName, StateData#state{
|
|
|
|
output = [],
|
|
|
|
timer = undefined,
|
|
|
|
req_list = ReqList,
|
|
|
|
out_of_order_receiver = false}};
|
|
|
|
true ->
|
|
|
|
send_receiver_reply(StateData#state.http_receiver, {ok, empty}),
|
|
|
|
cancel_timer(StateData#state.wait_timer),
|
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
{reply, Reply, StateName,
|
|
|
|
StateData#state{output = [],
|
|
|
|
http_receiver = undefined,
|
|
|
|
wait_timer = undefined,
|
|
|
|
timer = Timer,
|
|
|
|
req_list = ReqList}}
|
|
|
|
end
|
2009-06-16 19:46:21 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(peername, _From, StateName,
|
|
|
|
StateData) ->
|
2009-06-16 19:46:55 +02:00
|
|
|
Reply = {ok, StateData#state.ip},
|
|
|
|
{reply, Reply, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Reply = ok, {reply, Reply, StateName, StateData}.
|
2009-06-16 19:43:35 +02:00
|
|
|
|
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-06-16 19:48:13 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2009-06-16 19:43:35 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:37:52 +02:00
|
|
|
%% We reached the max_inactivity timeout:
|
2009-06-16 19:43:35 +02:00
|
|
|
handle_info({timeout, Timer, _}, _StateName,
|
2013-03-14 10:33:02 +01:00
|
|
|
#state{id = SID, timer = Timer} = StateData) ->
|
|
|
|
?INFO_MSG("Session timeout. Closing the HTTP bind "
|
|
|
|
"session: ~p",
|
|
|
|
[SID]),
|
2009-06-16 19:43:35 +02:00
|
|
|
{stop, normal, StateData};
|
2009-06-16 19:46:25 +02:00
|
|
|
handle_info({timeout, WaitTimer, _}, StateName,
|
|
|
|
#state{wait_timer = WaitTimer} = StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if StateData#state.http_receiver /= undefined ->
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
gen_fsm:reply(StateData#state.http_receiver,
|
|
|
|
{ok, empty}),
|
|
|
|
Rid = StateData#state.rid,
|
|
|
|
ReqList = [#hbr{rid = Rid, key = StateData#state.key,
|
|
|
|
out = []}
|
|
|
|
| [El
|
|
|
|
|| El <- StateData#state.req_list, El#hbr.rid /= Rid]],
|
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{http_receiver = undefined,
|
|
|
|
req_list = ReqList, wait_timer = undefined,
|
|
|
|
timer = Timer}};
|
|
|
|
true -> {next_state, StateName, StateData}
|
2009-06-16 19:46:21 +02:00
|
|
|
end;
|
2009-08-31 20:37:52 +02:00
|
|
|
handle_info({timeout, ShaperTimer, _}, StateName,
|
|
|
|
#state{shaper_timer = ShaperTimer} = StateData) ->
|
|
|
|
{next_state, StateName, StateData#state{shaper_timer = undefined}};
|
|
|
|
|
2009-06-16 19:43:35 +02:00
|
|
|
handle_info(_, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("terminate: Deleting session ~s",
|
|
|
|
[StateData#state.id]),
|
2010-05-05 22:23:06 +02:00
|
|
|
mnesia:dirty_delete({http_bind, StateData#state.id}),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_receiver_reply(StateData#state.http_receiver,
|
|
|
|
{ok, terminate}),
|
2009-06-16 19:43:35 +02:00
|
|
|
case StateData#state.waiting_input of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> ok;
|
|
|
|
C2SPid -> gen_fsm:send_event(C2SPid, closed)
|
2009-06-16 19:43:35 +02:00
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-06-16 19:47:03 +02:00
|
|
|
%% PUT / Get processing:
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_http_put_event(#http_put{rid = Rid,
|
|
|
|
attrs = Attrs, hold = Hold} =
|
|
|
|
Request,
|
2009-08-31 20:37:52 +02:00
|
|
|
StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("New request: ~p", [Request]),
|
|
|
|
RidAllow = rid_allow(StateData#state.rid, Rid, Attrs,
|
|
|
|
Hold, StateData#state.max_pause),
|
2009-08-31 20:37:52 +02:00
|
|
|
case RidAllow of
|
2013-03-14 10:33:02 +01:00
|
|
|
buffer ->
|
|
|
|
?DEBUG("Buffered request: ~p", [Request]),
|
|
|
|
PendingRequests = StateData#state.unprocessed_req_list,
|
|
|
|
Requests = lists:keydelete(Rid, 2, PendingRequests),
|
|
|
|
ReqList = [#hbr{rid = Rid, key = StateData#state.key,
|
|
|
|
out = []}
|
|
|
|
| [El
|
|
|
|
|| El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid > Rid - 1 - Hold]],
|
|
|
|
?DEBUG("reqlist: ~p", [ReqList]),
|
|
|
|
UnprocessedReqList = [Request | Requests],
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = set_inactivity_timer(0,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
{reply, ok, StateName,
|
|
|
|
StateData#state{unprocessed_req_list =
|
|
|
|
UnprocessedReqList,
|
|
|
|
req_list = ReqList, timer = Timer},
|
|
|
|
hibernate};
|
|
|
|
_ ->
|
|
|
|
process_http_put(Request, StateName, StateData,
|
|
|
|
RidAllow)
|
2009-08-31 20:37:52 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_http_put(#http_put{rid = Rid, attrs = Attrs,
|
|
|
|
payload = Payload, hold = Hold, stream = StreamTo,
|
|
|
|
ip = IP} =
|
|
|
|
Request,
|
2009-06-16 19:47:03 +02:00
|
|
|
StateName, StateData, RidAllow) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
?DEBUG("Actually processing request: ~p", [Request]),
|
2013-03-14 10:33:02 +01:00
|
|
|
Key = xml:get_attr_s(<<"key">>, Attrs),
|
|
|
|
NewKey = xml:get_attr_s(<<"newkey">>, Attrs),
|
|
|
|
KeyAllow = case RidAllow of
|
|
|
|
repeat -> true;
|
|
|
|
false -> false;
|
|
|
|
{true, _} ->
|
|
|
|
case StateData#state.key of
|
|
|
|
<<"">> -> true;
|
|
|
|
OldKey ->
|
2013-06-20 10:40:44 +02:00
|
|
|
NextKey = p1_sha:sha(Key),
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("Key/OldKey/NextKey: ~s/~s/~s",
|
|
|
|
[Key, OldKey, NextKey]),
|
|
|
|
if OldKey == NextKey -> true;
|
|
|
|
true -> ?DEBUG("wrong key: ~s", [Key]), false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
TNow = tnow(),
|
2013-03-14 10:33:02 +01:00
|
|
|
LastPoll = if Payload == [] -> TNow;
|
|
|
|
true -> 0
|
2009-06-16 19:47:03 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
if (Payload == []) and (Hold == 0) and
|
|
|
|
(TNow - StateData#state.last_poll < (?MIN_POLLING)) ->
|
|
|
|
Reply = {error, polling_too_frequently},
|
|
|
|
{reply, Reply, StateName, StateData};
|
|
|
|
KeyAllow ->
|
|
|
|
case RidAllow of
|
|
|
|
false ->
|
|
|
|
Reply = {error, not_exists},
|
|
|
|
{reply, Reply, StateName, StateData};
|
|
|
|
repeat ->
|
|
|
|
?DEBUG("REPEATING ~p", [Rid]),
|
|
|
|
case [El#hbr.out
|
|
|
|
|| El <- StateData#state.req_list, El#hbr.rid == Rid]
|
|
|
|
of
|
|
|
|
[] -> {error, not_exists};
|
|
|
|
[Out | _XS] ->
|
|
|
|
if (Rid == StateData#state.rid) and
|
|
|
|
(StateData#state.http_receiver /= undefined) ->
|
|
|
|
{reply, ok, StateName, StateData};
|
|
|
|
true ->
|
|
|
|
Reply = {repeat, lists:reverse(Out)},
|
|
|
|
{reply, Reply, StateName,
|
|
|
|
StateData#state{last_poll = LastPoll}}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
{true, Pause} ->
|
|
|
|
SaveKey = if NewKey == <<"">> -> Key;
|
|
|
|
true -> NewKey
|
|
|
|
end,
|
|
|
|
?DEBUG(" -- SaveKey: ~s~n", [SaveKey]),
|
|
|
|
ReqList1 = [El
|
|
|
|
|| El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid > Rid - 1 - Hold],
|
|
|
|
ReqList = case lists:keymember(Rid, #hbr.rid, ReqList1)
|
|
|
|
of
|
|
|
|
true -> ReqList1;
|
|
|
|
false ->
|
|
|
|
[#hbr{rid = Rid, key = StateData#state.key,
|
|
|
|
out = []}
|
|
|
|
| ReqList1]
|
|
|
|
end,
|
|
|
|
?DEBUG("reqlist: ~p", [ReqList]),
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = set_inactivity_timer(Pause,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
case StateData#state.waiting_input of
|
|
|
|
false ->
|
|
|
|
Input = lists:foldl(fun queue:in/2,
|
|
|
|
StateData#state.input, Payload),
|
|
|
|
Reply = ok,
|
|
|
|
process_buffered_request(Reply, StateName,
|
|
|
|
StateData#state{input = Input,
|
|
|
|
rid = Rid,
|
|
|
|
key = SaveKey,
|
|
|
|
ctime = TNow,
|
|
|
|
timer = Timer,
|
|
|
|
pause = Pause,
|
|
|
|
last_poll =
|
|
|
|
LastPoll,
|
|
|
|
req_list =
|
|
|
|
ReqList,
|
|
|
|
ip = IP});
|
|
|
|
C2SPid ->
|
|
|
|
case StreamTo of
|
|
|
|
{To, <<"">>} ->
|
|
|
|
gen_fsm:send_event(C2SPid,
|
|
|
|
{xmlstreamstart,
|
|
|
|
<<"stream:stream">>,
|
|
|
|
[{<<"to">>, To},
|
|
|
|
{<<"xmlns">>, ?NS_CLIENT},
|
|
|
|
{<<"xmlns:stream">>,
|
|
|
|
?NS_STREAM}]});
|
|
|
|
{To, Version} ->
|
|
|
|
gen_fsm:send_event(C2SPid,
|
|
|
|
{xmlstreamstart,
|
|
|
|
<<"stream:stream">>,
|
|
|
|
[{<<"to">>, To},
|
|
|
|
{<<"xmlns">>, ?NS_CLIENT},
|
|
|
|
{<<"version">>, Version},
|
|
|
|
{<<"xmlns:stream">>,
|
|
|
|
?NS_STREAM}]});
|
|
|
|
_ -> ok
|
|
|
|
end,
|
|
|
|
MaxInactivity = get_max_inactivity(StreamTo,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
MaxPause = get_max_inactivity(StreamTo,
|
|
|
|
StateData#state.max_pause),
|
|
|
|
?DEBUG("really sending now: ~p", [Payload]),
|
|
|
|
lists:foreach(fun ({xmlstreamend, End}) ->
|
|
|
|
gen_fsm:send_event(C2SPid,
|
|
|
|
{xmlstreamend,
|
|
|
|
End});
|
|
|
|
(El) ->
|
|
|
|
gen_fsm:send_event(C2SPid,
|
|
|
|
{xmlstreamelement,
|
|
|
|
El})
|
|
|
|
end,
|
|
|
|
Payload),
|
|
|
|
Reply = ok,
|
|
|
|
process_buffered_request(Reply, StateName,
|
|
|
|
StateData#state{input =
|
|
|
|
queue:new(),
|
|
|
|
rid = Rid,
|
|
|
|
key = SaveKey,
|
|
|
|
ctime = TNow,
|
|
|
|
timer = Timer,
|
|
|
|
pause = Pause,
|
|
|
|
last_poll =
|
|
|
|
LastPoll,
|
|
|
|
req_list =
|
|
|
|
ReqList,
|
|
|
|
max_inactivity =
|
|
|
|
MaxInactivity,
|
|
|
|
max_pause =
|
|
|
|
MaxPause,
|
|
|
|
ip = IP})
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
Reply = {error, bad_key},
|
|
|
|
{reply, Reply, StateName, StateData}
|
2009-06-16 19:47:03 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_buffered_request(Reply, StateName, StateData) ->
|
|
|
|
Rid = StateData#state.rid,
|
|
|
|
Requests = StateData#state.unprocessed_req_list,
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(Rid + 1, 2, Requests) of
|
|
|
|
{value, Request} ->
|
|
|
|
?DEBUG("Processing buffered request: ~p", [Request]),
|
|
|
|
NewRequests = lists:keydelete(Rid + 1, 2, Requests),
|
|
|
|
handle_http_put_event(Request, StateName,
|
|
|
|
StateData#state{unprocessed_req_list =
|
|
|
|
NewRequests});
|
|
|
|
_ -> {reply, Reply, StateName, StateData, hibernate}
|
2009-06-16 19:47:03 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize,
|
|
|
|
StreamStart, IP) ->
|
|
|
|
case http_put(Sid, Rid, Attrs, Payload, PayloadSize,
|
|
|
|
StreamStart, IP)
|
|
|
|
of
|
|
|
|
{error, not_exists} ->
|
|
|
|
?DEBUG("no session associated with sid: ~p", [Sid]),
|
|
|
|
{404, ?HEADER, <<"">>};
|
|
|
|
{{error, Reason}, Sess} ->
|
|
|
|
?DEBUG("Error on HTTP put. Reason: ~p", [Reason]),
|
|
|
|
handle_http_put_error(Reason, Sess);
|
|
|
|
{{repeat, OutPacket}, Sess} ->
|
|
|
|
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p",
|
|
|
|
[OutPacket]),
|
|
|
|
send_outpacket(Sess, OutPacket);
|
|
|
|
{{wait, Pause}, _Sess} ->
|
|
|
|
?DEBUG("Trafic Shaper: Delaying request ~p", [Rid]),
|
|
|
|
timer:sleep(Pause),
|
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize,
|
|
|
|
StreamStart, IP);
|
|
|
|
{ok, Sess} ->
|
|
|
|
prepare_response(Sess, Rid, [], StreamStart)
|
2009-06-16 19:43:35 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
http_put(Sid, Rid, Attrs, Payload, PayloadSize,
|
|
|
|
StreamStart, IP) ->
|
2009-06-16 19:48:22 +02:00
|
|
|
?DEBUG("Looking for session: ~p", [Sid]),
|
2009-06-16 19:43:47 +02:00
|
|
|
case mnesia:dirty_read({http_bind, Sid}) of
|
2009-06-16 19:43:35 +02:00
|
|
|
[] ->
|
2009-06-16 19:45:22 +02:00
|
|
|
{error, not_exists};
|
|
|
|
[#http_bind{pid = FsmRef, hold=Hold, to={To, StreamVersion}}=Sess] ->
|
2009-06-16 19:48:13 +02:00
|
|
|
NewStream =
|
2009-06-16 19:45:22 +02:00
|
|
|
case StreamStart of
|
|
|
|
true ->
|
|
|
|
{To, StreamVersion};
|
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>
|
2009-06-16 19:45:22 +02:00
|
|
|
end,
|
|
|
|
{gen_fsm:sync_send_all_state_event(
|
2009-08-31 20:37:52 +02:00
|
|
|
FsmRef, #http_put{rid = Rid, attrs = Attrs, payload = Payload,
|
|
|
|
payload_size = PayloadSize, hold = Hold,
|
|
|
|
stream = NewStream, ip = IP}, 30000), Sess}
|
2009-06-16 19:43:35 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_http_put_error(Reason,
|
|
|
|
#http_bind{pid = FsmRef, version = Version})
|
|
|
|
when Version >= 0 ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop, {put_error, Reason}}),
|
2009-06-16 19:45:22 +02:00
|
|
|
case Reason of
|
2013-03-14 10:33:02 +01:00
|
|
|
not_exists ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
xml:element_to_binary(#xmlel{name = <<"body">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_HTTP_BIND},
|
|
|
|
{<<"type">>, <<"terminate">>},
|
|
|
|
{<<"condition">>,
|
|
|
|
<<"item-not-found">>}],
|
|
|
|
children = []})};
|
|
|
|
bad_key ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
xml:element_to_binary(#xmlel{name = <<"body">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_HTTP_BIND},
|
|
|
|
{<<"type">>, <<"terminate">>},
|
|
|
|
{<<"condition">>,
|
|
|
|
<<"item-not-found">>}],
|
|
|
|
children = []})};
|
|
|
|
polling_too_frequently ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
xml:element_to_binary(#xmlel{name = <<"body">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_HTTP_BIND},
|
|
|
|
{<<"type">>, <<"terminate">>},
|
|
|
|
{<<"condition">>,
|
|
|
|
<<"policy-violation">>}],
|
|
|
|
children = []})}
|
2009-06-16 19:45:22 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_http_put_error(Reason,
|
|
|
|
#http_bind{pid = FsmRef}) ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop, {put_error_no_version, Reason}}),
|
2009-06-16 19:48:13 +02:00
|
|
|
case Reason of
|
2013-03-14 10:33:02 +01:00
|
|
|
not_exists -> %% bad rid
|
|
|
|
?DEBUG("Closing HTTP bind session (Bad rid).", []),
|
|
|
|
{404, ?HEADER, <<"">>};
|
|
|
|
bad_key ->
|
|
|
|
?DEBUG("Closing HTTP bind session (Bad key).", []),
|
|
|
|
{404, ?HEADER, <<"">>};
|
|
|
|
polling_too_frequently ->
|
|
|
|
?DEBUG("Closing HTTP bind session (User polling "
|
|
|
|
"too frequently).",
|
|
|
|
[]),
|
|
|
|
{403, ?HEADER, <<"">>}
|
2009-06-16 19:45:09 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Control RID ordering
|
|
|
|
rid_allow(none, _NewRid, _Attrs, _Hold, _MaxPause) ->
|
|
|
|
{true, 0};
|
|
|
|
rid_allow(OldRid, NewRid, Attrs, Hold, MaxPause) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("Previous rid / New rid: ~p/~p",
|
|
|
|
[OldRid, NewRid]),
|
2009-08-31 20:37:52 +02:00
|
|
|
if
|
2013-03-14 10:33:02 +01:00
|
|
|
%% We did not miss any packet, we can process it immediately:
|
|
|
|
NewRid == OldRid + 1 ->
|
|
|
|
case catch
|
|
|
|
jlib:binary_to_integer(xml:get_attr_s(<<"pause">>,
|
|
|
|
Attrs))
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> {true, 0};
|
|
|
|
Pause1 when Pause1 =< MaxPause ->
|
|
|
|
?DEBUG("got pause: ~p", [Pause1]), {true, Pause1};
|
|
|
|
_ -> {true, 0}
|
|
|
|
end;
|
|
|
|
%% We have missed packets, we need to cached it to process it later on:
|
|
|
|
(OldRid < NewRid) and (NewRid =< OldRid + Hold + 1) ->
|
|
|
|
buffer;
|
|
|
|
(NewRid =< OldRid) and (NewRid > OldRid - Hold - 1) ->
|
|
|
|
repeat;
|
|
|
|
true -> false
|
2009-08-31 20:37:52 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
update_shaper(ShaperState, PayloadSize) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{NewShaperState, Pause} = shaper:update(ShaperState,
|
|
|
|
PayloadSize),
|
|
|
|
if Pause > 0 ->
|
|
|
|
ShaperTimer = erlang:start_timer(Pause, self(),
|
|
|
|
activate),
|
|
|
|
{NewShaperState, ShaperTimer};
|
|
|
|
true -> {NewShaperState, undefined}
|
2009-08-31 20:37:52 +02:00
|
|
|
end.
|
|
|
|
|
2010-05-05 22:45:46 +02:00
|
|
|
prepare_response(Sess, Rid, OutputEls, StreamStart) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
receive after Sess#http_bind.process_delay -> ok end,
|
2009-06-16 19:46:21 +02:00
|
|
|
case catch http_get(Sess, Rid) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, cancel} ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='error' xmlns='", (?NS_HTTP_BIND)/binary,
|
|
|
|
"'/>">>};
|
|
|
|
{ok, empty} ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body xmlns='", (?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
{ok, terminate} ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
{ok, ROutPacket} ->
|
|
|
|
OutPacket = lists:reverse(ROutPacket),
|
|
|
|
?DEBUG("OutPacket: ~p", [OutputEls ++ OutPacket]),
|
|
|
|
prepare_outpacket_response(Sess, Rid,
|
|
|
|
OutputEls ++ OutPacket, StreamStart);
|
|
|
|
{'EXIT', {shutdown, _}} ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='system-shut"
|
|
|
|
"down' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>}
|
2009-06-16 19:45:09 +02:00
|
|
|
end.
|
2009-06-16 19:45:22 +02:00
|
|
|
|
2010-05-05 22:45:46 +02:00
|
|
|
%% Send output payloads on establised sessions
|
2013-03-14 10:33:02 +01:00
|
|
|
prepare_outpacket_response(Sess, _Rid, OutPacket,
|
|
|
|
false) ->
|
2010-05-05 22:45:46 +02:00
|
|
|
case catch send_outpacket(Sess, OutPacket) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
?DEBUG("Error in sending packet ~p ", [_Reason]),
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
SendRes -> SendRes
|
2010-05-05 22:45:46 +02:00
|
|
|
end;
|
|
|
|
%% Handle a new session along with its output payload
|
2013-03-14 10:33:02 +01:00
|
|
|
prepare_outpacket_response(#http_bind{id = Sid,
|
|
|
|
wait = Wait, hold = Hold, to = To} =
|
|
|
|
_Sess,
|
|
|
|
_Rid, OutPacket, true) ->
|
2010-05-05 22:45:46 +02:00
|
|
|
case OutPacket of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{xmlstreamstart, _, OutAttrs} | Els] ->
|
|
|
|
AuthID = xml:get_attr_s(<<"id">>, OutAttrs),
|
|
|
|
From = xml:get_attr_s(<<"from">>, OutAttrs),
|
|
|
|
Version = xml:get_attr_s(<<"version">>, OutAttrs),
|
|
|
|
OutEls = case Els of
|
|
|
|
[] -> [];
|
|
|
|
[{xmlstreamelement,
|
|
|
|
#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs = StreamAttribs, children = StreamEls}}
|
|
|
|
| StreamTail] ->
|
|
|
|
TypedTail = [check_default_xmlns(OEl)
|
|
|
|
|| {xmlstreamelement, OEl} <- StreamTail],
|
|
|
|
[#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns:stream">>, ?NS_STREAM}] ++
|
|
|
|
StreamAttribs,
|
|
|
|
children = StreamEls}]
|
|
|
|
++ TypedTail;
|
|
|
|
StreamTail ->
|
|
|
|
[check_default_xmlns(OEl)
|
|
|
|
|| {xmlstreamelement, OEl} <- StreamTail]
|
|
|
|
end,
|
|
|
|
case OutEls of
|
|
|
|
[#xmlel{name = <<"stream:error">>}] ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='host-unknow"
|
|
|
|
"n' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
_ ->
|
|
|
|
BOSH_attribs = [{<<"authid">>, AuthID},
|
|
|
|
{<<"xmlns:xmpp">>, ?NS_BOSH},
|
|
|
|
{<<"xmlns:stream">>, ?NS_STREAM}]
|
|
|
|
++
|
|
|
|
case OutEls of
|
|
|
|
[] -> [];
|
|
|
|
_ -> [{<<"xmpp:version">>, Version}]
|
|
|
|
end,
|
|
|
|
MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY),
|
|
|
|
MaxPause = get_max_pause(To),
|
|
|
|
{200, ?HEADER,
|
|
|
|
xml:element_to_binary(#xmlel{name = <<"body">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_HTTP_BIND},
|
|
|
|
{<<"sid">>, Sid},
|
|
|
|
{<<"wait">>,
|
|
|
|
iolist_to_binary(integer_to_list(Wait))},
|
|
|
|
{<<"requests">>,
|
|
|
|
iolist_to_binary(integer_to_list(Hold
|
|
|
|
+
|
|
|
|
1))},
|
|
|
|
{<<"inactivity">>,
|
|
|
|
iolist_to_binary(integer_to_list(trunc(MaxInactivity
|
|
|
|
/
|
|
|
|
1000)))},
|
|
|
|
{<<"maxpause">>,
|
|
|
|
iolist_to_binary(integer_to_list(MaxPause))},
|
|
|
|
{<<"polling">>,
|
|
|
|
iolist_to_binary(integer_to_list(trunc((?MIN_POLLING)
|
|
|
|
/
|
|
|
|
1000000)))},
|
|
|
|
{<<"ver">>, ?BOSH_VERSION},
|
|
|
|
{<<"from">>, From},
|
|
|
|
{<<"secure">>, <<"true">>}]
|
|
|
|
++ BOSH_attribs,
|
|
|
|
children = OutEls})}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='internal-se"
|
|
|
|
"rver-error' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>}
|
2010-05-05 22:45:46 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
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).
|
2009-06-16 19:46:21 +02:00
|
|
|
|
2009-06-16 19:45:22 +02:00
|
|
|
send_outpacket(#http_bind{pid = FsmRef}, OutPacket) ->
|
2009-06-16 19:45:09 +02:00
|
|
|
case OutPacket of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body xmlns='", (?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
[{xmlstreamend, _}] ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop, stream_closed}),
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body xmlns='", (?NS_HTTP_BIND)/binary, "'/>">>};
|
|
|
|
_ ->
|
|
|
|
AllElements = lists:all(fun ({xmlstreamelement,
|
|
|
|
#xmlel{name = <<"stream:error">>}}) ->
|
|
|
|
false;
|
|
|
|
({xmlstreamelement, _}) -> true;
|
|
|
|
({xmlstreamraw, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
OutPacket),
|
|
|
|
case AllElements of
|
|
|
|
true ->
|
|
|
|
TypedEls = lists:foldl(fun ({xmlstreamelement, El},
|
|
|
|
Acc) ->
|
|
|
|
Acc ++
|
|
|
|
[xml:element_to_binary(check_default_xmlns(El))];
|
|
|
|
({xmlstreamraw, R}, Acc) ->
|
|
|
|
Acc ++ [R]
|
|
|
|
end,
|
|
|
|
[], OutPacket),
|
|
|
|
Body = <<"<body xmlns='", (?NS_HTTP_BIND)/binary, "'>",
|
|
|
|
(iolist_to_binary(TypedEls))/binary, "</body>">>,
|
|
|
|
?DEBUG(" --- outgoing data --- ~n~s~n --- END "
|
|
|
|
"--- ~n",
|
|
|
|
[Body]),
|
|
|
|
{200, ?HEADER, Body};
|
|
|
|
false ->
|
|
|
|
case OutPacket of
|
|
|
|
[{xmlstreamstart, _, _} | SEls] ->
|
|
|
|
OutEls = case SEls of
|
|
|
|
[{xmlstreamelement,
|
|
|
|
#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs = StreamAttribs,
|
|
|
|
children = StreamEls}}
|
|
|
|
| StreamTail] ->
|
|
|
|
TypedTail = [check_default_xmlns(OEl)
|
|
|
|
|| {xmlstreamelement, OEl}
|
|
|
|
<- StreamTail],
|
|
|
|
[#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns:stream">>,
|
|
|
|
?NS_STREAM}]
|
|
|
|
++ StreamAttribs,
|
|
|
|
children = StreamEls}]
|
|
|
|
++ TypedTail;
|
|
|
|
StreamTail ->
|
|
|
|
[check_default_xmlns(OEl)
|
|
|
|
|| {xmlstreamelement, OEl} <- StreamTail]
|
|
|
|
end,
|
|
|
|
{200, ?HEADER,
|
|
|
|
xml:element_to_binary(#xmlel{name = <<"body">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_HTTP_BIND}],
|
|
|
|
children = OutEls})};
|
|
|
|
_ ->
|
|
|
|
SErrCond = lists:filter(fun ({xmlstreamelement,
|
|
|
|
#xmlel{name =
|
|
|
|
<<"stream:error">>}}) ->
|
|
|
|
true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
OutPacket),
|
|
|
|
StreamErrCond = case SErrCond of
|
|
|
|
[] -> null;
|
|
|
|
[{xmlstreamelement,
|
|
|
|
#xmlel{} = StreamErrorTag}
|
|
|
|
| _] ->
|
|
|
|
[StreamErrorTag]
|
|
|
|
end,
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
|
|
|
{stop,
|
|
|
|
{stream_error,
|
|
|
|
OutPacket}}),
|
|
|
|
case StreamErrCond of
|
|
|
|
null ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='internal-se"
|
|
|
|
"rver-error' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "'/>">>};
|
2009-08-31 20:37:52 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{200, ?HEADER,
|
|
|
|
<<"<body type='terminate' condition='remote-stre"
|
|
|
|
"am-error' xmlns='",
|
|
|
|
(?NS_HTTP_BIND)/binary, "' ", "xmlns:stream='",
|
|
|
|
(?NS_STREAM)/binary, "'>",
|
|
|
|
(elements_to_string(StreamErrCond))/binary,
|
|
|
|
"</body>">>}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-16 19:45:09 +02:00
|
|
|
end.
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-09-18 15:17:07 +02:00
|
|
|
parse_request(Data, PayloadSize, MaxStanzaSize) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("--- incoming data --- ~n~s~n --- END "
|
|
|
|
"--- ",
|
|
|
|
[Data]),
|
2009-06-16 19:44:13 +02:00
|
|
|
case xml_stream:parse_element(Data) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"body">>, attrs = Attrs,
|
|
|
|
children = Els} ->
|
|
|
|
Xmlns = xml:get_attr_s(<<"xmlns">>, Attrs),
|
|
|
|
if Xmlns /= (?NS_HTTP_BIND) -> {error, bad_request};
|
|
|
|
true ->
|
|
|
|
case catch
|
|
|
|
jlib:binary_to_integer(xml:get_attr_s(<<"rid">>,
|
|
|
|
Attrs))
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> {error, bad_request};
|
|
|
|
Rid ->
|
|
|
|
FixedEls = lists:filter(fun (I) ->
|
|
|
|
case I of
|
|
|
|
#xmlel{} -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Els),
|
|
|
|
Sid = xml:get_attr_s(<<"sid">>, Attrs),
|
|
|
|
if PayloadSize =< MaxStanzaSize ->
|
|
|
|
{ok, {Sid, Rid, Attrs, FixedEls}};
|
|
|
|
true -> {size_limit, Sid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
#xmlel{} -> {error, bad_request};
|
|
|
|
{error, _Reason} -> {error, bad_request}
|
2009-06-16 19:43:35 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
send_receiver_reply(undefined, _Reply) -> ok;
|
2009-08-31 20:37:52 +02:00
|
|
|
send_receiver_reply(Receiver, Reply) ->
|
|
|
|
gen_fsm:reply(Receiver, Reply).
|
|
|
|
|
2009-06-16 19:48:13 +02:00
|
|
|
%% Cancel timer and empty message queue.
|
2013-03-14 10:33:02 +01:00
|
|
|
cancel_timer(undefined) -> ok;
|
2009-06-16 19:43:35 +02:00
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
2013-03-14 10:33:02 +01:00
|
|
|
receive {timeout, Timer, _} -> ok after 0 -> ok end.
|
2009-06-16 19:43:35 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% If client asked for a pause (pause > 0), we apply the pause value
|
|
|
|
%% as inactivity timer:
|
2013-03-14 10:33:02 +01:00
|
|
|
set_inactivity_timer(Pause, _MaxInactivity)
|
|
|
|
when Pause > 0 ->
|
|
|
|
erlang:start_timer(Pause * 1000, self(), []);
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Otherwise, we apply the max_inactivity value as inactivity timer:
|
|
|
|
set_inactivity_timer(_Pause, MaxInactivity) ->
|
|
|
|
erlang:start_timer(MaxInactivity, self(), []).
|
|
|
|
|
2009-06-16 19:48:13 +02:00
|
|
|
%% TODO: Use tail recursion and list reverse ?
|
2013-03-14 10:33:02 +01:00
|
|
|
elements_to_string([]) -> [];
|
2009-06-16 19:43:35 +02:00
|
|
|
elements_to_string([El | Els]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[xml:element_to_binary(El) | elements_to_string(Els)].
|
2009-06-16 19:43:39 +02:00
|
|
|
|
2009-06-16 19:47:23 +02:00
|
|
|
%% @spec (To, Default::integer()) -> integer()
|
|
|
|
%% where To = [] | {Host::string(), Version::string()}
|
|
|
|
get_max_inactivity({Host, _}, Default) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case gen_mod:get_module_opt(Host, mod_http_bind, max_inactivity,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
undefined)
|
|
|
|
of
|
|
|
|
Seconds when is_integer(Seconds) -> Seconds * 1000;
|
|
|
|
undefined -> Default
|
2009-06-16 19:47:23 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_max_inactivity(_, Default) -> Default.
|
2009-06-16 19:43:39 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
get_max_pause({Host, _}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:get_module_opt(Host, mod_http_bind, max_pause,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
?MAX_PAUSE);
|
|
|
|
get_max_pause(_) -> ?MAX_PAUSE.
|
2009-08-31 20:37:52 +02:00
|
|
|
|
|
|
|
%% Current time as integer
|
|
|
|
tnow() ->
|
|
|
|
{TMegSec, TSec, TMSec} = now(),
|
|
|
|
(TMegSec * 1000000 + TSec) * 1000000 + TMSec.
|
2009-06-16 19:46:03 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_default_xmlns(#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = Els} =
|
|
|
|
El) ->
|
|
|
|
case xml:get_tag_attr_s(<<"xmlns">>, El) of
|
|
|
|
<<"">> ->
|
|
|
|
#xmlel{name = Name,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_CLIENT} | Attrs],
|
|
|
|
children = Els};
|
|
|
|
_ -> El
|
2010-06-01 20:52:15 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_default_xmlns(El) -> El.
|
2009-06-16 19:47:56 +02:00
|
|
|
|
2009-06-16 19:48:13 +02:00
|
|
|
%% Check that mod_http_bind has been defined in config file.
|
|
|
|
%% Print a warning in log file if this is not the case.
|
2009-06-16 19:47:56 +02:00
|
|
|
check_bind_module(XmppDomain) ->
|
|
|
|
case gen_mod:is_loaded(XmppDomain, mod_http_bind) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> true;
|
|
|
|
false ->
|
|
|
|
?ERROR_MSG("You are trying to use BOSH (HTTP Bind) "
|
|
|
|
"in host ~p, but the module mod_http_bind "
|
|
|
|
"is not started in that host. Configure "
|
|
|
|
"your BOSH client to connect to the correct "
|
|
|
|
"host, or add your desired host to the "
|
|
|
|
"configuration, or check your 'modules' "
|
|
|
|
"section in your ejabberd configuration "
|
|
|
|
"file.",
|
|
|
|
[XmppDomain]),
|
|
|
|
false
|
2009-06-16 19:47:56 +02:00
|
|
|
end.
|