http2::get_header: Take std::string_view

This commit is contained in:
Tatsuhiro Tsujikawa
2025-06-08 12:37:47 +09:00
parent 0bfdc8682d
commit d79fd53b67
4 changed files with 9 additions and 7 deletions

View File

@@ -317,7 +317,8 @@ void add_header(Headers &nva, const StringRef &name, const StringRef &value,
nva.push_back(to_header(name, value, no_index, token));
}
const Headers::value_type *get_header(const Headers &nva, const char *name) {
const Headers::value_type *get_header(const Headers &nva,
const std::string_view &name) {
const Headers::value_type *res = nullptr;
for (auto &nv : nva) {
if (nv.name == name) {

View File

@@ -116,7 +116,8 @@ void add_header(Headers &nva, const StringRef &name, const StringRef &value,
// Returns pointer to the entry in |nva| which has name |name|. If
// more than one entries which have the name |name|, last occurrence
// in |nva| is returned. If no such entry exist, returns nullptr.
const Headers::value_type *get_header(const Headers &nva, const char *name);
const Headers::value_type *get_header(const Headers &nva,
const std::string_view &name);
// Returns true if the value of |nv| is not empty.
bool non_empty_value(const HeaderRefs::value_type *nv);

View File

@@ -111,15 +111,15 @@ void test_http2_get_header(void) {
{"charlie", "4"}, {"delta", "5"}, {"echo", "6"},
{"content-length", "7"}};
const Headers::value_type *rv;
rv = http2::get_header(nva, "delta");
rv = http2::get_header(nva, "delta"sv);
assert_not_null(rv);
assert_stdstring_equal("delta", rv->name);
rv = http2::get_header(nva, "bravo");
rv = http2::get_header(nva, "bravo"sv);
assert_not_null(rv);
assert_stdstring_equal("bravo", rv->name);
rv = http2::get_header(nva, "foxtrot");
rv = http2::get_header(nva, "foxtrot"sv);
assert_null(rv);
}

View File

@@ -446,7 +446,7 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) {
http2::make_field_nv(kv.name, kv.value, http2::no_index(kv.no_index)));
}
auto method = http2::get_header(build_headers, ":method");
auto method = http2::get_header(build_headers, ":method"sv);
assert(method);
req->method = method->value;
@@ -1529,7 +1529,7 @@ void HttpClient::output_har(FILE *outfile) {
json_object_set_new(content, "size", json_integer(req->response_len));
auto content_type_ptr = http2::get_header(req->res_nva, "content-type");
auto content_type_ptr = http2::get_header(req->res_nva, "content-type"sv);
const char *content_type = "";
if (content_type_ptr) {