nghttpx: Use BlockAllocator for util::quote_string

This commit is contained in:
Tatsuhiro Tsujikawa 2016-10-02 00:07:26 +09:00
parent b85924bf70
commit 5e03b6a0db
4 changed files with 19 additions and 14 deletions

View File

@ -965,10 +965,10 @@ void write_altsvc(DefaultMemchunks *buf, BlockAllocator &balloc,
buf->append( buf->append(
util::percent_encode_token(balloc, StringRef{altsvc.protocol_id})); util::percent_encode_token(balloc, StringRef{altsvc.protocol_id}));
buf->append("=\""); buf->append("=\"");
buf->append(util::quote_string(altsvc.host)); buf->append(util::quote_string(balloc, StringRef{altsvc.host}));
buf->append(":"); buf->append(':');
buf->append(altsvc.service); buf->append(altsvc.service);
buf->append("\""); buf->append('"');
} }
} // namespace } // namespace

View File

@ -167,25 +167,27 @@ uint32_t hex_to_uint(char c) {
return 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), '"'); auto cnt = std::count(std::begin(target), std::end(target), '"');
if (cnt == 0) { if (cnt == 0) {
return target; return make_string_ref(balloc, target);
} }
std::string res; auto iov = make_byte_ref(balloc, target.size() + cnt + 1);
res.reserve(target.size() + cnt); auto p = iov.base;
for (auto c : target) { for (auto c : target) {
if (c == '"') { if (c == '"') {
res += "\\\""; *p++ = '\\';
*p++ = '"';
} else { } else {
res += c; *p++ = c;
} }
} }
*p = '\0';
return res; return StringRef{iov.base, p};
} }
namespace { namespace {

View File

@ -133,7 +133,7 @@ StringRef percent_encode_token(BlockAllocator &balloc, const StringRef &target);
// Returns quotedString version of |target|. Currently, this function // Returns quotedString version of |target|. Currently, this function
// just replace '"' with '\"'. // 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); std::string format_hex(const unsigned char *s, size_t len);

View File

@ -175,9 +175,12 @@ void test_util_percent_decode(void) {
} }
void test_util_quote_string(void) { void test_util_quote_string(void) {
CU_ASSERT("alpha" == util::quote_string("alpha")); BlockAllocator balloc(4096, 4096);
CU_ASSERT("" == util::quote_string("")); CU_ASSERT("alpha" ==
CU_ASSERT("\\\"alpha\\\"" == util::quote_string("\"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) { void test_util_utox(void) {