Commit cb2c9f65 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Make more net::HttpResponseHeaders methods take StringPieces.

In particular, make GetNormalizedHeader() and RemoveHeader() and take
StringPieces. Both are already calling StringPiece-friendly methods
internally, so this should reduce copies slightly.

Also make a number of methods take a "base::StringPiece" instead of
a "const base::StringPiece&".

Bug: 1068194
Change-Id: I7a27d505a8f5c20ddb5c1313b9de4f2cb1069dec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2149690
Commit-Queue: Matt Menke <mmenke@chromium.org>
Reviewed-by: default avatarEric Roman <eroman@chromium.org>
Auto-Submit: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759391}
parent 703bc461
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include "net/log/net_log_capture_mode.h" #include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_values.h" #include "net/log/net_log_values.h"
using base::StringPiece;
using base::Time; using base::Time;
using base::TimeDelta; using base::TimeDelta;
...@@ -327,14 +326,13 @@ void HttpResponseHeaders::MergeWithHeaders(const std::string& raw_headers, ...@@ -327,14 +326,13 @@ void HttpResponseHeaders::MergeWithHeaders(const std::string& raw_headers,
Parse(new_raw_headers); Parse(new_raw_headers);
} }
void HttpResponseHeaders::RemoveHeader(const std::string& name) { void HttpResponseHeaders::RemoveHeader(base::StringPiece name) {
// Copy up to the null byte. This just copies the status line. // Copy up to the null byte. This just copies the status line.
std::string new_raw_headers(raw_headers_.c_str()); std::string new_raw_headers(raw_headers_.c_str());
new_raw_headers.push_back('\0'); new_raw_headers.push_back('\0');
std::string lowercase_name = base::ToLowerASCII(name);
HeaderSet to_remove; HeaderSet to_remove;
to_remove.insert(lowercase_name); to_remove.insert(base::ToLowerASCII(name));
MergeWithHeaders(new_raw_headers, to_remove); MergeWithHeaders(new_raw_headers, to_remove);
} }
...@@ -420,7 +418,7 @@ void HttpResponseHeaders::AddHeader(base::StringPiece name, ...@@ -420,7 +418,7 @@ void HttpResponseHeaders::AddHeader(base::StringPiece name,
void HttpResponseHeaders::SetHeader(base::StringPiece name, void HttpResponseHeaders::SetHeader(base::StringPiece name,
base::StringPiece value) { base::StringPiece value) {
RemoveHeader(name.as_string()); RemoveHeader(name);
AddHeader(name, value); AddHeader(name, value);
} }
...@@ -515,7 +513,7 @@ void HttpResponseHeaders::Parse(const std::string& raw_input) { ...@@ -515,7 +513,7 @@ void HttpResponseHeaders::Parse(const std::string& raw_input) {
DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]); DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]);
} }
bool HttpResponseHeaders::GetNormalizedHeader(const std::string& name, bool HttpResponseHeaders::GetNormalizedHeader(base::StringPiece name,
std::string* value) const { std::string* value) const {
// If you hit this assertion, please use EnumerateHeader instead! // If you hit this assertion, please use EnumerateHeader instead!
DCHECK(!HttpUtil::IsNonCoalescingHeader(name)); DCHECK(!HttpUtil::IsNonCoalescingHeader(name));
...@@ -593,7 +591,7 @@ bool HttpResponseHeaders::EnumerateHeaderLines(size_t* iter, ...@@ -593,7 +591,7 @@ bool HttpResponseHeaders::EnumerateHeaderLines(size_t* iter,
} }
bool HttpResponseHeaders::EnumerateHeader(size_t* iter, bool HttpResponseHeaders::EnumerateHeader(size_t* iter,
const base::StringPiece& name, base::StringPiece name,
std::string* value) const { std::string* value) const {
size_t i; size_t i;
if (!iter || !*iter) { if (!iter || !*iter) {
...@@ -618,8 +616,8 @@ bool HttpResponseHeaders::EnumerateHeader(size_t* iter, ...@@ -618,8 +616,8 @@ bool HttpResponseHeaders::EnumerateHeader(size_t* iter,
return true; return true;
} }
bool HttpResponseHeaders::HasHeaderValue(const base::StringPiece& name, bool HttpResponseHeaders::HasHeaderValue(base::StringPiece name,
const base::StringPiece& value) const { base::StringPiece value) const {
// The value has to be an exact match. This is important since // The value has to be an exact match. This is important since
// 'cache-control: no-cache' != 'cache-control: no-cache="foo"' // 'cache-control: no-cache' != 'cache-control: no-cache="foo"'
size_t iter = 0; size_t iter = 0;
...@@ -631,7 +629,7 @@ bool HttpResponseHeaders::HasHeaderValue(const base::StringPiece& name, ...@@ -631,7 +629,7 @@ bool HttpResponseHeaders::HasHeaderValue(const base::StringPiece& name,
return false; return false;
} }
bool HttpResponseHeaders::HasHeader(const base::StringPiece& name) const { bool HttpResponseHeaders::HasHeader(base::StringPiece name) const {
return FindHeader(0, name) != std::string::npos; return FindHeader(0, name) != std::string::npos;
} }
...@@ -737,7 +735,7 @@ void HttpResponseHeaders::ParseStatusLine( ...@@ -737,7 +735,7 @@ void HttpResponseHeaders::ParseStatusLine(
} }
raw_headers_.push_back(' '); raw_headers_.push_back(' ');
raw_headers_.append(code, p); raw_headers_.append(code, p);
base::StringToInt(StringPiece(code, p), &response_code_); base::StringToInt(base::StringPiece(code, p), &response_code_);
// Skip whitespace. // Skip whitespace.
while (p < line_end && *p == ' ') while (p < line_end && *p == ' ')
...@@ -755,7 +753,7 @@ void HttpResponseHeaders::ParseStatusLine( ...@@ -755,7 +753,7 @@ void HttpResponseHeaders::ParseStatusLine(
} }
size_t HttpResponseHeaders::FindHeader(size_t from, size_t HttpResponseHeaders::FindHeader(size_t from,
const base::StringPiece& search) const { base::StringPiece search) const {
for (size_t i = from; i < parsed_.size(); ++i) { for (size_t i = from; i < parsed_.size(); ++i) {
if (parsed_[i].is_continuation()) if (parsed_[i].is_continuation())
continue; continue;
...@@ -767,9 +765,9 @@ size_t HttpResponseHeaders::FindHeader(size_t from, ...@@ -767,9 +765,9 @@ size_t HttpResponseHeaders::FindHeader(size_t from,
return std::string::npos; return std::string::npos;
} }
bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive, bool HttpResponseHeaders::GetCacheControlDirective(base::StringPiece directive,
TimeDelta* result) const { TimeDelta* result) const {
StringPiece name("cache-control"); base::StringPiece name("cache-control");
std::string value; std::string value;
size_t directive_size = directive.size(); size_t directive_size = directive.size();
...@@ -782,7 +780,7 @@ bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive, ...@@ -782,7 +780,7 @@ bool HttpResponseHeaders::GetCacheControlDirective(const StringPiece& directive,
value[directive_size] == '=') { value[directive_size] == '=') {
int64_t seconds; int64_t seconds;
base::StringToInt64( base::StringToInt64(
StringPiece(value.begin() + directive_size + 1, value.end()), base::StringPiece(value.begin() + directive_size + 1, value.end()),
&seconds); &seconds);
*result = TimeDelta::FromSeconds(seconds); *result = TimeDelta::FromSeconds(seconds);
return true; return true;
...@@ -1372,7 +1370,7 @@ bool HttpResponseHeaders::IsChunkEncoded() const { ...@@ -1372,7 +1370,7 @@ bool HttpResponseHeaders::IsChunkEncoded() const {
HasHeaderValue("Transfer-Encoding", "chunked"); HasHeaderValue("Transfer-Encoding", "chunked");
} }
bool HttpResponseHeaders::IsCookieResponseHeader(StringPiece name) { bool HttpResponseHeaders::IsCookieResponseHeader(base::StringPiece name) {
for (const char* cookie_header : kCookieResponseHeaders) { for (const char* cookie_header : kCookieResponseHeaders) {
if (base::EqualsCaseInsensitiveASCII(cookie_header, name)) if (base::EqualsCaseInsensitiveASCII(cookie_header, name))
return true; return true;
......
...@@ -95,7 +95,7 @@ class NET_EXPORT HttpResponseHeaders ...@@ -95,7 +95,7 @@ class NET_EXPORT HttpResponseHeaders
void Update(const HttpResponseHeaders& new_headers); void Update(const HttpResponseHeaders& new_headers);
// Removes all instances of a particular header. // Removes all instances of a particular header.
void RemoveHeader(const std::string& name); void RemoveHeader(base::StringPiece name);
// Removes all instances of particular headers. // Removes all instances of particular headers.
void RemoveHeaders(const std::unordered_set<std::string>& header_names); void RemoveHeaders(const std::unordered_set<std::string>& header_names);
...@@ -149,7 +149,7 @@ class NET_EXPORT HttpResponseHeaders ...@@ -149,7 +149,7 @@ class NET_EXPORT HttpResponseHeaders
// NOTE: Do not make any assumptions about the encoding of this output // NOTE: Do not make any assumptions about the encoding of this output
// string. It may be non-ASCII, and the encoding used by the server is not // string. It may be non-ASCII, and the encoding used by the server is not
// necessarily known to us. Do not assume that this output is UTF-8! // necessarily known to us. Do not assume that this output is UTF-8!
bool GetNormalizedHeader(const std::string& name, std::string* value) const; bool GetNormalizedHeader(base::StringPiece name, std::string* value) const;
// Returns the normalized status line. // Returns the normalized status line.
std::string GetStatusLine() const; std::string GetStatusLine() const;
...@@ -200,17 +200,16 @@ class NET_EXPORT HttpResponseHeaders ...@@ -200,17 +200,16 @@ class NET_EXPORT HttpResponseHeaders
// To handle cases such as this, use GetNormalizedHeader to return the full // To handle cases such as this, use GetNormalizedHeader to return the full
// concatenated header, and then parse manually. // concatenated header, and then parse manually.
bool EnumerateHeader(size_t* iter, bool EnumerateHeader(size_t* iter,
const base::StringPiece& name, base::StringPiece name,
std::string* value) const; std::string* value) const;
// Returns true if the response contains the specified header-value pair. // Returns true if the response contains the specified header-value pair.
// Both name and value are compared case insensitively. // Both name and value are compared case insensitively.
bool HasHeaderValue(const base::StringPiece& name, bool HasHeaderValue(base::StringPiece name, base::StringPiece value) const;
const base::StringPiece& value) const;
// Returns true if the response contains the specified header. // Returns true if the response contains the specified header.
// The name is compared case insensitively. // The name is compared case insensitively.
bool HasHeader(const base::StringPiece& name) const; bool HasHeader(base::StringPiece name) const;
// Get the mime type and charset values in lower case form from the headers. // Get the mime type and charset values in lower case form from the headers.
// Empty strings are returned if the values are not present. // Empty strings are returned if the values are not present.
...@@ -355,14 +354,14 @@ class NET_EXPORT HttpResponseHeaders ...@@ -355,14 +354,14 @@ class NET_EXPORT HttpResponseHeaders
std::string::const_iterator line_end, std::string::const_iterator line_end,
bool has_headers); bool has_headers);
// Find the header in our list (case-insensitive) starting with parsed_ at // Find the header in our list (case-insensitive) starting with |parsed_| at
// index |from|. Returns string::npos if not found. // index |from|. Returns string::npos if not found.
size_t FindHeader(size_t from, const base::StringPiece& name) const; size_t FindHeader(size_t from, base::StringPiece name) const;
// Search the Cache-Control header for a directive matching |directive|. If // Search the Cache-Control header for a directive matching |directive|. If
// present, treat its value as a time offset in seconds, write it to |result|, // present, treat its value as a time offset in seconds, write it to |result|,
// and return true. // and return true.
bool GetCacheControlDirective(const base::StringPiece& directive, bool GetCacheControlDirective(base::StringPiece directive,
base::TimeDelta* result) const; base::TimeDelta* result) const;
// Add a header->value pair to our list. If we already have header in our // Add a header->value pair to our list. If we already have header in our
......
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