summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcaseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-21 13:38:45 +0000
committercaseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-21 13:38:45 +0000
commit50ba404bc0cbfc6de3b7490cac5e60035ca62479 (patch)
treeba9daa52bab3a66f821b46da98247863f722ad1f
parentfad4724ea22db25a776ddded782dbf0ee833f696 (diff)
downloadchromium_src-50ba404bc0cbfc6de3b7490cac5e60035ca62479.zip
chromium_src-50ba404bc0cbfc6de3b7490cac5e60035ca62479.tar.gz
chromium_src-50ba404bc0cbfc6de3b7490cac5e60035ca62479.tar.bz2
Honor User-Agent header added by WebCore
- do not strip User-Agent while flattening headers in renderer - do not override User-Agent header in the network stack if one is already supplied by higher level; - added RequestHeaders::SetHeaderIfMissing TEST=URLRequestTestHTTP.OverrideUserAgent BUG=http://crbug.com/67063 Review URL=http://codereview.chromium.org/6065008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72131 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--net/http/http_request_headers.cc7
-rw-r--r--net/http/http_request_headers.h6
-rw-r--r--net/http/http_request_headers_unittest.cc8
-rw-r--r--net/url_request/url_request_http_job.cc20
-rw-r--r--net/url_request/url_request_unittest.cc29
-rw-r--r--webkit/glue/weburlloader_impl.cc6
6 files changed, 59 insertions, 17 deletions
diff --git a/net/http/http_request_headers.cc b/net/http/http_request_headers.cc
index fc2ebcd..93ead01 100644
--- a/net/http/http_request_headers.cc
+++ b/net/http/http_request_headers.cc
@@ -76,6 +76,13 @@ void HttpRequestHeaders::Clear() {
headers_.clear();
}
+void HttpRequestHeaders::SetHeaderIfMissing(const base::StringPiece& key,
+ const base::StringPiece& value) {
+ HeaderVector::iterator it = FindHeader(key);
+ if (it == headers_.end())
+ headers_.push_back(HeaderKeyValuePair(key.as_string(), value.as_string()));
+}
+
void HttpRequestHeaders::SetHeader(const base::StringPiece& key,
const base::StringPiece& value) {
HeaderVector::iterator it = FindHeader(key);
diff --git a/net/http/http_request_headers.h b/net/http/http_request_headers.h
index 734194a..2962ae2 100644
--- a/net/http/http_request_headers.h
+++ b/net/http/http_request_headers.h
@@ -95,6 +95,12 @@ class HttpRequestHeaders {
// in the vector remains the same. When comparing |key|, case is ignored.
void SetHeader(const base::StringPiece& key, const base::StringPiece& value);
+ // Sets the header value pair for |key| and |value|, if |key| does not exist.
+ // If |key| already exists, the call is a no-op.
+ // When comparing |key|, case is ignored.
+ void SetHeaderIfMissing(const base::StringPiece& key,
+ const base::StringPiece& value);
+
// Removes the first header that matches (case insensitive) |key|.
void RemoveHeader(const base::StringPiece& key);
diff --git a/net/http/http_request_headers_unittest.cc b/net/http/http_request_headers_unittest.cc
index f3abfbe2..384ceeb 100644
--- a/net/http/http_request_headers_unittest.cc
+++ b/net/http/http_request_headers_unittest.cc
@@ -67,6 +67,14 @@ TEST(HttpRequestHeaders, SetEmptyHeader) {
EXPECT_EQ("Foo: Bar\r\nBar:\r\n\r\n", headers.ToString());
}
+TEST(HttpRequestHeaders, SetHeaderIfMissing) {
+ HttpRequestHeaders headers;
+ headers.SetHeaderIfMissing("Foo", "Bar");
+ EXPECT_EQ("Foo: Bar\r\n\r\n", headers.ToString());
+ headers.SetHeaderIfMissing("Foo", "Baz");
+ EXPECT_EQ("Foo: Bar\r\n\r\n", headers.ToString());
+}
+
TEST(HttpRequestHeaders, RemoveHeader) {
HttpRequestHeaders headers;
headers.SetHeader("Foo", "bar");
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
index 21b84f2..df0c7f0 100644
--- a/net/url_request/url_request_http_job.cc
+++ b/net/url_request/url_request_http_job.cc
@@ -303,18 +303,12 @@ void URLRequestHttpJob::AddExtraHeaders() {
if (context) {
// Only add default Accept-Language and Accept-Charset if the request
// didn't have them specified.
- if (!request_info_.extra_headers.HasHeader(
- HttpRequestHeaders::kAcceptLanguage)) {
- request_info_.extra_headers.SetHeader(
- HttpRequestHeaders::kAcceptLanguage,
- context->accept_language());
- }
- if (!request_info_.extra_headers.HasHeader(
- HttpRequestHeaders::kAcceptCharset)) {
- request_info_.extra_headers.SetHeader(
- HttpRequestHeaders::kAcceptCharset,
- context->accept_charset());
- }
+ request_info_.extra_headers.SetHeaderIfMissing(
+ HttpRequestHeaders::kAcceptLanguage,
+ context->accept_language());
+ request_info_.extra_headers.SetHeaderIfMissing(
+ HttpRequestHeaders::kAcceptCharset,
+ context->accept_charset());
}
}
@@ -660,7 +654,7 @@ void URLRequestHttpJob::Start() {
request_info_.priority = request_->priority();
if (request_->context()) {
- request_info_.extra_headers.SetHeader(
+ request_info_.extra_headers.SetHeaderIfMissing(
HttpRequestHeaders::kUserAgent,
request_->context()->GetUserAgent(request_->url()));
}
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc
index e53a6e3..82efe66 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -2642,3 +2642,32 @@ TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
MessageLoop::current()->Run();
EXPECT_EQ(std::string("koi-8r"), d.data_received());
}
+
+// Check that default User-Agent header is sent.
+TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
+ ASSERT_TRUE(test_server_.Start());
+
+ TestDelegate d;
+ TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
+ req.set_context(new TestURLRequestContext());
+ req.Start();
+ MessageLoop::current()->Run();
+ EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
+}
+
+// Check that if request overrides the User-Agent header,
+// the default is not appended.
+TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
+ ASSERT_TRUE(test_server_.Start());
+
+ TestDelegate d;
+ TestURLRequest
+ req(test_server_.GetURL("echoheaderoverride?User-Agent"), &d);
+ req.set_context(new TestURLRequestContext());
+ net::HttpRequestHeaders headers;
+ headers.SetHeader(net::HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
+ req.SetExtraRequestHeaders(headers);
+ req.Start();
+ MessageLoop::current()->Run();
+ EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
+}
diff --git a/webkit/glue/weburlloader_impl.cc b/webkit/glue/weburlloader_impl.cc
index 92d9713..d22b509 100644
--- a/webkit/glue/weburlloader_impl.cc
+++ b/webkit/glue/weburlloader_impl.cc
@@ -69,10 +69,8 @@ class HeaderFlattener : public WebHTTPHeaderVisitor {
const std::string& value_utf8 = value.utf8();
// Skip over referrer headers found in the header map because we already
- // pulled it out as a separate parameter. We likewise prune the UA since
- // that will be added back by the network layer.
- if (LowerCaseEqualsASCII(name_utf8, "referer") ||
- LowerCaseEqualsASCII(name_utf8, "user-agent"))
+ // pulled it out as a separate parameter.
+ if (LowerCaseEqualsASCII(name_utf8, "referer"))
return;
// Skip over "Cache-Control: max-age=0" header if the corresponding