diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index a02fd2e5..70230e82 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -178,6 +178,7 @@ int main(int argc, char *argv[]) { !CU_add_test(pSuite, "util_strifind", shrpx::test_util_strifind) || !CU_add_test(pSuite, "util_random_alpha_digit", shrpx::test_util_random_alpha_digit) || + !CU_add_test(pSuite, "util_format_hex", shrpx::test_util_format_hex) || !CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) || !CU_add_test(pSuite, "buffer_write", nghttp2::test_buffer_write) || !CU_add_test(pSuite, "pool_recycle", nghttp2::test_pool_recycle) || diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 43c28649..900d4db3 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -1522,4 +1522,6 @@ void ClientHandler::set_tls_sni(const StringRef &sni) { StringRef ClientHandler::get_tls_sni() const { return sni_; } +BlockAllocator &ClientHandler::get_block_allocator() { return balloc_; } + } // namespace shrpx diff --git a/src/shrpx_client_handler.h b/src/shrpx_client_handler.h index 8a27c24b..c441a5bd 100644 --- a/src/shrpx_client_handler.h +++ b/src/shrpx_client_handler.h @@ -162,6 +162,8 @@ public: // Returns TLS SNI extension value client sent in this connection. StringRef get_tls_sni() const; + BlockAllocator &get_block_allocator(); + private: BlockAllocator balloc_; Connection conn_; diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index c59251be..7601a19d 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -243,6 +243,7 @@ int tls_session_new_cb(SSL *ssl, SSL_SESSION *session) { auto handler = static_cast(conn->data); auto worker = handler->get_worker(); auto dispatcher = worker->get_session_cache_memcached_dispatcher(); + auto &balloc = handler->get_block_allocator(); const unsigned char *id; unsigned int idlen; @@ -256,7 +257,8 @@ int tls_session_new_cb(SSL *ssl, SSL_SESSION *session) { auto req = make_unique(); req->op = MEMCACHED_OP_ADD; req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str(); - req->key += util::format_hex(id, idlen); + req->key += + util::format_hex(balloc, StringRef{id, static_cast(idlen)}); auto sessionlen = i2d_SSL_SESSION(session, nullptr); req->value.resize(sessionlen); @@ -295,6 +297,7 @@ SSL_SESSION *tls_session_get_cb(SSL *ssl, auto handler = static_cast(conn->data); auto worker = handler->get_worker(); auto dispatcher = worker->get_session_cache_memcached_dispatcher(); + auto &balloc = handler->get_block_allocator(); if (conn->tls.cached_session) { if (LOG_ENABLED(INFO)) { @@ -318,7 +321,8 @@ SSL_SESSION *tls_session_get_cb(SSL *ssl, auto req = make_unique(); req->op = MEMCACHED_OP_GET; req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str(); - req->key += util::format_hex(id, idlen); + req->key += + util::format_hex(balloc, StringRef{id, static_cast(idlen)}); req->cb = [conn](MemcachedRequest *, MemcachedResult res) { if (LOG_ENABLED(INFO)) { LOG(INFO) << "Memcached: returned status code " << res.status_code; diff --git a/src/util.cc b/src/util.cc index 19428acf..056c7cd9 100644 --- a/src/util.cc +++ b/src/util.cc @@ -379,6 +379,21 @@ std::string format_hex(const unsigned char *s, size_t len) { return res; } +StringRef format_hex(BlockAllocator &balloc, const StringRef &s) { + auto iov = make_byte_ref(balloc, s.size() * 2 + 1); + auto p = iov.base; + + for (auto cc : s) { + uint8_t c = cc; + *p++ = LOWER_XDIGITS[c >> 4]; + *p++ = LOWER_XDIGITS[c & 0xf]; + } + + *p = '\0'; + + return StringRef{iov.base, p}; +} + void to_token68(std::string &base64str) { std::transform(std::begin(base64str), std::end(base64str), std::begin(base64str), [](char c) { diff --git a/src/util.h b/src/util.h index db823f4c..e192864d 100644 --- a/src/util.h +++ b/src/util.h @@ -145,6 +145,8 @@ template std::string format_hex(const std::array &s) { return format_hex(s.data(), s.size()); } +StringRef format_hex(BlockAllocator &balloc, const StringRef &s); + std::string http_date(time_t t); // Returns given time |t| from epoch in Common Log format (e.g., diff --git a/src/util_test.cc b/src/util_test.cc index 66f4085e..e8e24fcf 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -552,4 +552,12 @@ void test_util_random_alpha_digit(void) { } } +void test_util_format_hex(void) { + BlockAllocator balloc(4096, 4096); + + CU_ASSERT("0ff0" == + util::format_hex(balloc, StringRef::from_lit("\x0f\xf0"))); + CU_ASSERT("" == util::format_hex(balloc, StringRef::from_lit(""))); +} + } // namespace shrpx diff --git a/src/util_test.h b/src/util_test.h index 6741da9e..c498cbad 100644 --- a/src/util_test.h +++ b/src/util_test.h @@ -63,6 +63,7 @@ void test_util_make_http_hostport(void); void test_util_make_hostport(void); void test_util_strifind(void); void test_util_random_alpha_digit(void); +void test_util_format_hex(void); } // namespace shrpx