Added Request::remote_endpoint_address and Request::remote_endpoint_port that are read when Request is created.

This commit is contained in:
eidheim 2015-06-27 10:43:17 +02:00
commit 5f34b5db5e
3 changed files with 17 additions and 2 deletions

View file

@ -57,7 +57,7 @@ int main() {
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/info$"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request from " << request->endpoint.address().to_string() << " (" << request->endpoint.port() << ")</h1>"; content_stream << "<h1>Request from " << request->remote_endpoint_address.to_string() << " (" << request->remote_endpoint_port << ")</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
for(auto& header: request->header) { for(auto& header: request->header) {
content_stream << header.first << ": " << header.second << "<br>"; content_stream << header.first << ": " << header.second << "<br>";

View file

@ -57,7 +57,7 @@ int main() {
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/info$"]["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
stringstream content_stream; stringstream content_stream;
content_stream << "<h1>Request from " << request->endpoint.address().to_string() << " (" << request->endpoint.port() << ")</h1>"; content_stream << "<h1>Request from " << request->remote_endpoint_address.to_string() << " (" << request->remote_endpoint_port << ")</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
for(auto& header: request->header) { for(auto& header: request->header) {
content_stream << header.first << ": " << header.second << "<br>"; content_stream << header.first << ": " << header.second << "<br>";

View file

@ -8,6 +8,7 @@
#include <unordered_map> #include <unordered_map>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <iostream>
namespace SimpleWeb { namespace SimpleWeb {
template <class socket_type> template <class socket_type>
@ -124,10 +125,23 @@ namespace SimpleWeb {
std::smatch path_match; std::smatch path_match;
boost::asio::ip::address remote_endpoint_address;
unsigned short remote_endpoint_port;
private: private:
Request(): content(&streambuf) {} Request(): content(&streambuf) {}
boost::asio::streambuf 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<std::string, std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string,
@ -231,6 +245,7 @@ namespace SimpleWeb {
//Create new streambuf (Request::streambuf) for async_read_until() //Create new streambuf (Request::streambuf) for async_read_until()
//shared_ptr is used to pass temporary objects to the asynchronous functions //shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Request> request(new Request()); std::shared_ptr<Request> request(new Request());
request->read_remote_endpoint_data(*socket);
//Set timeout on the following boost::asio::async-read or write function //Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer; std::shared_ptr<boost::asio::deadline_timer> timer;