src: Return StringRef from util::get_uri_field

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-08 21:38:15 +09:00
parent fe6ccd16da
commit 907eeeda8a
4 changed files with 23 additions and 23 deletions

View File

@ -1396,7 +1396,7 @@ std::string get_reqline(const char *uri, const http_parser_url &u) {
std::string reqline; std::string reqline;
if (util::has_uri_field(u, UF_PATH)) { 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 { } else {
reqline = "/"; reqline = "/";
} }
@ -1437,8 +1437,8 @@ bool parse_base_uri(std::string base_uri) {
return false; return false;
} }
config.scheme = util::get_uri_field(base_uri.c_str(), u, UF_SCHEMA); 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); 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); config.default_port = util::get_default_port(base_uri.c_str(), u);
if (util::has_uri_field(u, UF_PORT)) { if (util::has_uri_field(u, UF_PORT)) {
config.port = u.port; config.port = u.port;

View File

@ -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 Request::make_reqpath() const {
std::string path = util::has_uri_field(u, UF_PATH) 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)) { if (util::has_uri_field(u, UF_QUERY)) {
path += '?'; path += '?';
@ -200,21 +200,20 @@ std::string Request::make_reqpath() const {
namespace { namespace {
// Perform special handling |host| if it is IPv6 literal and includes // Perform special handling |host| if it is IPv6 literal and includes
// zone ID per RFC 6874. // 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), '%'); auto zone_start = std::find(std::begin(host), std::end(host), '%');
if (zone_start == std::end(host) || if (zone_start == std::end(host) ||
!util::ipv6_numeric_addr( !util::ipv6_numeric_addr(
std::string(std::begin(host), zone_start).c_str())) { std::string(std::begin(host), zone_start).c_str())) {
return host; return host.str();
} }
// case: ::1% // case: ::1%
if (zone_start + 1 == std::end(host)) { if (zone_start + 1 == std::end(host)) {
host.pop_back(); return StringRef{host.c_str(), host.size() - 1}.str();
return host;
} }
// case: ::1%12 or ::1%1 // case: ::1%12 or ::1%1
if (zone_start + 3 >= std::end(host)) { if (zone_start + 3 >= std::end(host)) {
return host; return host.str();
} }
// If we see "%25", followed by more characters, then decode %25 as // 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 + 3
: zone_start + 1; : zone_start + 1;
auto zone_id = util::percent_decode(zone_id_src, std::end(host)); auto zone_id = util::percent_decode(zone_id_src, std::end(host));
host.erase(zone_start + 1, std::end(host)); auto res = std::string(std::begin(host), zone_start + 1);
host += zone_id; res += zone_id;
return host; return res;
} }
} // namespace } // 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 scheme = util::get_uri_field(req->uri.c_str(), req->u, UF_SCHEMA);
auto build_headers = Headers{{":method", req->data_prd ? "POST" : "GET"}, auto build_headers = Headers{{":method", req->data_prd ? "POST" : "GET"},
{":path", path}, {":path", path},
{":scheme", scheme}, {":scheme", scheme.str()},
{":authority", client->hostport}, {":authority", client->hostport},
{"accept", "*/*"}, {"accept", "*/*"},
{"accept-encoding", "gzip, deflate"}, {"accept-encoding", "gzip, deflate"},
@ -1255,7 +1254,8 @@ void HttpClient::update_hostport() {
if (reqvec.empty()) { if (reqvec.empty()) {
return; 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; std::stringstream ss;
if (reqvec[0]->is_ipv6_literal_addr()) { if (reqvec[0]->is_ipv6_literal_addr()) {
// we may have zone ID, which must start with "%25", or "%". RFC // we may have zone ID, which must start with "%25", or "%". RFC
@ -2381,7 +2381,7 @@ int run(char **uris, int n) {
} }
requests.clear(); 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_host = std::move(host);
prev_port = port; prev_port = port;
} }

View File

@ -591,13 +591,13 @@ bool fieldeq(const char *uri, const http_parser_url &u,
return i == len && !t[i]; return i == len && !t[i];
} }
std::string get_uri_field(const char *uri, const http_parser_url &u, StringRef get_uri_field(const char *uri, const http_parser_url &u,
http_parser_url_fields field) { http_parser_url_fields field) {
if (util::has_uri_field(u, field)) { if (!util::has_uri_field(u, field)) {
return std::string(uri + u.field_data[field].off, u.field_data[field].len); return StringRef{};
} else {
return "";
} }
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) { uint16_t get_default_port(const char *uri, const http_parser_url &u) {

View File

@ -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, bool fieldeq(const char *uri, const http_parser_url &u,
http_parser_url_fields field, const char *t); http_parser_url_fields field, const char *t);
std::string get_uri_field(const char *uri, const http_parser_url &u, StringRef get_uri_field(const char *uri, const http_parser_url &u,
http_parser_url_fields field); http_parser_url_fields field);
uint16_t get_default_port(const char *uri, const http_parser_url &u); uint16_t get_default_port(const char *uri, const http_parser_url &u);