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(
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

View File

@ -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 {

View File

@ -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);

View File

@ -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) {