Skip to content

Commit

Permalink
Fixing the fix for issue yrashk#57
Browse files Browse the repository at this point in the history
The previous fix would mess up some escaping in different
circumstances. This commit should get rid of the problems by
adding smarter escaping.
  • Loading branch information
ferd committed Sep 10, 2011
1 parent 9018214 commit 9433433
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/socketio_transport_polling.erl
Expand Up @@ -297,7 +297,7 @@ send_message(Message, Req, Index, ServerModule, Sup) ->
send_message_1(Headers, Message, Req, Index, ServerModule) ->
Headers0 = [{"Content-Type", "text/javascript; charset=UTF-8"}|Headers],
Message0 = unicode:characters_to_list(jsx:format(jsx:term_to_json(list_to_binary(Message), [{strict, false}]))),
Message1 = "io.JSONP["++Index++"]._(" ++ Message0 ++ ");",
Message1 = "io.JSONP["++Index++"]._(\"" ++ escape(tl(Message0)) ++ ");",
apply(ServerModule, respond, [Req, 200, Headers0, Message1]).

cors_headers(Headers, Sup) ->
Expand Down Expand Up @@ -325,3 +325,10 @@ reset_duration({TimerRef, Time}) ->
erlang:cancel_timer(TimerRef),
NewRef = erlang:start_timer(Time, self(), polling),
{NewRef, Time}.

%% THis should deal with only one level of depth -- the rest is assumed to
%% have been escaped correctly by jsx.
escape([$"]) -> [$"];
escape([$"|Rest]) -> [$\\, $" | escape(Rest)];
escape([$\\|Rest]) -> [$\\,$\\ | escape(Rest)];
escape([CodePoint|Rest]) -> [CodePoint | escape(Rest)].
2 changes: 1 addition & 1 deletion test/socketio_transport_tests.erl
Expand Up @@ -47,7 +47,7 @@ transport_tests(Browser, Transport) ->
ets:insert(socketio_tests, {transport, Transport}),
error_logger:delete_report_handler(error_logger_tty_h), %% suppress annoying kernel logger
application:start(misultin),
application:start(socketio),
application:start(socketio),
{ok, Pid} = socketio_listener:start([{http_port, 8989},
{default_http_handler, ?MODULE}]),
EventMgr = socketio_listener:event_manager(Pid),
Expand Down

0 comments on commit 9433433

Please sign in to comment.