From 3ff148811b54380f56a14d97d69cac2f2b722471 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 20 Feb 2016 20:53:19 +0900 Subject: [PATCH] nghttpx: Use StringRef for add_hedeader --- src/shrpx_downstream.cc | 24 ++++++++---------------- src/shrpx_downstream.h | 8 ++++---- src/shrpx_http2_session.cc | 7 ++++--- src/shrpx_http2_upstream.cc | 7 ++++--- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index bd76592e..0d3b2618 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -338,14 +338,10 @@ void add_header(bool &key_prev, size_t &sum, Headers &headers, std::string name, } // namespace namespace { -void add_header(size_t &sum, Headers &headers, const uint8_t *name, - size_t namelen, const uint8_t *value, size_t valuelen, - bool no_index, int16_t token) { - sum += namelen + valuelen; - headers.emplace_back( - std::string(reinterpret_cast(name), namelen), - std::string(reinterpret_cast(value), valuelen), no_index, - token); +void add_header(size_t &sum, Headers &headers, const StringRef &name, + const StringRef &value, bool no_index, int16_t token) { + sum += name.size() + value.size(); + headers.emplace_back(name.str(), value.str(), no_index, token); } } // namespace @@ -430,11 +426,9 @@ void FieldStore::add_header(std::string name, std::string value, headers_.emplace_back(std::move(name), std::move(value), false, token); } -void FieldStore::add_header(const uint8_t *name, size_t namelen, - const uint8_t *value, size_t valuelen, +void FieldStore::add_header(const StringRef &name, const StringRef &value, bool no_index, int16_t token) { - shrpx::add_header(buffer_size_, headers_, name, namelen, value, valuelen, - no_index, token); + shrpx::add_header(buffer_size_, headers_, name, value, no_index, token); } void FieldStore::append_last_header_key(const char *data, size_t len) { @@ -449,13 +443,11 @@ void FieldStore::append_last_header_value(const char *data, size_t len) { void FieldStore::clear_headers() { headers_.clear(); } -void FieldStore::add_trailer(const uint8_t *name, size_t namelen, - const uint8_t *value, size_t valuelen, +void FieldStore::add_trailer(const StringRef &name, const StringRef &value, bool no_index, int16_t token) { // Header size limit should be applied to all header and trailer // fields combined. - shrpx::add_header(buffer_size_, trailers_, name, namelen, value, valuelen, - no_index, token); + shrpx::add_header(buffer_size_, trailers_, name, value, no_index, token); } void FieldStore::add_trailer_lower(const StringRef &name, diff --git a/src/shrpx_downstream.h b/src/shrpx_downstream.h index 03286b83..53778e49 100644 --- a/src/shrpx_downstream.h +++ b/src/shrpx_downstream.h @@ -81,8 +81,8 @@ public: void add_header_lower(const StringRef &name, const StringRef &value); void add_header(std::string name, std::string value, int16_t token); - void add_header(const uint8_t *name, size_t namelen, const uint8_t *value, - size_t valuelen, bool no_index, int16_t token); + void add_header(const StringRef &name, const StringRef &value, bool no_index, + int16_t token); void append_last_header_key(const char *data, size_t len); void append_last_header_value(const char *data, size_t len); @@ -97,8 +97,8 @@ public: // Empties headers. void clear_headers(); - void add_trailer(const uint8_t *name, size_t namelen, const uint8_t *value, - size_t valuelen, bool no_index, int16_t token); + void add_trailer(const StringRef &name, const StringRef &value, bool no_index, + int16_t token); void add_trailer_lower(const StringRef &name, const StringRef &value); void append_last_trailer_key(const char *data, size_t len); diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 24283172..2c96a579 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -739,12 +739,12 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, if (trailer) { // just store header fields for trailer part - resp.fs.add_trailer(name, namelen, value, valuelen, + resp.fs.add_trailer(StringRef{name, namelen}, StringRef{value, valuelen}, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); return 0; } - resp.fs.add_header(name, namelen, value, valuelen, + resp.fs.add_header(StringRef{name, namelen}, StringRef{value, valuelen}, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); return 0; } @@ -778,7 +778,8 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, } auto token = http2::lookup_token(name, namelen); - promised_req.fs.add_header(name, namelen, value, valuelen, + promised_req.fs.add_header(StringRef{name, namelen}, + StringRef{value, valuelen}, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); return 0; } diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index f13ea759..4506081c 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -205,12 +205,12 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, if (frame->headers.cat == NGHTTP2_HCAT_HEADERS) { // just store header fields for trailer part - req.fs.add_trailer(name, namelen, value, valuelen, + req.fs.add_trailer(StringRef{name, namelen}, StringRef{value, valuelen}, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); return 0; } - req.fs.add_header(name, namelen, value, valuelen, + req.fs.add_header(StringRef{name, namelen}, StringRef{value, valuelen}, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); return 0; } @@ -593,7 +593,8 @@ int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame *frame, req.path = http2::rewrite_clean_path(nv.value, nv.value + nv.valuelen); break; } - req.fs.add_header(nv.name, nv.namelen, nv.value, nv.valuelen, + req.fs.add_header(StringRef{nv.name, nv.namelen}, + StringRef{nv.value, nv.valuelen}, nv.flags & NGHTTP2_NV_FLAG_NO_INDEX, token); }