Commit 3f2b54cf authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

src: Refactor using StringRef

parent acbf38fd
...@@ -1453,28 +1453,21 @@ StringRef to_method_string(int method_token) { ...@@ -1453,28 +1453,21 @@ StringRef to_method_string(int method_token) {
return StringRef{http_method_str(static_cast<http_method>(method_token))}; return StringRef{http_method_str(static_cast<http_method>(method_token))};
} }
int get_pure_path_component(const char **base, size_t *baselen, StringRef get_pure_path_component(const std::string &uri) {
const std::string &uri) {
int rv; int rv;
http_parser_url u{}; http_parser_url u{};
rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u); rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u);
if (rv != 0) { if (rv != 0) {
return -1; return StringRef{};
} }
if (u.field_set & (1 << UF_PATH)) { if (u.field_set & (1 << UF_PATH)) {
auto &f = u.field_data[UF_PATH]; auto &f = u.field_data[UF_PATH];
*base = uri.c_str() + f.off; return StringRef{uri.c_str() + f.off, f.len};
*baselen = f.len;
return 0;
} }
*base = "/"; return StringRef::from_lit("/");
*baselen = 1;
return 0;
} }
int construct_push_component(std::string &scheme, std::string &authority, int construct_push_component(std::string &scheme, std::string &authority,
......
...@@ -374,11 +374,10 @@ std::string rewrite_clean_path(InputIt first, InputIt last) { ...@@ -374,11 +374,10 @@ std::string rewrite_clean_path(InputIt first, InputIt last) {
return path; return path;
} }
// Stores path component of |uri| in *base. Its extracted length is // Returns path component of |uri|. The returned path does not
// stored in *baselen. The extracted path does not include query // include query component. This function returns empty string if it
// component. This function returns 0 if it succeeds, or -1. // fails.
int get_pure_path_component(const char **base, size_t *baselen, StringRef get_pure_path_component(const std::string &uri);
const std::string &uri);
// Deduces scheme, authority and path from given |uri|, and stores // Deduces scheme, authority and path from given |uri|, and stores
// them in |scheme|, |authority|, and |path| respectively. If |uri| // them in |scheme|, |authority|, and |path| respectively. If |uri|
......
...@@ -866,32 +866,25 @@ void test_http2_rewrite_clean_path(void) { ...@@ -866,32 +866,25 @@ void test_http2_rewrite_clean_path(void) {
} }
void test_http2_get_pure_path_component(void) { void test_http2_get_pure_path_component(void) {
const char *base;
size_t len;
std::string path; std::string path;
path = "/"; path = "/";
CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); CU_ASSERT("/" == http2::get_pure_path_component(path));
CU_ASSERT(util::streq_l("/", base, len));
path = "/foo"; path = "/foo";
CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); CU_ASSERT("/foo" == http2::get_pure_path_component(path));
CU_ASSERT(util::streq_l("/foo", base, len));
path = "https://example.org/bar"; path = "https://example.org/bar";
CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); CU_ASSERT("/bar" == http2::get_pure_path_component(path));
CU_ASSERT(util::streq_l("/bar", base, len));
path = "https://example.org/alpha?q=a"; path = "https://example.org/alpha?q=a";
CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); CU_ASSERT("/alpha" == http2::get_pure_path_component(path));
CU_ASSERT(util::streq_l("/alpha", base, len));
path = "https://example.org/bravo?q=a#fragment"; path = "https://example.org/bravo?q=a#fragment";
CU_ASSERT(0 == http2::get_pure_path_component(&base, &len, path)); CU_ASSERT("/bravo" == http2::get_pure_path_component(path));
CU_ASSERT(util::streq_l("/bravo", base, len));
path = "\x01\x02"; 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) { void test_http2_construct_push_component(void) {
......
...@@ -1734,14 +1734,12 @@ int Http2Upstream::on_downstream_reset(bool no_retry) { ...@@ -1734,14 +1734,12 @@ int Http2Upstream::on_downstream_reset(bool no_retry) {
int Http2Upstream::prepare_push_promise(Downstream *downstream) { int Http2Upstream::prepare_push_promise(Downstream *downstream) {
int rv; int rv;
const char *base;
size_t baselen;
const auto &req = downstream->request(); const auto &req = downstream->request();
const auto &resp = downstream->response(); const auto &resp = downstream->response();
rv = http2::get_pure_path_component(&base, &baselen, req.path); auto base = http2::get_pure_path_component(req.path);
if (rv != 0) { if (base.empty()) {
return 0; return 0;
} }
...@@ -1755,8 +1753,8 @@ int Http2Upstream::prepare_push_promise(Downstream *downstream) { ...@@ -1755,8 +1753,8 @@ int Http2Upstream::prepare_push_promise(Downstream *downstream) {
const std::string *scheme_ptr, *authority_ptr; const std::string *scheme_ptr, *authority_ptr;
std::string scheme, authority, path; std::string scheme, authority, path;
rv = http2::construct_push_component(scheme, authority, path, rv = http2::construct_push_component(scheme, authority, path, base,
StringRef{base, baselen}, link.uri); link.uri);
if (rv != 0) { if (rv != 0) {
continue; continue;
} }
...@@ -1858,21 +1856,18 @@ int Http2Upstream::initiate_push(Downstream *downstream, const char *uri, ...@@ -1858,21 +1856,18 @@ int Http2Upstream::initiate_push(Downstream *downstream, const char *uri,
return 0; return 0;
} }
const char *base;
size_t baselen;
const auto &req = downstream->request(); const auto &req = downstream->request();
rv = http2::get_pure_path_component(&base, &baselen, req.path); auto base = http2::get_pure_path_component(req.path);
if (rv != 0) { if (base.empty()) {
return -1; return -1;
} }
const std::string *scheme_ptr, *authority_ptr; const std::string *scheme_ptr, *authority_ptr;
std::string scheme, authority, path; std::string scheme, authority, path;
rv = http2::construct_push_component( rv = http2::construct_push_component(scheme, authority, path, base,
scheme, authority, path, StringRef{base, baselen}, StringRef{uri, len}); StringRef{uri, len});
if (rv != 0) { if (rv != 0) {
return -1; return -1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment