summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2019-01-09 15:25:30 +0000
committerHåvard Pettersen <havardpe@oath.com>2019-01-09 15:25:30 +0000
commitea89f88cd0673f533fa314f6f9e7a34d84988eca (patch)
tree5273a8417413ce344f90551133fec50da35ce495 /vespalib
parent5934b46fa964fabd9cd774f67c43fc1e879a1b0e (diff)
use dequoted path for dispatching
also expose query parameters through request proxy
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/portal/portal_test.cpp36
-rw-r--r--vespalib/src/vespa/vespalib/portal/http_request.h1
-rw-r--r--vespalib/src/vespa/vespalib/portal/portal.cpp30
-rw-r--r--vespalib/src/vespa/vespalib/portal/portal.h4
4 files changed, 70 insertions, 1 deletions
diff --git a/vespalib/src/tests/portal/portal_test.cpp b/vespalib/src/tests/portal/portal_test.cpp
index 299340fd131..ee5d10a313a 100644
--- a/vespalib/src/tests/portal/portal_test.cpp
+++ b/vespalib/src/tests/portal/portal_test.cpp
@@ -346,4 +346,40 @@ TEST_MT_FFF("require that portal destruction waits for request completion", 3,
//-----------------------------------------------------------------------------
+TEST("require that query parameters can be inspected") {
+ auto portal = Portal::create(null_crypto(), 0);
+ MyGetHandler handler([](Portal::GetRequest request)
+ {
+ EXPECT_EQUAL(request.get_uri(), "/test?a=b&x=y");
+ EXPECT_EQUAL(request.get_path(), "/test");
+ EXPECT_TRUE(request.has_param("a"));
+ EXPECT_TRUE(request.has_param("x"));
+ EXPECT_TRUE(!request.has_param("b"));
+ EXPECT_EQUAL(request.get_param("a"), "b");
+ EXPECT_EQUAL(request.get_param("x"), "y");
+ EXPECT_EQUAL(request.get_param("b"), "");
+ auto params = request.export_params();
+ EXPECT_EQUAL(params.size(), 2u);
+ EXPECT_EQUAL(params["a"], "b");
+ EXPECT_EQUAL(params["x"], "y");
+ request.respond_with_content("a", "b");
+ });
+ auto bound = portal->bind("/test", handler);
+ auto result = fetch(portal->listen_port(), null_crypto(), "/test?a=b&x=y");
+ EXPECT_EQUAL(result, make_expected_response("a", "b"));
+}
+
+TEST("require that request path is dequoted before handler dispatching") {
+ auto portal = Portal::create(null_crypto(), 0);
+ MyGetHandler handler([](Portal::GetRequest request)
+ {
+ EXPECT_EQUAL(request.get_uri(), "/%5btest%5D");
+ EXPECT_EQUAL(request.get_path(), "/[test]");
+ request.respond_with_content("a", "b");
+ });
+ auto bound = portal->bind("/[test]", handler);
+ auto result = fetch(portal->listen_port(), null_crypto(), "/%5btest%5D");
+ EXPECT_EQUAL(result, make_expected_response("a", "b"));
+}
+
TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/portal/http_request.h b/vespalib/src/vespa/vespalib/portal/http_request.h
index db586f08ea2..39467c3b248 100644
--- a/vespalib/src/vespa/vespalib/portal/http_request.h
+++ b/vespalib/src/vespa/vespalib/portal/http_request.h
@@ -48,6 +48,7 @@ public:
const vespalib::string &get_path() const { return _path; }
bool has_param(const vespalib::string &name) const;
const vespalib::string &get_param(const vespalib::string &name) const;
+ std::map<vespalib::string, vespalib::string> export_params() const { return _params; }
};
} // namespace vespalib::portal
diff --git a/vespalib/src/vespa/vespalib/portal/portal.cpp b/vespalib/src/vespa/vespalib/portal/portal.cpp
index 0d62d5728d1..ec2f1b78c03 100644
--- a/vespalib/src/vespa/vespalib/portal/portal.cpp
+++ b/vespalib/src/vespa/vespalib/portal/portal.cpp
@@ -48,6 +48,34 @@ Portal::GetRequest::get_uri() const
return _conn->get_request().get_uri();
}
+const vespalib::string &
+Portal::GetRequest::get_path() const
+{
+ assert(active());
+ return _conn->get_request().get_path();
+}
+
+bool
+Portal::GetRequest::has_param(const vespalib::string &name) const
+{
+ assert(active());
+ return _conn->get_request().has_param(name);
+}
+
+const vespalib::string &
+Portal::GetRequest::get_param(const vespalib::string &name) const
+{
+ assert(active());
+ return _conn->get_request().get_param(name);
+}
+
+std::map<vespalib::string, vespalib::string>
+Portal::GetRequest::export_params() const
+{
+ assert(active());
+ return _conn->get_request().export_params();
+}
+
void
Portal::GetRequest::respond_with_content(const vespalib::string &content_type,
const vespalib::string &content)
@@ -131,7 +159,7 @@ Portal::handle_http(portal::HttpConnection *conn)
conn->respond_with_error(501, "Not Implemented");
} else {
GetHandler *get_handler = nullptr;
- auto guard = lookup_get_handler(conn->get_request().get_uri(), get_handler);
+ auto guard = lookup_get_handler(conn->get_request().get_path(), get_handler);
if (guard.valid()) {
assert(get_handler != nullptr);
conn->resolve_host(_my_host);
diff --git a/vespalib/src/vespa/vespalib/portal/portal.h b/vespalib/src/vespa/vespalib/portal/portal.h
index 93424dda90c..fc3b81b37e6 100644
--- a/vespalib/src/vespa/vespalib/portal/portal.h
+++ b/vespalib/src/vespa/vespalib/portal/portal.h
@@ -60,6 +60,10 @@ public:
const vespalib::string &get_header(const vespalib::string &name) const;
const vespalib::string &get_host() const;
const vespalib::string &get_uri() const;
+ const vespalib::string &get_path() const;
+ bool has_param(const vespalib::string &name) const;
+ const vespalib::string &get_param(const vespalib::string &name) const;
+ std::map<vespalib::string, vespalib::string> export_params() const;
void respond_with_content(const vespalib::string &content_type,
const vespalib::string &content);
void respond_with_error(int code, const vespalib::string &msg);