url_request_test_job.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_test_job.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <list> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/lazy_instance.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/message_loop.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/string_util.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/io_buffer.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_headers.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::list<URLRequestTestJob*> URLRequestJobList; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::LazyInstance<URLRequestJobList>::Leaky 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs = LAZY_INSTANCE_INITIALIZER; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static getters for known URLs 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestTestJob::test_url_1() { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GURL("test:url1"); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestTestJob::test_url_2() { 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GURL("test:url2"); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestTestJob::test_url_3() { 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GURL("test:url3"); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestTestJob::test_url_error() { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GURL("test:error"); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static getters for known URL responses 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_data_1() { 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string("<html><title>Test One</title></html>"); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_data_2() { 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string("<html><title>Test Two Two</title></html>"); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_data_3() { 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string("<html><title>Test Three Three Three</title></html>"); 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static getter for simple response headers 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_headers() { 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const char kHeaders[] = 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "HTTP/1.1 200 OK\0" 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "Content-type: text/html\0" 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "\0"; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(kHeaders, arraysize(kHeaders)); 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static getter for redirect response headers 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_redirect_headers() { 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const char kHeaders[] = 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "HTTP/1.1 302 MOVED\0" 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "Location: somewhere\0" 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "\0"; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(kHeaders, arraysize(kHeaders)); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static getter for error response headers 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string URLRequestTestJob::test_error_headers() { 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const char kHeaders[] = 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "HTTP/1.1 500 BOO HOO\0" 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "\0"; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(kHeaders, arraysize(kHeaders)); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequestTestJob::Factory(URLRequest* request, 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NetworkDelegate* network_delegate, 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& scheme) { 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return new URLRequestTestJob(request, network_delegate); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestTestJob::URLRequestTestJob(URLRequest* request, 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NetworkDelegate* network_delegate) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : URLRequestJob(request, network_delegate), 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) auto_advance_(false), 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_(WAITING), 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) priority_(DEFAULT_PRIORITY), 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset_(0), 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_(NULL), 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_size_(0), 96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) weak_factory_(this) { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestTestJob::URLRequestTestJob(URLRequest* request, 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NetworkDelegate* network_delegate, 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool auto_advance) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : URLRequestJob(request, network_delegate), 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) auto_advance_(auto_advance), 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_(WAITING), 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) priority_(DEFAULT_PRIORITY), 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset_(0), 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_(NULL), 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_size_(0), 109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) weak_factory_(this) { 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestTestJob::URLRequestTestJob(URLRequest* request, 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NetworkDelegate* network_delegate, 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& response_headers, 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& response_data, 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool auto_advance) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : URLRequestJob(request, network_delegate), 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) auto_advance_(auto_advance), 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_(WAITING), 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) priority_(DEFAULT_PRIORITY), 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_headers_(new HttpResponseHeaders(response_headers)), 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_data_(response_data), 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset_(0), 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_(NULL), 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_size_(0), 126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) weak_factory_(this) { 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestTestJob::~URLRequestTestJob() { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().erase( 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::remove( 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().end()); 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(mime_type); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!response_headers_) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return response_headers_->GetMimeType(mime_type); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void URLRequestTestJob::SetPriority(RequestPriority priority) { 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) priority_ = priority; 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::Start() { 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Start reading asynchronously so that all error reporting and data 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callbacks happen as they would for network requests. 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::MessageLoop::current()->PostTask( 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync, 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) weak_factory_.GetWeakPtr())); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::StartAsync() { 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!response_headers_) { 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_headers_ = new HttpResponseHeaders(test_headers()); 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (request_->url().spec() == test_url_1().spec()) { 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_data_ = test_data_1(); 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else if (request_->url().spec() == test_url_2().spec()) { 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_data_ = test_data_2(); 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else if (request_->url().spec() == test_url_3().spec()) { 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) response_data_ = test_data_3(); 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AdvanceJob(); 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // unexpected url, return error 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FIXME(brettw) we may want to use WININET errors or have some more types 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of errors 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ERR_INVALID_URL)); 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FIXME(brettw): this should emulate a network error, and not just fail 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // initiating a connection 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AdvanceJob(); 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) this->NotifyHeadersComplete(); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size, 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int *bytes_read) { 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (stage_ == WAITING) { 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_ = buf; 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) async_buf_size_ = buf_size; 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(bytes_read); 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *bytes_read = 0; 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (offset_ >= static_cast<int>(response_data_.length())) { 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; // done reading 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int to_read = buf_size; 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (to_read + offset_ > static_cast<int>(response_data_.length())) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) to_read = static_cast<int>(response_data_.length()) - offset_; 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset_ += to_read; 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *bytes_read = to_read; 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (response_headers_) 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) info->headers = response_headers_; 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void URLRequestTestJob::GetLoadTimingInfo( 217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadTimingInfo* load_timing_info) const { 218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Preserve the times the URLRequest is responsible for, but overwrite all 219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // the others. 220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::TimeTicks request_start = load_timing_info->request_start; 221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::Time request_start_time = load_timing_info->request_start_time; 222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) *load_timing_info = load_timing_info_; 223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_timing_info->request_start = request_start; 224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_timing_info->request_start_time = request_start_time; 225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int URLRequestTestJob::GetResponseCode() const { 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (response_headers_) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return response_headers_->response_code(); 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return -1; 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequestTestJob::IsRedirectResponse(GURL* location, 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int* http_status_code) { 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!response_headers_) 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string value; 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!response_headers_->IsRedirect(&value)) 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *location = request_->url().Resolve(value); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *http_status_code = response_headers_->response_code(); 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::Kill() { 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = DONE; 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) URLRequestJob::Kill(); 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) weak_factory_.InvalidateWeakPtrs(); 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().erase( 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::remove( 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().end()); 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::ProcessNextOperation() { 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch (stage_) { 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case WAITING: 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Must call AdvanceJob() prior to NotifyReadComplete() since that may 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // delete |this|. 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AdvanceJob(); 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = DATA_AVAILABLE; 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // OK if ReadRawData wasn't called yet. 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (async_buf_) { 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int bytes_read; 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read)) 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED() << "This should not return false in DATA_AVAILABLE."; 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SetStatus(URLRequestStatus()); // clear the io pending flag 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (NextReadAsync()) { 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Make all future reads return io pending until the next 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ProcessNextOperation(). 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = WAITING; 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NotifyReadComplete(bytes_read); 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case DATA_AVAILABLE: 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AdvanceJob(); 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = ALL_DATA; // done sending data 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case ALL_DATA: 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) stage_ = DONE; 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case DONE: 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED() << "Invalid stage"; 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequestTestJob::NextReadAsync() { 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestTestJob::AdvanceJob() { 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (auto_advance_) { 29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::MessageLoop::current()->PostTask( 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation, 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) weak_factory_.GetWeakPtr())); 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().push_back(this); 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequestTestJob::ProcessOnePendingMessage() { 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (g_pending_jobs.Get().empty()) 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) URLRequestTestJob* next_job(g_pending_jobs.Get().front()); 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) g_pending_jobs.Get().pop_front(); 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) next_job->ProcessNextOperation(); 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net 321