Fixes issue mentioned in #164: remote endpoint address and port was not correctly set. Request::remote_endpoint_address and Request::remote_endpoint_port are now functions instead of variables in order to reduce unnecessary instructions.
This commit is contained in:
parent
b9d4be229f
commit
6e0a1ec9e8
5 changed files with 35 additions and 19 deletions
|
|
@ -89,7 +89,7 @@ int main() {
|
|||
// Responds with request-information
|
||||
server.resource["^/info$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||
stringstream stream;
|
||||
stream << "<h1>Request from " << request->remote_endpoint_address << ":" << request->remote_endpoint_port << "</h1>";
|
||||
stream << "<h1>Request from " << request->remote_endpoint_address() << ":" << request->remote_endpoint_port() << "</h1>";
|
||||
|
||||
stream << request->method << " " << request->path << " HTTP/" << request->http_version;
|
||||
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ int main() {
|
|||
// Responds with request-information
|
||||
server.resource["^/info$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
|
||||
stringstream stream;
|
||||
stream << "<h1>Request from " << request->remote_endpoint_address << ":" << request->remote_endpoint_port << "</h1>";
|
||||
stream << "<h1>Request from " << request->remote_endpoint_address() << ":" << request->remote_endpoint_port() << "</h1>";
|
||||
|
||||
stream << request->method << " " << request->path << " HTTP/" << request->http_version;
|
||||
|
||||
|
|
|
|||
|
|
@ -183,8 +183,10 @@ namespace SimpleWeb {
|
|||
friend class Session;
|
||||
|
||||
asio::streambuf streambuf;
|
||||
Request(std::size_t max_request_streambuf_size, const std::string &remote_endpoint_address = std::string(), unsigned short remote_endpoint_port = 0) noexcept
|
||||
: streambuf(max_request_streambuf_size), content(streambuf), remote_endpoint_address(remote_endpoint_address), remote_endpoint_port(remote_endpoint_port) {}
|
||||
std::shared_ptr<asio::ip::tcp::endpoint> remote_endpoint;
|
||||
|
||||
Request(std::size_t max_request_streambuf_size, std::shared_ptr<asio::ip::tcp::endpoint> remote_endpoint) noexcept
|
||||
: streambuf(max_request_streambuf_size), remote_endpoint(std::move(remote_endpoint)), content(streambuf) {}
|
||||
|
||||
public:
|
||||
std::string method, path, query_string, http_version;
|
||||
|
|
@ -195,8 +197,18 @@ namespace SimpleWeb {
|
|||
|
||||
regex::smatch path_match;
|
||||
|
||||
std::string remote_endpoint_address;
|
||||
unsigned short remote_endpoint_port;
|
||||
std::string remote_endpoint_address() noexcept {
|
||||
try {
|
||||
return remote_endpoint->address().to_string();
|
||||
}
|
||||
catch(...) {
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned short remote_endpoint_port() noexcept {
|
||||
return remote_endpoint->port();
|
||||
}
|
||||
|
||||
/// Returns query keys with percent-decoded values.
|
||||
CaseInsensitiveMultimap parse_query_string() noexcept {
|
||||
|
|
@ -217,6 +229,8 @@ namespace SimpleWeb {
|
|||
|
||||
std::unique_ptr<asio::steady_timer> timer;
|
||||
|
||||
std::shared_ptr<asio::ip::tcp::endpoint> remote_endpoint;
|
||||
|
||||
void close() noexcept {
|
||||
error_code ec;
|
||||
std::unique_lock<std::mutex> lock(socket_close_mutex); // The following operations seems to be needed to run sequentially
|
||||
|
|
@ -250,13 +264,11 @@ namespace SimpleWeb {
|
|||
class Session {
|
||||
public:
|
||||
Session(std::size_t max_request_streambuf_size, std::shared_ptr<Connection> connection) noexcept : connection(std::move(connection)) {
|
||||
try {
|
||||
auto remote_endpoint = this->connection->socket->lowest_layer().remote_endpoint();
|
||||
request = std::shared_ptr<Request>(new Request(max_request_streambuf_size, remote_endpoint.address().to_string(), remote_endpoint.port()));
|
||||
}
|
||||
catch(...) {
|
||||
request = std::shared_ptr<Request>(new Request(max_request_streambuf_size));
|
||||
if(!this->connection->remote_endpoint) {
|
||||
error_code ec;
|
||||
this->connection->remote_endpoint = std::make_shared<asio::ip::tcp::endpoint>(this->connection->socket->lowest_layer().remote_endpoint(ec));
|
||||
}
|
||||
request = std::shared_ptr<Request>(new Request(max_request_streambuf_size, this->connection->remote_endpoint));
|
||||
}
|
||||
|
||||
std::shared_ptr<Connection> connection;
|
||||
|
|
@ -579,10 +591,10 @@ namespace SimpleWeb {
|
|||
|
||||
protected:
|
||||
void accept() override {
|
||||
auto session = std::make_shared<Session>(config.max_request_streambuf_size, create_connection(*io_service));
|
||||
auto connection = create_connection(*io_service);
|
||||
|
||||
acceptor->async_accept(*session->connection->socket, [this, session](const error_code &ec) {
|
||||
auto lock = session->connection->handler_runner->continue_lock();
|
||||
acceptor->async_accept(*connection->socket, [this, connection](const error_code &ec) {
|
||||
auto lock = connection->handler_runner->continue_lock();
|
||||
if(!lock)
|
||||
return;
|
||||
|
||||
|
|
@ -590,6 +602,8 @@ namespace SimpleWeb {
|
|||
if(ec != asio::error::operation_aborted)
|
||||
this->accept();
|
||||
|
||||
auto session = std::make_shared<Session>(config.max_request_streambuf_size, connection);
|
||||
|
||||
if(!ec) {
|
||||
asio::ip::tcp::no_delay option(true);
|
||||
error_code ec;
|
||||
|
|
|
|||
|
|
@ -48,16 +48,18 @@ namespace SimpleWeb {
|
|||
asio::ssl::context context;
|
||||
|
||||
void accept() override {
|
||||
auto session = std::make_shared<Session>(config.max_request_streambuf_size, create_connection(*io_service, context));
|
||||
auto connection = create_connection(*io_service, context);
|
||||
|
||||
acceptor->async_accept(session->connection->socket->lowest_layer(), [this, session](const error_code &ec) {
|
||||
auto lock = session->connection->handler_runner->continue_lock();
|
||||
acceptor->async_accept(connection->socket->lowest_layer(), [this, connection](const error_code &ec) {
|
||||
auto lock = connection->handler_runner->continue_lock();
|
||||
if(!lock)
|
||||
return;
|
||||
|
||||
if(ec != asio::error::operation_aborted)
|
||||
this->accept();
|
||||
|
||||
auto session = std::make_shared<Session>(config.max_request_streambuf_size, connection);
|
||||
|
||||
if(!ec) {
|
||||
asio::ip::tcp::no_delay option(true);
|
||||
error_code ec;
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ int main() {
|
|||
|
||||
asio::io_service io_service;
|
||||
asio::ip::tcp::socket socket(io_service);
|
||||
SimpleWeb::Server<HTTP>::Request request(static_cast<size_t>(-1));
|
||||
SimpleWeb::Server<HTTP>::Request request(static_cast<size_t>(-1), nullptr);
|
||||
{
|
||||
request.query_string = "";
|
||||
auto queries = request.parse_query_string();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue