Unverified Commit 2ef937c4 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #593 from lnihlen/address-in-request

adds address copying to request, and test
parents 399d04ee fe9a6f69
...@@ -178,6 +178,12 @@ public: ...@@ -178,6 +178,12 @@ public:
std::shared_ptr<Tcp::Peer> peer() const; std::shared_ptr<Tcp::Peer> peer() const;
#endif #endif
const Address& address() const;
void copyAddress(const Address& address) {
address_ = address;
}
private: private:
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME #ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
void associatePeer(const std::shared_ptr<Tcp::Peer>& peer) { void associatePeer(const std::shared_ptr<Tcp::Peer>& peer) {
...@@ -188,6 +194,7 @@ private: ...@@ -188,6 +194,7 @@ private:
} }
#endif #endif
Method method_; Method method_;
std::string resource_; std::string resource_;
Uri::Query query_; Uri::Query query_;
...@@ -195,6 +202,7 @@ private: ...@@ -195,6 +202,7 @@ private:
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME #ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
std::weak_ptr<Tcp::Peer> peer_; std::weak_ptr<Tcp::Peer> peer_;
#endif #endif
Address address_;
}; };
class Handler; class Handler;
......
...@@ -564,6 +564,11 @@ Request::cookies() const { ...@@ -564,6 +564,11 @@ Request::cookies() const {
return cookies_; return cookies_;
} }
const Address&
Request::address() const {
return address_;
}
#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME #ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
std::shared_ptr<Tcp::Peer> std::shared_ptr<Tcp::Peer>
Request::peer() const { Request::peer() const {
...@@ -792,6 +797,8 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer ...@@ -792,6 +797,8 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
#endif #endif
auto request = parser.request; auto request = parser.request;
request.copyAddress(peer->address());
auto connection = request.headers().tryGet<Header::Connection>(); auto connection = request.headers().tryGet<Header::Connection>();
if (connection) { if (connection) {
......
...@@ -78,6 +78,20 @@ private: ...@@ -78,6 +78,20 @@ private:
std::string fileName_; std::string fileName_;
}; };
struct AddressEchoHandler : public Http::Handler
{
HTTP_PROTOTYPE(AddressEchoHandler)
AddressEchoHandler() { }
void onRequest(const Http::Request& request, Http::ResponseWriter writer) override
{
std::string requestAddress = request.address().host();
writer.send(Http::Code::Ok, requestAddress);
std::cout << "[server] Sent: " << requestAddress << std::endl;
}
};
int clientLogicFunc(int response_size, int clientLogicFunc(int response_size,
const std::string& server_page, const std::string& server_page,
int timeout_seconds, int timeout_seconds,
...@@ -309,3 +323,43 @@ TEST(http_server_test, server_with_static_file) ...@@ -309,3 +323,43 @@ TEST(http_server_test, server_with_static_file)
ASSERT_EQ(data, resultData); ASSERT_EQ(data, resultData);
} }
TEST(http_server_test, server_request_copies_address)
{
const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address);
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<AddressEchoHandler>());
server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client;
client.init();
auto rb = client.get(server_address);
auto response = rb.send();
std::string resultData;
response.then([&resultData](Http::Response resp)
{
std::cout << "Response code is " << resp.code() << std::endl;
if (resp.code() == Http::Code::Ok)
{
resultData = resp.body();
}
},
Async::Throw);
const int WAIT_TIME = 2;
Async::Barrier<Http::Response> barrier(response);
barrier.wait_for(std::chrono::seconds(WAIT_TIME));
client.shutdown();
server.shutdown();
ASSERT_EQ("127.0.0.1", resultData);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment