1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome_frame/urlmon_url_request.h"
#include <wininet.h>
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome_frame/chrome_frame_activex_base.h"
#include "chrome_frame/urlmon_upload_data_stream.h"
#include "chrome_frame/utils.h"
#include "net/http/http_util.h"
#include "net/http/http_response_headers.h"
static const LARGE_INTEGER kZero = {0};
static const ULARGE_INTEGER kUnsignedZero = {0};
int UrlmonUrlRequest::instance_count_ = 0;
const char kXFrameOptionsHeader[] = "X-Frame-Options";
UrlmonUrlRequest::UrlmonUrlRequest()
: pending_read_size_(0),
status_(URLRequestStatus::FAILED, net::ERR_FAILED),
thread_(PlatformThread::CurrentId()),
redirect_status_(0),
parent_window_(NULL),
worker_thread_(NULL) {
DLOG(INFO) << StringPrintf("Created request. Obj: %X", this)
<< " Count: " << ++instance_count_;
}
UrlmonUrlRequest::~UrlmonUrlRequest() {
DLOG(INFO) << StringPrintf("Deleted request. Obj: %X", this)
<< " Count: " << --instance_count_;
}
bool UrlmonUrlRequest::Start() {
DCHECK_EQ(PlatformThread::CurrentId(), thread_);
if (!worker_thread_) {
NOTREACHED() << __FUNCTION__ << " Urlmon request thread not initialized";
return false;
}
Create(HWND_MESSAGE);
if (!IsWindow()) {
NOTREACHED() << "Failed to create urlmon message window: "
<< GetLastError();
return false;
}
// Take a self reference to maintain COM lifetime. This will be released
// in OnFinalMessage
AddRef();
request_handler()->AddRequest(this);
worker_thread_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &UrlmonUrlRequest::StartAsync));
return true;
}
void UrlmonUrlRequest::Stop() {
DCHECK_EQ(PlatformThread::CurrentId(), thread_);
if (!worker_thread_) {
NOTREACHED() << __FUNCTION__ << " Urlmon request thread not initialized";
return;
}
// We can remove the request from the map safely here if it is still valid.
// There is an additional reference on the UrlmonUrlRequest instance which
// is released when the task scheduled by the EndRequest function executes.
request_handler()->RemoveRequest(this);
worker_thread_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &UrlmonUrlRequest::StopAsync));
}
void UrlmonUrlRequest::StartAsync() {
DCHECK(worker_thread_ != NULL);
status_.set_status(URLRequestStatus::IO_PENDING);
HRESULT hr = StartAsyncDownload();
if (FAILED(hr)) {
// Do not call EndRequest() here since it will attempt to free references
// that have not been established.
status_.set_os_error(HresultToNetError(hr));
status_.set_status(URLRequestStatus::FAILED);
DLOG(ERROR) << "StartAsyncDownload failed";
EndRequest();
return;
}
}
void UrlmonUrlRequest::StopAsync() {
DCHECK(worker_thread_ != NULL);
if (binding_) {
binding_->Abort();
} else {
status_.set_status(URLRequestStatus::CANCELED);
status_.set_os_error(net::ERR_FAILED);
EndRequest();
}
}
void UrlmonUrlRequest::OnFinalMessage(HWND window) {
m_hWnd = NULL;
// Release the outstanding reference in the context of the UI thread to
// ensure that our instance gets deleted in the same thread which created it.
Release();
}
bool UrlmonUrlRequest::Read(int bytes_to_read) {
DCHECK_EQ(PlatformThread::CurrentId(), thread_);
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this);
if (!worker_thread_) {
NOTREACHED() << __FUNCTION__ << " Urlmon request thread not initialized";
return false;
}
worker_thread_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &UrlmonUrlRequest::ReadAsync,
bytes_to_read));
return true;
}
void UrlmonUrlRequest::ReadAsync(int bytes_to_read) {
// Send cached data if available.
CComObjectStackEx<SendStream> send_stream;
send_stream.Initialize(this);
size_t bytes_copied = 0;
if (cached_data_.is_valid() && cached_data_.Read(&send_stream, bytes_to_read,
&bytes_copied)) {
DLOG(INFO) << StringPrintf("URL: %s Obj: %X - bytes read from cache: %d",
url().c_str(), this, bytes_copied);
return;
}
// if the request is finished or there's nothing more to read
// then end the request
if (!status_.is_io_pending() || !binding_) {
DLOG(INFO) << StringPrintf("URL: %s Obj: %X. Response finished. Status: %d",
url().c_str(), this, status_.status());
EndRequest();
return;
}
pending_read_size_ = bytes_to_read;
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
"- Read pending for: " << bytes_to_read;
}
STDMETHODIMP UrlmonUrlRequest::OnStartBinding(
DWORD reserved, IBinding *binding) {
binding_ = binding;
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::GetPriority(LONG *priority) {
if (!priority)
return E_POINTER;
*priority = THREAD_PRIORITY_NORMAL;
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnLowResource(DWORD reserved) {
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnProgress(ULONG progress, ULONG max_progress,
ULONG status_code, LPCWSTR status_text) {
switch (status_code) {
case BINDSTATUS_REDIRECTING:
DCHECK(status_text != NULL);
DLOG(INFO) << "URL: " << url() << " redirected to "
<< status_text;
redirect_url_ = status_text;
// Fetch the redirect status as they aren't all equal (307 in particular
// retains the HTTP request verb).
redirect_status_ = GetHttpResponseStatus();
// NOTE: Even though RFC 2616 says to preserve the request method when
// following a 302 redirect, normal browsers don't do that. Instead they
// all convert a POST into a GET in response to a 302 and so shall we.
// For 307 redirects, browsers preserve the method. The RFC says to
// prompt the user to confirm the generation of a new POST request, but
// IE omits this prompt and so shall we.
if (redirect_status_ != 307 &&
LowerCaseEqualsASCII(method(), "post")) {
set_method("get");
ClearPostData();
}
break;
default:
DLOG(INFO) << " Obj: " << std::hex << this << " OnProgress(" << url()
<< StringPrintf(L") code: %i status: %ls", status_code, status_text);
break;
}
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnStopBinding(HRESULT result, LPCWSTR error) {
DCHECK(worker_thread_ != NULL);
DCHECK_EQ(PlatformThread::CurrentId(), worker_thread_->thread_id());
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
" - Request stopped, Result: " << std::hex << result <<
" Status: " << status_.status();
if (FAILED(result)) {
status_.set_status(URLRequestStatus::FAILED);
status_.set_os_error(HresultToNetError(result));
EndRequest();
} else {
status_.set_status(URLRequestStatus::SUCCESS);
status_.set_os_error(0);
}
DLOG(INFO) << "OnStopBinding received for request id: " << id();
// Release these variables after reporting EndRequest since we might need to
// access their state.
binding_ = NULL;
bind_context_ = NULL;
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::GetBindInfo(DWORD* bind_flags,
BINDINFO *bind_info) {
DCHECK(worker_thread_ != NULL);
DCHECK_EQ(PlatformThread::CurrentId(), worker_thread_->thread_id());
if ((bind_info == NULL) || (bind_info->cbSize == 0) || (bind_flags == NULL))
return E_INVALIDARG;
*bind_flags = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
if (LowerCaseEqualsASCII(method(), "get")) {
bind_info->dwBindVerb = BINDVERB_GET;
} else if (LowerCaseEqualsASCII(method(), "post")) {
bind_info->dwBindVerb = BINDVERB_POST;
// Bypass caching proxies on POSTs and avoid writing responses to POST
// requests to the browser's cache.
*bind_flags |= BINDF_GETNEWESTVERSION | BINDF_NOWRITECACHE |
BINDF_PRAGMA_NO_CACHE;
// Initialize the STGMEDIUM.
memset(&bind_info->stgmedData, 0, sizeof(STGMEDIUM));
bind_info->grfBindInfoF = 0;
bind_info->szCustomVerb = NULL;
if (get_upload_data(&bind_info->stgmedData.pstm) == S_OK) {
bind_info->stgmedData.tymed = TYMED_ISTREAM;
DLOG(INFO) << " Obj: " << std::hex << this << " POST request with "
<< Int64ToString(post_data_len()) << " bytes";
} else {
DLOG(INFO) << " Obj: " << std::hex << this
<< "POST request with no data!";
}
} else {
NOTREACHED() << "Unknown HTTP method.";
status_.set_status(URLRequestStatus::FAILED);
status_.set_os_error(net::ERR_INVALID_URL);
EndRequest();
return E_FAIL;
}
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnDataAvailable(DWORD flags, DWORD size,
FORMATETC* formatetc,
STGMEDIUM* storage) {
DCHECK(worker_thread_ != NULL);
DCHECK_EQ(PlatformThread::CurrentId(), worker_thread_->thread_id());
DLOG(INFO) << StringPrintf("URL: %s Obj: %X - Bytes available: %d",
url().c_str(), this, size);
if (!storage || (storage->tymed != TYMED_ISTREAM)) {
NOTREACHED();
return E_INVALIDARG;
}
IStream* read_stream = storage->pstm;
if (!read_stream) {
NOTREACHED();
return E_UNEXPECTED;
}
HRESULT hr = S_OK;
if (BSCF_FIRSTDATANOTIFICATION & flags) {
DCHECK(!cached_data_.is_valid());
cached_data_.Create();
}
// Always read data into cache. We have to read all the data here at this
// time or it won't be available later. Since the size of the data could
// be more than pending read size, it's not straightforward (or might even
// be impossible) to implement a true data pull model.
size_t bytes_available = 0;
cached_data_.Append(read_stream, &bytes_available);
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
" - Bytes read into cache: " << bytes_available;
if (pending_read_size_) {
CComObjectStackEx<SendStream> send_stream;
send_stream.Initialize(this);
cached_data_.Read(&send_stream, pending_read_size_, &pending_read_size_);
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
" - size read: " << pending_read_size_;
pending_read_size_ = 0;
} else {
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
" - waiting for remote read";
}
if (BSCF_LASTDATANOTIFICATION & flags) {
status_.set_status(URLRequestStatus::SUCCESS);
DLOG(INFO) << StringPrintf("URL: %s Obj: %X", url().c_str(), this) <<
" - end of data.";
}
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnObjectAvailable(REFIID iid, IUnknown *object) {
// We are calling BindToStorage on the moniker we should always get called
// back on OnDataAvailable and should never get OnObjectAvailable
NOTREACHED();
return E_NOTIMPL;
}
STDMETHODIMP UrlmonUrlRequest::BeginningTransaction(const wchar_t* url,
const wchar_t* current_headers, DWORD reserved,
wchar_t** additional_headers) {
DCHECK(worker_thread_ != NULL);
DCHECK_EQ(PlatformThread::CurrentId(), worker_thread_->thread_id());
if (!additional_headers) {
NOTREACHED();
return E_POINTER;
}
DLOG(INFO) << "URL: " << url << " Obj: " << std::hex << this <<
" - Request headers: \n" << current_headers;
HRESULT hr = S_OK;
std::string new_headers;
if (post_data_len() > 0) {
// Tack on the Content-Length header since when using an IStream type
// STGMEDIUM, it looks like it doesn't get set for us :(
new_headers = StringPrintf("Content-Length: %s\r\n",
Int64ToString(post_data_len()).c_str());
}
if (!extra_headers().empty()) {
// TODO(robertshield): We may need to sanitize headers on POST here.
new_headers += extra_headers();
}
if (!referrer().empty()) {
// Referrer is famously misspelled in HTTP:
new_headers += StringPrintf("Referer: %s\r\n", referrer().c_str());
}
if (!new_headers.empty()) {
*additional_headers = reinterpret_cast<wchar_t*>(
CoTaskMemAlloc((new_headers.size() + 1) * sizeof(wchar_t)));
if (*additional_headers == NULL) {
NOTREACHED();
hr = E_OUTOFMEMORY;
} else {
lstrcpynW(*additional_headers, ASCIIToWide(new_headers).c_str(),
new_headers.size());
}
}
return hr;
}
STDMETHODIMP UrlmonUrlRequest::OnResponse(DWORD dwResponseCode,
const wchar_t* response_headers, const wchar_t* request_headers,
wchar_t** additional_headers) {
DCHECK(worker_thread_ != NULL);
DCHECK_EQ(PlatformThread::CurrentId(), worker_thread_->thread_id());
std::string raw_headers = WideToUTF8(response_headers);
// Security check for frame busting headers. We don't honor the headers
// as-such, but instead simply kill requests which we've been asked to
// look for. This puts the onus on the user of the UrlRequest to specify
// whether or not requests should be inspected. For ActiveDocuments, the
// answer is "no", since WebKit's detection/handling is sufficient and since
// ActiveDocuments cannot be hosted as iframes. For NPAPI and ActiveX
// documents, the Initialize() function of the PluginUrlRequest object
// allows them to specify how they'd like requests handled. Both should
// set enable_frame_busting_ to true to avoid CSRF attacks.
// Should WebKit's handling of this ever change, we will need to re-visit
// how and when frames are killed to better mirror a policy which may
// do something other than kill the sub-document outright.
// NOTE(slightlyoff): We don't use net::HttpResponseHeaders here because
// of lingering ICU/base_noicu issues.
if (frame_busting_enabled_ &&
net::HttpUtil::HasHeader(raw_headers, kXFrameOptionsHeader)) {
DLOG(ERROR) << "X-Frame-Options header detected, navigation canceled";
return E_FAIL;
}
std::wstring url_for_persistent_cookies =
redirect_url_.empty() ? UTF8ToWide(url()) : redirect_url_;
std::string persistent_cookies;
DWORD cookie_size = 0; // NOLINT
InternetGetCookie(url_for_persistent_cookies.c_str(), NULL, NULL,
&cookie_size);
if (cookie_size) {
scoped_array<wchar_t> cookies(new wchar_t[cookie_size + 1]);
if (!InternetGetCookie(url_for_persistent_cookies.c_str(), NULL,
cookies.get(), &cookie_size)) {
NOTREACHED() << "InternetGetCookie failed. Error: " << GetLastError();
} else {
persistent_cookies = WideToUTF8(cookies.get());
}
}
OnResponseStarted("",
raw_headers.c_str(),
0,
base::Time(),
persistent_cookies,
redirect_url_.empty() ? std::string() :
WideToUTF8(redirect_url_),
redirect_status_);
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::GetWindow(const GUID& guid_reason,
HWND* parent_window) {
if (!parent_window) {
return E_INVALIDARG;
}
#ifndef NDEBUG
wchar_t guid[40] = {0};
::StringFromGUID2(guid_reason, guid, arraysize(guid));
DLOG(INFO) << " Obj: " << std::hex << this << " GetWindow: " <<
(guid_reason == IID_IAuthenticate ? L" - IAuthenticate" :
(guid_reason == IID_IHttpSecurity ? L"IHttpSecurity" :
(guid_reason == IID_IWindowForBindingUI ? L"IWindowForBindingUI" :
guid)));
#endif
// TODO(iyengar): This hits when running the URL request tests.
DLOG_IF(ERROR, !::IsWindow(parent_window_))
<< "UrlmonUrlRequest::GetWindow - no window!";
*parent_window = parent_window_;
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::Authenticate(HWND* parent_window,
LPWSTR* user_name,
LPWSTR* password) {
if (!parent_window) {
return E_INVALIDARG;
}
DCHECK(::IsWindow(parent_window_));
*parent_window = parent_window_;
return S_OK;
}
STDMETHODIMP UrlmonUrlRequest::OnSecurityProblem(DWORD problem) {
// Urlmon notifies the client of authentication problems, certificate
// errors, etc by querying the object implementing the IBindStatusCallback
// interface for the IHttpSecurity interface. If this interface is not
// implemented then Urlmon checks for the problem codes defined below
// and performs actions as defined below:-
// It invokes the ReportProgress method of the protocol sink with
// these problem codes and eventually invokes the ReportResult method
// on the protocol sink which ends up in a call to the OnStopBinding
// method of the IBindStatusCallBack interface.
// MSHTML's implementation of the IBindStatusCallback interface does not
// implement the IHttpSecurity interface. However it handles the
// OnStopBinding call with a HRESULT of 0x800c0019 and navigates to
// an interstitial page which presents the user with a choice of whether
// to abort the navigation.
// In our OnStopBinding implementation we stop the navigation and inform
// Chrome about the result. Ideally Chrome should behave in a manner similar
// to IE, i.e. display the SSL error interstitial page and if the user
// decides to proceed anyway we would turn off SSL warnings for that
// particular navigation and allow IE to download the content.
// We would need to return the certificate information to Chrome for display
// purposes. Currently we only return a dummy certificate to Chrome.
// At this point we decided that it is a lot of work at this point and
// decided to go with the easier option of implementing the IHttpSecurity
// interface and replicating the checks performed by Urlmon. This
// causes Urlmon to display a dialog box on the same lines as IE6.
DLOG(INFO) << __FUNCTION__ << " Security problem : " << problem;
// On IE6 the default IBindStatusCallback interface does not implement the
// IHttpSecurity interface and thus causes IE to put up a certificate error
// dialog box. We need to emulate this behavior for sites with mismatched
// certificates to work.
if (GetIEVersion() == IE_6)
return S_FALSE;
HRESULT hr = E_ABORT;
switch (problem) {
case ERROR_INTERNET_SEC_CERT_REV_FAILED: {
hr = RPC_E_RETRY;
break;
}
case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
case ERROR_INTERNET_SEC_CERT_CN_INVALID:
case ERROR_INTERNET_INVALID_CA: {
hr = S_FALSE;
break;
}
default: {
NOTREACHED() << "Unhandled security problem : " << problem;
break;
}
}
return hr;
}
HRESULT UrlmonUrlRequest::ConnectToExistingMoniker(IMoniker* moniker,
IBindCtx* context,
const std::wstring& url) {
if (!moniker || url.empty()) {
NOTREACHED() << "Invalid arguments";
return E_INVALIDARG;
}
DCHECK(moniker_.get() == NULL);
DCHECK(bind_context_.get() == NULL);
moniker_ = moniker;
bind_context_ = context;
set_url(WideToUTF8(url));
return S_OK;
}
HRESULT UrlmonUrlRequest::StartAsyncDownload() {
HRESULT hr = E_FAIL;
if (moniker_.get() == NULL) {
std::wstring wide_url = UTF8ToWide(url());
hr = CreateURLMonikerEx(NULL, wide_url.c_str(), moniker_.Receive(),
URL_MK_UNIFORM);
if (FAILED(hr)) {
NOTREACHED() << "CreateURLMonikerEx failed. Error: " << hr;
} else {
hr = CreateAsyncBindCtx(0, this, NULL, bind_context_.Receive());
DCHECK(SUCCEEDED(hr)) << "CreateAsyncBindCtx failed. Error: " << hr;
}
} else {
DCHECK(bind_context_.get() != NULL);
hr = RegisterBindStatusCallback(bind_context_, this, NULL, 0);
}
if (SUCCEEDED(hr)) {
ScopedComPtr<IStream> stream;
hr = moniker_->BindToStorage(bind_context_, NULL, __uuidof(IStream),
reinterpret_cast<void**>(stream.Receive()));
if (FAILED(hr)) {
// TODO(joshia): Look into. This currently fails for:
// http://user2:secret@localhost:1337/auth-basic?set-cookie-if-challenged
// when running the UrlRequest unit tests.
DLOG(ERROR) <<
StringPrintf("IUrlMoniker::BindToStorage failed. Error: 0x%08X.", hr)
<< std::endl << url();
DCHECK(hr == MK_E_SYNTAX);
}
}
DLOG_IF(ERROR, FAILED(hr))
<< StringPrintf(L"StartAsyncDownload failed: 0x%08X", hr);
return hr;
}
void UrlmonUrlRequest::EndRequest() {
DLOG(INFO) << __FUNCTION__;
// Special case. If the last request was a redirect and the current OS
// error value is E_ACCESSDENIED, that means an unsafe redirect was attempted.
// In that case, correct the OS error value to be the more specific
// ERR_UNSAFE_REDIRECT error value.
if (!status_.is_success() && status_.os_error() == net::ERR_ACCESS_DENIED) {
int status = GetHttpResponseStatus();
if (status >= 300 && status < 400) {
redirect_status_ = status; // store the latest redirect status value.
status_.set_os_error(net::ERR_UNSAFE_REDIRECT);
}
}
OnResponseEnd(status_);
// Remove the request mapping and release the outstanding reference to us in
// the context of the UI thread.
PostTask(FROM_HERE,
NewRunnableMethod(this, &UrlmonUrlRequest::EndRequestInternal));
}
void UrlmonUrlRequest::EndRequestInternal() {
// The request object could have been removed from the map in the
// OnRequestEnd callback which executes on receiving the
// AutomationMsg_RequestEnd IPC from Chrome.
request_handler()->RemoveRequest(this);
// The current instance could get destroyed in the context of DestroyWindow.
// We should not access the object after this.
DestroyWindow();
}
int UrlmonUrlRequest::GetHttpResponseStatus() const {
if (binding_ == NULL) {
DLOG(WARNING) << "GetHttpResponseStatus - no binding_";
return 0;
}
int http_status = 0;
ScopedComPtr<IWinInetHttpInfo> info;
if (SUCCEEDED(info.QueryFrom(binding_))) {
char status[10] = {0};
DWORD buf_size = sizeof(status);
if (SUCCEEDED(info->QueryInfo(HTTP_QUERY_STATUS_CODE, status, &buf_size,
0, NULL))) {
http_status = StringToInt(status);
} else {
NOTREACHED() << "Failed to get HTTP status";
}
} else {
NOTREACHED() << "failed to get IWinInetHttpInfo from binding_";
}
return http_status;
}
//
// UrlmonUrlRequest::Cache implementation.
//
size_t UrlmonUrlRequest::Cache::Size() {
size_t size = 0;
if (stream_) {
STATSTG cache_stat = {0};
stream_->Stat(&cache_stat, STATFLAG_NONAME);
DCHECK_EQ(0, cache_stat.cbSize.HighPart);
size = cache_stat.cbSize.LowPart;
}
return size;
}
size_t UrlmonUrlRequest::Cache::CurrentPos() {
size_t pos = 0;
if (stream_) {
ULARGE_INTEGER current_index = {0};
stream_->Seek(kZero, STREAM_SEEK_CUR, ¤t_index);
DCHECK_EQ(0, current_index.HighPart);
pos = current_index.LowPart;
}
return pos;
}
size_t UrlmonUrlRequest::Cache::SizeRemaining() {
size_t size = Size();
size_t pos = CurrentPos();
size_t size_remaining = 0;
if (size) {
DCHECK(pos <= size);
size_remaining = size - pos;
}
return size_remaining;
}
void UrlmonUrlRequest::Cache::Clear() {
if (!stream_) {
NOTREACHED();
return;
}
HRESULT hr = stream_->SetSize(kUnsignedZero);
DCHECK(SUCCEEDED(hr));
}
bool UrlmonUrlRequest::Cache::Read(IStream* dest, size_t size,
size_t* bytes_copied) {
if (!dest || !size) {
NOTREACHED();
return false;
}
// Copy the data and clear cache if there is no more data to copy.
ULARGE_INTEGER size_to_copy = {size, 0};
ULARGE_INTEGER size_written = {0};
stream_->CopyTo(dest, size_to_copy, NULL, &size_written);
if (size_written.LowPart && bytes_copied)
*bytes_copied = size_written.LowPart;
if (!SizeRemaining()) {
Clear();
stream_->Seek(kZero, STREAM_SEEK_SET, NULL);
}
return (size_written.LowPart != 0);
}
bool UrlmonUrlRequest::Cache::Append(IStream* source,
size_t* bytes_copied) {
if (!source) {
NOTREACHED();
return false;
}
size_t current_pos = CurrentPos();
stream_->Seek(kZero, STREAM_SEEK_END, NULL);
HRESULT hr = S_OK;
while (SUCCEEDED(hr)) {
DWORD chunk_read = 0; // NOLINT
hr = source->Read(read_buffer_, sizeof(read_buffer_), &chunk_read);
if (!chunk_read)
break;
DWORD chunk_written = 0; // NOLINT
stream_->Write(read_buffer_, chunk_read, &chunk_written);
DCHECK_EQ(chunk_read, chunk_written);
if (bytes_copied)
*bytes_copied += chunk_written;
}
LARGE_INTEGER last_read_position = {current_pos, 0};
stream_->Seek(last_read_position, STREAM_SEEK_SET, NULL);
return SUCCEEDED(hr);
}
bool UrlmonUrlRequest::Cache::Create() {
DCHECK(stream_ == NULL);
bool ret = SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, stream_.Receive()));
DCHECK(ret && stream_);
return ret;
}
net::Error UrlmonUrlRequest::HresultToNetError(HRESULT hr) {
// Useful reference:
// http://msdn.microsoft.com/en-us/library/ms775145(VS.85).aspx
net::Error ret = net::ERR_UNEXPECTED;
switch (hr) {
case S_OK:
ret = net::OK;
break;
case MK_E_SYNTAX:
ret = net::ERR_INVALID_URL;
break;
case INET_E_CANNOT_CONNECT:
ret = net::ERR_CONNECTION_FAILED;
break;
case INET_E_DOWNLOAD_FAILURE:
case INET_E_CONNECTION_TIMEOUT:
case E_ABORT:
ret = net::ERR_CONNECTION_ABORTED;
break;
case INET_E_DATA_NOT_AVAILABLE:
ret = net::ERR_EMPTY_RESPONSE;
break;
case INET_E_RESOURCE_NOT_FOUND:
// To behave more closely to the chrome network stack, we translate this
// error value as tunnel connection failed. This error value is tested
// in the ProxyTunnelRedirectTest and UnexpectedServerAuthTest tests.
ret = net::ERR_TUNNEL_CONNECTION_FAILED;
break;
case INET_E_INVALID_URL:
case INET_E_UNKNOWN_PROTOCOL:
case INET_E_REDIRECT_FAILED:
ret = net::ERR_INVALID_URL;
break;
case INET_E_INVALID_CERTIFICATE:
ret = net::ERR_CERT_INVALID;
break;
case E_ACCESSDENIED:
ret = net::ERR_ACCESS_DENIED;
break;
default:
DLOG(WARNING)
<< StringPrintf("TODO: translate HRESULT 0x%08X to net::Error", hr);
break;
}
return ret;
}
|