From 5f34b5db5ee0e24cccfb2daa5132827bf9dc02fe Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 27 Jun 2015 10:43:17 +0200 Subject: [PATCH] Added Request::remote_endpoint_address and Request::remote_endpoint_port that are read when Request is created. --- http_examples.cpp | 2 +- https_examples.cpp | 2 +- server_http.hpp | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/http_examples.cpp b/http_examples.cpp index f6d1173..c82be5b 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -57,7 +57,7 @@ int main() { //Responds with request-information server.resource["^/info$"]["GET"]=[](HttpServer::Response& response, shared_ptr request) { stringstream content_stream; - content_stream << "

Request from " << request->endpoint.address().to_string() << " (" << request->endpoint.port() << ")

"; + content_stream << "

Request from " << request->remote_endpoint_address.to_string() << " (" << request->remote_endpoint_port << ")

"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; for(auto& header: request->header) { content_stream << header.first << ": " << header.second << "
"; diff --git a/https_examples.cpp b/https_examples.cpp index 281effb..a6dd353 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -57,7 +57,7 @@ int main() { //Responds with request-information server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr request) { stringstream content_stream; - content_stream << "

Request from " << request->endpoint.address().to_string() << " (" << request->endpoint.port() << ")

"; + content_stream << "

Request from " << request->remote_endpoint_address.to_string() << " (" << request->remote_endpoint_port << ")

"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; for(auto& header: request->header) { content_stream << header.first << ": " << header.second << "
"; diff --git a/server_http.hpp b/server_http.hpp index d91907e..0e5b083 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace SimpleWeb { template @@ -124,10 +125,23 @@ namespace SimpleWeb { std::smatch path_match; + boost::asio::ip::address remote_endpoint_address; + unsigned short remote_endpoint_port; + private: Request(): content(&streambuf) {} boost::asio::streambuf streambuf; + + void read_remote_endpoint_data(socket_type& socket) { + try { + remote_endpoint_address=socket.lowest_layer().remote_endpoint().address(); + remote_endpoint_port=socket.lowest_layer().remote_endpoint().port(); + } + catch(const std::exception& e) { + std::cerr << e.what() << std::endl; + } + } }; std::unordered_map request(new Request()); + request->read_remote_endpoint_data(*socket); //Set timeout on the following boost::asio::async-read or write function std::shared_ptr timer;