Skip to content

Commit d1fc722

Browse files
authored
Merge pull request #560 from garethsb/bugfix/redirect-keeps-query
Keep the query when redirecting to add a trailing slash
2 parents f914b8e + e435295 commit d1fc722

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎Development/cmake/NmosCppTest.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(NMOS_CPP_TEST_MDNS_TEST_HEADERS
4040
)
4141

4242
set(NMOS_CPP_TEST_NMOS_TEST_SOURCES
43+
nmos/test/admin_ui_test.cpp
4344
nmos/test/annotation_api_test.cpp
4445
nmos/test/api_utils_test.cpp
4546
nmos/test/bcp_008_test.cpp

‎Development/nmos/admin_ui.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace nmos
2525
router.support(U("/") + api.pattern, methods::GET, [](http_request req, http_response res, const string_t&, const route_parameters&)
2626
{
2727
set_reply(res, status_codes::TemporaryRedirect); // or status_codes::MovedPermanently?
28-
res.headers().add(web::http::header_names::location, req.request_uri().path() + U('/'));
28+
// the query, as well as the path, identifies the resource, see RFC 3986 Section 3.4
29+
res.headers().add(web::http::header_names::location, web::uri_builder().set_path(req.request_uri().path() + U('/')).set_query(req.request_uri().query()).to_string());
2930
return pplx::task_from_result(true);
3031
});
3132

‎Development/nmos/filesystem_route.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ namespace nmos
9191
if (index_redirect.second)
9292
{
9393
set_reply(res, status_codes::TemporaryRedirect); // or status_codes::MovedPermanently?
94-
res.headers().add(web::http::header_names::location, boost::algorithm::erase_tail_copy(req.request_uri().path(), (int)relative_path.size()) + index_redirect.first);
94+
// the query, as well as the path, identifies the resource, see RFC 3986 Section 3.4
95+
res.headers().add(web::http::header_names::location, web::uri_builder().set_path(boost::algorithm::erase_tail_copy(req.request_uri().path(), (int)relative_path.size()) + index_redirect.first).set_query(req.request_uri().query()).to_string());
9596

9697
return pplx::task_from_result(true);
9798
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// The first "test" is of course whether the header compiles standalone
2+
#include "nmos/admin_ui.h"
3+
4+
#include "bst/filesystem.h"
5+
#include "bst/test/test.h"
6+
#include "cpprest/basic_utils.h" // for utility::us2s, utility::s2us
7+
#include "nmos/log_gate.h"
8+
9+
namespace
10+
{
11+
utility::string_t get_location(web::http::experimental::listener::api_router& router, const utility::string_t& request_uri)
12+
{
13+
web::http::http_request req(web::http::methods::GET);
14+
req.set_request_uri(web::uri(request_uri));
15+
web::http::http_response res;
16+
router(req, res, U(""), {}).wait();
17+
BST_REQUIRE_EQUAL(web::http::status_codes::TemporaryRedirect, res.status_code());
18+
return res.headers()[web::http::header_names::location];
19+
}
20+
}
21+
22+
////////////////////////////////////////////////////////////////////////////////////////////
23+
BST_TEST_CASE(testAdminUiRedirectKeepsQuery)
24+
{
25+
using utility::us2s;
26+
27+
std::ostream null_ostream(nullptr);
28+
nmos::experimental::log_model log_model;
29+
nmos::experimental::log_gate gate(null_ostream, null_ostream, log_model);
30+
31+
const auto filesystem_root = bst::filesystem::temp_directory_path() / "nmos-cpp-admin-ui-test";
32+
bst::filesystem::create_directory(filesystem_root);
33+
bst::filesystem::create_directory(filesystem_root / "sdpoker");
34+
35+
auto admin_ui = nmos::experimental::make_admin_ui(utility::s2us(filesystem_root.string()), gate);
36+
37+
BST_CHECK_STRING_EQUAL("/admin/", us2s(get_location(admin_ui, U("http://host:123/admin"))));
38+
BST_CHECK_STRING_EQUAL("/admin/?foo=bar", us2s(get_location(admin_ui, U("http://host:123/admin?foo=bar"))));
39+
BST_CHECK_STRING_EQUAL("/admin/sdpoker/", us2s(get_location(admin_ui, U("http://host:123/admin/sdpoker"))));
40+
BST_CHECK_STRING_EQUAL("/admin/sdpoker/?url=http%3A%2F%2Fnode%2Fsdp", us2s(get_location(admin_ui, U("http://host:123/admin/sdpoker?url=http%3A%2F%2Fnode%2Fsdp"))));
41+
42+
bst::filesystem::remove_all(filesystem_root);
43+
}

0 commit comments

Comments
 (0)