Permalink
Browse files

#8 Unify response when callback is not set - return 'undefined'

Also, return generic error when callback is not present.
  • Loading branch information...
1 parent f9d12a1 commit 6ccbe476d9e691b9fa9a923fd9cbc594a2f3352b @majek majek committed Oct 10, 2011
Showing with 27 additions and 14 deletions.
  1. +21 −12 src/sockjs_filters.erl
  2. +6 −2 src/sockjs_http.erl
View
@@ -139,9 +139,12 @@ xhr_streaming(Req, Headers, _Server, SessionId) ->
reply_loop(Req1, SessionId, false, fun fmt_xhr/1).
jsonp(Req, Headers, _Server, SessionId) ->
- Req1 = headers(Req, Headers),
- {CB, Req2} = sockjs_http:callback(Req1),
- reply_loop(Req2, SessionId, true, fun (Body) -> fmt_jsonp(Body, CB) end).
+ S = fun (CB) ->
+ Req1 = headers(Req, Headers),
+ reply_loop(Req1, SessionId, true,
+ fun (Body) -> fmt_jsonp(Body, CB) end)
+ end,
+ verify_callback(Req, S).
iframe(Req, Headers, _Server, _SessionId) ->
{ok, URL} = application:get_env(sockjs, sockjs_url),
@@ -160,20 +163,26 @@ eventsource(Req, Headers, _Server, SessionId) ->
reply_loop(Req1, SessionId, true, fun fmt_eventsource/1).
htmlfile(Req, Headers, _Server, SessionId) ->
+ S = fun (CB) ->
+ Req1 = headers(Req, Headers, "text/html; charset=UTF-8"),
+ IFrame0 = fmt(?IFRAME_HTMLFILE, [CB]),
+ %% Safari needs at least 1024 bytes to parse the
+ %% website. Relevant:
+ %% http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
+ Padding = list_to_binary(string:copies(" ", 1024 - size(IFrame0))),
+ IFrame = <<IFrame0/binary, Padding/binary, $\r, $\n, $\r, $\n>>,
+ chunk(Req1, IFrame),
+ reply_loop(Req1, SessionId, false, fun fmt_htmlfile/1)
+ end,
+ verify_callback(Req, S).
+
+verify_callback(Req, Success) ->
{CB, Req1} = sockjs_http:callback(Req),
case CB of
undefined ->
sockjs_http:reply(500, [], "\"callback\" parameter required", Req);
_ ->
- Req2 = headers(Req1, Headers, "text/html; charset=UTF-8"),
- IFrame0 = fmt(?IFRAME_HTMLFILE, [CB]),
- %% Safari needs at least 1024 bytes to parse the
- %% website. Relevant:
- %% http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
- Padding = list_to_binary(string:copies(" ", 1024 - size(IFrame0))),
- IFrame = <<IFrame0/binary, Padding/binary, $\r, $\n, $\r, $\n>>,
- chunk(Req2, IFrame),
- reply_loop(Req2, SessionId, false, fun fmt_htmlfile/1)
+ Success(CB)
end.
chunking_test(Req, Headers, _Server, _SessionId) ->
View
@@ -48,8 +48,12 @@ callback({cowboy, Req}) ->
{CB, Req1} = cowboy_http_req:qs_val(<<"c">>, Req),
{CB, {cowboy, Req1}};
callback({misultin, Req} = R) ->
- CB = list_to_binary(proplists:get_value("c", Req:parse_qs())),
- {CB, R}.
+ case proplists:get_value("c", Req:parse_qs()) of
+ undefined ->
+ {undefined, R};
+ CB ->
+ {list_to_binary(CB), R}
+ end.
header(K, {cowboy, Req})->
{H, _} = cowboy_http_req:header(K, Req),

0 comments on commit 6ccbe47

Please sign in to comment.