diff options
author | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-16 21:05:47 +0000 |
---|---|---|
committer | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-16 21:05:47 +0000 |
commit | a5c713fdc8d0bf21f65ebdae95827e423d14b07c (patch) | |
tree | 82e553e97f38aa159978805e415fa683189201e0 /net/url_request/url_request_test_job.cc | |
parent | 2cb1ecb7aa5b6bf5c17cfaa75293121c8bd73130 (diff) | |
download | chromium_src-a5c713fdc8d0bf21f65ebdae95827e423d14b07c.zip chromium_src-a5c713fdc8d0bf21f65ebdae95827e423d14b07c.tar.gz chromium_src-a5c713fdc8d0bf21f65ebdae95827e423d14b07c.tar.bz2 |
URLRequest::Interceptor enhancements1) Allow an interceptor to change its mind and not intercept after all. This allows the decision to start or not to start to be made asynchronously.2) Allow an interceptor to intercept on error conditions if the original job fails. This is to support the FALLBACK semantics in the appcache.Info about where this is going can be found in the appcache design doc at https://docs.google.com/a/google.com/Doc?docid=agv6ghfsqr_15f749cgt3&hl=enI still have to put together test cases, so I'm not ready to submit this yet, but wanted to get some feedback at this point.
Review URL: http://codereview.chromium.org/67019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13877 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request/url_request_test_job.cc')
-rw-r--r-- | net/url_request/url_request_test_job.cc | 150 |
1 files changed, 113 insertions, 37 deletions
diff --git a/net/url_request/url_request_test_job.cc b/net/url_request/url_request_test_job.cc index d1db02d..4953b19 100644 --- a/net/url_request/url_request_test_job.cc +++ b/net/url_request/url_request_test_job.cc @@ -42,6 +42,32 @@ std::string URLRequestTestJob::test_data_3() { return std::string("<html><title>Test Three Three Three</title></html>"); } +// static getter for simple response headers +std::string URLRequestTestJob::test_headers() { + const char headers[] = + "HTTP/1.1 200 OK\0" + "Content-type: text/html\0" + "\0"; + return std::string(headers, arraysize(headers)); +} + +// static getter for redirect response headers +std::string URLRequestTestJob::test_redirect_headers() { + const char headers[] = + "HTTP/1.1 302 MOVED\0" + "Location: somewhere\0" + "\0"; + return std::string(headers, arraysize(headers)); +} + +// static getter for error response headers +std::string URLRequestTestJob::test_error_headers() { + const char headers[] = + "HTTP/1.1 500 BOO HOO\0" + "\0"; + return std::string(headers, arraysize(headers)); +} + // static URLRequestJob* URLRequestTestJob::Factory(URLRequest* request, const std::string& scheme) { @@ -50,17 +76,44 @@ URLRequestJob* URLRequestTestJob::Factory(URLRequest* request, URLRequestTestJob::URLRequestTestJob(URLRequest* request) : URLRequestJob(request), + auto_advance_(false), stage_(WAITING), offset_(0), async_buf_(NULL), async_buf_size_(0) { } -// Force the response to set a reasonable MIME type +URLRequestTestJob::URLRequestTestJob(URLRequest* request, bool auto_advance) + : URLRequestJob(request), + auto_advance_(auto_advance), + stage_(WAITING), + offset_(0), + async_buf_(NULL), + async_buf_size_(0) { +} + +URLRequestTestJob::URLRequestTestJob(URLRequest* request, + const std::string& response_headers, + const std::string& response_data, + bool auto_advance) + : URLRequestJob(request), + auto_advance_(auto_advance), + stage_(WAITING), + response_headers_(new net::HttpResponseHeaders(response_headers)), + response_data_(response_data), + offset_(0), + async_buf_(NULL), + async_buf_size_(0) { +} + +URLRequestTestJob::~URLRequestTestJob() { +} + bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { DCHECK(mime_type); - *mime_type = "text/html"; - return true; + if (!response_headers_) + return false; + return response_headers_->GetMimeType(mime_type); } void URLRequestTestJob::Start() { @@ -71,25 +124,28 @@ void URLRequestTestJob::Start() { } void URLRequestTestJob::StartAsync() { - if (request_->url().spec() == test_url_1().spec()) { - data_ = test_data_1(); - stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. - } else if (request_->url().spec() == test_url_2().spec()) { - data_ = test_data_2(); - } else if (request_->url().spec() == test_url_3().spec()) { - data_ = test_data_3(); - } else { - // unexpected url, return error - // FIXME(brettw) we may want to use WININET errors or have some more types - // of errors - NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, - net::ERR_INVALID_URL)); - // FIXME(brettw): this should emulate a network error, and not just fail - // initiating a connection - return; + if (!response_headers_) { + response_headers_ = new net::HttpResponseHeaders(test_headers()); + if (request_->url().spec() == test_url_1().spec()) { + response_data_ = test_data_1(); + stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. + } else if (request_->url().spec() == test_url_2().spec()) { + response_data_ = test_data_2(); + } else if (request_->url().spec() == test_url_3().spec()) { + response_data_ = test_data_3(); + } else { + // unexpected url, return error + // FIXME(brettw) we may want to use WININET errors or have some more types + // of errors + NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, + net::ERR_INVALID_URL)); + // FIXME(brettw): this should emulate a network error, and not just fail + // initiating a connection + return; + } } - pending_jobs.push_back(scoped_refptr<URLRequestTestJob>(this)); + AdvanceJob(); this->NotifyHeadersComplete(); } @@ -106,15 +162,15 @@ bool URLRequestTestJob::ReadRawData(net::IOBuffer* buf, int buf_size, DCHECK(bytes_read); *bytes_read = 0; - if (offset_ >= static_cast<int>(data_.length())) { + if (offset_ >= static_cast<int>(response_data_.length())) { return true; // done reading } int to_read = buf_size; - if (to_read + offset_ > static_cast<int>(data_.length())) - to_read = static_cast<int>(data_.length()) - offset_; + if (to_read + offset_ > static_cast<int>(response_data_.length())) + to_read = static_cast<int>(response_data_.length()) - offset_; - memcpy(buf->data(), &data_.c_str()[offset_], to_read); + memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); offset_ += to_read; *bytes_read = to_read; @@ -122,19 +178,31 @@ bool URLRequestTestJob::ReadRawData(net::IOBuffer* buf, int buf_size, } void URLRequestTestJob::GetResponseInfo(net::HttpResponseInfo* info) { - const std::string kResponseHeaders = StringPrintf( - "HTTP/1.1 200 OK%c" - "Content-type: text/html%c" - "%c", 0, 0, 0); - info->headers = new net::HttpResponseHeaders(kResponseHeaders); + if (response_headers_) + info->headers = response_headers_; +} + +bool URLRequestTestJob::IsRedirectResponse(GURL* location, + int* http_status_code) { + if (!response_headers_) + return false; + + std::string value; + if (!response_headers_->IsRedirect(&value)) + return false; + + *location = request_->url().Resolve(value); + *http_status_code = response_headers_->response_code(); + return true; } + void URLRequestTestJob::Kill() { stage_ = DONE; URLRequestJob::Kill(); } -bool URLRequestTestJob::ProcessNextOperation() { +void URLRequestTestJob::ProcessNextOperation() { switch (stage_) { case WAITING: stage_ = DATA_AVAILABLE; @@ -152,14 +220,23 @@ bool URLRequestTestJob::ProcessNextOperation() { break; case ALL_DATA: stage_ = DONE; - return false; + return; case DONE: - return false; + return; default: NOTREACHED() << "Invalid stage"; - return false; + return; } - return true; + AdvanceJob(); +} + +void URLRequestTestJob::AdvanceJob() { + if (auto_advance_) { + MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( + this, &URLRequestTestJob::ProcessNextOperation)); + return; + } + pending_jobs.push_back(scoped_refptr<URLRequestTestJob>(this)); } // static @@ -170,8 +247,7 @@ bool URLRequestTestJob::ProcessOnePendingMessage() { scoped_refptr<URLRequestTestJob> next_job(pending_jobs[0]); pending_jobs.erase(pending_jobs.begin()); - if (next_job->ProcessNextOperation()) - pending_jobs.push_back(next_job); - + DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q + next_job->ProcessNextOperation(); return true; } |