Skip to content

Commit

Permalink
Take care to preserve the query-string in yaws_server:deliver_302/4
Browse files Browse the repository at this point in the history
It is an old bug on the redirect made when the trailing slash is missing.
The query-string was always missing from the redirect URL. So now, we add
it explicitly.
  • Loading branch information
Christopher Faulet committed Nov 20, 2012
1 parent 26e7ed2 commit 0f6226d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/yaws_server.erl
Expand Up @@ -2462,7 +2462,13 @@ deliver_302(CliSock, _Req, Arg, Path) ->
Headers = Arg#arg.headers,
DecPath = yaws_api:url_decode(Path),
RedirHost = yaws:redirect_host(SC, Headers#headers.host),
Loc = ["Location: ", Scheme, RedirHost, DecPath, "\r\n"],

%% QueryString must be added
Loc = case Arg#arg.querydata of
undefined -> ["Location: ", Scheme, RedirHost, DecPath, "\r\n"];
[] -> ["Location: ", Scheme, RedirHost, DecPath, "\r\n"];
Q -> ["Location: ", Scheme, RedirHost, DecPath, "?", Q, "\r\n"]
end,
new_redir_h(H, Loc),
deliver_accumulated(CliSock),
done_or_continue().
Expand Down
15 changes: 13 additions & 2 deletions test/t2/app_test.erl
Expand Up @@ -858,18 +858,29 @@ test_too_many_headers() ->

test_index_files() ->
io:format("index_files test\n", []),
?line {ok, Bin} = file:read_file("../../www/testdir/index.html"),
Content = binary_to_list(Bin),

%% "/" should be redirected to "/testdir", then to "/testdir/" and finally
%% get "/testdir/index.html"
Uri0 = "http://localhost:8010/",
?line {ok, Bin} = file:read_file("../../www/testdir/index.html"),
Content = binary_to_list(Bin),
?line {ok, "302", Hdrs1, _} = ibrowse:send_req(Uri0, [], get),
?line Uri1 = proplists:get_value("Location", Hdrs1),
?line "http://localhost:8010/testdir" = Uri1,
?line {ok, "302", Hdrs2, _} = ibrowse:send_req(Uri1, [], get),
?line Uri2 = proplists:get_value("Location", Hdrs2),
?line "http://localhost:8010/testdir/" = Uri2,
?line {ok, "200", _, Content} = ibrowse:send_req(Uri2, [], get),

%% Do the same thing but with a query-string
Uri3 = "http://localhost:8010/?a=1&b=2",
?line {ok, "302", Hdrs3, _} = ibrowse:send_req(Uri3, [], get),
?line Uri4 = proplists:get_value("Location", Hdrs3),
?line "http://localhost:8010/testdir?a=1&b=2" = Uri4,
?line {ok, "302", Hdrs4, _} = ibrowse:send_req(Uri4, [], get),
?line Uri5 = proplists:get_value("Location", Hdrs4),
?line "http://localhost:8010/testdir/?a=1&b=2" = Uri5,
?line {ok, "200", _, Content} = ibrowse:send_req(Uri5, [], get),
ok.

%% Do handshake, then send "hello" in a text frame
Expand Down

0 comments on commit 0f6226d

Please sign in to comment.