From dd741a58aed984c6d80e70ef2353f56be5d7fcb9 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 3 Mar 2015 23:37:08 +0900 Subject: [PATCH] Use header_map instead of wrapping it --- examples/asio-cl.cc | 4 +-- src/asio_client_request.cc | 2 +- src/asio_client_request_impl.cc | 6 ++--- src/asio_client_request_impl.h | 8 +++--- src/asio_client_response.cc | 2 +- src/asio_client_response_impl.cc | 4 +-- src/asio_client_response_impl.h | 6 ++--- src/asio_client_session.cc | 6 ++--- src/asio_client_session_impl.cc | 16 ++++++------ src/asio_client_session_impl.h | 2 +- src/asio_common.cc | 41 ------------------------------- src/includes/nghttp2/asio_http2.h | 35 ++++---------------------- 12 files changed, 33 insertions(+), 99 deletions(-) diff --git a/examples/asio-cl.cc b/examples/asio-cl.cc index 84d75107..f87b7948 100644 --- a/examples/asio-cl.cc +++ b/examples/asio-cl.cc @@ -36,8 +36,8 @@ using boost::asio::ip::tcp; using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::client; -void print_header(const http_header &h) { - for (auto &kv : h.items()) { +void print_header(const header_map &h) { + for (auto &kv : h) { std::cerr << kv.first << ": " << kv.second.value << "\n"; } std::cerr << std::endl; diff --git a/src/asio_client_request.cc b/src/asio_client_request.cc index 2ad6eed8..b1194940 100644 --- a/src/asio_client_request.cc +++ b/src/asio_client_request.cc @@ -54,7 +54,7 @@ const std::string &request::authority() const { return impl_->authority(); } const std::string &request::host() const { return impl_->host(); } -const http_header &request::header() const { return impl_->header(); } +const header_map &request::header() const { return impl_->header(); } request_impl &request::impl() { return *impl_; } diff --git a/src/asio_client_request_impl.cc b/src/asio_client_request_impl.cc index ed33abd7..47eb943f 100644 --- a/src/asio_client_request_impl.cc +++ b/src/asio_client_request_impl.cc @@ -68,11 +68,11 @@ read_cb::result_type request_impl::call_on_read(uint8_t *buf, std::size_t len) { return read_cb::result_type{}; } -void request_impl::header(http_header h) { header_ = std::move(h); } +void request_impl::header(header_map h) { header_ = std::move(h); } -http_header &request_impl::header() { return header_; } +header_map &request_impl::header() { return header_; } -const http_header &request_impl::header() const { return header_; } +const header_map &request_impl::header() const { return header_; } void request_impl::stream(class stream *strm) { strm_ = strm; } diff --git a/src/asio_client_request_impl.h b/src/asio_client_request_impl.h index 81dd8e82..be8cc220 100644 --- a/src/asio_client_request_impl.h +++ b/src/asio_client_request_impl.h @@ -57,9 +57,9 @@ public: void on_read(read_cb cb); read_cb::result_type call_on_read(uint8_t *buf, std::size_t len); - void header(http_header h); - http_header &header(); - const http_header &header() const; + void header(header_map h); + header_map &header(); + const header_map &header() const; void stream(class stream *strm); @@ -79,7 +79,7 @@ public: const std::string &host() const; private: - http_header header_; + header_map header_; response_cb response_cb_; request_cb push_request_cb_; close_cb close_cb_; diff --git a/src/asio_client_response.cc b/src/asio_client_response.cc index 87b5ba5e..6c66ef16 100644 --- a/src/asio_client_response.cc +++ b/src/asio_client_response.cc @@ -42,7 +42,7 @@ int response::status_code() const { return impl_->status_code(); } int64_t response::content_length() const { return impl_->content_length(); } -const http_header &response::header() const { return impl_->header(); } +const header_map &response::header() const { return impl_->header(); } response_impl &response::impl() { return *impl_; } diff --git a/src/asio_client_response_impl.cc b/src/asio_client_response_impl.cc index 9379b892..fce25a4f 100644 --- a/src/asio_client_response_impl.cc +++ b/src/asio_client_response_impl.cc @@ -48,9 +48,9 @@ void response_impl::content_length(int64_t n) { content_length_ = n; } int64_t response_impl::content_length() const { return content_length_; } -http_header &response_impl::header() { return header_; } +header_map &response_impl::header() { return header_; } -const http_header &response_impl::header() const { return header_; } +const header_map &response_impl::header() const { return header_; } } // namespace client } // namespace asio_http2 diff --git a/src/asio_client_response_impl.h b/src/asio_client_response_impl.h index 9c2ae2a5..6d22fda9 100644 --- a/src/asio_client_response_impl.h +++ b/src/asio_client_response_impl.h @@ -50,13 +50,13 @@ public: void content_length(int64_t n); int64_t content_length() const; - http_header &header(); - const http_header &header() const; + header_map &header(); + const header_map &header() const; private: data_cb data_cb_; - http_header header_; + header_map header_; int64_t content_length_; int status_code_; diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index c49f8cbb..27fdc002 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -56,20 +56,20 @@ void session::shutdown() { impl_->shutdown(); } request *session::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - http_header h) { + header_map h) { return impl_->submit(ec, method, uri, read_cb(), std::move(h)); } request *session::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - std::string data, http_header h) { + std::string data, header_map h) { return impl_->submit(ec, method, uri, string_reader(std::move(data)), std::move(h)); } request *session::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - read_cb cb, http_header h) { + read_cb cb, header_map h) { return impl_->submit(ec, method, uri, std::move(cb), std::move(h)); } diff --git a/src/asio_client_session_impl.cc b/src/asio_client_session_impl.cc index 6d146c3e..4b660a4a 100644 --- a/src/asio_client_session_impl.cc +++ b/src/asio_client_session_impl.cc @@ -134,9 +134,9 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, res.content_length(util::parse_uint(value, valuelen)); } - res.header().add(std::string(name, name + namelen), - std::string(value, value + valuelen), - flags & NGHTTP2_NV_FLAG_NO_INDEX); + res.header().emplace(std::string(name, name + namelen), + header_value(std::string(value, value + valuelen), + flags & NGHTTP2_NV_FLAG_NO_INDEX)); } break; } @@ -167,9 +167,9 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, req.host(std::string(value, value + valuelen)); // fall through default: - req.header().add(std::string(name, name + namelen), - std::string(value, value + valuelen), - flags & NGHTTP2_NV_FLAG_NO_INDEX); + req.header().emplace(std::string(name, name + namelen), + header_value(std::string(value, value + valuelen), + flags & NGHTTP2_NV_FLAG_NO_INDEX)); } break; @@ -345,7 +345,7 @@ std::unique_ptr session_impl::create_stream() { request *session_impl::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - read_cb cb, http_header h) { + read_cb cb, header_map h) { ec.clear(); auto nva = std::vector(); @@ -354,7 +354,7 @@ request *session_impl::submit(boost::system::error_code &ec, nva.push_back(http2::make_nv_ll(":scheme", "https")); nva.push_back(http2::make_nv_ll(":path", "/")); nva.push_back(http2::make_nv_ll(":authority", "localhost:3000")); - for (auto &kv : h.items()) { + for (auto &kv : h) { nva.push_back( http2::make_nv(kv.first, kv.second.value, kv.second.sensitive)); } diff --git a/src/asio_client_session_impl.h b/src/asio_client_session_impl.h index e4a3e163..9d139a28 100644 --- a/src/asio_client_session_impl.h +++ b/src/asio_client_session_impl.h @@ -65,7 +65,7 @@ public: stream *find_stream(int32_t stream_id); request *submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, read_cb cb, http_header h); + const std::string &uri, read_cb cb, header_map h); virtual tcp::socket &socket() = 0; virtual void read_socket(std::function< diff --git a/src/asio_common.cc b/src/asio_common.cc index ee77a630..34efefa5 100644 --- a/src/asio_common.cc +++ b/src/asio_common.cc @@ -57,46 +57,5 @@ read_cb string_reader(std::string data) { }; } -http_header::http_header() {} - -http_header::http_header( - std::initializer_list> ilist) { - for (auto &kv : ilist) { - auto name = kv.first; - util::inp_strlower(name); - items_.emplace(std::move(name), kv.second); - } -} - -http_header &http_header:: -operator=(std::initializer_list> ilist) { - items_.clear(); - for (auto &kv : ilist) { - auto name = kv.first; - util::inp_strlower(name); - items_.emplace(std::move(name), kv.second); - } - return *this; -} - -const header_map &http_header::items() const { return items_; } - -void http_header::add(std::string name, std::string value, bool sensitive) { - util::inp_strlower(name); - items_.emplace(name, header_value(value, sensitive)); -} - -const header_value *http_header::get(const std::string &name) const { - auto it = items_.find(name); - if (it == std::end(items_)) { - return nullptr; - } - return &(*it).second; -} - -std::size_t http_header::size() const { return items_.size(); } - -bool http_header::empty() const { return items_.empty(); } - } // namespace asio_http2 } // namespace nghttp2 diff --git a/src/includes/nghttp2/asio_http2.h b/src/includes/nghttp2/asio_http2.h index 10155cef..bf9e8573 100644 --- a/src/includes/nghttp2/asio_http2.h +++ b/src/includes/nghttp2/asio_http2.h @@ -72,31 +72,6 @@ struct header_value { using header_map = std::multimap; -class http_header { -public: - http_header(); - http_header(const http_header &other) = default; - http_header(http_header &&other) = default; - http_header( - std::initializer_list> ilist); - - http_header &operator=(const http_header &other) = default; - http_header &operator=(http_header &&other) = default; - http_header & - operator=(std::initializer_list> ilist); - - const header_map &items() const; - - void add(std::string name, std::string value, bool sensitive); - const header_value *get(const std::string &name) const; - - std::size_t size() const; - bool empty() const; - -private: - header_map items_; -}; - const boost::system::error_category &nghttp2_category() noexcept; typedef std::function data_cb; @@ -318,7 +293,7 @@ public: int64_t content_length() const; - const http_header &header() const; + const header_map &header() const; response_impl &impl(); @@ -350,7 +325,7 @@ public: const std::string &authority() const; const std::string &host() const; - const http_header &header() const; + const header_map &header() const; request_impl &impl(); @@ -375,11 +350,11 @@ public: void shutdown(); request *submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, http_header h = {}); + const std::string &uri, header_map h = {}); request *submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, std::string data, http_header h = {}); + const std::string &uri, std::string data, header_map h = {}); request *submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, read_cb cb, http_header h = {}); + const std::string &uri, read_cb cb, header_map h = {}); private: std::unique_ptr impl_;