From 907eeeda8a1f5a43e00e29badabcd32a8d19415e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 8 Mar 2016 21:38:15 +0900 Subject: [PATCH] src: Return StringRef from util::get_uri_field --- src/h2load.cc | 6 +++--- src/nghttp.cc | 24 ++++++++++++------------ src/util.cc | 12 ++++++------ src/util.h | 4 ++-- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/h2load.cc b/src/h2load.cc index dc164b28..14b1fca4 100644 --- a/src/h2load.cc +++ b/src/h2load.cc @@ -1396,7 +1396,7 @@ std::string get_reqline(const char *uri, const http_parser_url &u) { std::string reqline; if (util::has_uri_field(u, UF_PATH)) { - reqline = util::get_uri_field(uri, u, UF_PATH); + reqline = util::get_uri_field(uri, u, UF_PATH).str(); } else { reqline = "/"; } @@ -1437,8 +1437,8 @@ bool parse_base_uri(std::string base_uri) { return false; } - config.scheme = util::get_uri_field(base_uri.c_str(), u, UF_SCHEMA); - config.host = util::get_uri_field(base_uri.c_str(), u, UF_HOST); + config.scheme = util::get_uri_field(base_uri.c_str(), u, UF_SCHEMA).str(); + config.host = util::get_uri_field(base_uri.c_str(), u, UF_HOST).str(); config.default_port = util::get_default_port(base_uri.c_str(), u); if (util::has_uri_field(u, UF_PORT)) { config.port = u.port; diff --git a/src/nghttp.cc b/src/nghttp.cc index a1705124..af349519 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -187,7 +187,7 @@ int Request::update_html_parser(const uint8_t *data, size_t len, int fin) { std::string Request::make_reqpath() const { std::string path = util::has_uri_field(u, UF_PATH) - ? util::get_uri_field(uri.c_str(), u, UF_PATH) + ? util::get_uri_field(uri.c_str(), u, UF_PATH).str() : "/"; if (util::has_uri_field(u, UF_QUERY)) { path += '?'; @@ -200,21 +200,20 @@ std::string Request::make_reqpath() const { namespace { // Perform special handling |host| if it is IPv6 literal and includes // zone ID per RFC 6874. -std::string decode_host(std::string host) { +std::string decode_host(const StringRef &host) { auto zone_start = std::find(std::begin(host), std::end(host), '%'); if (zone_start == std::end(host) || !util::ipv6_numeric_addr( std::string(std::begin(host), zone_start).c_str())) { - return host; + return host.str(); } // case: ::1% if (zone_start + 1 == std::end(host)) { - host.pop_back(); - return host; + return StringRef{host.c_str(), host.size() - 1}.str(); } // case: ::1%12 or ::1%1 if (zone_start + 3 >= std::end(host)) { - return host; + return host.str(); } // If we see "%25", followed by more characters, then decode %25 as // '%'. @@ -222,9 +221,9 @@ std::string decode_host(std::string host) { ? zone_start + 3 : zone_start + 1; auto zone_id = util::percent_decode(zone_id_src, std::end(host)); - host.erase(zone_start + 1, std::end(host)); - host += zone_id; - return host; + auto res = std::string(std::begin(host), zone_start + 1); + res += zone_id; + return res; } } // namespace @@ -349,7 +348,7 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) { auto scheme = util::get_uri_field(req->uri.c_str(), req->u, UF_SCHEMA); auto build_headers = Headers{{":method", req->data_prd ? "POST" : "GET"}, {":path", path}, - {":scheme", scheme}, + {":scheme", scheme.str()}, {":authority", client->hostport}, {"accept", "*/*"}, {"accept-encoding", "gzip, deflate"}, @@ -1255,7 +1254,8 @@ void HttpClient::update_hostport() { if (reqvec.empty()) { return; } - scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_SCHEMA); + scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_SCHEMA) + .str(); std::stringstream ss; if (reqvec[0]->is_ipv6_literal_addr()) { // we may have zone ID, which must start with "%25", or "%". RFC @@ -2381,7 +2381,7 @@ int run(char **uris, int n) { } requests.clear(); } - prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA); + prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA).str(); prev_host = std::move(host); prev_port = port; } diff --git a/src/util.cc b/src/util.cc index 7805a957..784806be 100644 --- a/src/util.cc +++ b/src/util.cc @@ -591,13 +591,13 @@ bool fieldeq(const char *uri, const http_parser_url &u, return i == len && !t[i]; } -std::string get_uri_field(const char *uri, const http_parser_url &u, - http_parser_url_fields field) { - if (util::has_uri_field(u, field)) { - return std::string(uri + u.field_data[field].off, u.field_data[field].len); - } else { - return ""; +StringRef get_uri_field(const char *uri, const http_parser_url &u, + http_parser_url_fields field) { + if (!util::has_uri_field(u, field)) { + return StringRef{}; } + + return StringRef{uri + u.field_data[field].off, u.field_data[field].len}; } uint16_t get_default_port(const char *uri, const http_parser_url &u) { diff --git a/src/util.h b/src/util.h index 15894816..d03ffd8e 100644 --- a/src/util.h +++ b/src/util.h @@ -448,8 +448,8 @@ bool fieldeq(const char *uri1, const http_parser_url &u1, const char *uri2, bool fieldeq(const char *uri, const http_parser_url &u, http_parser_url_fields field, const char *t); -std::string get_uri_field(const char *uri, const http_parser_url &u, - http_parser_url_fields field); +StringRef get_uri_field(const char *uri, const http_parser_url &u, + http_parser_url_fields field); uint16_t get_default_port(const char *uri, const http_parser_url &u);