From 2fa28e790dde59de61c809c7f7f2864d7c34bfa4 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 4 Mar 2015 01:34:00 +0900 Subject: [PATCH] asio: Separate client API to asio_http2_client.h --- examples/asio-cl.cc | 5 +- src/asio_client_request.cc | 4 +- src/asio_client_request_impl.h | 2 +- src/asio_client_response.cc | 4 +- src/asio_client_response_impl.h | 2 +- src/asio_client_session.cc | 2 +- src/asio_client_session_impl.h | 8 +- src/asio_client_session_tcp_impl.h | 4 +- src/asio_client_session_tls_impl.h | 4 +- src/asio_client_stream.h | 2 +- src/asio_client_tls_context.h | 2 +- src/includes/Makefile.am | 2 +- src/includes/nghttp2/asio_http2.h | 94 ---------------- src/includes/nghttp2/asio_http2_client.h | 133 +++++++++++++++++++++++ 14 files changed, 149 insertions(+), 119 deletions(-) create mode 100644 src/includes/nghttp2/asio_http2_client.h diff --git a/examples/asio-cl.cc b/examples/asio-cl.cc index 8a7c6627..328ef40c 100644 --- a/examples/asio-cl.cc +++ b/examples/asio-cl.cc @@ -26,10 +26,7 @@ #include #include -#include - -#include -#include +#include using boost::asio::ip::tcp; diff --git a/src/asio_client_request.cc b/src/asio_client_request.cc index 8997a23b..dd92422f 100644 --- a/src/asio_client_request.cc +++ b/src/asio_client_request.cc @@ -22,7 +22,9 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include "nghttp2_config.h" + +#include #include "asio_client_request_impl.h" diff --git a/src/asio_client_request_impl.h b/src/asio_client_request_impl.h index e3769a16..0ad938da 100644 --- a/src/asio_client_request_impl.h +++ b/src/asio_client_request_impl.h @@ -27,7 +27,7 @@ #include "nghttp2_config.h" -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_response.cc b/src/asio_client_response.cc index e38355a0..b27039a9 100644 --- a/src/asio_client_response.cc +++ b/src/asio_client_response.cc @@ -22,7 +22,9 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include "nghttp2_config.h" + +#include #include "asio_client_response_impl.h" diff --git a/src/asio_client_response_impl.h b/src/asio_client_response_impl.h index 6d22fda9..e2c22862 100644 --- a/src/asio_client_response_impl.h +++ b/src/asio_client_response_impl.h @@ -27,7 +27,7 @@ #include "nghttp2_config.h" -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index e5956a8d..ed489372 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -24,7 +24,7 @@ */ #include "nghttp2_config.h" -#include +#include #include "asio_client_session_tcp_impl.h" #include "asio_client_session_tls_impl.h" diff --git a/src/asio_client_session_impl.h b/src/asio_client_session_impl.h index 3746cf93..7a84a2e3 100644 --- a/src/asio_client_session_impl.h +++ b/src/asio_client_session_impl.h @@ -27,13 +27,7 @@ #include "nghttp2_config.h" -#include - -#include - -#include - -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_session_tcp_impl.h b/src/asio_client_session_tcp_impl.h index f4096643..6138828b 100644 --- a/src/asio_client_session_tcp_impl.h +++ b/src/asio_client_session_tcp_impl.h @@ -27,9 +27,7 @@ #include "asio_client_session_impl.h" -#include - -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_session_tls_impl.h b/src/asio_client_session_tls_impl.h index 296ebd9d..484e3d45 100644 --- a/src/asio_client_session_tls_impl.h +++ b/src/asio_client_session_tls_impl.h @@ -27,9 +27,7 @@ #include "asio_client_session_impl.h" -#include - -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_stream.h b/src/asio_client_stream.h index 3c25f211..51fe9a8f 100644 --- a/src/asio_client_stream.h +++ b/src/asio_client_stream.h @@ -27,7 +27,7 @@ #include "nghttp2_config.h" -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_client_tls_context.h b/src/asio_client_tls_context.h index 7b0e97de..6287dab4 100644 --- a/src/asio_client_tls_context.h +++ b/src/asio_client_tls_context.h @@ -27,6 +27,6 @@ #include "nghttp2_config.h" -#include +#include #endif // ASIO_CLIENT_TLS_CONTEXT_H diff --git a/src/includes/Makefile.am b/src/includes/Makefile.am index e642d5f3..f662b4ca 100644 --- a/src/includes/Makefile.am +++ b/src/includes/Makefile.am @@ -20,4 +20,4 @@ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -nobase_include_HEADERS = nghttp2/asio_http2.h +nobase_include_HEADERS = nghttp2/asio_http2.h nghttp2/asio_http2_client.h diff --git a/src/includes/nghttp2/asio_http2.h b/src/includes/nghttp2/asio_http2.h index 65f17f50..3d3b4959 100644 --- a/src/includes/nghttp2/asio_http2.h +++ b/src/includes/nghttp2/asio_http2.h @@ -34,7 +34,6 @@ #include #include -#include #include @@ -287,99 +286,6 @@ std::string percent_decode(const std::string &s); // Returns HTTP date representation of current posix time |t|. std::string http_date(int64_t t); -namespace client { - -class response_impl; - -class response { -public: - response(); - ~response(); - - void on_data(data_cb cb) const; - - int status_code() const; - - int64_t content_length() const; - - const header_map &header() const; - - response_impl &impl(); - -private: - std::unique_ptr impl_; -}; - -class request; - -using response_cb = std::function; -using request_cb = std::function; -using connect_cb = - std::function; - -class request_impl; - -class request { -public: - request(); - ~request(); - - void on_response(response_cb cb) const; - void on_push(request_cb cb) const; - void on_close(close_cb cb) const; - - void cancel() const; - - const std::string &method() const; - - const uri_ref &uri() const; - - const header_map &header() const; - - request_impl &impl(); - -private: - std::unique_ptr impl_; -}; - -class session_impl; - -class session { -public: - session(boost::asio::io_service &io_service, const std::string &host, - const std::string &service); - session(boost::asio::io_service &io_service, - boost::asio::ssl::context &tls_context, const std::string &host, - const std::string &service); - ~session(); - - void on_connect(connect_cb cb); - void on_error(error_cb cb); - - void shutdown(); - - boost::asio::io_service &io_service(); - - const request *submit(boost::system::error_code &ec, - const std::string &method, const std::string &uri, - header_map h = {}); - const request *submit(boost::system::error_code &ec, - const std::string &method, const std::string &uri, - std::string data, header_map h = {}); - const request *submit(boost::system::error_code &ec, - const std::string &method, const std::string &uri, - read_cb cb, header_map h = {}); - -private: - std::unique_ptr impl_; -}; - -// configure |tls_ctx| for client use. Currently, we just set NPN -// callback for HTTP/2. -void configure_tls_context(boost::asio::ssl::context &tls_ctx); - -} // namespace client - } // namespace asio_http2 } // namespace nghttp2 diff --git a/src/includes/nghttp2/asio_http2_client.h b/src/includes/nghttp2/asio_http2_client.h new file mode 100644 index 00000000..17bbe17a --- /dev/null +++ b/src/includes/nghttp2/asio_http2_client.h @@ -0,0 +1,133 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2015 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef ASIO_HTTP2_CLIENT_H +#define ASIO_HTTP2_CLIENT_H + +#include + +#include + +namespace nghttp2 { + +namespace asio_http2 { + +namespace client { + +class response_impl; + +class response { +public: + response(); + ~response(); + + void on_data(data_cb cb) const; + + int status_code() const; + + int64_t content_length() const; + + const header_map &header() const; + + response_impl &impl(); + +private: + std::unique_ptr impl_; +}; + +class request; + +using response_cb = std::function; +using request_cb = std::function; +using connect_cb = + std::function; + +class request_impl; + +class request { +public: + request(); + ~request(); + + void on_response(response_cb cb) const; + void on_push(request_cb cb) const; + void on_close(close_cb cb) const; + + void cancel() const; + + const std::string &method() const; + + const uri_ref &uri() const; + + const header_map &header() const; + + request_impl &impl(); + +private: + std::unique_ptr impl_; +}; + +class session_impl; + +class session { +public: + session(boost::asio::io_service &io_service, const std::string &host, + const std::string &service); + session(boost::asio::io_service &io_service, + boost::asio::ssl::context &tls_context, const std::string &host, + const std::string &service); + ~session(); + + void on_connect(connect_cb cb); + void on_error(error_cb cb); + + void shutdown(); + + boost::asio::io_service &io_service(); + + const request *submit(boost::system::error_code &ec, + const std::string &method, const std::string &uri, + header_map h = {}); + const request *submit(boost::system::error_code &ec, + const std::string &method, const std::string &uri, + std::string data, header_map h = {}); + const request *submit(boost::system::error_code &ec, + const std::string &method, const std::string &uri, + read_cb cb, header_map h = {}); + +private: + std::unique_ptr impl_; +}; + +// configure |tls_ctx| for client use. Currently, we just set NPN +// callback for HTTP/2. +void configure_tls_context(boost::asio::ssl::context &tls_ctx); + +} // namespace client + +} // namespace asio_http2 + +} // namespace nghttp2 + +#endif // ASIO_HTTP2_CLIENT_H