Skip to content

Commit

Permalink
nghttpx: Rewrite Downstream::assemble_request_cookie using StringRef
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Mar 19, 2016
1 parent 71cc7a9 commit dcae6ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/shrpx_downstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,22 @@ search_header_linear_backwards(const HeaderRefs &headers,
}
} // namespace

std::string Downstream::assemble_request_cookie() const {
std::string cookie;
cookie = "";
StringRef Downstream::assemble_request_cookie() {
size_t len = 0;

for (auto &kv : req_.fs.headers()) {
if (kv.token != http2::HD_COOKIE) {
if (kv.token != http2::HD_COOKIE || kv.value.empty()) {
continue;
}

if (kv.value.empty()) {
len += kv.value.size() + str_size("; ");
}

auto iov = make_byte_ref(balloc_, len + 1);
auto p = iov.base;

for (auto &kv : req_.fs.headers()) {
if (kv.token != http2::HD_COOKIE || kv.value.empty()) {
continue;
}

Expand All @@ -280,18 +287,16 @@ std::string Downstream::assemble_request_cookie() const {
break;
}

if (end == std::end(kv.value)) {
cookie += kv.value;
} else {
cookie.append(std::begin(kv.value), end);
}
cookie += "; ";
p = std::copy(std::begin(kv.value), end, p);
p = util::copy_lit(p, "; ");
}
if (cookie.size() >= 2) {
cookie.erase(cookie.size() - 2);

// cut trailing "; "
if (p - iov.base >= 2) {
p -= 2;
}

return cookie;
return StringRef{iov.base, p};
}

size_t Downstream::count_crumble_request_cookie() {
Expand Down
2 changes: 1 addition & 1 deletion src/shrpx_downstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class Downstream {
void crumble_request_cookie(std::vector<nghttp2_nv> &nva);
// Assembles request cookies. The opposite operation against
// crumble_request_cookie().
std::string assemble_request_cookie() const;
StringRef assemble_request_cookie();

void
set_request_start_time(std::chrono::high_resolution_clock::time_point time);
Expand Down

0 comments on commit dcae6ef

Please sign in to comment.