Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added raw xml data to opening tag. Send proper iq response (well, mis…

…sing error case).
  • Loading branch information...
commit a2eb0a0cf0e8d24e10dd32fd46cd7084060f26ba 1 parent 6c0a816
@kaos authored
View
3  include/yaxs.hrl
@@ -18,7 +18,8 @@
prefix,
name,
attrs=[],
- body=[]
+ body=[],
+ data=[]
}).
-record(attribute, {
View
37 src/core/yaxs_core.erl
@@ -44,7 +44,7 @@ start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
new_session(Jid) ->
- gen_server:cast(?SERVER, #session{ pid=self(), jid=Jid }).
+ gen_server:call(?SERVER, #session{ pid=self(), jid=Jid }).
route_stanza(Stanza) ->
gen_server:cast(?SERVER, {route, Stanza}).
@@ -72,6 +72,27 @@ init([]) ->
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
+handle_call(#session{ pid=Pid } = Session,
+ _From,
+ #state{ sessions=Sessions } = State) ->
+
+ error_logger:info_msg("new_session: ~p~n", [Session]),
+ State1 = State#state{
+ sessions=
+ [
+ Session#session{
+ ref=erlang:monitor(
+ process,
+ Pid)
+ }
+ |Sessions]
+ },
+
+ % May want to return a {error, error-type} here...
+ %Res = {error, conflict},
+ Res = ok,
+ {reply, Res, State1};
+
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
@@ -86,20 +107,6 @@ handle_cast({route, Stanza}, State) ->
error_logger:info_msg("route_stanza: ~p~n", [Stanza]),
{noreply, State};
-handle_cast(#session{ pid=Pid } = Session,
- #state{ sessions=Sessions } = State) ->
- error_logger:info_msg("new_session: ~p~n", [Session]),
- {noreply, State#state{
- sessions=
- [
- Session#session{
- ref=erlang:monitor(
- process,
- Pid)
- }
- |Sessions]
- }};
-
handle_cast(_Msg, State) ->
{noreply, State}.
View
23 src/core/yaxs_core_bind.erl
@@ -44,8 +44,8 @@ handle(#tag{ name="bind", body=Body },
case proplists:get_value(bind, Tags) of
undefined ->
bind_resource(Body, Client);
- Res ->
- {tag, {already_bound, Res}}
+ _ ->
+ {error, conflict}
end.
@@ -72,7 +72,18 @@ do_bind_resource(Resource,
])
),
- R({jid, Jid}),
- yaxs_core:new_session(Jid),
- {tag, {bind, Resource}}.
-
+ case yaxs_core:new_session(Jid) of
+ ok ->
+ R({jid, Jid}),
+ [
+ {tag, {bind, Resource}},
+ {result, io_lib:format(
+ "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>"
+ "<jid>~s</jid>"
+ "</bind>",
+ [Jid]
+ )}
+ ];
+ Error ->
+ Error
+ end.
View
22 src/core/yaxs_core_iq.erl
@@ -29,10 +29,26 @@ init() ->
"iq"
]).
-handle(#stanza{ body=[Tag] },
- #yaxs_client{ response=_R } = Client) ->
+handle(#stanza{ id=Id, body=[Tag] },
+ #yaxs_client{ response=R } = Client) ->
- yaxs_event:publish(Tag, Client).
+ PubRes = yaxs_event:publish(Tag, Client),
+ IqRes = [Res || Res <- PubRes,
+ is_tuple(Res),
+ tuple_size(Res) == 2,
+ element(1, Res) == result
+ orelse element(1, Res) == error
+ ],
+ [{Type, Res}] = IqRes,
+
+ R(io_lib:format(
+ "<iq type='~s' id='~s'>"
+ "~s"
+ "</iq>",
+ [Type, Id, Res]
+ )
+ ),
+ PubRes -- IqRes.
%%====================================================================
View
26 src/test/yaxs_SUITE.erl
@@ -195,12 +195,12 @@ test_stream(_Config) ->
open_stream(Sock, "authenticated (fake)"),
- bind(Sock, 2),
-% bind(Sock, 1),
+ ok = bind(Sock, 2),
+ ok = bind(Sock, 1),
- gen_tcp:recv(Sock, 0, 100),
ok = gen_tcp:send(Sock,"<message to='foo@example.com'><body>bar</body></message>"),
+ timeout = wait_for_response(Sock, "message"),
{error, timeout} = gen_tcp:recv(Sock, 0, 500),
ok = gen_tcp:close(Sock).
@@ -215,7 +215,9 @@ bind(Sock, 1) ->
"<iq type='set' id='bind_1'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
</iq>"
- );
+ ),
+ wait_for_response(Sock, "bind:1");
+
bind(Sock, 2) ->
ok = gen_tcp:send(Sock,
"<iq type='set' id='bind_2'>
@@ -223,7 +225,8 @@ bind(Sock, 2) ->
<resource>someresource</resource>
</bind>
</iq>"
- ).
+ ),
+ wait_for_response(Sock, "bind:2").
open_stream(Sock, Comment) ->
ok = gen_tcp:send(
@@ -235,5 +238,14 @@ open_stream(Sock, Comment) ->
xmlns:stream='http://etherx.jabber.org/streams'
version='1.0'>"
),
- {ok, Data} = gen_tcp:recv(Sock, 0, 1000),
- ct:pal("Response{~s}:~n~s~n", [Comment, Data]).
+ wait_for_response(Sock, Comment).
+
+wait_for_response(Sock, Comment) ->
+ case gen_tcp:recv(Sock, 0, 1000) of
+ {ok, Data} ->
+ ct:pal("Response{~s}:~n~s~n", [Comment, Data]),
+ ok;
+ {error, Why} ->
+ ct:pal("No Response{~s}:~s~n", [Comment, Why]),
+ Why
+ end.
View
47 src/yaxs_client.erl
@@ -114,14 +114,15 @@ wait_for_socket({socket_ready, Sock}, #state{ client=Client} = State) ->
wait_for_stream({sax, {open, {"http://etherx.jabber.org/streams"=NS,
"stream"=P, "stream"=N, Attrs}
}
- }, State) ->
+ },
+ #state{ open_tags = [Tag] } = State) ->
- Tag = #tag{ namespace=NS,
- prefix=P,
- name=N,
- attrs=Attrs },
-
- State1 = publish(Tag, State#state{ open_tags = [Tag] }),
+ Tag1 = Tag#tag{ namespace=NS,
+ prefix=P,
+ name=N,
+ attrs=Attrs },
+
+ State1 = publish(Tag1, State#state{ open_tags = [Tag1] }),
{next_state, setup_stream, State1};
wait_for_stream({sax, _Event}, State) ->
@@ -144,11 +145,16 @@ setup_stream(reset_stream, #state{ client=#yaxs_client{ tags=Tags }=Client } = S
}
};
-setup_stream({sax, {open, {NS, P, N, A}}}, #state{ open_tags = Tags } = State) ->
- {next_state, setup_stream,
- State#state{ open_tags = [#tag{ namespace=NS, prefix=P, name=N, attrs=A }
- |Tags] }
- };
+setup_stream({sax, {open, {NS, P, N, A}}}, #state{ open_tags = [Tag|Tags] } = State) ->
+ Tags1 = case Tag#tag.name of
+ undefined ->
+ [Tag#tag{ namespace=NS, prefix=P, name=N, attrs=A }
+ |Tags];
+ _ ->
+ [#tag{ namespace=NS, prefix=P, name=N, attrs=A },
+ Tag | Tags]
+ end,
+ {next_state, setup_stream, State#state{ open_tags = Tags1 }};
setup_stream({sax, {close, _Tag}}, #state{ open_tags = [OTag|Tags] } = State) ->
State1 = case Tags of
@@ -206,15 +212,20 @@ streaming({sax, close}, #state{ client=Client } = State) ->
%%--------------------------------------------------------------------
handle_event({data, Data},
StateName,
- #state{ client=#yaxs_client{ sock=Sock } } = State) ->
+ #state{
+ client = #yaxs_client{ sock=Sock },
+ open_tags = Tags
+ } = State) ->
+
inet:setopts(Sock, [{active, once}]),
try
{next_state, StateName,
- State#state{ sax =
- yaxs_sax:parse(Data,
- fun sax_event/2,
- State#state.sax)
- }
+ State#state{
+ sax = yaxs_sax:parse(Data,
+ fun sax_event/2,
+ State#state.sax),
+ open_tags = [#tag{ data=Data }|Tags]
+ }
}
catch
throw:Error ->
View
2  src/yaxs_con.erl
@@ -101,7 +101,7 @@ handle_info({inet_async, Sock, _Ref, {ok, Client}}, State) ->
end,
{ok, Pid} = supervisor:start_child(yaxs_client_sup, []),
- sys:trace(Pid, true),
+ %sys:trace(Pid, true),
gen_tcp:controlling_process(Client, Pid),
(State#state.client):set_socket(Pid, Client),
View
4 src/yaxs_event.erl
@@ -141,10 +141,10 @@ list_mods(Event) ->
gen_server:call(?SERVER, {list_mods, Event}).
do_publish(Event, Client, Mods) ->
- error_logger:info_msg("do publish:~nEvent:~p~nClient:~p~nMods:~p~n", [Event, Client, Mods]),
+ %error_logger:info_msg("do publish:~nEvent:~p~nClient:~p~nMods:~p~n", [Event, Client, Mods]),
Res = lists:flatten(
[Mod:handle(Event, Client) || Mod <- Mods]
),
- error_logger:info_msg("publish result: ~p~nMods: ~p~n", [Res, Mods]),
+ %error_logger:info_msg("publish result: ~p~nMods: ~p~n", [Res, Mods]),
Res.
Please sign in to comment.
Something went wrong with that request. Please try again.