src: Refactor using StringRef
This commit is contained in:
parent
acbf38fd3c
commit
3f2b54cfc4
15
src/http2.cc
15
src/http2.cc
|
@ -1453,28 +1453,21 @@ StringRef to_method_string(int method_token) {
|
|||
return StringRef{http_method_str(static_cast<http_method>(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,
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue