Chromium Code Reviews| Index: chrome_frame/http_negotiate.cc |
| diff --git a/chrome_frame/http_negotiate.cc b/chrome_frame/http_negotiate.cc |
| index 7ca3cead1b087ab92f729dcff720280534e8248c..08e51ae9a95becae91179942d764587ce2dc173b 100644 |
| --- a/chrome_frame/http_negotiate.cc |
| +++ b/chrome_frame/http_negotiate.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -83,8 +83,9 @@ class SimpleBindStatusCallback : public CComObjectRootEx<CComSingleThreadModel>, |
| }; |
| } // end namespace |
| -std::string AppendCFUserAgentString(LPCWSTR headers, |
| - LPCWSTR additional_headers) { |
| + |
| +std::string ExtractUserAgentFromHeaders(LPCWSTR headers, |
|
grt (UTC plus 2)
2012/03/20 17:27:45
the name of this method doesn't make its behavior
robertshield
2012/03/26 02:43:33
Done.
|
| + LPCWSTR additional_headers) { |
| using net::HttpUtil; |
| std::string ascii_headers; |
| @@ -107,23 +108,44 @@ std::string AppendCFUserAgentString(LPCWSTR headers, |
| user_agent_value = original_it.values(); |
| } |
| + return user_agent_value; |
| +} |
| + |
| +std::string FilterHeaders(const std::string& old_headers, |
|
grt (UTC plus 2)
2012/03/20 17:27:45
i find "filter" a bit vague. how about ExcludeFie
robertshield
2012/03/26 02:43:33
Done.
|
| + const char* exclude) { |
| + using net::HttpUtil; |
| + std::string new_headers; |
| + HttpUtil::HeadersIterator headers_iterator(old_headers.begin(), |
| + old_headers.end(), "\r\n"); |
| + while (headers_iterator.GetNext()) { |
| + std::string name(headers_iterator.name()); |
| + if (!LowerCaseEqualsASCII(name, exclude)) { |
| + new_headers += name + ": " + headers_iterator.values() + "\r\n"; |
|
grt (UTC plus 2)
2012/03/20 17:27:45
these three uses of operator+ involve lots of temp
robertshield
2012/03/26 02:43:33
Done.
|
| + } |
| + } |
| + |
| + return new_headers; |
| +} |
| + |
| +std::string AppendCFUserAgentString(LPCWSTR headers, |
| + LPCWSTR additional_headers) { |
| + std::string user_agent_value(ExtractUserAgentFromHeaders(headers, |
| + additional_headers)); |
| + |
| // Use the default "User-Agent" if none was provided. |
| if (user_agent_value.empty()) |
| user_agent_value = http_utils::GetDefaultUserAgent(); |
| // Now add chromeframe to it. |
| - user_agent_value = http_utils::AddChromeFrameToUserAgentValue( |
| - user_agent_value); |
| + user_agent_value = |
| + http_utils::AddChromeFrameToUserAgentValue(user_agent_value); |
| - // Build new headers, skip the existing user agent value from |
| - // existing headers. |
| + // Build a new set of additional headers, skipping the existing user agent |
| + // value if present. |
| std::string new_headers; |
| - headers_iterator.Reset(); |
| - while (headers_iterator.GetNext()) { |
| - std::string name(headers_iterator.name()); |
| - if (!LowerCaseEqualsASCII(name, kLowerCaseUserAgent)) { |
| - new_headers += name + ": " + headers_iterator.values() + "\r\n"; |
| - } |
| + if (additional_headers) { |
| + std::string ascii_headers = WideToASCII(additional_headers); |
|
grt (UTC plus 2)
2012/03/20 17:27:45
nit: use () rather than =
robertshield
2012/03/26 02:43:33
Done.
|
| + new_headers = FilterHeaders(ascii_headers, kLowerCaseUserAgent); |
| } |
| new_headers += "User-Agent: " + user_agent_value; |
|
grt (UTC plus 2)
2012/03/20 17:27:45
i'd advocate for .append or += three times here, t
robertshield
2012/03/26 02:43:33
Done.
|
| @@ -131,32 +153,50 @@ std::string AppendCFUserAgentString(LPCWSTR headers, |
| return new_headers; |
| } |
| +// Unconditionally adds the specified |user_agent_value| to the given set of |
| +// |headers|. |
| std::string ReplaceOrAddUserAgent(LPCWSTR headers, |
| const std::string& user_agent_value) { |
| - using net::HttpUtil; |
| - |
| std::string new_headers; |
| if (headers) { |
| std::string ascii_headers(WideToASCII(headers)); |
| - |
| - // Extract "User-Agent" from the headers. |
| - HttpUtil::HeadersIterator headers_iterator(ascii_headers.begin(), |
| - ascii_headers.end(), "\r\n"); |
| - |
| // Build new headers, skip the existing user agent value from |
| // existing headers. |
| - while (headers_iterator.GetNext()) { |
| - std::string name(headers_iterator.name()); |
| - if (!LowerCaseEqualsASCII(name, kLowerCaseUserAgent)) { |
| - new_headers += name + ": " + headers_iterator.values() + "\r\n"; |
| - } |
| - } |
| + new_headers = FilterHeaders(ascii_headers, kLowerCaseUserAgent); |
| } |
| new_headers += "User-Agent: " + user_agent_value; |
|
grt (UTC plus 2)
2012/03/20 17:27:45
use += instead of +
robertshield
2012/03/26 02:43:33
Done.
|
| new_headers += "\r\n"; |
| return new_headers; |
| } |
| +// Removes CF from any user agent header found in |headers| or |
| +// |additional_headers| and |
|
grt (UTC plus 2)
2012/03/20 17:27:45
and...?
robertshield
2012/03/26 02:43:33
... and this is where I fell asleep :-)
|
| +std::string RemoveCFUserAgentString(LPCWSTR headers, |
| + LPCWSTR additional_headers) { |
| + std::string user_agent_value(ExtractUserAgentFromHeaders(headers, |
| + additional_headers)); |
| + |
| + // Use the default "User-Agent" if none was provided. |
| + if (user_agent_value.empty()) |
| + user_agent_value = http_utils::GetDefaultUserAgent(); |
| + |
| + // Now remove chromeframe from it. |
| + user_agent_value = |
| + http_utils::RemoveChromeFrameFromUserAgentValue(user_agent_value); |
| + |
| + // Build a new set of additional headers, skipping the existing user agent |
| + // value if present. |
| + std::string new_headers; |
| + if (additional_headers) { |
| + std::string ascii_headers = WideToASCII(additional_headers); |
| + new_headers = FilterHeaders(ascii_headers, kLowerCaseUserAgent); |
| + } |
| + |
| + new_headers += "User-Agent: " + user_agent_value; |
|
grt (UTC plus 2)
2012/03/20 17:27:45
use += rather than +
robertshield
2012/03/26 02:43:33
Done.
|
| + new_headers += "\r\n"; |
| + return new_headers; |
| +} |
| + |
| HttpNegotiatePatch::HttpNegotiatePatch() { |
| } |
| @@ -231,14 +271,18 @@ HRESULT HttpNegotiatePatch::BeginningTransaction( |
| } |
| if (modify_user_agent_) { |
| std::string updated_headers; |
| + |
| if (IsGcfDefaultRenderer() && |
| RendererTypeForUrl(url) == RENDERER_TYPE_CHROME_DEFAULT_RENDERER) { |
| // Replace the user-agent header with Chrome's. |
| updated_headers = ReplaceOrAddUserAgent(*additional_headers, |
| http_utils::GetChromeUserAgent()); |
| + } else if (ShouldRemoveUAForUrl(url) == USER_AGENT_REMOVE) { |
| + updated_headers = RemoveCFUserAgentString(headers, *additional_headers); |
| } else { |
| updated_headers = AppendCFUserAgentString(headers, *additional_headers); |
| } |
| + |
| *additional_headers = reinterpret_cast<wchar_t*>(::CoTaskMemRealloc( |
| *additional_headers, |
| (updated_headers.length() + 1) * sizeof(wchar_t))); |