OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 | |
5 #include "content/browser/devtools/devtools_url_interceptor_request_job.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "content/browser/devtools/protocol/network_handler.h" | |
10 #include "net/base/elements_upload_data_stream.h" | |
11 #include "net/base/io_buffer.h" | |
12 #include "net/base/upload_bytes_element_reader.h" | |
13 #include "net/base/upload_element_reader.h" | |
14 #include "net/cert/cert_status_flags.h" | |
15 #include "net/http/http_response_headers.h" | |
16 #include "net/http/http_util.h" | |
17 #include "net/url_request/url_request_context.h" | |
18 | |
19 namespace content { | |
20 | |
21 using CommandStatus = DevToolsURLRequestInterceptor::CommandStatus; | |
22 | |
23 namespace { | |
24 class ProxyUploadElementReader : public net::UploadElementReader { | |
25 public: | |
26 explicit ProxyUploadElementReader(net::UploadElementReader* reader) | |
27 : reader_(reader) {} | |
28 | |
29 ~ProxyUploadElementReader() override {} | |
30 | |
31 // net::UploadElementReader overrides: | |
32 int Init(const net::CompletionCallback& callback) override { | |
33 return reader_->Init(callback); | |
34 } | |
35 | |
36 uint64_t GetContentLength() const override { | |
37 return reader_->GetContentLength(); | |
38 } | |
39 | |
40 uint64_t BytesRemaining() const override { return reader_->BytesRemaining(); } | |
41 | |
42 bool IsInMemory() const override { return reader_->IsInMemory(); } | |
43 | |
44 int Read(net::IOBuffer* buf, | |
45 int buf_length, | |
46 const net::CompletionCallback& callback) override { | |
47 return reader_->Read(buf, buf_length, callback); | |
48 } | |
49 | |
50 private: | |
51 net::UploadElementReader* reader_; // NOT OWNED | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(ProxyUploadElementReader); | |
54 }; | |
55 | |
56 std::unique_ptr<net::UploadElementReader> GetUploadData( | |
57 net::URLRequest* request) { | |
58 if (!request->has_upload()) | |
59 return nullptr; | |
60 | |
61 const net::UploadDataStream* stream = request->get_upload(); | |
62 if (!stream->GetElementReaders()) | |
63 return nullptr; | |
64 | |
65 DCHECK_EQ(1u, stream->GetElementReaders()->size()); | |
66 return base::MakeUnique<ProxyUploadElementReader>( | |
67 (*stream->GetElementReaders())[0].get()); | |
68 } | |
69 } // namespace | |
70 | |
71 DevToolsURLInterceptorRequestJob::DevToolsURLInterceptorRequestJob( | |
72 base::WeakPtr<DevToolsURLRequestInterceptor::State> | |
73 devtools_url_request_interceptor_state, | |
74 const std::string& interception_id, | |
75 net::URLRequest* original_request, | |
76 net::NetworkDelegate* original_network_delegate, | |
77 base::WeakPtr<protocol::NetworkHandler> network_handler, | |
78 bool is_redirect) | |
79 : net::URLRequestJob(original_request, original_network_delegate), | |
80 devtools_url_request_interceptor_state_( | |
81 devtools_url_request_interceptor_state), | |
82 request_details_(original_request->url(), | |
83 original_request->method(), | |
84 GetUploadData(original_request), | |
85 original_request->extra_request_headers(), | |
86 original_request->priority(), | |
87 original_request->context()), | |
88 waiting_for_user_response_(false), | |
89 interception_id_(interception_id), | |
90 network_handler_(network_handler), | |
91 io_thread_task_runner_( | |
dgozman
2017/05/25 21:29:35
Is this used anywhere?
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Once several patch sets ago, Its gone now.
| |
92 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | |
93 is_redirect_(is_redirect), | |
94 weak_ptr_factory_(this) { | |
95 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
96 devtools_url_request_interceptor_state_->RegisterJob(this, interception_id_); | |
dgozman
2017/05/25 21:29:35
if () ... since it's a weak pointer?
I also think
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Lets do the latter, we can reduce the number of me
| |
97 } | |
98 | |
99 DevToolsURLInterceptorRequestJob::~DevToolsURLInterceptorRequestJob() { | |
100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
101 if (devtools_url_request_interceptor_state_) { | |
102 devtools_url_request_interceptor_state_->UnregisterJob(interception_id_); | |
103 } | |
104 } | |
105 | |
106 // net::URLRequestJob implementation: | |
107 void DevToolsURLInterceptorRequestJob::SetExtraRequestHeaders( | |
108 const net::HttpRequestHeaders& headers) { | |
109 request_details_.extra_request_headers = headers; | |
110 } | |
111 | |
112 void DevToolsURLInterceptorRequestJob::Start() { | |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
114 if (is_redirect_) { | |
115 // If this is a fetch in response to a redirect, we have already sent the | |
116 // Network.requestIntercepted event and the user opted to allow it so | |
117 // there's no need to send another. We can just start the SubRequest. | |
118 sub_request_.reset(new SubRequest(request_details_, this)); | |
119 } else { | |
120 waiting_for_user_response_ = true; | |
121 BrowserThread::PostTask( | |
122 BrowserThread::UI, FROM_HERE, | |
123 base::Bind(&DevToolsURLInterceptorRequestJob:: | |
124 SendRequestInterceptedEventOnUiThread, | |
125 weak_ptr_factory_.GetWeakPtr())); | |
dgozman
2017/05/25 21:29:35
Cannot post to another thread with weak pointer cr
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Done.
| |
126 } | |
127 } | |
128 | |
129 void DevToolsURLInterceptorRequestJob::SendRequestInterceptedEventOnUiThread() { | |
130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
131 if (network_handler_) { | |
132 network_handler_->frontend()->RequestIntercepted( | |
133 interception_id_, | |
134 protocol::NetworkHandler::CreateRequestFromURLRequest(request())); | |
dgozman
2017/05/25 21:29:35
You should call this method before posting to UI t
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Done.
| |
135 } | |
136 } | |
137 | |
138 void DevToolsURLInterceptorRequestJob::Kill() { | |
139 if (sub_request_) | |
140 sub_request_->Cancel(); | |
141 | |
142 URLRequestJob::Kill(); | |
143 } | |
144 | |
145 int DevToolsURLInterceptorRequestJob::ReadRawData(net::IOBuffer* buf, | |
146 int buf_size) { | |
147 if (sub_request_) { | |
148 int size = sub_request_->request()->Read(buf, buf_size); | |
149 return size; | |
150 } else { | |
151 CHECK(mock_response_details_); | |
152 return mock_response_details_->ReadRawData(buf, buf_size); | |
153 } | |
154 } | |
155 | |
156 int DevToolsURLInterceptorRequestJob::GetResponseCode() const { | |
157 if (sub_request_) { | |
158 return sub_request_->request()->GetResponseCode(); | |
159 } else { | |
160 CHECK(mock_response_details_); | |
161 return mock_response_details_->response_headers()->response_code(); | |
162 } | |
163 } | |
164 | |
165 void DevToolsURLInterceptorRequestJob::GetResponseInfo( | |
166 net::HttpResponseInfo* info) { | |
167 // NOTE this can get called during URLRequestJob::NotifyStartError in which | |
168 // case we might not have either a sub request or a mock response. | |
169 if (sub_request_) { | |
170 *info = sub_request_->request()->response_info(); | |
171 } else if (mock_response_details_) { | |
172 info->headers = mock_response_details_->response_headers(); | |
173 } | |
174 } | |
175 | |
176 const net::HttpResponseHeaders* | |
177 DevToolsURLInterceptorRequestJob::GetHttpResponseHeaders() const { | |
178 if (sub_request_) { | |
179 net::URLRequest* request = sub_request_->request(); | |
180 return request->response_info().headers.get(); | |
181 } | |
182 CHECK(mock_response_details_); | |
183 return mock_response_details_->response_headers().get(); | |
184 } | |
185 | |
186 bool DevToolsURLInterceptorRequestJob::GetMimeType( | |
187 std::string* mime_type) const { | |
188 const net::HttpResponseHeaders* response_headers = GetHttpResponseHeaders(); | |
189 if (!response_headers) | |
190 return false; | |
191 return response_headers->GetMimeType(mime_type); | |
192 } | |
193 | |
194 bool DevToolsURLInterceptorRequestJob::GetCharset(std::string* charset) { | |
195 const net::HttpResponseHeaders* response_headers = GetHttpResponseHeaders(); | |
196 if (!response_headers) | |
197 return false; | |
198 return response_headers->GetCharset(charset); | |
199 } | |
200 | |
201 void DevToolsURLInterceptorRequestJob::GetLoadTimingInfo( | |
202 net::LoadTimingInfo* load_timing_info) const { | |
203 if (sub_request_) { | |
204 sub_request_->request()->GetLoadTimingInfo(load_timing_info); | |
205 } else { | |
206 CHECK(mock_response_details_); | |
207 // Since this request is mocked most of the fields are irrelevant. | |
208 load_timing_info->receive_headers_end = | |
209 mock_response_details_->response_time(); | |
210 } | |
211 } | |
212 | |
213 void DevToolsURLInterceptorRequestJob::OnAuthRequired( | |
214 net::URLRequest* request, | |
215 net::AuthChallengeInfo* auth_info) { | |
216 NOTREACHED(); | |
dgozman
2017/05/25 21:29:35
Can this happen? Should we do something meaningful
alex clarke (OOO till 29th)
2017/05/26 19:37:02
I don't think it can happen because the SubRequest
| |
217 } | |
218 | |
219 void DevToolsURLInterceptorRequestJob::OnCertificateRequested( | |
220 net::URLRequest* request, | |
221 net::SSLCertRequestInfo* cert_request_info) { | |
222 NOTREACHED(); | |
223 } | |
224 | |
225 void DevToolsURLInterceptorRequestJob::OnSSLCertificateError( | |
226 net::URLRequest* request, | |
227 const net::SSLInfo& ssl_info, | |
228 bool fatal) { | |
229 NOTREACHED(); | |
230 } | |
231 | |
232 void DevToolsURLInterceptorRequestJob::OnResponseStarted( | |
233 net::URLRequest* request, | |
234 int net_error) { | |
235 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
236 DCHECK(sub_request_); | |
237 DCHECK_EQ(request, sub_request_->request()); | |
238 DCHECK_NE(net::ERR_IO_PENDING, net_error); | |
239 | |
240 if (net_error != net::OK) { | |
241 sub_request_->Cancel(); | |
242 | |
243 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
244 static_cast<net::Error>(net_error))); | |
245 return; | |
246 } | |
247 | |
248 NotifyHeadersComplete(); | |
249 } | |
250 | |
251 void DevToolsURLInterceptorRequestJob::OnReadCompleted(net::URLRequest* request, | |
252 int num_bytes) { | |
253 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
254 DCHECK_EQ(request, sub_request_->request()); | |
255 | |
256 ReadRawDataComplete(num_bytes); | |
257 } | |
258 | |
259 void DevToolsURLInterceptorRequestJob::OnReceivedRedirect( | |
260 net::URLRequest* request, | |
261 const net::RedirectInfo& redirectinfo, | |
262 bool* defer_redirect) { | |
263 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
264 *defer_redirect = true; | |
265 | |
266 size_t iter = 0; | |
267 std::string header_name; | |
268 std::string header_value; | |
269 std::unique_ptr<protocol::DictionaryValue> headers_dict( | |
270 protocol::DictionaryValue::create()); | |
271 while (request->response_headers()->EnumerateHeaderLines(&iter, &header_name, | |
272 &header_value)) { | |
273 headers_dict->setString(header_name, header_value); | |
274 } | |
275 | |
276 redirect_.reset(new net::RedirectInfo(redirectinfo)); | |
277 sub_request_->Cancel(); | |
278 sub_request_.reset(); | |
279 | |
280 waiting_for_user_response_ = true; | |
281 BrowserThread::PostTask( | |
282 BrowserThread::UI, FROM_HERE, | |
283 base::Bind(&DevToolsURLInterceptorRequestJob:: | |
284 SendRedirectInterceptedEventOnUiThread, | |
285 weak_ptr_factory_.GetWeakPtr(), | |
dgozman
2017/05/25 21:29:35
Ditto.
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Done.
| |
286 base::Owned(headers_dict.release()), redirectinfo.status_code, | |
287 redirectinfo.new_url.spec())); | |
288 } | |
289 | |
290 void DevToolsURLInterceptorRequestJob::SendRedirectInterceptedEventOnUiThread( | |
291 protocol::DictionaryValue* headers_dict, | |
292 int http_status_code, | |
293 std::string redirect_url) { | |
294 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
295 return network_handler_->frontend()->RequestIntercepted( | |
296 interception_id_, | |
297 protocol::NetworkHandler::CreateRequestFromURLRequest(request()), | |
dgozman
2017/05/25 21:29:35
Ditto.
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Done.
| |
298 protocol::Object::fromValue(headers_dict, nullptr), http_status_code, | |
299 redirect_url); | |
300 } | |
301 | |
302 CommandStatus DevToolsURLInterceptorRequestJob::ContinueRequest( | |
303 std::unique_ptr<DevToolsURLRequestInterceptor::Modifications> | |
304 modifications) { | |
305 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
306 if (!waiting_for_user_response_) | |
307 return CommandStatus::CommandAlreadyProcessed; | |
308 waiting_for_user_response_ = false; | |
309 | |
310 if (modifications->error_reason) { | |
311 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
312 *modifications->error_reason)); | |
313 return CommandStatus::OK; | |
314 } | |
315 | |
316 if (modifications->raw_response) { | |
317 mock_response_details_.reset(new MockResponseDetails( | |
318 std::move(*modifications->raw_response), base::TimeTicks::Now())); | |
319 | |
320 std::string value; | |
321 if (mock_response_details_->response_headers()->IsRedirect(&value)) { | |
322 DCHECK(devtools_url_request_interceptor_state_); | |
323 devtools_url_request_interceptor_state_->ExpectRequestAfterRedirect( | |
324 request(), interception_id_); | |
325 } | |
326 NotifyHeadersComplete(); | |
327 return CommandStatus::OK; | |
328 } | |
329 | |
330 if (redirect_) { | |
dgozman
2017/05/25 21:29:35
So, this is to proceed after redirect? Very nice!
alex clarke (OOO till 29th)
2017/05/26 19:37:02
Acknowledged.
| |
331 // NOTE we don't append the text form of the status code because | |
332 // net::HttpResponseHeaders doesn't need that. | |
333 std::string raw_headers = | |
334 base::StringPrintf("HTTP/1.1 %d", redirect_->status_code); | |
335 raw_headers.append(1, '\0'); | |
336 raw_headers.append("Location: "); | |
337 raw_headers.append( | |
338 modifications->modified_url.fromMaybe(redirect_->new_url.spec())); | |
339 raw_headers.append(2, '\0'); | |
340 mock_response_details_.reset(new MockResponseDetails( | |
341 make_scoped_refptr(new net::HttpResponseHeaders(raw_headers)), "", 0, | |
342 base::TimeTicks::Now())); | |
343 redirect_.reset(); | |
344 | |
345 DCHECK(devtools_url_request_interceptor_state_); | |
346 devtools_url_request_interceptor_state_->ExpectRequestAfterRedirect( | |
347 request(), interception_id_); | |
348 NotifyHeadersComplete(); | |
349 } else { | |
350 // Note this redirect is not visible to the caller by design. If they want a | |
351 // visible redirect they can mock a response with a 302. | |
352 if (modifications->modified_url.isJust()) | |
353 request_details_.url = GURL(modifications->modified_url.fromJust()); | |
354 | |
355 if (modifications->modified_method.isJust()) | |
356 request_details_.method = modifications->modified_method.fromJust(); | |
357 | |
358 if (modifications->modified_post_data.isJust()) { | |
359 const std::string& post_data = | |
360 modifications->modified_post_data.fromJust(); | |
361 std::vector<char> data(post_data.begin(), post_data.end()); | |
362 request_details_.post_data.reset( | |
363 new net::UploadOwnedBytesElementReader(&data)); | |
364 } | |
365 | |
366 if (modifications->modified_headers.isJust()) { | |
367 request_details_.extra_request_headers.Clear(); | |
368 std::unique_ptr<protocol::DictionaryValue> headers = | |
369 modifications->modified_headers.fromJust()->toValue(); | |
370 for (size_t i = 0; i < headers->size(); i++) { | |
371 std::string value; | |
372 if (headers->at(i).second->asString(&value)) { | |
373 request_details_.extra_request_headers.SetHeader(headers->at(i).first, | |
374 value); | |
375 } | |
376 } | |
377 } | |
378 | |
379 // The reason we start a sub request is because we are in full control of it | |
380 // and can choose to ignore it if, for example, the fetch encounters a | |
381 // redirect that the user chooses to replace with a mock response. | |
382 sub_request_.reset(new SubRequest(request_details_, this)); | |
383 } | |
384 return CommandStatus::OK; | |
385 } | |
386 | |
387 DevToolsURLInterceptorRequestJob::RequestDetails::RequestDetails( | |
388 const GURL& url, | |
389 const std::string& method, | |
390 std::unique_ptr<net::UploadElementReader> post_data, | |
391 const net::HttpRequestHeaders& extra_request_headers, | |
392 const net::RequestPriority& priority, | |
393 const net::URLRequestContext* url_request_context) | |
394 : url(url), | |
395 method(method), | |
396 post_data(std::move(post_data)), | |
397 extra_request_headers(extra_request_headers), | |
398 priority(priority), | |
399 url_request_context(url_request_context) {} | |
400 | |
401 DevToolsURLInterceptorRequestJob::RequestDetails::~RequestDetails() {} | |
402 | |
403 DevToolsURLInterceptorRequestJob::SubRequest::SubRequest( | |
404 DevToolsURLInterceptorRequestJob::RequestDetails& request_details, | |
405 DevToolsURLInterceptorRequestJob* devtools_interceptor_request_job) | |
406 : devtools_interceptor_request_job_(devtools_interceptor_request_job), | |
407 devtools_url_request_interceptor_state_( | |
408 devtools_interceptor_request_job_ | |
409 ->devtools_url_request_interceptor_state_), | |
410 fetch_in_progress_(true) { | |
411 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
412 request_ = request_details.url_request_context->CreateRequest( | |
413 request_details.url, request_details.priority, | |
414 devtools_interceptor_request_job_), | |
415 request_->set_method(request_details.method); | |
416 request_->SetExtraRequestHeaders(request_details.extra_request_headers); | |
417 | |
418 if (request_details.post_data) { | |
419 request_->set_upload(net::ElementsUploadDataStream::CreateWithReader( | |
420 std::move(request_details.post_data), 0)); | |
421 } | |
422 | |
423 if (!devtools_url_request_interceptor_state_) | |
dgozman
2017/05/25 21:29:35
IIUC, this only happens on profile shutdown, so we
alex clarke (OOO till 29th)
2017/05/26 19:37:02
We can also make the state refcounted which guaran
| |
424 return; | |
425 devtools_url_request_interceptor_state_->RegisterSubRequest(request_.get()); | |
426 request_->Start(); | |
427 } | |
428 | |
429 DevToolsURLInterceptorRequestJob::SubRequest::~SubRequest() { | |
430 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
431 | |
432 if (!devtools_url_request_interceptor_state_) | |
433 return; | |
434 devtools_url_request_interceptor_state_->UnregisterSubRequest(request_.get()); | |
435 } | |
436 | |
437 void DevToolsURLInterceptorRequestJob::SubRequest::Cancel() { | |
438 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
439 if (!fetch_in_progress_) | |
440 return; | |
441 | |
442 fetch_in_progress_ = false; | |
443 request_->Cancel(); | |
444 } | |
445 | |
446 DevToolsURLInterceptorRequestJob::MockResponseDetails::MockResponseDetails( | |
447 std::string response_bytes, | |
448 base::TimeTicks response_time) | |
449 : response_bytes_(std::move(response_bytes)), | |
450 read_offset_(0), | |
451 response_time_(response_time) { | |
452 int header_size = net::HttpUtil::LocateEndOfHeaders(response_bytes_.c_str(), | |
453 response_bytes_.size()); | |
454 if (header_size == -1) { | |
455 LOG(WARNING) << "Can't find headers in result"; | |
456 response_headers_ = new net::HttpResponseHeaders(""); | |
457 } else { | |
458 response_headers_ = | |
459 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( | |
460 response_bytes_.c_str(), header_size)); | |
461 read_offset_ = header_size; | |
462 } | |
463 | |
464 CHECK_LE(read_offset_, response_bytes_.size()); | |
465 } | |
466 | |
467 DevToolsURLInterceptorRequestJob::MockResponseDetails::MockResponseDetails( | |
468 const scoped_refptr<net::HttpResponseHeaders>& response_headers, | |
469 std::string response_bytes, | |
470 size_t read_offset, | |
471 base::TimeTicks response_time) | |
472 : response_headers_(response_headers), | |
473 response_bytes_(std::move(response_bytes)), | |
474 read_offset_(read_offset), | |
475 response_time_(response_time) {} | |
476 | |
477 DevToolsURLInterceptorRequestJob::MockResponseDetails::~MockResponseDetails() {} | |
478 | |
479 int DevToolsURLInterceptorRequestJob::MockResponseDetails::ReadRawData( | |
480 net::IOBuffer* buf, | |
481 int buf_size) { | |
482 size_t bytes_available = response_bytes_.size() - read_offset_; | |
483 size_t bytes_to_copy = | |
484 std::min(static_cast<size_t>(buf_size), bytes_available); | |
485 if (bytes_to_copy > 0) { | |
486 std::memcpy(buf->data(), &response_bytes_.data()[read_offset_], | |
487 bytes_to_copy); | |
488 read_offset_ += bytes_to_copy; | |
489 } | |
490 return bytes_to_copy; | |
491 } | |
492 | |
493 } // namespace content | |
OLD | NEW |