| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/automation/url_request_automation_job.h" | 5 #include "chrome/browser/automation/url_request_automation_job.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "chrome/browser/automation/automation_resource_message_filter.h" | 9 #include "chrome/browser/automation/automation_resource_message_filter.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/browser/renderer_host/render_view_host.h" | 11 #include "chrome/browser/renderer_host/render_view_host.h" |
| 12 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 12 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 13 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" | 13 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" |
| 14 #include "chrome/test/automation/automation_messages.h" | 14 #include "chrome/test/automation/automation_messages.h" |
| 15 #include "net/base/cookie_monster.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
| 18 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 19 | 20 |
| 20 using base::Time; | 21 using base::Time; |
| 21 using base::TimeDelta; | 22 using base::TimeDelta; |
| 22 | 23 |
| 23 // The list of filtered headers that are removed from requests sent via | 24 // The list of filtered headers that are removed from requests sent via |
| 24 // StartAsync(). These must be lower case. | 25 // StartAsync(). These must be lower case. |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 redirect_url_ = response.redirect_url; | 250 redirect_url_ = response.redirect_url; |
| 250 redirect_status_ = response.redirect_status; | 251 redirect_status_ = response.redirect_status; |
| 251 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || | 252 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || |
| 252 (redirect_status_ >= 300 && redirect_status_ < 400)); | 253 (redirect_status_ >= 300 && redirect_status_ < 400)); |
| 253 | 254 |
| 254 GURL url_for_cookies = | 255 GURL url_for_cookies = |
| 255 GURL(redirect_url_.empty() ? request_->url().spec().c_str() : | 256 GURL(redirect_url_.empty() ? request_->url().spec().c_str() : |
| 256 redirect_url_.c_str()); | 257 redirect_url_.c_str()); |
| 257 | 258 |
| 258 URLRequestContext* ctx = request_->context(); | 259 URLRequestContext* ctx = request_->context(); |
| 260 std::vector<std::string> response_cookies; |
| 259 | 261 |
| 260 if (!response.headers.empty()) { | 262 if (!response.headers.empty()) { |
| 261 headers_ = new net::HttpResponseHeaders( | 263 headers_ = new net::HttpResponseHeaders( |
| 262 net::HttpUtil::AssembleRawHeaders(response.headers.data(), | 264 net::HttpUtil::AssembleRawHeaders(response.headers.data(), |
| 263 response.headers.size())); | 265 response.headers.size())); |
| 264 // Parse and set HTTP cookies. | 266 // Parse and set HTTP cookies. |
| 265 const std::string name = "Set-Cookie"; | 267 const std::string name = "Set-Cookie"; |
| 266 std::string value; | 268 std::string value; |
| 267 std::vector<std::string> response_cookies; | |
| 268 | 269 |
| 269 void* iter = NULL; | 270 void* iter = NULL; |
| 270 while (headers_->EnumerateHeader(&iter, name, &value)) { | 271 while (headers_->EnumerateHeader(&iter, name, &value)) { |
| 271 if (request_->context()->InterceptCookie(request_, &value)) | 272 if (request_->context()->InterceptCookie(request_, &value)) |
| 272 response_cookies.push_back(value); | 273 response_cookies.push_back(value); |
| 273 } | 274 } |
| 274 | 275 |
| 275 if (response_cookies.size()) { | 276 if (response_cookies.size()) { |
| 276 if (ctx && ctx->cookie_store() && | 277 if (ctx && ctx->cookie_store() && |
| 277 ctx->cookie_policy()->CanSetCookie( | 278 ctx->cookie_policy()->CanSetCookie( |
| 278 url_for_cookies, request_->first_party_for_cookies())) { | 279 url_for_cookies, request_->first_party_for_cookies())) { |
| 279 net::CookieOptions options; | 280 net::CookieOptions options; |
| 280 options.set_include_httponly(); | 281 options.set_include_httponly(); |
| 281 ctx->cookie_store()->SetCookiesWithOptions(url_for_cookies, | 282 ctx->cookie_store()->SetCookiesWithOptions(url_for_cookies, |
| 282 response_cookies, | 283 response_cookies, |
| 283 options); | 284 options); |
| 284 } | 285 } |
| 285 } | 286 } |
| 286 } | 287 } |
| 287 | 288 |
| 288 if (ctx && ctx->cookie_store() && !response.persistent_cookies.empty() && | 289 if (ctx && ctx->cookie_store() && !response.persistent_cookies.empty() && |
| 289 ctx->cookie_policy()->CanSetCookie( | 290 ctx->cookie_policy()->CanSetCookie( |
| 290 url_for_cookies, request_->first_party_for_cookies())) { | 291 url_for_cookies, request_->first_party_for_cookies())) { |
| 291 StringTokenizer cookie_parser(response.persistent_cookies, ";"); | 292 StringTokenizer cookie_parser(response.persistent_cookies, ";"); |
| 292 | 293 |
| 293 while (cookie_parser.GetNext()) { | 294 while (cookie_parser.GetNext()) { |
| 294 net::CookieOptions options; | 295 std::string cookie_string = cookie_parser.token(); |
| 295 ctx->cookie_store()->SetCookieWithOptions(url_for_cookies, | 296 // Only allow cookies with valid name value pairs. |
| 296 cookie_parser.token(), | 297 if (cookie_string.find('=') != std::string::npos) { |
| 297 options); | 298 TrimWhitespace(cookie_string, TRIM_ALL, &cookie_string); |
| 299 // Ignore duplicate cookies, i.e. cookies passed in from the host |
| 300 // browser which also exist in the response header. |
| 301 if (!IsCookiePresentInCookieHeader(cookie_string, |
| 302 response_cookies)) { |
| 303 net::CookieOptions options; |
| 304 ctx->cookie_store()->SetCookieWithOptions(url_for_cookies, |
| 305 cookie_string, |
| 306 options); |
| 307 } |
| 308 } |
| 298 } | 309 } |
| 299 } | 310 } |
| 300 | 311 |
| 301 NotifyHeadersComplete(); | 312 NotifyHeadersComplete(); |
| 302 } | 313 } |
| 303 | 314 |
| 304 void URLRequestAutomationJob::OnDataAvailable( | 315 void URLRequestAutomationJob::OnDataAvailable( |
| 305 int tab, int id, const std::string& bytes) { | 316 int tab, int id, const std::string& bytes) { |
| 306 DLOG(INFO) << "URLRequestAutomationJob: " << | 317 DLOG(INFO) << "URLRequestAutomationJob: " << |
| 307 request_->url().spec() << " - data available, Size: " << bytes.size(); | 318 request_->url().spec() << " - data available, Size: " << bytes.size(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 message_filter_->Send(new AutomationMsg_RequestStart(0, tab_, id_, | 440 message_filter_->Send(new AutomationMsg_RequestStart(0, tab_, id_, |
| 430 automation_request)); | 441 automation_request)); |
| 431 } | 442 } |
| 432 | 443 |
| 433 void URLRequestAutomationJob::DisconnectFromMessageFilter() { | 444 void URLRequestAutomationJob::DisconnectFromMessageFilter() { |
| 434 if (message_filter_) { | 445 if (message_filter_) { |
| 435 message_filter_->UnRegisterRequest(this); | 446 message_filter_->UnRegisterRequest(this); |
| 436 message_filter_ = NULL; | 447 message_filter_ = NULL; |
| 437 } | 448 } |
| 438 } | 449 } |
| 450 |
| 451 bool URLRequestAutomationJob::IsCookiePresentInCookieHeader( |
| 452 const std::string& cookie_line, |
| 453 const std::vector<std::string>& header_cookies) { |
| 454 net::CookieMonster::ParsedCookie parsed_current_cookie(cookie_line); |
| 455 for (size_t index = 0; index < header_cookies.size(); index++) { |
| 456 net::CookieMonster::ParsedCookie parsed_header_cookie( |
| 457 header_cookies[index]); |
| 458 |
| 459 if (parsed_header_cookie.Name() == parsed_current_cookie.Name()) |
| 460 return true; |
| 461 } |
| 462 |
| 463 return false; |
| 464 } |
| 465 |
| OLD | NEW |