nghttpx: Use BlockAllocator to encode alt-svc token

This commit is contained in:
Tatsuhiro Tsujikawa 2016-10-02 00:00:46 +09:00
parent 19707aac55
commit b85924bf70
4 changed files with 24 additions and 16 deletions

View File

@ -960,8 +960,10 @@ std::unique_ptr<Downstream> HttpsUpstream::pop_downstream() {
}
namespace {
void write_altsvc(DefaultMemchunks *buf, const AltSvc &altsvc) {
buf->append(util::percent_encode_token(altsvc.protocol_id));
void write_altsvc(DefaultMemchunks *buf, BlockAllocator &balloc,
const AltSvc &altsvc) {
buf->append(
util::percent_encode_token(balloc, StringRef{altsvc.protocol_id}));
buf->append("=\"");
buf->append(util::quote_string(altsvc.host));
buf->append(":");
@ -1073,10 +1075,10 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) {
buf->append("Alt-Svc: ");
auto &altsvcs = httpconf.altsvcs;
write_altsvc(buf, altsvcs[0]);
write_altsvc(buf, downstream->get_block_allocator(), altsvcs[0]);
for (size_t i = 1; i < altsvcs.size(); ++i) {
buf->append(", ");
write_altsvc(buf, altsvcs[i]);
write_altsvc(buf, downstream->get_block_allocator(), altsvcs[i]);
}
buf->append("\r\n");
}

View File

@ -131,11 +131,10 @@ bool in_attr_char(char c) {
std::find(std::begin(bad), std::end(bad), c) == std::end(bad);
}
std::string percent_encode_token(const std::string &target) {
std::string dest;
dest.resize(target.size() * 3);
auto p = std::begin(dest);
StringRef percent_encode_token(BlockAllocator &balloc,
const StringRef &target) {
auto iov = make_byte_ref(balloc, target.size() * 3 + 1);
auto p = iov.base;
for (auto first = std::begin(target); first != std::end(target); ++first) {
uint8_t c = *first;
@ -149,8 +148,10 @@ std::string percent_encode_token(const std::string &target) {
*p++ = UPPER_XDIGITS[c >> 4];
*p++ = UPPER_XDIGITS[(c & 0x0f)];
}
dest.resize(p - std::begin(dest));
return dest;
*p = '\0';
return StringRef{iov.base, p};
}
uint32_t hex_to_uint(char c) {

View File

@ -129,7 +129,7 @@ std::string percent_decode(InputIt first, InputIt last) {
StringRef percent_decode(BlockAllocator &balloc, const StringRef &src);
// Percent encode |target| if character is not in token or '%'.
std::string percent_encode_token(const std::string &target);
StringRef percent_encode_token(BlockAllocator &balloc, const StringRef &target);
// Returns quotedString version of |target|. Currently, this function
// just replace '"' with '\"'.

View File

@ -134,10 +134,15 @@ void test_util_to_token68(void) {
}
void test_util_percent_encode_token(void) {
CU_ASSERT("h2" == util::percent_encode_token("h2"));
CU_ASSERT("h3~" == util::percent_encode_token("h3~"));
CU_ASSERT("100%25" == util::percent_encode_token("100%"));
CU_ASSERT("http%202" == util::percent_encode_token("http 2"));
BlockAllocator balloc(4096, 4096);
CU_ASSERT("h2" ==
util::percent_encode_token(balloc, StringRef::from_lit("h2")));
CU_ASSERT("h3~" ==
util::percent_encode_token(balloc, StringRef::from_lit("h3~")));
CU_ASSERT("100%25" ==
util::percent_encode_token(balloc, StringRef::from_lit("100%")));
CU_ASSERT("http%202" ==
util::percent_encode_token(balloc, StringRef::from_lit("http 2")));
}
void test_util_percent_encode_path(void) {