From 3f2b54cfc4d252b236709f53d79c7b32bb277f18 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 4 Mar 2016 00:33:35 +0900 Subject: [PATCH] src: Refactor using StringRef --- src/http2.cc | 15 ++++----------- src/http2.h | 9 ++++----- src/http2_test.cc | 19 ++++++------------- src/shrpx_http2_upstream.cc | 21 ++++++++------------- 4 files changed, 22 insertions(+), 42 deletions(-) diff --git a/src/http2.cc b/src/http2.cc index 6bc898b9..ebc2a8a5 100644 --- a/src/http2.cc +++ b/src/http2.cc @@ -1453,28 +1453,21 @@ StringRef to_method_string(int method_token) { return StringRef{http_method_str(static_cast(method_token))}; } -int get_pure_path_component(const char **base, size_t *baselen, - const std::string &uri) { +StringRef get_pure_path_component(const std::string &uri) { int rv; http_parser_url u{}; rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u); if (rv != 0) { - return -1; + return StringRef{}; } if (u.field_set & (1 << UF_PATH)) { auto &f = u.field_data[UF_PATH]; - *base = uri.c_str() + f.off; - *baselen = f.len; - - return 0; + return StringRef{uri.c_str() + f.off, f.len}; } - *base = "/"; - *baselen = 1; - - return 0; + return StringRef::from_lit("/"); } int construct_push_component(std::string &scheme, std::string &authority, diff --git a/src/http2.h b/src/http2.h index 23707b45..7a05dd45 100644 --- a/src/http2.h +++ b/src/http2.h @@ -374,11 +374,10 @@ std::string rewrite_clean_path(InputIt first, InputIt last) { return path; } -// Stores path component of |uri| in *base. Its extracted length is -// stored in *baselen. The extracted path does not include query -// component. This function returns 0 if it succeeds, or -1. -int get_pure_path_component(const char **base, size_t *baselen, - const std::string &uri); +// Returns path component of |uri|. The returned path does not +// include query component. This function returns empty string if it +// fails. +StringRef get_pure_path_component(const std::string &uri); // Deduces scheme, authority and path from given |uri|, and stores // them in |scheme|, |authority|, and |path| respectively. If |uri| diff --git a/src/http2_test.cc b/src/http2_test.cc index e3b19693..4d4c7669 100644 --- a/src/http2_test.cc +++ b/src/http2_test.cc @@ -866,32 +866,25 @@ void test_http2_rewrite_clean_path(void) { } void test_http2_get_pure_path_component(void) { - const char *base; - size_t len; std::string path; path = "/"; - CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); - CU_ASSERT(util::streq_l("/", base, len)); + CU_ASSERT("/" == http2::get_pure_path_component(path)); path = "/foo"; - CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); - CU_ASSERT(util::streq_l("/foo", base, len)); + CU_ASSERT("/foo" == http2::get_pure_path_component(path)); path = "https://example.org/bar"; - CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); - CU_ASSERT(util::streq_l("/bar", base, len)); + CU_ASSERT("/bar" == http2::get_pure_path_component(path)); path = "https://example.org/alpha?q=a"; - CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); - CU_ASSERT(util::streq_l("/alpha", base, len)); + CU_ASSERT("/alpha" == http2::get_pure_path_component(path)); path = "https://example.org/bravo?q=a#fragment"; - CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); - CU_ASSERT(util::streq_l("/bravo", base, len)); + CU_ASSERT("/bravo" == http2::get_pure_path_component(path)); path = "\x01\x02"; - CU_ASSERT(-1 == http2::get_pure_path_component(&base, &len, path)); + CU_ASSERT("" == http2::get_pure_path_component(path)); } void test_http2_construct_push_component(void) { diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 048cc50b..cb4ef196 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -1734,14 +1734,12 @@ int Http2Upstream::on_downstream_reset(bool no_retry) { int Http2Upstream::prepare_push_promise(Downstream *downstream) { int rv; - const char *base; - size_t baselen; const auto &req = downstream->request(); const auto &resp = downstream->response(); - rv = http2::get_pure_path_component(&base, &baselen, req.path); - if (rv != 0) { + auto base = http2::get_pure_path_component(req.path); + if (base.empty()) { return 0; } @@ -1755,8 +1753,8 @@ int Http2Upstream::prepare_push_promise(Downstream *downstream) { const std::string *scheme_ptr, *authority_ptr; std::string scheme, authority, path; - rv = http2::construct_push_component(scheme, authority, path, - StringRef{base, baselen}, link.uri); + rv = http2::construct_push_component(scheme, authority, path, base, + link.uri); if (rv != 0) { continue; } @@ -1858,21 +1856,18 @@ int Http2Upstream::initiate_push(Downstream *downstream, const char *uri, return 0; } - const char *base; - size_t baselen; - const auto &req = downstream->request(); - rv = http2::get_pure_path_component(&base, &baselen, req.path); - if (rv != 0) { + auto base = http2::get_pure_path_component(req.path); + if (base.empty()) { return -1; } const std::string *scheme_ptr, *authority_ptr; std::string scheme, authority, path; - rv = http2::construct_push_component( - scheme, authority, path, StringRef{base, baselen}, StringRef{uri, len}); + rv = http2::construct_push_component(scheme, authority, path, base, + StringRef{uri, len}); if (rv != 0) { return -1; }