Use header_map instead of wrapping it

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-03 23:37:08 +09:00
parent 26304546c4
commit dd741a58ae
12 changed files with 33 additions and 99 deletions

View File

@ -36,8 +36,8 @@ using boost::asio::ip::tcp;
using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::client; using namespace nghttp2::asio_http2::client;
void print_header(const http_header &h) { void print_header(const header_map &h) {
for (auto &kv : h.items()) { for (auto &kv : h) {
std::cerr << kv.first << ": " << kv.second.value << "\n"; std::cerr << kv.first << ": " << kv.second.value << "\n";
} }
std::cerr << std::endl; std::cerr << std::endl;

View File

@ -54,7 +54,7 @@ const std::string &request::authority() const { return impl_->authority(); }
const std::string &request::host() const { return impl_->host(); } 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_; } request_impl &request::impl() { return *impl_; }

View File

@ -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{}; 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; } void request_impl::stream(class stream *strm) { strm_ = strm; }

View File

@ -57,9 +57,9 @@ public:
void on_read(read_cb cb); void on_read(read_cb cb);
read_cb::result_type call_on_read(uint8_t *buf, std::size_t len); read_cb::result_type call_on_read(uint8_t *buf, std::size_t len);
void header(http_header h); void header(header_map h);
http_header &header(); header_map &header();
const http_header &header() const; const header_map &header() const;
void stream(class stream *strm); void stream(class stream *strm);
@ -79,7 +79,7 @@ public:
const std::string &host() const; const std::string &host() const;
private: private:
http_header header_; header_map header_;
response_cb response_cb_; response_cb response_cb_;
request_cb push_request_cb_; request_cb push_request_cb_;
close_cb close_cb_; close_cb close_cb_;

View File

@ -42,7 +42,7 @@ int response::status_code() const { return impl_->status_code(); }
int64_t response::content_length() const { return impl_->content_length(); } 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_; } response_impl &response::impl() { return *impl_; }

View File

@ -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_; } 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 client
} // namespace asio_http2 } // namespace asio_http2

View File

@ -50,13 +50,13 @@ public:
void content_length(int64_t n); void content_length(int64_t n);
int64_t content_length() const; int64_t content_length() const;
http_header &header(); header_map &header();
const http_header &header() const; const header_map &header() const;
private: private:
data_cb data_cb_; data_cb data_cb_;
http_header header_; header_map header_;
int64_t content_length_; int64_t content_length_;
int status_code_; int status_code_;

View File

@ -56,20 +56,20 @@ void session::shutdown() { impl_->shutdown(); }
request *session::submit(boost::system::error_code &ec, request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, 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)); return impl_->submit(ec, method, uri, read_cb(), std::move(h));
} }
request *session::submit(boost::system::error_code &ec, request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, 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)), return impl_->submit(ec, method, uri, string_reader(std::move(data)),
std::move(h)); std::move(h));
} }
request *session::submit(boost::system::error_code &ec, request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, 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)); return impl_->submit(ec, method, uri, std::move(cb), std::move(h));
} }

View File

@ -134,9 +134,9 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
res.content_length(util::parse_uint(value, valuelen)); res.content_length(util::parse_uint(value, valuelen));
} }
res.header().add(std::string(name, name + namelen), res.header().emplace(std::string(name, name + namelen),
std::string(value, value + valuelen), header_value(std::string(value, value + valuelen),
flags & NGHTTP2_NV_FLAG_NO_INDEX); flags & NGHTTP2_NV_FLAG_NO_INDEX));
} }
break; break;
} }
@ -167,9 +167,9 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
req.host(std::string(value, value + valuelen)); req.host(std::string(value, value + valuelen));
// fall through // fall through
default: default:
req.header().add(std::string(name, name + namelen), req.header().emplace(std::string(name, name + namelen),
std::string(value, value + valuelen), header_value(std::string(value, value + valuelen),
flags & NGHTTP2_NV_FLAG_NO_INDEX); flags & NGHTTP2_NV_FLAG_NO_INDEX));
} }
break; break;
@ -345,7 +345,7 @@ std::unique_ptr<stream> session_impl::create_stream() {
request *session_impl::submit(boost::system::error_code &ec, request *session_impl::submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, const std::string &method, const std::string &uri,
read_cb cb, http_header h) { read_cb cb, header_map h) {
ec.clear(); ec.clear();
auto nva = std::vector<nghttp2_nv>(); auto nva = std::vector<nghttp2_nv>();
@ -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(":scheme", "https"));
nva.push_back(http2::make_nv_ll(":path", "/")); nva.push_back(http2::make_nv_ll(":path", "/"));
nva.push_back(http2::make_nv_ll(":authority", "localhost:3000")); nva.push_back(http2::make_nv_ll(":authority", "localhost:3000"));
for (auto &kv : h.items()) { for (auto &kv : h) {
nva.push_back( nva.push_back(
http2::make_nv(kv.first, kv.second.value, kv.second.sensitive)); http2::make_nv(kv.first, kv.second.value, kv.second.sensitive));
} }

View File

@ -65,7 +65,7 @@ public:
stream *find_stream(int32_t stream_id); stream *find_stream(int32_t stream_id);
request *submit(boost::system::error_code &ec, const std::string &method, 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 tcp::socket &socket() = 0;
virtual void read_socket(std::function< virtual void read_socket(std::function<

View File

@ -57,46 +57,5 @@ read_cb string_reader(std::string data) {
}; };
} }
http_header::http_header() {}
http_header::http_header(
std::initializer_list<std::pair<std::string, header_value>> 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<std::pair<std::string, header_value>> 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 asio_http2
} // namespace nghttp2 } // namespace nghttp2

View File

@ -72,31 +72,6 @@ struct header_value {
using header_map = std::multimap<std::string, header_value>; using header_map = std::multimap<std::string, header_value>;
class http_header {
public:
http_header();
http_header(const http_header &other) = default;
http_header(http_header &&other) = default;
http_header(
std::initializer_list<std::pair<std::string, header_value>> ilist);
http_header &operator=(const http_header &other) = default;
http_header &operator=(http_header &&other) = default;
http_header &
operator=(std::initializer_list<std::pair<std::string, header_value>> 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; const boost::system::error_category &nghttp2_category() noexcept;
typedef std::function<void(const uint8_t *, std::size_t)> data_cb; typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
@ -318,7 +293,7 @@ public:
int64_t content_length() const; int64_t content_length() const;
const http_header &header() const; const header_map &header() const;
response_impl &impl(); response_impl &impl();
@ -350,7 +325,7 @@ public:
const std::string &authority() const; const std::string &authority() const;
const std::string &host() const; const std::string &host() const;
const http_header &header() const; const header_map &header() const;
request_impl &impl(); request_impl &impl();
@ -375,11 +350,11 @@ public:
void shutdown(); void shutdown();
request *submit(boost::system::error_code &ec, const std::string &method, 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, 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, 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: private:
std::unique_ptr<session_impl> impl_; std::unique_ptr<session_impl> impl_;