| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This class simulates what wininet does when a dns lookup fails. | 4 // This class simulates what wininet does when a dns lookup fails. |
| 5 | 5 |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/test/net/url_request_abort_on_end_job.h" | 12 #include "content/test/net/url_request_abort_on_end_job.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | |
| 18 #include "net/url_request/url_request_filter.h" | 17 #include "net/url_request/url_request_filter.h" |
| 19 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 const char kPageContent[] = "some data\r\n"; | 21 const char kPageContent[] = "some data\r\n"; |
| 23 } | 22 } |
| 24 | 23 |
| 25 const char URLRequestAbortOnEndJob::k400AbortOnEndUrl[] = | 24 const char URLRequestAbortOnEndJob::k400AbortOnEndUrl[] = |
| 26 "http://url.handled.by.abort.on.end/400"; | 25 "http://url.handled.by.abort.on.end/400"; |
| 27 | 26 |
| 28 // static | 27 // static |
| 29 void URLRequestAbortOnEndJob::AddUrlHandler() { | 28 void URLRequestAbortOnEndJob::AddUrlHandler() { |
| 30 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 29 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 31 filter->AddUrlHandler(GURL(k400AbortOnEndUrl), | 30 filter->AddUrlHandler(GURL(k400AbortOnEndUrl), |
| 32 &URLRequestAbortOnEndJob::Factory); | 31 &URLRequestAbortOnEndJob::Factory); |
| 33 } | 32 } |
| 34 | 33 |
| 35 // static | 34 // static |
| 36 net::URLRequestJob* URLRequestAbortOnEndJob::Factory( | 35 net::URLRequestJob* URLRequestAbortOnEndJob::Factory( |
| 37 net::URLRequest* request, | 36 net::URLRequest* request, |
| 37 net::NetworkDelegate* network_delegate, |
| 38 const std::string& scheme) { | 38 const std::string& scheme) { |
| 39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 40 return new URLRequestAbortOnEndJob(request); | 40 return new URLRequestAbortOnEndJob(request, network_delegate); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Private const version. | 43 // Private const version. |
| 44 void URLRequestAbortOnEndJob::GetResponseInfoConst( | 44 void URLRequestAbortOnEndJob::GetResponseInfoConst( |
| 45 net::HttpResponseInfo* info) const { | 45 net::HttpResponseInfo* info) const { |
| 46 // Send back mock headers. | 46 // Send back mock headers. |
| 47 std::string raw_headers; | 47 std::string raw_headers; |
| 48 if (LowerCaseEqualsASCII(k400AbortOnEndUrl, | 48 if (LowerCaseEqualsASCII(k400AbortOnEndUrl, |
| 49 request_->url().spec().c_str())) { | 49 request_->url().spec().c_str())) { |
| 50 raw_headers.append( | 50 raw_headers.append( |
| 51 "HTTP/1.1 400 This is not OK\n" | 51 "HTTP/1.1 400 This is not OK\n" |
| 52 "Content-type: text/plain\n"); | 52 "Content-type: text/plain\n"); |
| 53 } else { | 53 } else { |
| 54 NOTREACHED(); | 54 NOTREACHED(); |
| 55 } | 55 } |
| 56 // ParseRawHeaders expects \0 to end each header line. | 56 // ParseRawHeaders expects \0 to end each header line. |
| 57 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 57 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 58 info->headers = new net::HttpResponseHeaders(raw_headers); | 58 info->headers = new net::HttpResponseHeaders(raw_headers); |
| 59 } | 59 } |
| 60 | 60 |
| 61 URLRequestAbortOnEndJob::URLRequestAbortOnEndJob(net::URLRequest* request) | 61 URLRequestAbortOnEndJob::URLRequestAbortOnEndJob( |
| 62 : URLRequestJob(request, request->context()->network_delegate()), | 62 net::URLRequest* request, net::NetworkDelegate* network_delegate) |
| 63 : URLRequestJob(request, network_delegate), |
| 63 sent_data_(false), | 64 sent_data_(false), |
| 64 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 65 } | 66 } |
| 66 | 67 |
| 67 URLRequestAbortOnEndJob::~URLRequestAbortOnEndJob() { | 68 URLRequestAbortOnEndJob::~URLRequestAbortOnEndJob() { |
| 68 } | 69 } |
| 69 | 70 |
| 70 void URLRequestAbortOnEndJob::GetResponseInfo(net::HttpResponseInfo* info) { | 71 void URLRequestAbortOnEndJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 71 GetResponseInfoConst(info); | 72 GetResponseInfoConst(info); |
| 72 } | 73 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 98 return true; | 99 return true; |
| 99 } | 100 } |
| 100 | 101 |
| 101 SetStatus(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 102 SetStatus(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 102 net::ERR_CONNECTION_ABORTED)); | 103 net::ERR_CONNECTION_ABORTED)); |
| 103 *bytes_read = -1; | 104 *bytes_read = -1; |
| 104 return false; | 105 return false; |
| 105 } | 106 } |
| 106 | 107 |
| 107 | 108 |
| OLD | NEW |