From 5e03b6a0db5c91d382794e68d485dd8896358013 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 2 Oct 2016 00:07:26 +0900 Subject: [PATCH] nghttpx: Use BlockAllocator for util::quote_string --- src/shrpx_https_upstream.cc | 6 +++--- src/util.cc | 16 +++++++++------- src/util.h | 2 +- src/util_test.cc | 9 ++++++--- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 4e2b5db3..24d8b83a 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -965,10 +965,10 @@ void write_altsvc(DefaultMemchunks *buf, BlockAllocator &balloc, buf->append( util::percent_encode_token(balloc, StringRef{altsvc.protocol_id})); buf->append("=\""); - buf->append(util::quote_string(altsvc.host)); - buf->append(":"); + buf->append(util::quote_string(balloc, StringRef{altsvc.host})); + buf->append(':'); buf->append(altsvc.service); - buf->append("\""); + buf->append('"'); } } // namespace diff --git a/src/util.cc b/src/util.cc index 86d3c9f4..19428acf 100644 --- a/src/util.cc +++ b/src/util.cc @@ -167,25 +167,27 @@ uint32_t hex_to_uint(char c) { return c; } -std::string quote_string(const std::string &target) { +StringRef quote_string(BlockAllocator &balloc, const StringRef &target) { auto cnt = std::count(std::begin(target), std::end(target), '"'); if (cnt == 0) { - return target; + return make_string_ref(balloc, target); } - std::string res; - res.reserve(target.size() + cnt); + auto iov = make_byte_ref(balloc, target.size() + cnt + 1); + auto p = iov.base; for (auto c : target) { if (c == '"') { - res += "\\\""; + *p++ = '\\'; + *p++ = '"'; } else { - res += c; + *p++ = c; } } + *p = '\0'; - return res; + return StringRef{iov.base, p}; } namespace { diff --git a/src/util.h b/src/util.h index ccd72a4a..db823f4c 100644 --- a/src/util.h +++ b/src/util.h @@ -133,7 +133,7 @@ StringRef percent_encode_token(BlockAllocator &balloc, const StringRef &target); // Returns quotedString version of |target|. Currently, this function // just replace '"' with '\"'. -std::string quote_string(const std::string &target); +StringRef quote_string(BlockAllocator &balloc, const StringRef &target); std::string format_hex(const unsigned char *s, size_t len); diff --git a/src/util_test.cc b/src/util_test.cc index 375183b8..66f4085e 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -175,9 +175,12 @@ void test_util_percent_decode(void) { } void test_util_quote_string(void) { - CU_ASSERT("alpha" == util::quote_string("alpha")); - CU_ASSERT("" == util::quote_string("")); - CU_ASSERT("\\\"alpha\\\"" == util::quote_string("\"alpha\"")); + BlockAllocator balloc(4096, 4096); + CU_ASSERT("alpha" == + util::quote_string(balloc, StringRef::from_lit("alpha"))); + CU_ASSERT("" == util::quote_string(balloc, StringRef::from_lit(""))); + CU_ASSERT("\\\"alpha\\\"" == + util::quote_string(balloc, StringRef::from_lit("\"alpha\""))); } void test_util_utox(void) {