OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // This class simulates what wininet does when a dns lookup fails. | |
5 | |
6 #include <algorithm> | |
7 #include <cstring> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/string_util.h" | |
11 #include "base/task.h" | |
12 #include "content/browser/net/url_request_abort_on_end_job.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "net/base/io_buffer.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/http/http_response_headers.h" | |
17 #include "net/url_request/url_request_filter.h" | |
18 #include "net/url_request/url_request_status.h" | |
19 | |
20 namespace { | |
21 const char kPageContent[] = "some data\r\n"; | |
22 } | |
23 | |
24 const char URLRequestAbortOnEndJob::k400AbortOnEndUrl[] = | |
25 "http://url.handled.by.abort.on.end/400"; | |
26 | |
27 // static | |
28 void URLRequestAbortOnEndJob::AddUrlHandler() { | |
29 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
30 filter->AddUrlHandler(GURL(k400AbortOnEndUrl), | |
31 &URLRequestAbortOnEndJob::Factory); | |
32 } | |
33 | |
34 // static | |
35 net::URLRequestJob* URLRequestAbortOnEndJob::Factory( | |
36 net::URLRequest* request, | |
37 const std::string& scheme) { | |
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
39 return new URLRequestAbortOnEndJob(request); | |
40 } | |
41 | |
42 // Private const version. | |
43 void URLRequestAbortOnEndJob::GetResponseInfoConst( | |
44 net::HttpResponseInfo* info) const { | |
45 // Send back mock headers. | |
46 std::string raw_headers; | |
47 if (LowerCaseEqualsASCII(k400AbortOnEndUrl, | |
48 request_->url().spec().c_str())) { | |
49 raw_headers.append( | |
50 "HTTP/1.1 400 This is not OK\n" | |
51 "Content-type: text/plain\n"); | |
52 } else { | |
53 NOTREACHED(); | |
54 } | |
55 // ParseRawHeaders expects \0 to end each header line. | |
56 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | |
57 info->headers = new net::HttpResponseHeaders(raw_headers); | |
58 } | |
59 | |
60 URLRequestAbortOnEndJob::URLRequestAbortOnEndJob(net::URLRequest* request) | |
61 : URLRequestJob(request), sent_data_(false), | |
62 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
63 } | |
64 | |
65 URLRequestAbortOnEndJob::~URLRequestAbortOnEndJob() { | |
66 } | |
67 | |
68 void URLRequestAbortOnEndJob::GetResponseInfo(net::HttpResponseInfo* info) { | |
69 GetResponseInfoConst(info); | |
70 } | |
71 | |
72 bool URLRequestAbortOnEndJob::GetMimeType(std::string* mime_type) const { | |
73 net::HttpResponseInfo info; | |
74 GetResponseInfoConst(&info); | |
75 return info.headers && info.headers->GetMimeType(mime_type); | |
76 } | |
77 | |
78 void URLRequestAbortOnEndJob::StartAsync() { | |
79 NotifyHeadersComplete(); | |
80 } | |
81 | |
82 void URLRequestAbortOnEndJob::Start() { | |
83 MessageLoop::current()->PostTask( | |
84 FROM_HERE, | |
85 base::Bind(&URLRequestAbortOnEndJob::StartAsync, | |
86 weak_factory_.GetWeakPtr())); | |
87 } | |
88 | |
89 bool URLRequestAbortOnEndJob::ReadRawData(net::IOBuffer* buf, | |
90 const int max_bytes, | |
91 int* bytes_read) { | |
92 if (!sent_data_) { | |
93 *bytes_read = std::max(size_t(max_bytes), sizeof(kPageContent)); | |
mmenke
2011/11/09 02:09:26
This should be min, methinks.
| |
94 std::memcpy(buf->data(), kPageContent, *bytes_read); | |
95 sent_data_ = true; | |
96 return true; | |
97 } | |
98 | |
99 SetStatus(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
100 net::ERR_CONNECTION_ABORTED)); | |
101 *bytes_read = -1; | |
102 return false; | |
103 } | |
104 | |
105 | |
OLD | NEW |