• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/url_request/url_request_test_job.h"
6 
7 #include <algorithm>
8 #include <list>
9 #include <memory>
10 
11 #include "base/compiler_specific.h"
12 #include "base/containers/cxx20_erase_list.h"
13 #include "base/functional/bind.h"
14 #include "base/lazy_instance.h"
15 #include "base/location.h"
16 #include "base/strings/string_util.h"
17 #include "base/task/single_thread_task_runner.h"
18 #include "base/time/time.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "net/http/http_response_headers.h"
22 #include "net/http/http_util.h"
23 
24 namespace net {
25 
26 namespace {
27 
28 typedef std::list<URLRequestTestJob*> URLRequestJobList;
29 base::LazyInstance<URLRequestJobList>::Leaky
30     g_pending_jobs = LAZY_INSTANCE_INITIALIZER;
31 
32 }  // namespace
33 
34 // static getters for known URLs
test_url_1()35 GURL URLRequestTestJob::test_url_1() {
36   return GURL("test:url1");
37 }
38 
test_url_2()39 GURL URLRequestTestJob::test_url_2() {
40   return GURL("test:url2");
41 }
42 
test_url_3()43 GURL URLRequestTestJob::test_url_3() {
44   return GURL("test:url3");
45 }
46 
test_url_4()47 GURL URLRequestTestJob::test_url_4() {
48   return GURL("test:url4");
49 }
50 
test_url_auto_advance_async_reads_1()51 GURL URLRequestTestJob::test_url_auto_advance_async_reads_1() {
52   return GURL("test:url_auto_advance_async_reads_1");
53 }
54 
test_url_error()55 GURL URLRequestTestJob::test_url_error() {
56   return GURL("test:error");
57 }
58 
test_url_redirect_to_url_1()59 GURL URLRequestTestJob::test_url_redirect_to_url_1() {
60   return GURL("test:redirect_to_1");
61 }
62 
test_url_redirect_to_url_2()63 GURL URLRequestTestJob::test_url_redirect_to_url_2() {
64   return GURL("test:redirect_to_2");
65 }
66 
67 // static getters for known URL responses
test_data_1()68 std::string URLRequestTestJob::test_data_1() {
69   return std::string("<html><title>Test One</title></html>");
70 }
test_data_2()71 std::string URLRequestTestJob::test_data_2() {
72   return std::string("<html><title>Test Two Two</title></html>");
73 }
test_data_3()74 std::string URLRequestTestJob::test_data_3() {
75   return std::string("<html><title>Test Three Three Three</title></html>");
76 }
test_data_4()77 std::string URLRequestTestJob::test_data_4() {
78   return std::string("<html><title>Test Four Four Four Four</title></html>");
79 }
80 
81 // static getter for simple response headers
test_headers()82 std::string URLRequestTestJob::test_headers() {
83   static const char kHeaders[] =
84       "HTTP/1.1 200 OK\n"
85       "Content-type: text/html\n"
86       "\n";
87   return std::string(kHeaders, std::size(kHeaders));
88 }
89 
90 // static getter for redirect response headers
test_redirect_headers()91 std::string URLRequestTestJob::test_redirect_headers() {
92   static const char kHeaders[] =
93       "HTTP/1.1 302 MOVED\n"
94       "Location: somewhere\n"
95       "\n";
96   return std::string(kHeaders, std::size(kHeaders));
97 }
98 
99 // static getter for redirect response headers
test_redirect_to_url_1_headers()100 std::string URLRequestTestJob::test_redirect_to_url_1_headers() {
101   std::string headers = "HTTP/1.1 302 MOVED";
102   headers.push_back('\n');
103   headers += "Location: ";
104   headers += test_url_1().spec();
105   headers.push_back('\n');
106   headers.push_back('\n');
107   return headers;
108 }
109 
110 // static getter for redirect response headers
test_redirect_to_url_2_headers()111 std::string URLRequestTestJob::test_redirect_to_url_2_headers() {
112   std::string headers = "HTTP/1.1 302 MOVED";
113   headers.push_back('\n');
114   headers += "Location: ";
115   headers += test_url_2().spec();
116   headers.push_back('\n');
117   headers.push_back('\n');
118   return headers;
119 }
120 
121 // static getter for error response headers
test_error_headers()122 std::string URLRequestTestJob::test_error_headers() {
123   static const char kHeaders[] =
124       "HTTP/1.1 500 BOO HOO\n"
125       "\n";
126   return std::string(kHeaders, std::size(kHeaders));
127 }
128 
URLRequestTestJob(URLRequest * request,bool auto_advance)129 URLRequestTestJob::URLRequestTestJob(URLRequest* request, bool auto_advance)
130     : URLRequestJob(request),
131       auto_advance_(auto_advance),
132       response_headers_length_(0) {}
133 
URLRequestTestJob(URLRequest * request,const std::string & response_headers,const std::string & response_data,bool auto_advance)134 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
135                                      const std::string& response_headers,
136                                      const std::string& response_data,
137                                      bool auto_advance)
138     : URLRequestJob(request),
139       auto_advance_(auto_advance),
140       response_data_(response_data),
141       response_headers_(base::MakeRefCounted<net::HttpResponseHeaders>(
142           net::HttpUtil::AssembleRawHeaders(response_headers))),
143       response_headers_length_(response_headers.size()) {}
144 
~URLRequestTestJob()145 URLRequestTestJob::~URLRequestTestJob() {
146   base::Erase(g_pending_jobs.Get(), this);
147 }
148 
GetMimeType(std::string * mime_type) const149 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const {
150   DCHECK(mime_type);
151   if (!response_headers_.get())
152     return false;
153   return response_headers_->GetMimeType(mime_type);
154 }
155 
SetPriority(RequestPriority priority)156 void URLRequestTestJob::SetPriority(RequestPriority priority) {
157   priority_ = priority;
158 }
159 
Start()160 void URLRequestTestJob::Start() {
161   // Start reading asynchronously so that all error reporting and data
162   // callbacks happen as they would for network requests.
163   base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
164       FROM_HERE, base::BindOnce(&URLRequestTestJob::StartAsync,
165                                 weak_factory_.GetWeakPtr()));
166 }
167 
StartAsync()168 void URLRequestTestJob::StartAsync() {
169   if (!response_headers_.get()) {
170     SetResponseHeaders(test_headers());
171     if (request_->url() == test_url_1()) {
172       response_data_ = test_data_1();
173       stage_ = DATA_AVAILABLE;  // Simulate a synchronous response for this one.
174     } else if (request_->url() == test_url_2()) {
175       response_data_ = test_data_2();
176     } else if (request_->url() == test_url_3()) {
177       response_data_ = test_data_3();
178     } else if (request_->url() == test_url_4()) {
179       response_data_ = test_data_4();
180     } else if (request_->url() == test_url_auto_advance_async_reads_1()) {
181       response_data_ = test_data_1();
182       stage_ = DATA_AVAILABLE;  // Data is available immediately.
183       async_reads_ = true;      // All reads complete asynchronously.
184     } else if (request_->url() == test_url_redirect_to_url_1()) {
185       SetResponseHeaders(test_redirect_to_url_1_headers());
186     } else if (request_->url() == test_url_redirect_to_url_2()) {
187       SetResponseHeaders(test_redirect_to_url_2_headers());
188     } else {
189       AdvanceJob();
190 
191       // Return an error on unexpected urls.
192       NotifyStartError(ERR_INVALID_URL);
193       return;
194     }
195   }
196 
197   AdvanceJob();
198 
199   this->NotifyHeadersComplete();
200 }
201 
SetResponseHeaders(const std::string & response_headers)202 void URLRequestTestJob::SetResponseHeaders(
203     const std::string& response_headers) {
204   response_headers_ = base::MakeRefCounted<HttpResponseHeaders>(
205       net::HttpUtil::AssembleRawHeaders(response_headers));
206   response_headers_length_ = response_headers.size();
207 }
208 
CopyDataForRead(IOBuffer * buf,int buf_size)209 int URLRequestTestJob::CopyDataForRead(IOBuffer* buf, int buf_size) {
210   int bytes_read = 0;
211   if (offset_ < static_cast<int>(response_data_.length())) {
212     bytes_read = buf_size;
213     if (bytes_read + offset_ > static_cast<int>(response_data_.length()))
214       bytes_read = static_cast<int>(response_data_.length()) - offset_;
215 
216     memcpy(buf->data(), &response_data_.c_str()[offset_], bytes_read);
217     offset_ += bytes_read;
218   }
219   return bytes_read;
220 }
221 
ReadRawData(IOBuffer * buf,int buf_size)222 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) {
223   if (stage_ == WAITING || async_reads_) {
224     async_buf_ = buf;
225     async_buf_size_ = buf_size;
226     if (stage_ != WAITING) {
227       stage_ = WAITING;
228       base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
229           FROM_HERE, base::BindOnce(&URLRequestTestJob::ProcessNextOperation,
230                                     weak_factory_.GetWeakPtr()));
231     }
232     return ERR_IO_PENDING;
233   }
234 
235   return CopyDataForRead(buf, buf_size);
236 }
237 
GetResponseInfo(HttpResponseInfo * info)238 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) {
239   if (response_headers_.get())
240     info->headers = response_headers_;
241 }
242 
GetLoadTimingInfo(LoadTimingInfo * load_timing_info) const243 void URLRequestTestJob::GetLoadTimingInfo(
244     LoadTimingInfo* load_timing_info) const {
245   // Preserve the times the URLRequest is responsible for, but overwrite all
246   // the others.
247   base::TimeTicks request_start = load_timing_info->request_start;
248   base::Time request_start_time = load_timing_info->request_start_time;
249   *load_timing_info = load_timing_info_;
250   load_timing_info->request_start = request_start;
251   load_timing_info->request_start_time = request_start_time;
252 }
253 
GetTotalReceivedBytes() const254 int64_t URLRequestTestJob::GetTotalReceivedBytes() const {
255   return response_headers_length_ + offset_;
256 }
257 
IsRedirectResponse(GURL * location,int * http_status_code,bool * insecure_scheme_was_upgraded)258 bool URLRequestTestJob::IsRedirectResponse(GURL* location,
259                                            int* http_status_code,
260                                            bool* insecure_scheme_was_upgraded) {
261   if (!response_headers_.get())
262     return false;
263 
264   std::string value;
265   if (!response_headers_->IsRedirect(&value))
266     return false;
267 
268   *insecure_scheme_was_upgraded = false;
269   *location = request_->url().Resolve(value);
270   *http_status_code = response_headers_->response_code();
271   return true;
272 }
273 
Kill()274 void URLRequestTestJob::Kill() {
275   stage_ = DONE;
276   URLRequestJob::Kill();
277   weak_factory_.InvalidateWeakPtrs();
278   base::Erase(g_pending_jobs.Get(), this);
279 }
280 
ProcessNextOperation()281 void URLRequestTestJob::ProcessNextOperation() {
282   switch (stage_) {
283     case WAITING:
284       // Must call AdvanceJob() prior to NotifyReadComplete() since that may
285       // delete |this|.
286       AdvanceJob();
287       stage_ = DATA_AVAILABLE;
288       // OK if ReadRawData wasn't called yet.
289       if (async_buf_) {
290         int result = CopyDataForRead(async_buf_.get(), async_buf_size_);
291         if (result < 0)
292           NOTREACHED() << "Reads should not fail in DATA_AVAILABLE.";
293         if (NextReadAsync()) {
294           // Make all future reads return io pending until the next
295           // ProcessNextOperation().
296           stage_ = WAITING;
297         }
298         ReadRawDataComplete(result);
299       }
300       break;
301     case DATA_AVAILABLE:
302       AdvanceJob();
303       stage_ = ALL_DATA;  // done sending data
304       break;
305     case ALL_DATA:
306       stage_ = DONE;
307       return;
308     case DONE:
309       return;
310     default:
311       NOTREACHED() << "Invalid stage";
312       return;
313   }
314 }
315 
NextReadAsync()316 bool URLRequestTestJob::NextReadAsync() {
317   return false;
318 }
319 
AdvanceJob()320 void URLRequestTestJob::AdvanceJob() {
321   if (auto_advance_) {
322     base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
323         FROM_HERE, base::BindOnce(&URLRequestTestJob::ProcessNextOperation,
324                                   weak_factory_.GetWeakPtr()));
325     return;
326   }
327   g_pending_jobs.Get().push_back(this);
328 }
329 
330 // static
ProcessOnePendingMessage()331 bool URLRequestTestJob::ProcessOnePendingMessage() {
332   if (g_pending_jobs.Get().empty())
333     return false;
334 
335   URLRequestTestJob* next_job(g_pending_jobs.Get().front());
336   g_pending_jobs.Get().pop_front();
337 
338   DCHECK(!next_job->auto_advance());  // auto_advance jobs should be in this q
339   next_job->ProcessNextOperation();
340   return true;
341 }
342 
343 }  // namespace net
344