2009-06-16 20:23:40 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_http_bind.erl
|
|
|
|
%%% Author : Stefan Strigler <steve@zeank.in-berlin.de>
|
2009-06-16 20:27:35 +02:00
|
|
|
%%% Purpose : Implements XMPP over BOSH (XEP-0205) (formerly known as
|
2009-06-16 20:25:18 +02:00
|
|
|
%%% HTTP Binding)
|
2009-06-16 20:23:40 +02:00
|
|
|
%%% Created : 21 Sep 2005 by Stefan Strigler <steve@zeank.in-berlin.de>
|
2009-08-31 20:40:25 +02:00
|
|
|
%%% Modified: may 2009 by Mickael Remond, Alexey Schepin
|
2009-06-16 20:23:40 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_http_bind).
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2009-06-16 20:26:40 +02:00
|
|
|
-export([start_link/3,
|
2009-06-16 20:23:40 +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:40:25 +02:00
|
|
|
send_xml/2,
|
2009-06-16 20:27:35 +02:00
|
|
|
sockname/1,
|
2009-06-16 20:26:31 +02:00
|
|
|
peername/1,
|
2009-08-31 20:40:25 +02:00
|
|
|
setopts/2,
|
2009-06-16 20:23:40 +02:00
|
|
|
controlling_process/2,
|
2009-08-31 20:40:25 +02:00
|
|
|
become_controller/2,
|
2010-06-02 16:01:36 +02:00
|
|
|
change_controller/2,
|
2009-08-31 20:40:25 +02:00
|
|
|
custom_receiver/1,
|
|
|
|
reset_stream/1,
|
|
|
|
change_shaper/2,
|
|
|
|
monitor/1,
|
2009-06-16 20:23:40 +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 20:26:31 +02:00
|
|
|
process_request/2]).
|
2009-06-16 20:23:40 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
|
|
|
-include("ejabberd_http.hrl").
|
2010-02-15 22:39:32 +01:00
|
|
|
-include("http_bind.hrl").
|
2010-01-27 19:53:27 +01:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
2009-06-16 20:28:06 +02:00
|
|
|
|
2010-06-07 13:44:55 +02:00
|
|
|
-record(http_bind, {id, pid, to, hold, wait, process_delay, version}).
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:26:31 +02:00
|
|
|
-define(NULL_PEER, {{0, 0, 0, 0}, 0}).
|
|
|
|
|
2009-06-16 20:23:40 +02:00
|
|
|
%% http binding request
|
2009-06-16 20:24:18 +02:00
|
|
|
-record(hbr, {rid,
|
|
|
|
key,
|
|
|
|
out}).
|
2009-06-16 20:23:40 +02:00
|
|
|
|
|
|
|
-record(state, {id,
|
2009-06-16 20:25:10 +02:00
|
|
|
rid = none,
|
2009-06-16 20:23:40 +02:00
|
|
|
key,
|
2009-06-16 20:26:40 +02:00
|
|
|
socket,
|
2010-10-07 00:16:24 +02:00
|
|
|
output = [],
|
2009-08-31 20:40:25 +02:00
|
|
|
input = queue:new(),
|
2009-06-16 20:23:40 +02:00
|
|
|
waiting_input = false,
|
2009-08-31 20:40:25 +02:00
|
|
|
shaper_state,
|
|
|
|
shaper_timer,
|
2009-06-16 20:23:40 +02:00
|
|
|
last_receiver,
|
|
|
|
last_poll,
|
2009-06-16 20:26:02 +02:00
|
|
|
http_receiver,
|
|
|
|
wait_timer,
|
2009-06-16 20:23:40 +02:00
|
|
|
ctime = 0,
|
|
|
|
timer,
|
2009-06-16 20:26:31 +02:00
|
|
|
pause=0,
|
2009-08-31 20:40:25 +02:00
|
|
|
unprocessed_req_list = [], % list of request that have been delayed for proper reordering: {Request, PID}
|
2009-06-16 20:26:44 +02:00
|
|
|
req_list = [], % list of requests (cache)
|
2009-06-16 20:27:01 +02:00
|
|
|
max_inactivity,
|
2009-08-31 20:40:25 +02:00
|
|
|
max_pause,
|
2009-06-16 20:27:01 +02:00
|
|
|
ip = ?NULL_PEER
|
2009-06-16 20:23:40 +02:00
|
|
|
}).
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
%% Internal request format:
|
|
|
|
-record(http_put, {rid,
|
|
|
|
attrs,
|
|
|
|
payload,
|
|
|
|
payload_size,
|
|
|
|
hold,
|
|
|
|
stream,
|
|
|
|
ip}).
|
2009-06-16 20:24:18 +02:00
|
|
|
|
|
|
|
%%-define(DBGFSM, true).
|
2009-06-16 20:23:40 +02:00
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
2010-04-14 16:50:32 +02:00
|
|
|
-define(BOSH_VERSION_b, <<"1.8">>).
|
2009-06-16 20:24:38 +02:00
|
|
|
|
2009-06-16 20:23:40 +02:00
|
|
|
-define(MAX_REQUESTS, 2). % number of simultaneous requests
|
2009-06-16 20:25:10 +02:00
|
|
|
-define(MIN_POLLING, 2000000). % don't poll faster than that or we will
|
2009-06-16 20:26:07 +02:00
|
|
|
% shoot you (time in microsec)
|
2009-06-16 20:25:22 +02:00
|
|
|
-define(MAX_WAIT, 3600). % max num of secs to keep a request on hold
|
2010-05-05 22:30:35 +02:00
|
|
|
-define(MAX_INACTIVITY, 120000). % msecs to wait before terminating
|
2009-06-16 20:24:26 +02:00
|
|
|
% idle sessions
|
2009-06-16 20:27:35 +02:00
|
|
|
-define(MAX_PAUSE, 120). % may num of sec a client is allowed to pause
|
2009-06-16 20:25:22 +02:00
|
|
|
% the session
|
|
|
|
|
2010-06-07 13:44:55 +02:00
|
|
|
%% Wait 100ms before continue processing, to allow the client provide more related stanzas.
|
|
|
|
-define(PROCESS_DELAY_DEFAULT, 100).
|
|
|
|
-define(PROCESS_DELAY_MIN, 0).
|
|
|
|
-define(PROCESS_DELAY_MAX, 1000).
|
|
|
|
|
2010-11-17 20:29:36 +01:00
|
|
|
%% Line copied from mod_http_bind.erl
|
|
|
|
-define(PROCNAME_MHB, ejabberd_mod_http_bind).
|
2009-06-16 20:24:18 +02:00
|
|
|
|
2009-06-16 20:23:40 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-06-16 20:27:18 +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:29:36 +01:00
|
|
|
SupervisorProc = gen_mod:get_module_proc(XMPPDomain, ?PROCNAME_MHB),
|
|
|
|
case catch supervisor:start_child(SupervisorProc, [Sid, Key, IP]) of
|
2010-06-02 16:01:36 +02:00
|
|
|
{ok, Pid} ->
|
|
|
|
{ok, Pid};
|
|
|
|
{error, _} = Err ->
|
|
|
|
case check_bind_module(XMPPDomain) of
|
|
|
|
false ->
|
|
|
|
{error, "Cannot start HTTP bind session"};
|
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Cannot start HTTP bind session: ~p", [Err]),
|
|
|
|
Err
|
|
|
|
end;
|
2010-11-17 20:29:36 +01:00
|
|
|
{'EXIT', {noproc, _}} = Exit ->
|
|
|
|
?DEBUG("Cannot start HTTP bind session because mod_http_bind seems stopped:~n~p", [Exit]),
|
|
|
|
{error, Exit};
|
2010-06-02 16:01:36 +02:00
|
|
|
Exit ->
|
|
|
|
?ERROR_MSG("Cannot start HTTP bind session: ~p", [Exit]),
|
|
|
|
{error, Exit}
|
2009-06-16 20:27:18 +02:00
|
|
|
end.
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:26:40 +02:00
|
|
|
start_link(Sid, Key, IP) ->
|
|
|
|
gen_fsm:start_link(?MODULE, [Sid, Key, IP], ?FSMOPTS).
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:26:40 +02:00
|
|
|
send({http_bind, FsmRef, _IP}, Packet) ->
|
2009-06-16 20:23:40 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, {send, Packet}).
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
send_xml({http_bind, FsmRef, _IP}, Packet) ->
|
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, {send_xml, Packet}).
|
|
|
|
|
2009-06-16 20:26:40 +02:00
|
|
|
setopts({http_bind, FsmRef, _IP}, Opts) ->
|
2009-06-16 20:23:40 +02:00
|
|
|
case lists:member({active, once}, Opts) of
|
|
|
|
true ->
|
2009-06-16 20:23:45 +02:00
|
|
|
gen_fsm:send_all_state_event(FsmRef, {activate, self()});
|
2009-06-16 20:23:40 +02:00
|
|
|
_ ->
|
2010-06-02 16:01:36 +02:00
|
|
|
case lists:member({active, false}, Opts) of
|
|
|
|
true ->
|
|
|
|
gen_fsm:sync_send_all_state_event(
|
|
|
|
FsmRef, deactivate_socket);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2009-06-16 20:23:40 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
controlling_process(_Socket, _Pid) ->
|
|
|
|
ok.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
custom_receiver({http_bind, FsmRef, _IP}) ->
|
|
|
|
{receiver, ?MODULE, FsmRef}.
|
|
|
|
|
|
|
|
become_controller(FsmRef, C2SPid) ->
|
|
|
|
gen_fsm:send_all_state_event(FsmRef, {become_controller, C2SPid}).
|
|
|
|
|
2010-06-02 16:01:36 +02:00
|
|
|
change_controller({http_bind, FsmRef, _IP}, C2SPid) ->
|
|
|
|
become_controller(FsmRef, C2SPid).
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
reset_stream({http_bind, _FsmRef, _IP}) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
change_shaper({http_bind, FsmRef, _IP}, Shaper) ->
|
|
|
|
gen_fsm:send_all_state_event(FsmRef, {change_shaper, Shaper}).
|
|
|
|
|
|
|
|
monitor({http_bind, FsmRef, _IP}) ->
|
|
|
|
erlang:monitor(process, FsmRef).
|
|
|
|
|
2009-06-16 20:26:40 +02:00
|
|
|
close({http_bind, FsmRef, _IP}) ->
|
2009-06-16 20:27:45 +02:00
|
|
|
catch gen_fsm:sync_send_all_state_event(FsmRef, {stop, close}).
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:24:22 +02:00
|
|
|
sockname(_Socket) ->
|
2009-06-16 20:26:31 +02:00
|
|
|
{ok, ?NULL_PEER}.
|
|
|
|
|
2009-06-16 20:26:40 +02:00
|
|
|
peername({http_bind, _FsmRef, IP}) ->
|
|
|
|
{ok, IP}.
|
2009-06-16 20:24:18 +02:00
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
|
|
|
|
%% Entry point for data coming from client through ejabberd HTTP server:
|
2009-06-16 20:26:31 +02:00
|
|
|
process_request(Data, IP) ->
|
2009-08-31 20:40:25 +02:00
|
|
|
Opts1 = ejabberd_c2s_config:get_c2s_limits(),
|
|
|
|
Opts = [{xml_socket, true} | Opts1],
|
|
|
|
MaxStanzaSize =
|
|
|
|
case lists:keysearch(max_stanza_size, 1, Opts) of
|
|
|
|
{value, {_, Size}} -> Size;
|
|
|
|
_ -> infinity
|
|
|
|
end,
|
|
|
|
PayloadSize = iolist_size(Data),
|
|
|
|
case catch parse_request(Data, PayloadSize, MaxStanzaSize) of
|
|
|
|
%% No existing session:
|
2009-06-16 20:25:10 +02:00
|
|
|
{ok, {"", Rid, Attrs, Payload}} ->
|
2010-11-29 20:44:31 +01:00
|
|
|
case exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"to">>, "") of
|
2009-06-16 20:24:42 +02:00
|
|
|
"" ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Session not created (Improper addressing)", []),
|
2009-06-16 20:24:18 +02:00
|
|
|
{200, ?HEADER, "<body type='terminate' "
|
|
|
|
"condition='improper-addressing' "
|
2010-01-27 19:53:27 +01:00
|
|
|
"xmlns='" ++ ?NS_HTTP_BIND_s ++ "'/>"};
|
2009-06-16 20:24:42 +02:00
|
|
|
XmppDomain ->
|
|
|
|
%% create new session
|
2010-06-02 16:01:36 +02:00
|
|
|
Sid = make_sid(),
|
2009-06-16 20:27:18 +02:00
|
|
|
case start(XmppDomain, Sid, "", IP) of
|
|
|
|
{error, _} ->
|
2010-06-02 16:01:36 +02:00
|
|
|
{500, ?HEADER, "<body type='terminate' "
|
2009-06-16 20:27:18 +02:00
|
|
|
"condition='internal-server-error' "
|
2010-06-02 16:01:36 +02:00
|
|
|
"xmlns='" ++ ?NS_HTTP_BIND_s ++
|
|
|
|
"'>Internal Server Error</body>"};
|
2009-06-16 20:27:18 +02:00
|
|
|
{ok, Pid} ->
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_session_start(
|
|
|
|
Pid, XmppDomain, Sid, Rid, Attrs,
|
|
|
|
Payload, PayloadSize, IP)
|
2009-06-16 20:27:18 +02:00
|
|
|
end
|
2009-06-16 20:24:42 +02:00
|
|
|
end;
|
2009-08-31 20:40:25 +02:00
|
|
|
%% Existing session
|
2009-06-16 20:25:10 +02:00
|
|
|
{ok, {Sid, Rid, Attrs, Payload1}} ->
|
2009-06-16 20:27:35 +02:00
|
|
|
StreamStart =
|
2010-11-29 20:44:31 +01:00
|
|
|
case exmpp_xml:get_attribute_from_list_as_list(Attrs, ?NS_BOSH, <<"restart">>, "") of
|
2009-06-16 20:24:42 +02:00
|
|
|
"true" ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
2010-11-29 20:44:31 +01:00
|
|
|
Payload2 = case exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"type">>, "") of
|
2009-06-16 20:24:42 +02:00
|
|
|
"terminate" ->
|
|
|
|
%% close stream
|
2009-08-31 20:40:25 +02:00
|
|
|
Payload1 ++ [{xmlstreamend, "stream:stream"}];
|
2009-06-16 20:24:42 +02:00
|
|
|
_ ->
|
2009-06-16 20:25:10 +02:00
|
|
|
Payload1
|
2009-06-16 20:24:42 +02:00
|
|
|
end,
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload2, PayloadSize,
|
|
|
|
StreamStart, IP);
|
2009-09-18 15:33:12 +02:00
|
|
|
{size_limit, Sid} ->
|
2010-06-02 16:01:36 +02:00
|
|
|
case get_session(Sid) of
|
|
|
|
{error, _} ->
|
2009-09-18 15:33:12 +02:00
|
|
|
{404, ?HEADER, ""};
|
2010-06-02 16:01:36 +02:00
|
|
|
{ok, #http_bind{pid = FsmRef}} ->
|
2009-09-18 15:33:12 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, {stop, close}),
|
|
|
|
{200, ?HEADER, "<body type='terminate' "
|
|
|
|
"condition='undefined-condition' "
|
2010-01-27 19:53:27 +01:00
|
|
|
"xmlns='" ++ ?NS_HTTP_BIND_s ++ "'>Request Too Large</body>"}
|
2009-09-18 15:33:12 +02:00
|
|
|
end;
|
2010-05-10 22:45:24 +02:00
|
|
|
Error ->
|
|
|
|
?DEBUG("Received bad request: ~p~nParsing error: ~p", [Data, Error]),
|
2009-06-16 20:24:42 +02:00
|
|
|
{400, ?HEADER, ""}
|
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_session_start(Pid, XmppDomain, Sid, Rid, Attrs,
|
|
|
|
Payload, PayloadSize, IP) ->
|
2009-06-16 20:27:18 +02:00
|
|
|
?DEBUG("got pid: ~p", [Pid]),
|
2010-11-29 20:44:31 +01:00
|
|
|
Wait = case string:to_integer(exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"wait">>, "")) of
|
2009-06-16 20:27:18 +02:00
|
|
|
{error, _} ->
|
|
|
|
?MAX_WAIT;
|
|
|
|
{CWait, _} ->
|
2009-06-16 20:27:35 +02:00
|
|
|
if
|
2009-06-16 20:27:18 +02:00
|
|
|
(CWait > ?MAX_WAIT) ->
|
|
|
|
?MAX_WAIT;
|
|
|
|
true ->
|
|
|
|
CWait
|
|
|
|
end
|
|
|
|
end,
|
2010-11-29 20:44:31 +01:00
|
|
|
Hold = case string:to_integer(exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"hold">>, "")) of
|
2009-06-16 20:27:18 +02:00
|
|
|
{error, _} ->
|
|
|
|
(?MAX_REQUESTS - 1);
|
|
|
|
{CHold, _} ->
|
2009-06-16 20:27:35 +02:00
|
|
|
if
|
2009-06-16 20:27:18 +02:00
|
|
|
(CHold > (?MAX_REQUESTS - 1)) ->
|
|
|
|
(?MAX_REQUESTS - 1);
|
|
|
|
true ->
|
|
|
|
CHold
|
|
|
|
end
|
|
|
|
end,
|
2010-11-29 20:44:31 +01:00
|
|
|
Pdelay = case string:to_integer(exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"process-delay">>, "")) of
|
2010-06-07 13:44:55 +02:00
|
|
|
{error, _} ->
|
|
|
|
?PROCESS_DELAY_DEFAULT;
|
|
|
|
{CPdelay, _} when
|
|
|
|
(?PROCESS_DELAY_MIN =< CPdelay) and
|
|
|
|
(CPdelay =< ?PROCESS_DELAY_MAX) ->
|
|
|
|
CPdelay;
|
|
|
|
{CPdelay, _} ->
|
2010-11-09 14:11:38 +01:00
|
|
|
lists:max([lists:min([CPdelay, ?PROCESS_DELAY_MAX]), ?PROCESS_DELAY_MIN])
|
2010-06-07 13:44:55 +02:00
|
|
|
end,
|
2009-06-16 20:27:35 +02:00
|
|
|
Version =
|
2009-06-16 20:27:18 +02:00
|
|
|
case catch list_to_float(
|
2010-11-29 20:44:31 +01:00
|
|
|
exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"ver">>, "")) of
|
2009-06-16 20:27:18 +02:00
|
|
|
{'EXIT', _} -> 0.0;
|
|
|
|
V -> V
|
|
|
|
end,
|
2010-11-29 20:44:31 +01:00
|
|
|
XmppVersion = exmpp_xml:get_attribute_from_list_as_list(Attrs, ?NS_BOSH, <<"version">>, ""),
|
2009-06-16 20:27:45 +02:00
|
|
|
?DEBUG("Create session: ~p", [Sid]),
|
2010-06-02 16:01:36 +02:00
|
|
|
mnesia:async_dirty(
|
2009-06-16 20:27:18 +02:00
|
|
|
fun() ->
|
|
|
|
mnesia:write(
|
|
|
|
#http_bind{id = Sid,
|
|
|
|
pid = Pid,
|
2009-06-16 20:27:35 +02:00
|
|
|
to = {XmppDomain,
|
2009-06-16 20:27:18 +02:00
|
|
|
XmppVersion},
|
|
|
|
hold = Hold,
|
|
|
|
wait = Wait,
|
2010-06-07 13:44:55 +02:00
|
|
|
process_delay = Pdelay,
|
2009-06-16 20:27:18 +02:00
|
|
|
version = Version
|
|
|
|
})
|
|
|
|
end),
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, true, IP).
|
2009-06-16 20:27:18 +02:00
|
|
|
|
2009-06-16 20:23:40 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2009-06-16 20:27:35 +02:00
|
|
|
%% {stop, StopReason}
|
2009-06-16 20:23:40 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-06-16 20:26:40 +02:00
|
|
|
init([Sid, Key, IP]) ->
|
|
|
|
?DEBUG("started: ~p", [{Sid, Key, IP}]),
|
2009-06-16 20:25:42 +02:00
|
|
|
|
|
|
|
%% Read c2s options from the first ejabberd_c2s configuration in
|
|
|
|
%% the config file listen section
|
|
|
|
%% TODO: We should have different access and shaper values for
|
|
|
|
%% each connector. The default behaviour should be however to use
|
|
|
|
%% the default c2s restrictions if not defined for the current
|
|
|
|
%% connector.
|
2009-08-31 20:40:25 +02:00
|
|
|
Opts1 = ejabberd_c2s_config:get_c2s_limits(),
|
|
|
|
Opts = [{xml_socket, true} | Opts1],
|
2009-06-16 20:25:42 +02:00
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
Shaper = none,
|
|
|
|
ShaperState = shaper:new(Shaper),
|
2009-06-16 20:26:40 +02:00
|
|
|
Socket = {http_bind, self(), IP},
|
|
|
|
ejabberd_socket:start(ejabberd_c2s, ?MODULE, Socket, Opts),
|
2009-06-16 20:23:40 +02:00
|
|
|
Timer = erlang:start_timer(?MAX_INACTIVITY, self(), []),
|
2009-06-16 20:23:53 +02:00
|
|
|
{ok, loop, #state{id = Sid,
|
2009-06-16 20:23:40 +02:00
|
|
|
key = Key,
|
2009-06-16 20:26:40 +02:00
|
|
|
socket = Socket,
|
2009-08-31 20:40:25 +02:00
|
|
|
shaper_state = ShaperState,
|
2009-06-16 20:27:01 +02:00
|
|
|
max_inactivity = ?MAX_INACTIVITY,
|
2009-08-31 20:40:25 +02:00
|
|
|
max_pause = ?MAX_PAUSE,
|
2009-06-16 20:23:40 +02:00
|
|
|
timer = Timer}}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2009-06-16 20:27:35 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2009-06-16 20:23:40 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_event({become_controller, C2SPid}, StateName, StateData) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
erlang:monitor(process, C2SPid),
|
2009-06-16 20:23:45 +02:00
|
|
|
case StateData#state.input of
|
2009-08-31 20:40:25 +02:00
|
|
|
cancel ->
|
2009-06-16 20:23:45 +02:00
|
|
|
{next_state, StateName, StateData#state{
|
2009-08-31 20:40:25 +02:00
|
|
|
waiting_input = C2SPid}};
|
2009-06-16 20:23:45 +02:00
|
|
|
Input ->
|
2009-08-31 20:40:25 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Event) ->
|
|
|
|
C2SPid ! Event
|
|
|
|
end, queue:to_list(Input)),
|
2009-06-16 20:23:45 +02:00
|
|
|
{next_state, StateName, StateData#state{
|
2009-08-31 20:40:25 +02:00
|
|
|
input = queue:new(),
|
|
|
|
waiting_input = C2SPid}}
|
2009-06-16 20:23:45 +02:00
|
|
|
end;
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_event({change_shaper, Shaper}, StateName, StateData) ->
|
|
|
|
NewShaperState = shaper:new(Shaper),
|
|
|
|
{next_state, StateName, StateData#state{shaper_state = NewShaperState}};
|
2009-06-16 20:23:40 +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 20:27:35 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2009-06-16 20:23:40 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_sync_event({send_xml, Packet}, _From, StateName,
|
|
|
|
#state{http_receiver = undefined} = StateData) ->
|
|
|
|
Output = [Packet | StateData#state.output],
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData#state{output = Output}};
|
|
|
|
handle_sync_event({send_xml, Packet}, _From, StateName, StateData) ->
|
|
|
|
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,
|
|
|
|
ReqList = [#hbr{rid = Rid,
|
|
|
|
key = StateData#state.key,
|
|
|
|
out = Output
|
|
|
|
} |
|
|
|
|
[El || El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid /= Rid ]
|
|
|
|
],
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName,
|
|
|
|
StateData#state{output = [],
|
|
|
|
http_receiver = undefined,
|
|
|
|
req_list = ReqList,
|
|
|
|
wait_timer = undefined,
|
|
|
|
timer = Timer}};
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:27:45 +02:00
|
|
|
handle_sync_event({stop,close}, _From, _StateName, StateData) ->
|
2009-06-16 20:23:40 +02:00
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
2009-06-16 20:27:45 +02:00
|
|
|
handle_sync_event({stop,stream_closed}, _From, _StateName, StateData) ->
|
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
2010-06-02 16:01:36 +02:00
|
|
|
handle_sync_event(deactivate_socket, _From, StateName, StateData) ->
|
|
|
|
%% Input = case StateData#state.input of
|
|
|
|
%% cancel ->
|
|
|
|
%% queue:new();
|
|
|
|
%% Q ->
|
|
|
|
%% Q
|
|
|
|
%% end,
|
|
|
|
{reply, ok, StateName, StateData#state{waiting_input = false}};
|
2009-06-16 20:27:45 +02:00
|
|
|
handle_sync_event({stop,Reason}, _From, _StateName, StateData) ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Closing bind session ~p - Reason: ~p", [StateData#state.id, Reason]),
|
2009-06-16 20:27:45 +02:00
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, StateData};
|
|
|
|
|
2009-06-16 20:26:44 +02:00
|
|
|
%% HTTP PUT: Receive packets from the client
|
2009-08-31 20:40:25 +02: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
|
2009-06-16 20:26:44 +02:00
|
|
|
end,
|
2009-08-31 20:40:25 +02:00
|
|
|
Reply = {wait, Pause},
|
|
|
|
?DEBUG("Shaper timer for RID ~p: ~p", [Rid, Reply]),
|
|
|
|
{reply, Reply, StateName, StateData};
|
2009-06-16 20:27:35 +02:00
|
|
|
|
2009-09-18 15:33:12 +02:00
|
|
|
handle_sync_event(#http_put{payload_size = PayloadSize} = Request,
|
2009-08-31 20:40:25 +02:00
|
|
|
_From, StateName, StateData) ->
|
|
|
|
?DEBUG("New request: ~p",[Request]),
|
|
|
|
%% Updating trafic shaper
|
|
|
|
{NewShaperState, NewShaperTimer} =
|
|
|
|
update_shaper(StateData#state.shaper_state, PayloadSize),
|
|
|
|
|
|
|
|
handle_http_put_event(Request, StateName,
|
|
|
|
StateData#state{shaper_state = NewShaperState,
|
|
|
|
shaper_timer = NewShaperTimer});
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:26:44 +02:00
|
|
|
%% HTTP GET: send packets to the client
|
2009-06-16 20:26:02 +02:00
|
|
|
handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
|
2009-06-16 20:25:22 +02:00
|
|
|
%% setup timer
|
2009-08-31 20:40:25 +02:00
|
|
|
send_receiver_reply(StateData#state.http_receiver, {ok, empty}),
|
2009-06-16 20:27:35 +02:00
|
|
|
cancel_timer(StateData#state.wait_timer),
|
2009-08-31 20:40:25 +02:00
|
|
|
TNow = tnow(),
|
2009-06-16 20:27:35 +02:00
|
|
|
if
|
|
|
|
(Hold > 0) and
|
2009-08-31 20:40:25 +02:00
|
|
|
(StateData#state.output == []) and
|
2009-06-16 20:27:35 +02:00
|
|
|
((TNow - StateData#state.ctime) < (Wait*1000*1000)) and
|
|
|
|
(StateData#state.rid == Rid) and
|
2009-08-31 20:40:25 +02:00
|
|
|
(StateData#state.input /= cancel) and
|
2009-06-16 20:25:22 +02:00
|
|
|
(StateData#state.pause == 0) ->
|
2009-06-16 20:26:02 +02:00
|
|
|
WaitTimer = erlang:start_timer(Wait * 1000, self(), []),
|
2009-08-31 20:40:25 +02:00
|
|
|
%% MR: Not sure we should cancel the state timer here.
|
2009-06-16 20:27:45 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-06-16 20:26:02 +02:00
|
|
|
{next_state, StateName, StateData#state{
|
|
|
|
http_receiver = From,
|
|
|
|
wait_timer = WaitTimer,
|
2009-06-16 20:27:24 +02:00
|
|
|
timer = undefined}};
|
2009-08-31 20:40:25 +02:00
|
|
|
(StateData#state.input == cancel) ->
|
2009-06-16 20:27:35 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-08-31 20:40:25 +02:00
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
2009-06-16 20:26:02 +02:00
|
|
|
Reply = {ok, cancel},
|
|
|
|
{reply, Reply, StateName, StateData#state{
|
2009-08-31 20:40:25 +02:00
|
|
|
input = queue:new(),
|
2009-06-16 20:26:02 +02:00
|
|
|
http_receiver = undefined,
|
|
|
|
wait_timer = undefined,
|
2009-06-16 20:27:24 +02:00
|
|
|
timer = Timer}};
|
2009-06-16 20:23:40 +02:00
|
|
|
true ->
|
2009-06-16 20:27:35 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-08-31 20:40:25 +02:00
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
|
|
|
Reply = {ok, StateData#state.output},
|
2009-06-16 20:23:40 +02:00
|
|
|
%% save request
|
2009-08-31 20:40:25 +02:00
|
|
|
ReqList = [#hbr{rid = Rid,
|
|
|
|
key = StateData#state.key,
|
|
|
|
out = StateData#state.output
|
2009-06-16 20:27:35 +02:00
|
|
|
} |
|
|
|
|
[El || El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid /= Rid ]
|
2009-06-16 20:23:40 +02:00
|
|
|
],
|
2009-06-16 20:26:02 +02:00
|
|
|
{reply, Reply, StateName, StateData#state{
|
2009-08-31 20:40:25 +02:00
|
|
|
output = [],
|
2009-06-16 20:26:02 +02:00
|
|
|
http_receiver = undefined,
|
|
|
|
wait_timer = undefined,
|
|
|
|
timer = Timer,
|
|
|
|
req_list = ReqList}}
|
|
|
|
end;
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-06-16 20:26:36 +02:00
|
|
|
handle_sync_event(peername, _From, StateName, StateData) ->
|
|
|
|
Reply = {ok, StateData#state.ip},
|
|
|
|
{reply, Reply, StateName, StateData};
|
|
|
|
|
2009-06-16 20:23:40 +02:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
|
|
|
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 20:27:35 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2009-06-16 20:23:40 +02:00
|
|
|
%%----------------------------------------------------------------------
|
2009-08-31 20:40:25 +02:00
|
|
|
%% We reached the max_inactivity timeout:
|
2009-06-16 20:23:40 +02:00
|
|
|
handle_info({timeout, Timer, _}, _StateName,
|
2009-06-16 20:27:45 +02:00
|
|
|
#state{id=SID, timer = Timer} = StateData) ->
|
2010-03-14 10:30:01 +01:00
|
|
|
?INFO_MSG("Session timeout. Closing the HTTP bind session: ~p", [SID]),
|
2009-06-16 20:23:40 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2009-06-16 20:26:07 +02:00
|
|
|
handle_info({timeout, WaitTimer, _}, StateName,
|
|
|
|
#state{wait_timer = WaitTimer} = StateData) ->
|
2009-06-16 20:26:02 +02:00
|
|
|
if
|
|
|
|
StateData#state.http_receiver /= undefined ->
|
2009-06-16 20:27:35 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-08-31 20:40:25 +02:00
|
|
|
Timer = set_inactivity_timer(StateData#state.pause,
|
|
|
|
StateData#state.max_inactivity),
|
2009-06-16 20:26:02 +02:00
|
|
|
gen_fsm:reply(StateData#state.http_receiver, {ok, empty}),
|
2009-08-31 20:40:25 +02:00
|
|
|
Rid = StateData#state.rid,
|
|
|
|
ReqList = [#hbr{rid = Rid,
|
|
|
|
key = StateData#state.key,
|
|
|
|
out = []
|
|
|
|
} |
|
|
|
|
[El || El <- StateData#state.req_list,
|
|
|
|
El#hbr.rid /= Rid ]
|
|
|
|
],
|
2009-06-16 20:26:02 +02:00
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{http_receiver = undefined,
|
2009-08-31 20:40:25 +02:00
|
|
|
req_list = ReqList,
|
2009-06-16 20:26:07 +02:00
|
|
|
wait_timer = undefined,
|
|
|
|
timer = Timer}};
|
2009-06-16 20:26:02 +02:00
|
|
|
true ->
|
|
|
|
{next_state, StateName, StateData}
|
|
|
|
end;
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_info({timeout, ShaperTimer, _}, StateName,
|
|
|
|
#state{shaper_timer = ShaperTimer} = StateData) ->
|
|
|
|
{next_state, StateName, StateData#state{shaper_timer = undefined}};
|
|
|
|
|
2010-06-02 16:01:36 +02:00
|
|
|
handle_info({'DOWN', _MRef, process, C2SPid, _}, _StateName,
|
|
|
|
#state{waiting_input = C2SPid} = StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2009-06-16 20:23:40 +02:00
|
|
|
handle_info(_, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _StateName, StateData) ->
|
2009-06-16 20:27:45 +02: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}),
|
2009-08-31 20:40:25 +02:00
|
|
|
send_receiver_reply(StateData#state.http_receiver, {ok, terminate}),
|
2009-06-16 20:23:40 +02:00
|
|
|
case StateData#state.waiting_input of
|
|
|
|
false ->
|
2009-08-31 20:40:25 +02:00
|
|
|
ok;
|
|
|
|
C2SPid ->
|
|
|
|
gen_fsm:send_event(C2SPid, closed)
|
2009-06-16 20:23:40 +02:00
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-06-16 20:26:44 +02:00
|
|
|
%% PUT / Get processing:
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_http_put_event(#http_put{rid = Rid, attrs = Attrs,
|
|
|
|
hold = Hold} = Request,
|
|
|
|
StateName, StateData) ->
|
|
|
|
?DEBUG("New request: ~p",[Request]),
|
|
|
|
%% Check if Rid valid
|
|
|
|
RidAllow = rid_allow(StateData#state.rid, Rid, Attrs, Hold,
|
|
|
|
StateData#state.max_pause),
|
|
|
|
|
|
|
|
%% Check if Rid is in sequence or out of sequence:
|
|
|
|
case RidAllow of
|
|
|
|
buffer ->
|
|
|
|
?DEBUG("Buffered request: ~p", [Request]),
|
|
|
|
%% Request is out of sequence:
|
|
|
|
PendingRequests = StateData#state.unprocessed_req_list,
|
|
|
|
%% In case an existing RID was already buffered:
|
|
|
|
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, buffered, StateName,
|
|
|
|
StateData#state{unprocessed_req_list = UnprocessedReqList,
|
|
|
|
req_list = ReqList,
|
|
|
|
timer = Timer}};
|
|
|
|
_ ->
|
|
|
|
%% Request is in sequence:
|
|
|
|
process_http_put(Request, StateName, StateData, RidAllow)
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_http_put(#http_put{rid = Rid, attrs = Attrs, payload = Payload,
|
|
|
|
hold = Hold, stream = StreamTo,
|
|
|
|
ip = IP} = Request,
|
2009-06-16 20:26:44 +02:00
|
|
|
StateName, StateData, RidAllow) ->
|
2009-08-31 20:40:25 +02:00
|
|
|
?DEBUG("Actually processing request: ~p", [Request]),
|
2009-06-16 20:26:44 +02:00
|
|
|
%% Check if key valid
|
2010-11-29 20:44:31 +01:00
|
|
|
Key = exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"key">>, ""),
|
|
|
|
NewKey = exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"newkey">>, ""),
|
2009-06-16 20:26:44 +02:00
|
|
|
KeyAllow =
|
|
|
|
case RidAllow of
|
2009-06-16 20:27:35 +02:00
|
|
|
repeat ->
|
2009-06-16 20:26:44 +02:00
|
|
|
true;
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
{true, _} ->
|
|
|
|
case StateData#state.key of
|
|
|
|
"" ->
|
|
|
|
true;
|
|
|
|
OldKey ->
|
2009-06-16 20:27:31 +02:00
|
|
|
NextKey = sha:sha(Key),
|
2009-06-16 20:26:44 +02: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:40:25 +02:00
|
|
|
TNow = tnow(),
|
2009-06-16 20:27:35 +02:00
|
|
|
LastPoll = if
|
2009-08-31 20:40:25 +02:00
|
|
|
Payload == [] ->
|
2009-06-16 20:26:44 +02:00
|
|
|
TNow;
|
|
|
|
true ->
|
|
|
|
0
|
|
|
|
end,
|
|
|
|
if
|
2009-08-31 20:40:25 +02:00
|
|
|
(Payload == []) and
|
2009-06-16 20:26:44 +02:00
|
|
|
(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]),
|
2009-06-16 20:26:48 +02:00
|
|
|
Reply = case [El#hbr.out ||
|
2009-08-31 20:40:25 +02:00
|
|
|
El <- StateData#state.req_list,
|
2009-06-16 20:26:48 +02:00
|
|
|
El#hbr.rid == Rid] of
|
|
|
|
[] ->
|
|
|
|
{error, not_exists};
|
|
|
|
[Out | _XS] ->
|
2009-08-31 20:40:25 +02:00
|
|
|
{repeat, lists:reverse(Out)}
|
|
|
|
end,
|
|
|
|
{reply, Reply, StateName, StateData#state{input = cancel,
|
|
|
|
last_poll = LastPoll}};
|
2009-06-16 20:26:44 +02:00
|
|
|
{true, Pause} ->
|
2009-06-16 20:27:35 +02:00
|
|
|
SaveKey = if
|
2009-06-16 20:26:44 +02:00
|
|
|
NewKey == "" ->
|
|
|
|
Key;
|
|
|
|
true ->
|
|
|
|
NewKey
|
|
|
|
end,
|
|
|
|
?DEBUG(" -- SaveKey: ~s~n", [SaveKey]),
|
|
|
|
|
|
|
|
%% save request
|
2009-08-31 20:40:25 +02:00
|
|
|
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,
|
2009-06-16 20:26:48 +02:00
|
|
|
?DEBUG("reqlist: ~p", [ReqList]),
|
2009-06-16 20:27:35 +02:00
|
|
|
|
2009-06-16 20:26:44 +02:00
|
|
|
%% setup next timer
|
2009-06-16 20:27:35 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2009-08-31 20:40:25 +02:00
|
|
|
Timer = set_inactivity_timer(Pause,
|
|
|
|
StateData#state.max_inactivity),
|
2009-06-16 20:26:44 +02:00
|
|
|
case StateData#state.waiting_input of
|
|
|
|
false ->
|
2009-08-31 20:40:25 +02:00
|
|
|
Input =
|
|
|
|
lists:foldl(
|
|
|
|
fun queue:in/2,
|
|
|
|
StateData#state.input, Payload),
|
2009-06-16 20:26:44 +02:00
|
|
|
Reply = ok,
|
2009-06-16 20:27:35 +02:00
|
|
|
process_buffered_request(Reply, StateName,
|
2009-06-16 20:26:44 +02:00
|
|
|
StateData#state{input = Input,
|
|
|
|
rid = Rid,
|
|
|
|
key = SaveKey,
|
|
|
|
ctime = TNow,
|
|
|
|
timer = Timer,
|
|
|
|
pause = Pause,
|
|
|
|
last_poll = LastPoll,
|
|
|
|
req_list = ReqList,
|
|
|
|
ip = IP
|
|
|
|
});
|
2009-08-31 20:40:25 +02:00
|
|
|
C2SPid ->
|
|
|
|
case StreamTo of
|
|
|
|
{To, ""} ->
|
2010-05-10 13:56:58 +02:00
|
|
|
DecNs = [{?NS_XMPP, "stream"},
|
|
|
|
{?NS_JABBER_CLIENT, none}],
|
2010-11-29 20:44:31 +01:00
|
|
|
StreamAttrs = [#xmlattr{name = <<"to">>, value = list_to_binary(To)}],
|
2010-05-10 13:56:58 +02:00
|
|
|
StreamEl = #xmlel{name = stream, ns = ?NS_XMPP, declared_ns = DecNs,
|
|
|
|
attrs = StreamAttrs},
|
2009-08-31 20:40:25 +02:00
|
|
|
gen_fsm:send_event(
|
|
|
|
C2SPid,
|
2010-04-14 16:50:32 +02:00
|
|
|
{xmlstreamstart, StreamEl});
|
2009-08-31 20:40:25 +02:00
|
|
|
{To, Version} ->
|
2010-05-10 13:56:58 +02:00
|
|
|
DecNs = [{?NS_XMPP, "stream"},
|
|
|
|
{?NS_JABBER_CLIENT, none}],
|
2010-11-29 20:44:31 +01:00
|
|
|
StreamAttrs = [?XMLATTR(<<"to">>, list_to_binary(To)),
|
|
|
|
?XMLATTR(<<"version">>, list_to_binary(Version))],
|
2010-05-10 13:56:58 +02:00
|
|
|
StreamEl = #xmlel{name = stream, ns = ?NS_XMPP, declared_ns = DecNs,
|
|
|
|
attrs = StreamAttrs},
|
2009-08-31 20:40:25 +02:00
|
|
|
gen_fsm:send_event(
|
|
|
|
C2SPid,
|
2010-04-14 16:50:32 +02:00
|
|
|
{xmlstreamstart, StreamEl});
|
2009-08-31 20:40:25 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
|
2009-06-16 20:27:01 +02:00
|
|
|
MaxInactivity = get_max_inactivity(StreamTo, StateData#state.max_inactivity),
|
2009-08-31 20:40:25 +02:00
|
|
|
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),
|
2009-06-16 20:26:44 +02:00
|
|
|
Reply = ok,
|
2009-06-16 20:27:35 +02:00
|
|
|
process_buffered_request(Reply, StateName,
|
2009-08-31 20:40:25 +02:00
|
|
|
StateData#state{input = queue:new(),
|
2009-06-16 20:26:44 +02:00
|
|
|
rid = Rid,
|
|
|
|
key = SaveKey,
|
|
|
|
ctime = TNow,
|
|
|
|
timer = Timer,
|
|
|
|
pause = Pause,
|
|
|
|
last_poll = LastPoll,
|
|
|
|
req_list = ReqList,
|
2009-06-16 20:27:01 +02:00
|
|
|
max_inactivity = MaxInactivity,
|
2009-08-31 20:40:25 +02:00
|
|
|
max_pause = MaxPause,
|
2009-06-16 20:26:44 +02:00
|
|
|
ip = IP
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
Reply = {error, bad_key},
|
|
|
|
{reply, Reply, StateName, StateData}
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_buffered_request(Reply, StateName, StateData) ->
|
|
|
|
Rid = StateData#state.rid,
|
|
|
|
Requests = StateData#state.unprocessed_req_list,
|
|
|
|
case lists:keysearch(Rid+1, 2, Requests) of
|
|
|
|
{value, Request} ->
|
|
|
|
?DEBUG("Processing buffered request: ~p", [Request]),
|
2009-08-31 20:40:25 +02:00
|
|
|
NewRequests = lists:keydelete(Rid+1, 2, Requests),
|
|
|
|
handle_http_put_event(
|
|
|
|
Request, StateName,
|
|
|
|
StateData#state{unprocessed_req_list = NewRequests});
|
2009-06-16 20:26:44 +02:00
|
|
|
_ ->
|
2010-02-04 19:55:43 +01:00
|
|
|
{reply, Reply, StateName, StateData, hibernate}
|
2009-06-16 20:26:44 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
|
|
|
|
case http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) of
|
2009-06-16 20:25:10 +02:00
|
|
|
{error, not_exists} ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("no session associated with sid: ~p", [Sid]),
|
2009-06-16 20:25:10 +02:00
|
|
|
{404, ?HEADER, ""};
|
|
|
|
{{error, Reason}, Sess} ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Error on HTTP put. Reason: ~p", [Reason]),
|
2009-06-16 20:25:10 +02:00
|
|
|
handle_http_put_error(Reason, Sess);
|
|
|
|
{{repeat, OutPacket}, Sess} ->
|
2009-06-16 20:27:35 +02:00
|
|
|
?DEBUG("http_put said 'repeat!' ...~nOutPacket: ~p", [OutPacket]),
|
2009-06-16 20:25:10 +02:00
|
|
|
send_outpacket(Sess, OutPacket);
|
2009-08-31 20:40:25 +02:00
|
|
|
{{wait, Pause}, _Sess} ->
|
|
|
|
?DEBUG("Trafic Shaper: Delaying request ~p", [Rid]),
|
|
|
|
timer:sleep(Pause),
|
|
|
|
%{200, ?HEADER,
|
2010-04-14 16:50:32 +02:00
|
|
|
% exmpp_xml:document_to_list(
|
2009-08-31 20:40:25 +02:00
|
|
|
% {xmlelement, "body",
|
2010-01-27 19:53:27 +01:00
|
|
|
% [{"xmlns", ?NS_HTTP_BIND_s},
|
2009-08-31 20:40:25 +02:00
|
|
|
% {"type", "error"}], []})};
|
|
|
|
handle_http_put(Sid, Rid, Attrs, Payload, PayloadSize,
|
|
|
|
StreamStart, IP);
|
|
|
|
{buffered, _Sess} ->
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-06-16 20:25:10 +02:00
|
|
|
{ok, Sess} ->
|
2010-05-28 00:40:07 +02:00
|
|
|
prepare_response(Sess, Rid, [], StreamStart)
|
2009-06-16 20:23:40 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
http_put(Sid, Rid, Attrs, Payload, PayloadSize, StreamStart, IP) ->
|
2009-06-16 20:27:45 +02:00
|
|
|
?DEBUG("Looking for session: ~p", [Sid]),
|
2010-06-02 16:01:36 +02:00
|
|
|
case get_session(Sid) of
|
|
|
|
{error, _} ->
|
2009-06-16 20:25:10 +02:00
|
|
|
{error, not_exists};
|
2010-06-02 16:01:36 +02:00
|
|
|
{ok, #http_bind{pid = FsmRef, hold=Hold, to={To, StreamVersion}}=Sess}->
|
2009-06-16 20:27:35 +02:00
|
|
|
NewStream =
|
2009-06-16 20:25:10 +02:00
|
|
|
case StreamStart of
|
|
|
|
true ->
|
|
|
|
{To, StreamVersion};
|
|
|
|
_ ->
|
|
|
|
""
|
|
|
|
end,
|
|
|
|
{gen_fsm:sync_send_all_state_event(
|
2009-08-31 20:40:25 +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 20:23:40 +02:00
|
|
|
end.
|
|
|
|
|
2009-06-16 20:27:35 +02:00
|
|
|
handle_http_put_error(Reason, #http_bind{pid=FsmRef, version=Version})
|
2009-06-16 20:25:10 +02:00
|
|
|
when Version >= 0 ->
|
2009-06-16 20:27:45 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef, {stop, {put_error,Reason}}),
|
2009-06-16 20:25:10 +02:00
|
|
|
case Reason of
|
|
|
|
not_exists ->
|
2009-06-16 20:27:35 +02:00
|
|
|
{200, ?HEADER,
|
2010-04-14 16:50:32 +02:00
|
|
|
exmpp_xml:document_to_list(
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'body',
|
|
|
|
ns = ?NS_HTTP_BIND_s,
|
|
|
|
attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"terminate">>
|
2010-01-27 19:53:27 +01:00
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"item-not-found">>
|
2010-01-27 19:53:27 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})};
|
2009-06-16 20:25:10 +02:00
|
|
|
bad_key ->
|
2009-06-16 20:27:35 +02:00
|
|
|
{200, ?HEADER,
|
2010-04-14 16:50:32 +02:00
|
|
|
exmpp_xml:document_to_list(
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'body',
|
|
|
|
ns = ?NS_HTTP_BIND_s,
|
|
|
|
attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"terminate">>
|
2010-01-27 19:53:27 +01:00
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"item-not-found">>
|
2010-01-27 19:53:27 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})};
|
2009-06-16 20:25:10 +02:00
|
|
|
polling_too_frequently ->
|
2009-06-16 20:27:35 +02:00
|
|
|
{200, ?HEADER,
|
2010-04-14 16:50:32 +02:00
|
|
|
exmpp_xml:document_to_list(
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'body',
|
|
|
|
ns = ?NS_HTTP_BIND_s,
|
|
|
|
attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"terminate">>
|
2010-01-27 19:53:27 +01:00
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"type">>,
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = ?NS_HTTP_BIND_s,
|
2010-05-10 22:44:55 +02:00
|
|
|
value = <<"policy-violation">>
|
2010-01-27 19:53:27 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})}
|
2009-06-16 20:25:10 +02:00
|
|
|
end;
|
|
|
|
handle_http_put_error(Reason, #http_bind{pid=FsmRef}) ->
|
2009-06-16 20:27:45 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,{stop, {put_error_no_version, Reason}}),
|
2009-06-16 20:27:35 +02:00
|
|
|
case Reason of
|
2009-06-16 20:25:10 +02:00
|
|
|
not_exists -> %% bad rid
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Closing HTTP bind session (Bad rid).", []),
|
2009-06-16 20:24:58 +02:00
|
|
|
{404, ?HEADER, ""};
|
2009-06-16 20:25:10 +02:00
|
|
|
bad_key ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Closing HTTP bind session (Bad key).", []),
|
2009-06-16 20:25:10 +02:00
|
|
|
{404, ?HEADER, ""};
|
|
|
|
polling_too_frequently ->
|
2010-02-19 00:08:04 +01:00
|
|
|
?DEBUG("Closing HTTP bind session (User polling too frequently).", []),
|
2009-06-16 20:27:35 +02:00
|
|
|
{403, ?HEADER, ""}
|
2009-06-16 20:24:58 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
%% Control RID ordering
|
|
|
|
rid_allow(none, _NewRid, _Attrs, _Hold, _MaxPause) ->
|
|
|
|
%% First request - nothing saved so far
|
|
|
|
{true, 0};
|
|
|
|
rid_allow(OldRid, NewRid, Attrs, Hold, MaxPause) ->
|
|
|
|
?DEBUG("Previous rid / New rid: ~p/~p", [OldRid, NewRid]),
|
|
|
|
if
|
|
|
|
%% We did not miss any packet, we can process it immediately:
|
|
|
|
NewRid == OldRid + 1 ->
|
|
|
|
case catch list_to_integer(
|
2010-11-29 20:44:31 +01:00
|
|
|
exmpp_xml:get_attribute_from_list_as_list(Attrs, <<"pause">>, "")) of
|
2009-08-31 20:40:25 +02:00
|
|
|
{'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
|
|
|
|
end.
|
|
|
|
|
|
|
|
update_shaper(ShaperState, PayloadSize) ->
|
|
|
|
{NewShaperState, Pause} = shaper:update(ShaperState, PayloadSize),
|
|
|
|
if
|
|
|
|
Pause > 0 ->
|
|
|
|
ShaperTimer = erlang:start_timer(Pause, self(), activate), %% MR: Seems timer is not needed. Activate is not handled
|
|
|
|
{NewShaperState, ShaperTimer};
|
|
|
|
true ->
|
|
|
|
{NewShaperState, undefined}
|
|
|
|
end.
|
|
|
|
|
2010-05-28 00:40:07 +02:00
|
|
|
prepare_response(Sess, Rid, OutputEls, StreamStart) ->
|
2010-06-07 13:44:55 +02:00
|
|
|
receive after Sess#http_bind.process_delay -> ok end,
|
2009-06-16 20:26:02 +02:00
|
|
|
case catch http_get(Sess, Rid) of
|
2009-06-16 20:24:58 +02:00
|
|
|
{ok, cancel} ->
|
|
|
|
%% actually it would be better if we could completely
|
|
|
|
%% cancel this request, but then we would have to hack
|
|
|
|
%% ejabberd_http and I'm too lazy now
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body type='error' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-06-16 20:26:02 +02:00
|
|
|
{ok, empty} ->
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-06-16 20:26:07 +02:00
|
|
|
{ok, terminate} ->
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body type='terminate' xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-08-31 20:40:25 +02:00
|
|
|
{ok, ROutPacket} ->
|
|
|
|
OutPacket = lists:reverse(ROutPacket),
|
2010-05-28 00:40:07 +02:00
|
|
|
?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} ->
|
2010-06-01 20:52:15 +02:00
|
|
|
?DEBUG("Error in sending packet ~p ", [_Reason]),
|
2010-05-28 00:40:07 +02:00
|
|
|
{200, ?HEADER,
|
|
|
|
"<body type='terminate' xmlns='"++
|
|
|
|
?NS_HTTP_BIND_s++"'/>"};
|
|
|
|
SendRes ->
|
|
|
|
SendRes
|
|
|
|
end;
|
|
|
|
%% 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] ->
|
2010-11-29 20:44:31 +01:00
|
|
|
AuthID = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, <<"id">>, ""),
|
|
|
|
FromB = exmpp_xml:get_attribute_from_list_as_binary(OutAttrs, <<"from">>, <<>>),
|
|
|
|
Version = exmpp_xml:get_attribute_from_list_as_list(OutAttrs, <<"version">>, ""),
|
2010-05-28 00:40:07 +02:00
|
|
|
OutEls =
|
|
|
|
case Els of
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
[{xmlstreamelement,
|
|
|
|
#xmlel{name = 'features',
|
|
|
|
declared_ns = [{undefined, ?NS_XMPP_pfx}],
|
|
|
|
attrs = StreamAttribs,
|
|
|
|
children = StreamEls}}
|
|
|
|
| StreamTail] ->
|
|
|
|
%% {xmlelement, "stream:features",
|
|
|
|
%% StreamAttribs, StreamEls}}
|
|
|
|
%% | StreamTail] ->
|
|
|
|
TypedTail =
|
|
|
|
[check_default_xmlns(OEl) ||
|
|
|
|
{xmlstreamelement, OEl} <-
|
|
|
|
StreamTail],
|
|
|
|
[#xmlel{name = 'features',
|
|
|
|
declared_ns = [{?NS_XMPP_s, ?NS_XMPP_pfx}],
|
|
|
|
attrs = StreamAttribs,
|
|
|
|
children = StreamEls}] ++
|
|
|
|
%% [{xmlelement, "stream:features",
|
|
|
|
%% [{"xmlns:stream",
|
|
|
|
%% ?NS_XMPP_s}] ++
|
|
|
|
%% StreamAttribs, StreamEls}] ++
|
2009-08-31 20:40:25 +02:00
|
|
|
TypedTail;
|
2010-05-28 00:40:07 +02:00
|
|
|
StreamTail ->
|
|
|
|
[check_default_xmlns(OEl) ||
|
|
|
|
{xmlstreamelement, OEl} <-
|
|
|
|
StreamTail]
|
|
|
|
end,
|
|
|
|
case OutEls of
|
|
|
|
[#xmlel{name = 'stream:error'}] ->
|
|
|
|
{200, ?HEADER, "<body type='terminate' "
|
|
|
|
"condition='host-unknown' "
|
|
|
|
"xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
|
|
|
_ ->
|
|
|
|
BOSH_attribs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[#xmlattr{name = <<"authid">>, value = list_to_binary(AuthID)},
|
|
|
|
#xmlattr{name = <<"xmlns:stream">>, value = ?NS_XMPP_b}] ++
|
|
|
|
[#xmlattr{name = <<"version">>, ns = ?NS_BOSH_s, value = list_to_binary(Version)}],
|
2010-05-28 00:40:07 +02:00
|
|
|
MaxInactivity = get_max_inactivity(To, ?MAX_INACTIVITY),
|
|
|
|
MaxPause = get_max_pause(To),
|
|
|
|
{200, ?HEADER,
|
|
|
|
exmpp_xml:document_to_list(
|
|
|
|
#xmlel{name = 'body',
|
|
|
|
ns = ?NS_HTTP_BIND_s,
|
|
|
|
declared_ns = [{?NS_XBOSH_s, ?NS_XBOSH_pfx}],
|
|
|
|
attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"sid">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(Sid)
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"wait">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(integer_to_list(Wait))
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"requests">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(integer_to_list(Hold+1))
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"inactivity">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(integer_to_list(trunc(MaxInactivity/1000)))
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"maxpause">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(integer_to_list(MaxPause))
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"polling">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = list_to_binary(integer_to_list(trunc(?MIN_POLLING/1000000)))
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"ver">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = ?BOSH_VERSION_b
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"from">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = FromB
|
|
|
|
},
|
2010-11-29 20:44:31 +01:00
|
|
|
#xmlattr{name = <<"secure">>,
|
2010-05-28 00:40:07 +02:00
|
|
|
value = <<"true">>
|
|
|
|
}
|
|
|
|
] ++ BOSH_attribs,
|
|
|
|
children = OutEls})}
|
2010-01-27 19:53:27 +01:00
|
|
|
% {xmlelement,"body",
|
|
|
|
% [{"xmlns",
|
|
|
|
% ?NS_HTTP_BIND_s},
|
|
|
|
% {"sid", Sid},
|
|
|
|
% {"wait", integer_to_list(Wait)},
|
|
|
|
% {"requests", integer_to_list(Hold+1)},
|
|
|
|
% {"inactivity",
|
|
|
|
% integer_to_list(
|
|
|
|
% trunc(MaxInactivity/1000))},
|
|
|
|
% {"maxpause",
|
|
|
|
% integer_to_list(MaxPause)},
|
|
|
|
% {"polling",
|
|
|
|
% integer_to_list(
|
|
|
|
% trunc(?MIN_POLLING/1000000))},
|
|
|
|
% {"ver", ?BOSH_VERSION},
|
|
|
|
% {"from", From},
|
|
|
|
% {"secure", "true"} %% we're always being secure
|
|
|
|
% ] ++ BOSH_attribs,OutEls})};
|
2010-05-28 00:40:07 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{200, ?HEADER, "<body type='terminate' "
|
|
|
|
"condition='internal-server-error' "
|
|
|
|
"xmlns='"++?NS_HTTP_BIND_s++"'/>"}
|
2009-06-16 20:24:58 +02:00
|
|
|
end.
|
2009-06-16 20:25:10 +02:00
|
|
|
|
2010-05-28 00:40:07 +02:00
|
|
|
|
2009-06-16 20:25:10 +02:00
|
|
|
http_get(#http_bind{pid = FsmRef, wait = Wait, hold = Hold}, Rid) ->
|
2009-06-16 20:26:02 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(
|
|
|
|
FsmRef, {http_get, Rid, Wait, Hold}, 2 * ?MAX_WAIT * 1000).
|
|
|
|
|
2009-06-16 20:25:10 +02:00
|
|
|
send_outpacket(#http_bind{pid = FsmRef}, OutPacket) ->
|
2009-06-16 20:24:58 +02:00
|
|
|
case OutPacket of
|
2009-08-31 20:40:25 +02:00
|
|
|
[] ->
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-08-31 20:40:25 +02:00
|
|
|
[{xmlstreamend, _}] ->
|
2009-06-16 20:27:45 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,{stop,stream_closed}),
|
2010-01-27 19:53:27 +01:00
|
|
|
{200, ?HEADER, "<body xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-06-16 20:24:58 +02:00
|
|
|
_ ->
|
2009-08-31 20:40:25 +02:00
|
|
|
%% TODO: We parse to add a default namespace to packet,
|
|
|
|
%% The spec says adding the jabber:client namespace if
|
|
|
|
%% mandatory, even if some implementation do not do that
|
|
|
|
%% change on packets.
|
|
|
|
%% I think this should be an option to avoid modifying
|
|
|
|
%% packet in most case.
|
|
|
|
AllElements =
|
|
|
|
lists:all(fun({xmlstreamelement,
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'error',
|
|
|
|
declared_ns = [{undefined, ?NS_XMPP_pfx}]}}) -> false;
|
|
|
|
% {xmlelement, "stream:error", _, _}}) -> false;
|
2009-08-31 20:40:25 +02:00
|
|
|
({xmlstreamelement, _}) -> true;
|
2010-06-01 20:52:15 +02:00
|
|
|
({xmlstreamraw, _}) -> true;
|
2009-08-31 20:40:25 +02:00
|
|
|
(_) -> false
|
|
|
|
end, OutPacket),
|
|
|
|
case AllElements of
|
|
|
|
true ->
|
2010-06-01 20:52:15 +02:00
|
|
|
TypedEls = lists:foldr(fun({xmlstreamelement, El}, Acc) ->
|
|
|
|
Acc ++
|
|
|
|
[exmpp_xml:document_to_list(
|
|
|
|
check_default_xmlns(El)
|
|
|
|
)];
|
|
|
|
({xmlstreamraw, R}, Acc) ->
|
|
|
|
Acc ++ [R]
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
OutPacket),
|
|
|
|
|
|
|
|
Body = "<body xmlns='"++?NS_HTTP_BIND_s++"'>"
|
|
|
|
++ TypedEls ++
|
|
|
|
"</body>",
|
2009-06-16 20:24:58 +02:00
|
|
|
?DEBUG(" --- outgoing data --- ~n~s~n --- END --- ~n",
|
2009-08-31 20:40:25 +02:00
|
|
|
[Body]),
|
|
|
|
{200, ?HEADER, Body};
|
|
|
|
false ->
|
|
|
|
case OutPacket of
|
|
|
|
[{xmlstreamstart, _, _} | SEls] ->
|
|
|
|
OutEls =
|
|
|
|
case SEls of
|
|
|
|
[{xmlstreamelement,
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'features',
|
2010-10-07 00:16:24 +02:00
|
|
|
declared_ns = [{undefined, "stream"}],
|
2010-01-27 19:53:27 +01:00
|
|
|
attrs = StreamAttribs,
|
|
|
|
children = StreamEls}} |
|
|
|
|
% {xmlelement,
|
|
|
|
% "stream:features",
|
|
|
|
% StreamAttribs, StreamEls}} |
|
2009-08-31 20:40:25 +02:00
|
|
|
StreamTail] ->
|
|
|
|
TypedTail =
|
|
|
|
[check_default_xmlns(OEl) ||
|
|
|
|
{xmlstreamelement, OEl} <-
|
|
|
|
StreamTail],
|
2010-01-27 19:53:27 +01:00
|
|
|
[#xmlel{name = 'features',
|
|
|
|
declared_ns = [{?NS_XMPP_s, ?NS_XMPP_pfx}],
|
|
|
|
attrs = StreamAttribs,
|
|
|
|
children = StreamEls}] ++
|
|
|
|
% [{xmlelement,
|
|
|
|
% "stream:features",
|
|
|
|
% [{"xmlns:stream",
|
|
|
|
% ?NS_XMPP_s}] ++
|
|
|
|
% StreamAttribs, StreamEls}] ++
|
2009-08-31 20:40:25 +02:00
|
|
|
TypedTail;
|
|
|
|
StreamTail ->
|
|
|
|
[check_default_xmlns(OEl) ||
|
|
|
|
{xmlstreamelement, OEl} <-
|
|
|
|
StreamTail]
|
|
|
|
end,
|
|
|
|
{200, ?HEADER,
|
2010-04-14 16:50:32 +02:00
|
|
|
exmpp_xml:document_to_list(
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'body',
|
|
|
|
ns = ?NS_HTTP_BIND_s,
|
|
|
|
children = OutEls})};
|
|
|
|
% {xmlelement,"body",
|
|
|
|
% [{"xmlns",
|
|
|
|
% ?NS_HTTP_BIND_s}],
|
|
|
|
% OutEls})};
|
2009-08-31 20:40:25 +02:00
|
|
|
_ ->
|
|
|
|
SErrCond =
|
|
|
|
lists:filter(
|
|
|
|
fun({xmlstreamelement,
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{name = 'error',
|
|
|
|
declared_ns = [{undefined, ?NS_XMPP_pfx}]}}) ->
|
|
|
|
% {xmlelement, "stream:error",
|
|
|
|
% _, _}}) ->
|
2009-08-31 20:40:25 +02:00
|
|
|
true;
|
|
|
|
(_) -> false
|
|
|
|
end, OutPacket),
|
2009-06-16 20:27:35 +02:00
|
|
|
StreamErrCond =
|
2009-08-31 20:40:25 +02:00
|
|
|
case SErrCond of
|
|
|
|
[] ->
|
|
|
|
null;
|
|
|
|
[{xmlstreamelement,
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{children = _Cond} =
|
|
|
|
% {xmlelement, _, _, _Cond} =
|
2009-08-31 20:40:25 +02:00
|
|
|
StreamErrorTag} | _] ->
|
|
|
|
[StreamErrorTag]
|
|
|
|
end,
|
2009-06-16 20:25:10 +02:00
|
|
|
gen_fsm:sync_send_all_state_event(FsmRef,
|
2009-06-16 20:27:45 +02:00
|
|
|
{stop, {stream_error,OutPacket}}),
|
2009-06-16 20:24:58 +02:00
|
|
|
case StreamErrCond of
|
|
|
|
null ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
"<body type='terminate' "
|
|
|
|
"condition='internal-server-error' "
|
2010-01-27 19:53:27 +01:00
|
|
|
"xmlns='"++?NS_HTTP_BIND_s++"'/>"};
|
2009-06-16 20:24:58 +02:00
|
|
|
_ ->
|
|
|
|
{200, ?HEADER,
|
|
|
|
"<body type='terminate' "
|
|
|
|
"condition='remote-stream-error' "
|
2010-01-27 19:53:27 +01:00
|
|
|
"xmlns='"++?NS_HTTP_BIND_s++"' " ++
|
|
|
|
"xmlns:stream='"++?NS_XMPP_s++"'>" ++
|
2009-06-16 20:24:58 +02:00
|
|
|
elements_to_string(StreamErrCond) ++
|
|
|
|
"</body>"}
|
2009-08-31 20:40:25 +02:00
|
|
|
end
|
|
|
|
end
|
2009-06-16 20:24:58 +02:00
|
|
|
end
|
|
|
|
end.
|
2009-06-16 20:23:40 +02:00
|
|
|
|
2009-09-18 15:33:12 +02:00
|
|
|
parse_request(Data, PayloadSize, MaxStanzaSize) ->
|
2009-06-16 20:27:35 +02:00
|
|
|
?DEBUG("--- incoming data --- ~n~s~n --- END --- ", [Data]),
|
2009-08-31 20:40:25 +02:00
|
|
|
%% MR: I do not think it works if put put several elements in the
|
|
|
|
%% same body:
|
2010-04-14 16:50:32 +02:00
|
|
|
case exmpp_xmlstream:parse_element(Data) of
|
|
|
|
[#xmlel{name = 'body',
|
2010-01-27 19:53:27 +01:00
|
|
|
ns = Xmlns,
|
|
|
|
attrs = Attrs,
|
2010-04-14 16:50:32 +02:00
|
|
|
children = Els}] = [Xml] ->
|
2010-01-27 19:53:27 +01:00
|
|
|
% {xmlelement, "body", Attrs, Els} ->
|
|
|
|
% Xmlns = xml:get_attr_s("xmlns",Attrs),
|
2009-06-16 20:27:35 +02:00
|
|
|
if
|
2010-04-14 16:50:32 +02:00
|
|
|
Xmlns /= ?NS_HTTP_BIND ->
|
2009-06-16 20:24:18 +02:00
|
|
|
{error, bad_request};
|
|
|
|
true ->
|
2010-01-27 19:53:27 +01:00
|
|
|
%case catch list_to_integer(xml:get_attr_s("rid", Attrs)) of
|
2010-11-29 20:44:31 +01:00
|
|
|
case catch list_to_integer(exmpp_xml:get_attribute_as_list(Xml, <<"rid">>, "")) of
|
2010-01-27 19:53:27 +01:00
|
|
|
|
2009-06-16 20:25:10 +02:00
|
|
|
{'EXIT', _} ->
|
|
|
|
{error, bad_request};
|
|
|
|
Rid ->
|
2009-08-31 20:40:25 +02:00
|
|
|
%% I guess this is to remove XMLCDATA: Is it really needed ?
|
2009-06-16 20:27:35 +02:00
|
|
|
FixedEls =
|
2009-06-16 20:25:10 +02:00
|
|
|
lists:filter(
|
2009-06-16 20:27:35 +02:00
|
|
|
fun(I) ->
|
|
|
|
case I of
|
2010-01-27 19:53:27 +01:00
|
|
|
#xmlel{} ->
|
|
|
|
%{xmlelement, _, _, _} ->
|
2009-06-16 20:25:10 +02:00
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, Els),
|
2010-11-29 20:44:31 +01:00
|
|
|
Sid = exmpp_xml:get_attribute_as_list(Xml, <<"sid">>, ""),
|
2009-09-18 15:33:12 +02:00
|
|
|
if
|
|
|
|
PayloadSize =< MaxStanzaSize ->
|
|
|
|
{ok, {Sid, Rid, Attrs, FixedEls}};
|
|
|
|
true ->
|
|
|
|
{size_limit, Sid}
|
|
|
|
end
|
2009-06-16 20:25:10 +02:00
|
|
|
end
|
2009-06-16 20:24:18 +02:00
|
|
|
end;
|
2010-04-14 16:50:32 +02:00
|
|
|
[#xmlel{}] ->
|
2010-01-27 19:53:27 +01:00
|
|
|
% {xmlelement, _Name, _Attrs, _Els} ->
|
2009-06-16 20:23:40 +02:00
|
|
|
{error, bad_request}
|
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
send_receiver_reply(undefined, _Reply) ->
|
|
|
|
ok;
|
|
|
|
send_receiver_reply(Receiver, Reply) ->
|
|
|
|
gen_fsm:reply(Receiver, Reply).
|
|
|
|
|
|
|
|
|
2009-06-16 20:27:35 +02:00
|
|
|
%% Cancel timer and empty message queue.
|
|
|
|
cancel_timer(undefined) ->
|
|
|
|
ok;
|
2009-06-16 20:23:40 +02:00
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
|
|
|
receive
|
|
|
|
{timeout, Timer, _} ->
|
|
|
|
ok
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
%% If client asked for a pause (pause > 0), we apply the pause value
|
|
|
|
%% as inactivity timer:
|
|
|
|
set_inactivity_timer(Pause, _MaxInactivity) when Pause > 0 ->
|
|
|
|
erlang:start_timer(Pause*1000, self(), []);
|
|
|
|
%% Otherwise, we apply the max_inactivity value as inactivity timer:
|
|
|
|
set_inactivity_timer(_Pause, MaxInactivity) ->
|
|
|
|
erlang:start_timer(MaxInactivity, self(), []).
|
|
|
|
|
|
|
|
|
2009-06-16 20:27:35 +02:00
|
|
|
%% TODO: Use tail recursion and list reverse ?
|
2009-06-16 20:23:40 +02:00
|
|
|
elements_to_string([]) ->
|
|
|
|
[];
|
|
|
|
elements_to_string([El | Els]) ->
|
2010-04-14 16:50:32 +02:00
|
|
|
exmpp_xml:document_to_list(El) ++ elements_to_string(Els).
|
2009-06-16 20:23:45 +02:00
|
|
|
|
2009-06-16 20:27:01 +02:00
|
|
|
%% @spec (To, Default::integer()) -> integer()
|
|
|
|
%% where To = [] | {Host::string(), Version::string()}
|
|
|
|
get_max_inactivity({Host, _}, Default) ->
|
|
|
|
case gen_mod:get_module_opt(Host, mod_http_bind, max_inactivity, undefined) of
|
|
|
|
Seconds when is_integer(Seconds) ->
|
|
|
|
Seconds * 1000;
|
|
|
|
undefined ->
|
|
|
|
Default
|
|
|
|
end;
|
|
|
|
get_max_inactivity(_, Default) ->
|
|
|
|
Default.
|
2009-06-16 20:23:45 +02:00
|
|
|
|
2009-08-31 20:40:25 +02:00
|
|
|
get_max_pause({Host, _}) ->
|
|
|
|
gen_mod:get_module_opt(Host, mod_http_bind, max_pause, ?MAX_PAUSE);
|
|
|
|
get_max_pause(_) ->
|
|
|
|
?MAX_PAUSE.
|
|
|
|
|
|
|
|
%% Current time as integer
|
|
|
|
tnow() ->
|
|
|
|
{TMegSec, TSec, TMSec} = now(),
|
|
|
|
(TMegSec * 1000000 + TSec) * 1000000 + TMSec.
|
2009-06-16 20:25:46 +02:00
|
|
|
|
2010-01-27 19:53:27 +01:00
|
|
|
check_default_xmlns(#xmlel{name = Name, ns = Xmlns, attrs = Attrs, children = Els} = El) ->
|
|
|
|
%check_default_xmlns({xmlelement, Name, Attrs, Els} = El) ->
|
|
|
|
case Xmlns of
|
|
|
|
% case xml:get_tag_attr_s("xmlns", El) of
|
|
|
|
"" -> #xmlel{name = Name, ns = ?NS_JABBER_CLIENT_s, attrs = Attrs, children = Els};
|
|
|
|
% "" -> {xmlelement, Name, [{"xmlns", ?NS_JABBER_CLIENT_s} | Attrs], Els};
|
2009-06-16 20:27:35 +02:00
|
|
|
_ -> El
|
2010-06-01 20:52:15 +02:00
|
|
|
end;
|
|
|
|
check_default_xmlns(El) ->
|
|
|
|
El.
|
2009-06-16 20:27:18 +02:00
|
|
|
|
2009-06-16 20:27:35 +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 20:27:18 +02:00
|
|
|
check_bind_module(XmppDomain) ->
|
|
|
|
case gen_mod:is_loaded(XmppDomain, mod_http_bind) of
|
2010-06-02 16:01:36 +02:00
|
|
|
true -> true;
|
2009-08-31 20:40:25 +02:00
|
|
|
false -> ?ERROR_MSG("You are trying to use BOSH (HTTP Bind), but the module mod_http_bind is not started.~n"
|
2010-06-02 16:01:36 +02:00
|
|
|
"Check your 'modules' section in your ejabberd configuration file.",[]),
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
make_sid() ->
|
|
|
|
sha:sha(term_to_binary({now(), make_ref()}))
|
|
|
|
++ "-" ++ ejabberd_cluster:node_id().
|
|
|
|
|
|
|
|
get_session(SID) ->
|
|
|
|
case string:tokens(SID, "-") of
|
|
|
|
[_, NodeID] ->
|
|
|
|
case ejabberd_cluster:get_node_by_id(NodeID) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
case mnesia:dirty_read({http_bind, SID}) of
|
|
|
|
[] ->
|
|
|
|
{error, enoent};
|
|
|
|
[Session] ->
|
|
|
|
{ok, Session}
|
|
|
|
end;
|
|
|
|
Node ->
|
|
|
|
case catch rpc:call(Node, mnesia, dirty_read,
|
|
|
|
[{http_bind, SID}], 5000) of
|
|
|
|
[Session] ->
|
|
|
|
{ok, Session};
|
|
|
|
_ ->
|
|
|
|
{error, enoent}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, enoent}
|
2009-06-16 20:27:18 +02:00
|
|
|
end.
|