| 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" | |
| 16 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 18 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
| 19 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 20 | 19 |
| 21 using base::Time; | 20 using base::Time; |
| 22 using base::TimeDelta; | 21 using base::TimeDelta; |
| 23 | 22 |
| 24 // The list of filtered headers that are removed from requests sent via | 23 // The list of filtered headers that are removed from requests sent via |
| 25 // StartAsync(). These must be lower case. | 24 // StartAsync(). These must be lower case. |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 redirect_url_ = response.redirect_url; | 249 redirect_url_ = response.redirect_url; |
| 251 redirect_status_ = response.redirect_status; | 250 redirect_status_ = response.redirect_status; |
| 252 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || | 251 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || |
| 253 (redirect_status_ >= 300 && redirect_status_ < 400)); | 252 (redirect_status_ >= 300 && redirect_status_ < 400)); |
| 254 | 253 |
| 255 GURL url_for_cookies = | 254 GURL url_for_cookies = |
| 256 GURL(redirect_url_.empty() ? request_->url().spec().c_str() : | 255 GURL(redirect_url_.empty() ? request_->url().spec().c_str() : |
| 257 redirect_url_.c_str()); | 256 redirect_url_.c_str()); |
| 258 | 257 |
| 259 URLRequestContext* ctx = request_->context(); | 258 URLRequestContext* ctx = request_->context(); |
| 260 std::vector<std::string> response_cookies; | |
| 261 | 259 |
| 262 if (!response.headers.empty()) { | 260 if (!response.headers.empty()) { |
| 263 headers_ = new net::HttpResponseHeaders( | 261 headers_ = new net::HttpResponseHeaders( |
| 264 net::HttpUtil::AssembleRawHeaders(response.headers.data(), | 262 net::HttpUtil::AssembleRawHeaders(response.headers.data(), |
| 265 response.headers.size())); | 263 response.headers.size())); |
| 266 // Parse and set HTTP cookies. | 264 // Parse and set HTTP cookies. |
| 267 const std::string name = "Set-Cookie"; | 265 const std::string name = "Set-Cookie"; |
| 268 std::string value; | 266 std::string value; |
| 267 std::vector<std::string> response_cookies; |
| 269 | 268 |
| 270 void* iter = NULL; | 269 void* iter = NULL; |
| 271 while (headers_->EnumerateHeader(&iter, name, &value)) { | 270 while (headers_->EnumerateHeader(&iter, name, &value)) { |
| 272 if (request_->context()->InterceptCookie(request_, &value)) | 271 if (request_->context()->InterceptCookie(request_, &value)) |
| 273 response_cookies.push_back(value); | 272 response_cookies.push_back(value); |
| 274 } | 273 } |
| 275 | 274 |
| 276 if (response_cookies.size()) { | 275 if (response_cookies.size()) { |
| 277 if (ctx && ctx->cookie_store() && | 276 if (ctx && ctx->cookie_store() && |
| 278 ctx->cookie_policy()->CanSetCookie( | 277 ctx->cookie_policy()->CanSetCookie( |
| 279 url_for_cookies, request_->first_party_for_cookies())) { | 278 url_for_cookies, request_->first_party_for_cookies())) { |
| 280 net::CookieOptions options; | 279 net::CookieOptions options; |
| 281 options.set_include_httponly(); | 280 options.set_include_httponly(); |
| 282 ctx->cookie_store()->SetCookiesWithOptions(url_for_cookies, | 281 ctx->cookie_store()->SetCookiesWithOptions(url_for_cookies, |
| 283 response_cookies, | 282 response_cookies, |
| 284 options); | 283 options); |
| 285 } | 284 } |
| 286 } | 285 } |
| 287 } | 286 } |
| 288 | 287 |
| 289 if (ctx && ctx->cookie_store() && !response.persistent_cookies.empty() && | 288 if (ctx && ctx->cookie_store() && !response.persistent_cookies.empty() && |
| 290 ctx->cookie_policy()->CanSetCookie( | 289 ctx->cookie_policy()->CanSetCookie( |
| 291 url_for_cookies, request_->first_party_for_cookies())) { | 290 url_for_cookies, request_->first_party_for_cookies())) { |
| 292 StringTokenizer cookie_parser(response.persistent_cookies, ";"); | 291 StringTokenizer cookie_parser(response.persistent_cookies, ";"); |
| 293 | 292 |
| 294 while (cookie_parser.GetNext()) { | 293 while (cookie_parser.GetNext()) { |
| 295 std::string cookie_string = cookie_parser.token(); | 294 net::CookieOptions options; |
| 296 // Only allow cookies with valid name value pairs. | 295 ctx->cookie_store()->SetCookieWithOptions(url_for_cookies, |
| 297 if (cookie_string.find('=') != std::string::npos) { | 296 cookie_parser.token(), |
| 298 TrimWhitespace(cookie_string, TRIM_ALL, &cookie_string); | 297 options); |
| 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 } | |
| 309 } | 298 } |
| 310 } | 299 } |
| 311 | 300 |
| 312 NotifyHeadersComplete(); | 301 NotifyHeadersComplete(); |
| 313 } | 302 } |
| 314 | 303 |
| 315 void URLRequestAutomationJob::OnDataAvailable( | 304 void URLRequestAutomationJob::OnDataAvailable( |
| 316 int tab, int id, const std::string& bytes) { | 305 int tab, int id, const std::string& bytes) { |
| 317 DLOG(INFO) << "URLRequestAutomationJob: " << | 306 DLOG(INFO) << "URLRequestAutomationJob: " << |
| 318 request_->url().spec() << " - data available, Size: " << bytes.size(); | 307 request_->url().spec() << " - data available, Size: " << bytes.size(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 message_filter_->Send(new AutomationMsg_RequestStart(0, tab_, id_, | 429 message_filter_->Send(new AutomationMsg_RequestStart(0, tab_, id_, |
| 441 automation_request)); | 430 automation_request)); |
| 442 } | 431 } |
| 443 | 432 |
| 444 void URLRequestAutomationJob::DisconnectFromMessageFilter() { | 433 void URLRequestAutomationJob::DisconnectFromMessageFilter() { |
| 445 if (message_filter_) { | 434 if (message_filter_) { |
| 446 message_filter_->UnRegisterRequest(this); | 435 message_filter_->UnRegisterRequest(this); |
| 447 message_filter_ = NULL; | 436 message_filter_ = NULL; |
| 448 } | 437 } |
| 449 } | 438 } |
| 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 |