From a30dad4f5e2ce6a3ebd8a955fd1790dc53353bc8 Mon Sep 17 00:00:00 2001 From: Andreas Pohl Date: Wed, 16 Dec 2015 18:38:21 +0100 Subject: [PATCH 1/2] libnghttp2_asio: Added access to a requests remote endpoint --- src/asio_server_connection.h | 5 +++-- src/asio_server_http2_handler.cc | 8 +++++++- src/asio_server_http2_handler.h | 6 +++++- src/asio_server_request.cc | 4 ++++ src/asio_server_request_impl.cc | 8 ++++++++ src/asio_server_request_impl.h | 5 +++++ src/includes/nghttp2/asio_http2_server.h | 3 +++ 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/asio_server_connection.h b/src/asio_server_connection.h index 3aba7336..beac04f8 100644 --- a/src/asio_server_connection.h +++ b/src/asio_server_connection.h @@ -70,8 +70,9 @@ public: /// Start the first asynchronous operation for the connection. void start() { - handler_ = std::make_shared(socket_.get_io_service(), - [this]() { do_write(); }, mux_); + handler_ = std::make_shared( + socket_.get_io_service(), socket_.lowest_layer().remote_endpoint(), + [this]() { do_write(); }, mux_); if (handler_->start() != 0) { return; } diff --git a/src/asio_server_http2_handler.cc b/src/asio_server_http2_handler.cc index 47cd18c5..bf594f87 100644 --- a/src/asio_server_http2_handler.cc +++ b/src/asio_server_http2_handler.cc @@ -136,6 +136,9 @@ int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame, break; } + auto& req = strm->request().impl(); + req.remote_endpoint(handler->remote_endpoint()); + handler->call_on_request(*strm); if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { @@ -225,8 +228,9 @@ int on_frame_not_send_callback(nghttp2_session *session, } // namespace http2_handler::http2_handler(boost::asio::io_service &io_service, + boost::asio::ip::tcp::endpoint ep, connection_write writefun, serve_mux &mux) - : writefun_(writefun), mux_(mux), io_service_(io_service), + : writefun_(writefun), mux_(mux), io_service_(io_service), remote_ep_(ep), session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false), tstamp_cached_(time(nullptr)), formatted_date_(util::http_date(tstamp_cached_)) {} @@ -449,6 +453,8 @@ response *http2_handler::push_promise(boost::system::error_code &ec, boost::asio::io_service &http2_handler::io_service() { return io_service_; } +boost::asio::ip::tcp::endpoint http2_handler::remote_endpoint() { return remote_ep_; } + callback_guard::callback_guard(http2_handler &h) : handler(h) { handler.enter_callback(); } diff --git a/src/asio_server_http2_handler.h b/src/asio_server_http2_handler.h index 068d4527..19dcfb77 100644 --- a/src/asio_server_http2_handler.h +++ b/src/asio_server_http2_handler.h @@ -53,7 +53,8 @@ using connection_write = std::function; class http2_handler : public std::enable_shared_from_this { public: - http2_handler(boost::asio::io_service &io_service, connection_write writefun, + http2_handler(boost::asio::io_service &io_service, + boost::asio::ip::tcp::endpoint ep, connection_write writefun, serve_mux &mux); ~http2_handler(); @@ -89,6 +90,8 @@ public: boost::asio::io_service &io_service(); + boost::asio::ip::tcp::endpoint remote_endpoint(); + const std::string &http_date(); template @@ -152,6 +155,7 @@ private: connection_write writefun_; serve_mux &mux_; boost::asio::io_service &io_service_; + boost::asio::ip::tcp::endpoint remote_ep_; nghttp2_session *session_; const uint8_t *buf_; std::size_t buflen_; diff --git a/src/asio_server_request.cc b/src/asio_server_request.cc index 0241c489..8237ff76 100644 --- a/src/asio_server_request.cc +++ b/src/asio_server_request.cc @@ -50,6 +50,10 @@ void request::on_data(data_cb cb) const { request_impl &request::impl() const { return *impl_; } +boost::asio::ip::tcp::endpoint request::remote_endpoint() const { + return impl_->remote_endpoint(); +} + } // namespace server } // namespace asio_http2 } // namespace nghttp2 diff --git a/src/asio_server_request_impl.cc b/src/asio_server_request_impl.cc index 3a78208f..a7ab88c7 100644 --- a/src/asio_server_request_impl.cc +++ b/src/asio_server_request_impl.cc @@ -54,6 +54,14 @@ void request_impl::call_on_data(const uint8_t *data, std::size_t len) { } } +boost::asio::ip::tcp::endpoint request_impl::remote_endpoint() const { + return remote_ep_; +} + +void request_impl::remote_endpoint(boost::asio::ip::tcp::endpoint ep) { + remote_ep_ = ep; +} + } // namespace server } // namespace asio_http2 } // namespace nghttp2 diff --git a/src/asio_server_request_impl.h b/src/asio_server_request_impl.h index 8d4b0149..087b65ac 100644 --- a/src/asio_server_request_impl.h +++ b/src/asio_server_request_impl.h @@ -28,6 +28,7 @@ #include "nghttp2_config.h" #include +#include namespace nghttp2 { namespace asio_http2 { @@ -54,12 +55,16 @@ public: void stream(class stream *s); void call_on_data(const uint8_t *data, std::size_t len); + boost::asio::ip::tcp::endpoint remote_endpoint() const; + void remote_endpoint(boost::asio::ip::tcp::endpoint ep); + private: class stream *strm_; header_map header_; std::string method_; uri_ref uri_; data_cb on_data_cb_; + boost::asio::ip::tcp::endpoint remote_ep_; }; } // namespace server diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index 8756022e..f98c6c79 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -59,6 +59,9 @@ public: // Application must not call this directly. request_impl &impl() const; + // Returns the remote endpoint of the request + boost::asio::ip::tcp::endpoint remote_endpoint() const; + private: std::unique_ptr impl_; }; From 9f2d064d7c8d1db3969bfa77ddc44734bd3b34a4 Mon Sep 17 00:00:00 2001 From: Andreas Pohl Date: Sat, 19 Dec 2015 14:08:15 +0100 Subject: [PATCH 2/2] libnghttp2_asio: Optimized remote endpoint interface to const ref where possible --- src/asio_server_http2_handler.cc | 4 +++- src/asio_server_http2_handler.h | 2 +- src/asio_server_request.cc | 2 +- src/asio_server_request_impl.cc | 2 +- src/asio_server_request_impl.h | 2 +- src/includes/nghttp2/asio_http2_server.h | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/asio_server_http2_handler.cc b/src/asio_server_http2_handler.cc index bf594f87..fced5248 100644 --- a/src/asio_server_http2_handler.cc +++ b/src/asio_server_http2_handler.cc @@ -453,7 +453,9 @@ response *http2_handler::push_promise(boost::system::error_code &ec, boost::asio::io_service &http2_handler::io_service() { return io_service_; } -boost::asio::ip::tcp::endpoint http2_handler::remote_endpoint() { return remote_ep_; } +const boost::asio::ip::tcp::endpoint &http2_handler::remote_endpoint() { + return remote_ep_; +} callback_guard::callback_guard(http2_handler &h) : handler(h) { handler.enter_callback(); diff --git a/src/asio_server_http2_handler.h b/src/asio_server_http2_handler.h index 19dcfb77..a907e78f 100644 --- a/src/asio_server_http2_handler.h +++ b/src/asio_server_http2_handler.h @@ -90,7 +90,7 @@ public: boost::asio::io_service &io_service(); - boost::asio::ip::tcp::endpoint remote_endpoint(); + const boost::asio::ip::tcp::endpoint &remote_endpoint(); const std::string &http_date(); diff --git a/src/asio_server_request.cc b/src/asio_server_request.cc index 8237ff76..9612363b 100644 --- a/src/asio_server_request.cc +++ b/src/asio_server_request.cc @@ -50,7 +50,7 @@ void request::on_data(data_cb cb) const { request_impl &request::impl() const { return *impl_; } -boost::asio::ip::tcp::endpoint request::remote_endpoint() const { +const boost::asio::ip::tcp::endpoint &request::remote_endpoint() const { return impl_->remote_endpoint(); } diff --git a/src/asio_server_request_impl.cc b/src/asio_server_request_impl.cc index a7ab88c7..915b03e8 100644 --- a/src/asio_server_request_impl.cc +++ b/src/asio_server_request_impl.cc @@ -54,7 +54,7 @@ void request_impl::call_on_data(const uint8_t *data, std::size_t len) { } } -boost::asio::ip::tcp::endpoint request_impl::remote_endpoint() const { +const boost::asio::ip::tcp::endpoint &request_impl::remote_endpoint() const { return remote_ep_; } diff --git a/src/asio_server_request_impl.h b/src/asio_server_request_impl.h index 087b65ac..b4a37ff1 100644 --- a/src/asio_server_request_impl.h +++ b/src/asio_server_request_impl.h @@ -55,7 +55,7 @@ public: void stream(class stream *s); void call_on_data(const uint8_t *data, std::size_t len); - boost::asio::ip::tcp::endpoint remote_endpoint() const; + const boost::asio::ip::tcp::endpoint &remote_endpoint() const; void remote_endpoint(boost::asio::ip::tcp::endpoint ep); private: diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index f98c6c79..94bdc911 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -60,7 +60,7 @@ public: request_impl &impl() const; // Returns the remote endpoint of the request - boost::asio::ip::tcp::endpoint remote_endpoint() const; + const boost::asio::ip::tcp::endpoint &remote_endpoint() const; private: std::unique_ptr impl_;