summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2018-12-10 14:13:55 +0000
committerHåvard Pettersen <havardpe@oath.com>2019-01-02 12:55:03 +0000
commit3fd782ca16cea574d69a0afd4a727337308ced09 (patch)
tree931676182fd84a3ffb1f41d072747c48ef8dc6a3 /staging_vespalib
parent77f69efafd70cefeec471ebc8b8dd06643d24dfe (diff)
minimalistic portal integration into state server
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/tests/state_server/state_server_test.cpp10
-rw-r--r--staging_vespalib/src/vespa/vespalib/net/http_server.cpp70
-rw-r--r--staging_vespalib/src/vespa/vespalib/net/http_server.h31
-rw-r--r--staging_vespalib/src/vespa/vespalib/net/state_server.cpp1
4 files changed, 18 insertions, 94 deletions
diff --git a/staging_vespalib/src/tests/state_server/state_server_test.cpp b/staging_vespalib/src/tests/state_server/state_server_test.cpp
index 9d901a695e3..b688887c3fb 100644
--- a/staging_vespalib/src/tests/state_server/state_server_test.cpp
+++ b/staging_vespalib/src/tests/state_server/state_server_test.cpp
@@ -40,10 +40,7 @@ vespalib::string run_cmd(const vespalib::string &cmd) {
}
vespalib::string getPage(int port, const vespalib::string &path, const vespalib::string &extra_params = "") {
- vespalib::string result = run_cmd(make_string("curl -s %s http://localhost:%d%s", extra_params.c_str(), port, path.c_str()));
- vespalib::string chunked_result = run_cmd(make_string("curl -H transfer-encoding:chunked -s %s http://localhost:%d%s", extra_params.c_str(), port, path.c_str()));
- ASSERT_EQUAL(result, chunked_result);
- return result;
+ return run_cmd(make_string("curl -s %s http://localhost:%d%s", extra_params.c_str(), port, path.c_str()));
}
vespalib::string getFull(int port, const vespalib::string &path) { return getPage(port, path, "-D -"); }
@@ -63,7 +60,6 @@ struct DummyHandler : JsonGetHandler {
//-----------------------------------------------------------------------------
TEST_F("require that unknown url returns 404 response", HttpServer(0)) {
- f1.start();
std::string expect("HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n"
"\r\n");
@@ -73,7 +69,6 @@ TEST_F("require that unknown url returns 404 response", HttpServer(0)) {
TEST_FF("require that empty known url returns 404 response", DummyHandler(""), HttpServer(0)) {
auto token = f2.repo().bind(my_path, f1);
- f2.start();
std::string expect("HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n"
"\r\n");
@@ -83,7 +78,6 @@ TEST_FF("require that empty known url returns 404 response", DummyHandler(""), H
TEST_FF("require that non-empty known url returns expected headers", DummyHandler("[123]"), HttpServer(0)) {
auto token = f2.repo().bind(my_path, f1);
- f2.start();
vespalib::string expect("HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: application/json\r\n"
@@ -100,7 +94,6 @@ TEST_FFFF("require that handler is selected based on longest matching url prefix
auto token2 = f4.repo().bind("/foo/bar", f2);
auto token1 = f4.repo().bind("/foo", f1);
auto token3 = f4.repo().bind("/foo/bar/baz", f3);
- f4.start();
int port = f4.port();
EXPECT_EQUAL("", getPage(port, "/fox"));
EXPECT_EQUAL("[1]", getPage(port, "/foo"));
@@ -121,7 +114,6 @@ struct EchoHost : JsonGetHandler {
TEST_FF("require that host is passed correctly", EchoHost(), HttpServer(0)) {
auto token = f2.repo().bind(my_path, f1);
- f2.start();
EXPECT_EQUAL(make_string("%s:%d", HostName::get().c_str(), f2.port()), f2.host());
vespalib::string default_result = make_string("[\"%s\"]", f2.host().c_str());
vespalib::string localhost_result = make_string("[\"%s:%d\"]", "localhost", f2.port());
diff --git a/staging_vespalib/src/vespa/vespalib/net/http_server.cpp b/staging_vespalib/src/vespa/vespalib/net/http_server.cpp
index 71b2e5bc173..99a66fccde5 100644
--- a/staging_vespalib/src/vespa/vespalib/net/http_server.cpp
+++ b/staging_vespalib/src/vespa/vespalib/net/http_server.cpp
@@ -1,83 +1,33 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "http_server.h"
-#include <vespa/vespalib/util/exceptions.h>
-#include <vespa/vespalib/util/host_name.h>
-#include <vespa/vespalib/util/stringfmt.h>
+#include <vespa/vespalib/net/crypto_engine.h>
namespace vespalib {
-namespace {
-
-void respond_not_found(Fast_HTTPConnection &conn) {
- conn.Output(conn.GetHTTPVersion().c_str());
- conn.Output(" 404 Not Found\r\n");
- conn.Output("Connection: close\r\n\r\n");
-}
-
-void write_json_header(Fast_HTTPConnection &conn) {
- conn.Output(conn.GetHTTPVersion().c_str());
- conn.Output(" 200 OK\r\n");
- conn.Output("Connection: close\r\n");
- conn.Output("Content-Type: application/json\r\n\r\n");
-}
-
-} // namespace vespalib::<unnamed>
-
void
-HttpServer::handle_get(const string &url, const string &host_in,
- Fast_HTTPConnection &conn) const
+HttpServer::get(Portal::GetRequest req)
{
- std::map<vespalib::string,vespalib::string> params;
- vespalib::string json_result = _handler_repo.get(host_in.empty() ? _my_host : host_in, url, params);
+ vespalib::string json_result = _handler_repo.get(req.get_host(), req.get_uri(), {});
if (json_result.empty()) {
- respond_not_found(conn);
+ req.respond_with_error(404, "Not Found");
} else {
- write_json_header(conn);
- conn.OutputData(json_result.data(), json_result.size());
+ req.respond_with_content("application/json", json_result);
}
}
//-----------------------------------------------------------------------------
HttpServer::HttpServer(int port_in)
- : _requested_port(port_in),
- _started(false),
- _actual_port(0),
- _my_host(),
- _handler_repo(),
- _server(new Server(port_in, *this))
-{
- _server->SetKeepAlive(false);
-}
-
-HttpServer::~HttpServer() { }
-
-void
-HttpServer::start()
+ : _handler_repo(),
+ _server(Portal::create(CryptoEngine::get_default(), port_in)),
+ _root(_server->bind("/", *this))
{
- if (_started) {
- return;
- }
- int ret_code = _server->Start();
- if (ret_code != FASTLIB_SUCCESS &&
- ret_code != FASTLIB_HTTPSERVER_ALREADYSTARTED)
- {
- if (ret_code == FASTLIB_HTTPSERVER_BADLISTEN) {
- throw PortListenException(_requested_port, "HTTP");
- } else {
- throw FatalException("failed to start vespalib HTTP server");
- }
- }
- _actual_port = _server->getListenPort();
- _my_host = make_string("%s:%d", HostName::get().c_str(), _actual_port);
- _started = true;
}
-void
-HttpServer::stop()
+HttpServer::~HttpServer()
{
- _server.reset(nullptr);
+ _root.reset();
}
} // namespace vespalib
diff --git a/staging_vespalib/src/vespa/vespalib/net/http_server.h b/staging_vespalib/src/vespa/vespalib/net/http_server.h
index 6bc1ac69414..4346ef65063 100644
--- a/staging_vespalib/src/vespa/vespalib/net/http_server.h
+++ b/staging_vespalib/src/vespa/vespalib/net/http_server.h
@@ -2,7 +2,7 @@
#pragma once
-#include <vespa/fastlib/net/httpserver.h>
+#include <vespa/vespalib/portal/portal.h>
#include "json_handler_repo.h"
namespace vespalib {
@@ -15,38 +15,21 @@ namespace vespalib {
* the server has been started. Request dispatching is done using a
* JsonHandlerRepo.
**/
-class HttpServer
+class HttpServer : public Portal::GetHandler
{
private:
- struct Server : Fast_HTTPServer {
- typedef std::unique_ptr<Server> UP;
- const HttpServer &parent;
- virtual void onGetRequest(const vespalib::string &url, const vespalib::string &host,
- Fast_HTTPConnection &conn) override
- {
- parent.handle_get(url, host, conn);
- }
- Server(int port, const HttpServer &parent_in) : Fast_HTTPServer(port), parent(parent_in) {}
- };
-
- int _requested_port;
- volatile bool _started;
- int _actual_port;
- vespalib::string _my_host;
JsonHandlerRepo _handler_repo;
- Server::UP _server; // need separate object for controlled shutdown
+ Portal::SP _server;
+ Portal::Token::UP _root;
- void handle_get(const vespalib::string &url, const vespalib::string &host,
- Fast_HTTPConnection &conn) const;
+ void get(Portal::GetRequest req) override;
public:
typedef std::unique_ptr<HttpServer> UP;
HttpServer(int port_in);
~HttpServer();
- const vespalib::string &host() const { return _my_host; }
+ const vespalib::string &host() const { return _server->my_host(); }
JsonHandlerRepo &repo() { return _handler_repo; }
- void start();
- int port() const { return (_actual_port != 0) ? _actual_port : _requested_port; }
- void stop();
+ int port() const { return _server->listen_port(); }
};
} // namespace vespalib
diff --git a/staging_vespalib/src/vespa/vespalib/net/state_server.cpp b/staging_vespalib/src/vespa/vespalib/net/state_server.cpp
index bb81a3aa535..3d238b07463 100644
--- a/staging_vespalib/src/vespa/vespalib/net/state_server.cpp
+++ b/staging_vespalib/src/vespa/vespalib/net/state_server.cpp
@@ -14,7 +14,6 @@ StateServer::StateServer(int port,
{
_tokens.push_back(_server.repo().bind("/state/v1", _api));
_tokens.push_back(_server.repo().bind("/metrics/total", _api));
- _server.start();
}
StateServer::~StateServer() = default;