Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/DebugOnly.h"
8
#include "mozilla/dom/FetchDriver.h"
9
10
#include "nsIAsyncVerifyRedirectCallback.h"
11
#include "mozilla/dom/Document.h"
12
#include "nsIInputStream.h"
13
#include "nsIOutputStream.h"
14
#include "nsIFileChannel.h"
15
#include "nsIHttpChannel.h"
16
#include "nsIHttpChannelInternal.h"
17
#include "nsISupportsPriority.h"
18
#include "nsIThreadRetargetableRequest.h"
19
#include "nsIUploadChannel2.h"
20
#include "nsIInterfaceRequestorUtils.h"
21
#include "nsIPipe.h"
22
23
#include "nsContentPolicyUtils.h"
24
#include "nsDataHandler.h"
25
#include "nsNetUtil.h"
26
#include "nsPrintfCString.h"
27
#include "nsProxyRelease.h"
28
#include "nsStreamUtils.h"
29
#include "nsStringStream.h"
30
#include "nsHttpChannel.h"
31
32
#include "mozilla/dom/BlobURLProtocolHandler.h"
33
#include "mozilla/dom/File.h"
34
#include "mozilla/dom/PerformanceStorage.h"
35
#include "mozilla/dom/UserActivation.h"
36
#include "mozilla/dom/WorkerCommon.h"
37
#include "mozilla/net/NeckoChannelParams.h"
38
#include "mozilla/ipc/PBackgroundSharedTypes.h"
39
#include "mozilla/StaticPrefs_browser.h"
40
#include "mozilla/StaticPrefs_network.h"
41
#include "mozilla/StaticPrefs_privacy.h"
42
#include "mozilla/Unused.h"
43
44
#include "Fetch.h"
45
#include "FetchUtil.h"
46
#include "InternalRequest.h"
47
#include "InternalResponse.h"
48
49
namespace mozilla {
50
namespace dom {
51
52
namespace {
53
54
void GetBlobURISpecFromChannel(nsIRequest* aRequest, nsCString& aBlobURISpec) {
55
MOZ_ASSERT(aRequest);
56
57
aBlobURISpec.SetIsVoid(true);
58
59
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
60
if (!channel) {
61
return;
62
}
63
64
nsCOMPtr<nsIURI> uri;
65
nsresult rv = channel->GetURI(getter_AddRefs(uri));
66
if (NS_FAILED(rv)) {
67
return;
68
}
69
70
if (!dom::IsBlobURI(uri)) {
71
return;
72
}
73
74
uri->GetSpec(aBlobURISpec);
75
}
76
77
bool ShouldCheckSRI(const InternalRequest* const aRequest,
78
const InternalResponse* const aResponse) {
79
MOZ_DIAGNOSTIC_ASSERT(aRequest);
80
MOZ_DIAGNOSTIC_ASSERT(aResponse);
81
82
return !aRequest->GetIntegrity().IsEmpty() &&
83
aResponse->Type() != ResponseType::Error;
84
}
85
86
} // anonymous namespace
87
88
//-----------------------------------------------------------------------------
89
// AlternativeDataStreamListener
90
//-----------------------------------------------------------------------------
91
class AlternativeDataStreamListener final
92
: public nsIStreamListener,
93
public nsIThreadRetargetableStreamListener {
94
public:
95
NS_DECL_THREADSAFE_ISUPPORTS
96
NS_DECL_NSIREQUESTOBSERVER
97
NS_DECL_NSISTREAMLISTENER
98
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
99
100
// The status of AlternativeDataStreamListener
101
// LOADING: is the initial status, loading the alternative data
102
// COMPLETED: Alternative data loading is completed
103
// CANCELED: Alternative data loading is canceled, this would make
104
// AlternativeDataStreamListener ignore all channel callbacks
105
// FALLBACK: fallback the channel callbacks to FetchDriver
106
// Depends on different situaions, the status transition could be followings
107
// 1. LOADING->COMPLETED
108
// This is the normal status transition for alternative data loading
109
//
110
// 2. LOADING->CANCELED
111
// LOADING->COMPLETED->CANCELED
112
// Alternative data loading could be canceled when cacheId from alternative
113
// data channel does not match with from main data channel(The cacheID
114
// checking is in FetchDriver::OnStartRequest).
115
// Notice the alternative data loading could finish before the cacheID
116
// checking, so the statust transition could be
117
// LOADING->COMPLETED->CANCELED
118
//
119
// 3. LOADING->FALLBACK
120
// For the case that alternative data loading could not be initialized,
121
// i.e. alternative data does not exist or no preferred alternative data
122
// type is requested. Once the status becomes FALLBACK,
123
// AlternativeDataStreamListener transits the channel callback request to
124
// FetchDriver, and the status should not go back to LOADING, COMPLETED, or
125
// CANCELED anymore.
126
enum eStatus { LOADING = 0, COMPLETED, CANCELED, FALLBACK };
127
128
AlternativeDataStreamListener(FetchDriver* aFetchDriver, nsIChannel* aChannel,
129
const nsACString& aAlternativeDataType);
130
eStatus Status();
131
void Cancel();
132
uint64_t GetAlternativeDataCacheEntryId();
133
const nsACString& GetAlternativeDataType() const;
134
already_AddRefed<nsICacheInfoChannel> GetCacheInfoChannel();
135
already_AddRefed<nsIInputStream> GetAlternativeInputStream();
136
137
private:
138
~AlternativeDataStreamListener() = default;
139
140
// This creates a strong reference cycle with FetchDriver and its
141
// mAltDataListener. We need to clear at least one reference of them once the
142
// data loading finishes.
143
RefPtr<FetchDriver> mFetchDriver;
144
nsCString mAlternativeDataType;
145
nsCOMPtr<nsIInputStream> mPipeAlternativeInputStream;
146
nsCOMPtr<nsIOutputStream> mPipeAlternativeOutputStream;
147
uint64_t mAlternativeDataCacheEntryId;
148
nsCOMPtr<nsICacheInfoChannel> mCacheInfoChannel;
149
nsCOMPtr<nsIChannel> mChannel;
150
Atomic<eStatus> mStatus;
151
};
152
153
NS_IMPL_ISUPPORTS(AlternativeDataStreamListener, nsIStreamListener,
154
nsIThreadRetargetableStreamListener)
155
156
AlternativeDataStreamListener::AlternativeDataStreamListener(
157
FetchDriver* aFetchDriver, nsIChannel* aChannel,
158
const nsACString& aAlternativeDataType)
159
: mFetchDriver(aFetchDriver),
160
mAlternativeDataType(aAlternativeDataType),
161
mAlternativeDataCacheEntryId(0),
162
mChannel(aChannel),
163
mStatus(AlternativeDataStreamListener::LOADING) {
164
MOZ_DIAGNOSTIC_ASSERT(mFetchDriver);
165
MOZ_DIAGNOSTIC_ASSERT(mChannel);
166
}
167
168
AlternativeDataStreamListener::eStatus AlternativeDataStreamListener::Status() {
169
return mStatus;
170
}
171
172
void AlternativeDataStreamListener::Cancel() {
173
mAlternativeDataCacheEntryId = 0;
174
mCacheInfoChannel = nullptr;
175
mPipeAlternativeOutputStream = nullptr;
176
mPipeAlternativeInputStream = nullptr;
177
if (mChannel && mStatus != AlternativeDataStreamListener::FALLBACK) {
178
// if mStatus is fallback, we need to keep channel to forward request back
179
// to FetchDriver
180
mChannel->Cancel(NS_BINDING_ABORTED);
181
mChannel = nullptr;
182
}
183
mStatus = AlternativeDataStreamListener::CANCELED;
184
}
185
186
uint64_t AlternativeDataStreamListener::GetAlternativeDataCacheEntryId() {
187
return mAlternativeDataCacheEntryId;
188
}
189
190
const nsACString& AlternativeDataStreamListener::GetAlternativeDataType()
191
const {
192
return mAlternativeDataType;
193
}
194
195
already_AddRefed<nsIInputStream>
196
AlternativeDataStreamListener::GetAlternativeInputStream() {
197
nsCOMPtr<nsIInputStream> inputStream = mPipeAlternativeInputStream;
198
return inputStream.forget();
199
}
200
201
already_AddRefed<nsICacheInfoChannel>
202
AlternativeDataStreamListener::GetCacheInfoChannel() {
203
nsCOMPtr<nsICacheInfoChannel> channel = mCacheInfoChannel;
204
return channel.forget();
205
}
206
207
NS_IMETHODIMP
208
AlternativeDataStreamListener::OnStartRequest(nsIRequest* aRequest) {
209
AssertIsOnMainThread();
210
MOZ_ASSERT(!mAlternativeDataType.IsEmpty());
211
// Checking the alternative data type is the same between we asked and the
212
// saved in the channel.
213
nsAutoCString alternativeDataType;
214
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(aRequest);
215
mStatus = AlternativeDataStreamListener::LOADING;
216
if (cic && NS_SUCCEEDED(cic->GetAlternativeDataType(alternativeDataType)) &&
217
mAlternativeDataType.Equals(alternativeDataType) &&
218
NS_SUCCEEDED(cic->GetCacheEntryId(&mAlternativeDataCacheEntryId))) {
219
MOZ_DIAGNOSTIC_ASSERT(!mPipeAlternativeInputStream);
220
MOZ_DIAGNOSTIC_ASSERT(!mPipeAlternativeOutputStream);
221
nsresult rv =
222
NS_NewPipe(getter_AddRefs(mPipeAlternativeInputStream),
223
getter_AddRefs(mPipeAlternativeOutputStream),
224
0 /* default segment size */, UINT32_MAX /* infinite pipe */,
225
true /* non-blocking input, otherwise you deadlock */,
226
false /* blocking output, since the pipe is 'in'finite */);
227
228
if (NS_FAILED(rv)) {
229
mFetchDriver->FailWithNetworkError(rv);
230
return rv;
231
}
232
233
MOZ_DIAGNOSTIC_ASSERT(!mCacheInfoChannel);
234
mCacheInfoChannel = cic;
235
236
// call FetchDriver::HttpFetch to load main body
237
MOZ_ASSERT(mFetchDriver);
238
return mFetchDriver->HttpFetch();
239
240
} else {
241
// Needn't load alternative data, since alternative data does not exist.
242
// Set status to FALLBACK to reuse the opened channel to load main body,
243
// then call FetchDriver::OnStartRequest to continue the work. Unfortunately
244
// can't change the stream listener to mFetchDriver, need to keep
245
// AlternativeDataStreamListener alive to redirect OnDataAvailable and
246
// OnStopRequest to mFetchDriver.
247
MOZ_ASSERT(alternativeDataType.IsEmpty());
248
mStatus = AlternativeDataStreamListener::FALLBACK;
249
mAlternativeDataCacheEntryId = 0;
250
MOZ_ASSERT(mFetchDriver);
251
return mFetchDriver->OnStartRequest(aRequest);
252
}
253
return NS_OK;
254
}
255
256
NS_IMETHODIMP
257
AlternativeDataStreamListener::OnDataAvailable(nsIRequest* aRequest,
258
nsIInputStream* aInputStream,
259
uint64_t aOffset,
260
uint32_t aCount) {
261
if (mStatus == AlternativeDataStreamListener::LOADING) {
262
MOZ_ASSERT(mPipeAlternativeOutputStream);
263
uint32_t read = 0;
264
return aInputStream->ReadSegments(
265
NS_CopySegmentToStream, mPipeAlternativeOutputStream, aCount, &read);
266
}
267
if (mStatus == AlternativeDataStreamListener::FALLBACK) {
268
MOZ_ASSERT(mFetchDriver);
269
return mFetchDriver->OnDataAvailable(aRequest, aInputStream, aOffset,
270
aCount);
271
}
272
return NS_OK;
273
}
274
275
NS_IMETHODIMP
276
AlternativeDataStreamListener::OnStopRequest(nsIRequest* aRequest,
277
nsresult aStatusCode) {
278
AssertIsOnMainThread();
279
280
// Alternative data loading is going to finish, breaking the reference cycle
281
// here by taking the ownership to a loacl variable.
282
RefPtr<FetchDriver> fetchDriver = mFetchDriver.forget();
283
284
if (mStatus == AlternativeDataStreamListener::CANCELED) {
285
// do nothing
286
return NS_OK;
287
}
288
289
if (mStatus == AlternativeDataStreamListener::FALLBACK) {
290
MOZ_ASSERT(fetchDriver);
291
return fetchDriver->OnStopRequest(aRequest, aStatusCode);
292
}
293
294
MOZ_DIAGNOSTIC_ASSERT(mStatus == AlternativeDataStreamListener::LOADING);
295
296
MOZ_ASSERT(!mAlternativeDataType.IsEmpty() && mPipeAlternativeOutputStream &&
297
mPipeAlternativeInputStream);
298
299
mPipeAlternativeOutputStream->Close();
300
mPipeAlternativeOutputStream = nullptr;
301
302
// Cleanup the states for alternative data if needed.
303
if (NS_FAILED(aStatusCode)) {
304
mAlternativeDataCacheEntryId = 0;
305
mCacheInfoChannel = nullptr;
306
mPipeAlternativeInputStream = nullptr;
307
}
308
mStatus = AlternativeDataStreamListener::COMPLETED;
309
// alternative data loading finish, call FetchDriver::FinishOnStopRequest to
310
// continue the final step for the case FetchDriver::OnStopRequest is called
311
// earlier than AlternativeDataStreamListener::OnStopRequest
312
MOZ_ASSERT(fetchDriver);
313
fetchDriver->FinishOnStopRequest(this);
314
return NS_OK;
315
}
316
317
NS_IMETHODIMP
318
AlternativeDataStreamListener::CheckListenerChain() { return NS_OK; }
319
//-----------------------------------------------------------------------------
320
// FetchDriver
321
//-----------------------------------------------------------------------------
322
323
NS_IMPL_ISUPPORTS(FetchDriver, nsIStreamListener, nsIChannelEventSink,
324
nsIInterfaceRequestor, nsIThreadRetargetableStreamListener)
325
326
FetchDriver::FetchDriver(InternalRequest* aRequest, nsIPrincipal* aPrincipal,
327
nsILoadGroup* aLoadGroup,
328
nsIEventTarget* aMainThreadEventTarget,
329
nsICookieSettings* aCookieSettings,
330
PerformanceStorage* aPerformanceStorage,
331
bool aIsTrackingFetch)
332
: mPrincipal(aPrincipal),
333
mLoadGroup(aLoadGroup),
334
mRequest(aRequest),
335
mMainThreadEventTarget(aMainThreadEventTarget),
336
mCookieSettings(aCookieSettings),
337
mPerformanceStorage(aPerformanceStorage),
338
mNeedToObserveOnDataAvailable(false),
339
mIsTrackingFetch(aIsTrackingFetch),
340
mOnStopRequestCalled(false)
341
#ifdef DEBUG
342
,
343
mResponseAvailableCalled(false),
344
mFetchCalled(false)
345
#endif
346
{
347
AssertIsOnMainThread();
348
349
MOZ_ASSERT(aRequest);
350
MOZ_ASSERT(aPrincipal);
351
MOZ_ASSERT(aMainThreadEventTarget);
352
}
353
354
FetchDriver::~FetchDriver() {
355
AssertIsOnMainThread();
356
357
// We assert this since even on failures, we should call
358
// FailWithNetworkError().
359
MOZ_ASSERT(mResponseAvailableCalled);
360
}
361
362
nsresult FetchDriver::Fetch(AbortSignalImpl* aSignalImpl,
363
FetchDriverObserver* aObserver) {
364
AssertIsOnMainThread();
365
#ifdef DEBUG
366
MOZ_ASSERT(!mFetchCalled);
367
mFetchCalled = true;
368
#endif
369
370
mObserver = aObserver;
371
372
// FIXME(nsm): Deal with HSTS.
373
374
MOZ_RELEASE_ASSERT(!mRequest->IsSynchronous(),
375
"Synchronous fetch not supported");
376
377
UniquePtr<mozilla::ipc::PrincipalInfo> principalInfo(
378
new mozilla::ipc::PrincipalInfo());
379
nsresult rv = PrincipalToPrincipalInfo(mPrincipal, principalInfo.get());
380
if (NS_WARN_IF(NS_FAILED(rv))) {
381
return rv;
382
}
383
384
mRequest->SetPrincipalInfo(std::move(principalInfo));
385
386
// If the signal is aborted, it's time to inform the observer and terminate
387
// the operation.
388
if (aSignalImpl) {
389
if (aSignalImpl->Aborted()) {
390
Abort();
391
return NS_OK;
392
}
393
394
Follow(aSignalImpl);
395
}
396
397
rv = HttpFetch(mRequest->GetPreferredAlternativeDataType());
398
if (NS_FAILED(rv)) {
399
FailWithNetworkError(rv);
400
}
401
402
// Any failure is handled by FailWithNetworkError notifying the aObserver.
403
return NS_OK;
404
}
405
406
// This function implements the "HTTP Fetch" algorithm from the Fetch spec.
407
// Functionality is often split between here, the CORS listener proxy and the
408
// Necko HTTP implementation.
409
nsresult FetchDriver::HttpFetch(
410
const nsACString& aPreferredAlternativeDataType) {
411
MOZ_ASSERT(NS_IsMainThread());
412
413
// Step 1. "Let response be null."
414
mResponse = nullptr;
415
mOnStopRequestCalled = false;
416
nsresult rv;
417
418
nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
419
NS_ENSURE_SUCCESS(rv, rv);
420
421
nsAutoCString url;
422
mRequest->GetURL(url);
423
nsCOMPtr<nsIURI> uri;
424
rv = NS_NewURI(getter_AddRefs(uri), url);
425
NS_ENSURE_SUCCESS(rv, rv);
426
427
// Unsafe requests aren't allowed with when using no-core mode.
428
if (mRequest->Mode() == RequestMode::No_cors && mRequest->UnsafeRequest() &&
429
(!mRequest->HasSimpleMethod() ||
430
!mRequest->Headers()->HasOnlySimpleHeaders())) {
431
MOZ_ASSERT(false, "The API should have caught this");
432
return NS_ERROR_DOM_BAD_URI;
433
}
434
435
// non-GET requests aren't allowed for blob.
436
if (IsBlobURI(uri)) {
437
nsAutoCString method;
438
mRequest->GetMethod(method);
439
if (!method.EqualsLiteral("GET")) {
440
return NS_ERROR_DOM_NETWORK_ERR;
441
}
442
}
443
444
// Step 2 deals with letting ServiceWorkers intercept requests. This is
445
// handled by Necko after the channel is opened.
446
// FIXME(nsm): Bug 1119026: The channel's skip service worker flag should be
447
// set based on the Request's flag.
448
449
// Step 3.1 "If the CORS preflight flag is set and one of these conditions is
450
// true..." is handled by the CORS proxy.
451
//
452
// Step 3.2 "Set request's skip service worker flag." This isn't required
453
// since Necko will fall back to the network if the ServiceWorker does not
454
// respond with a valid Response.
455
//
456
// NS_StartCORSPreflight() will automatically kick off the original request
457
// if it succeeds, so we need to have everything setup for the original
458
// request too.
459
460
// Step 3.3 "Let credentials flag be set if one of
461
// - request's credentials mode is "include"
462
// - request's credentials mode is "same-origin" and either the CORS flag
463
// is unset or response tainting is "opaque"
464
// is true, and unset otherwise."
465
466
// Set skip serviceworker flag.
467
// While the spec also gates on the client being a ServiceWorker, we can't
468
// infer that here. Instead we rely on callers to set the flag correctly.
469
const nsLoadFlags bypassFlag = mRequest->SkipServiceWorker()
470
? nsIChannel::LOAD_BYPASS_SERVICE_WORKER
471
: 0;
472
473
nsSecurityFlags secFlags = 0;
474
if (mRequest->Mode() == RequestMode::Cors) {
475
secFlags |= nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
476
} else if (mRequest->Mode() == RequestMode::Same_origin ||
477
mRequest->Mode() == RequestMode::Navigate) {
478
secFlags |= nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS;
479
} else if (mRequest->Mode() == RequestMode::No_cors) {
480
secFlags |= nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
481
} else {
482
MOZ_ASSERT_UNREACHABLE("Unexpected request mode!");
483
return NS_ERROR_UNEXPECTED;
484
}
485
486
if (mRequest->GetRedirectMode() != RequestRedirect::Follow) {
487
secFlags |= nsILoadInfo::SEC_DONT_FOLLOW_REDIRECTS;
488
}
489
490
// This is handles the use credentials flag in "HTTP
491
// network or cache fetch" in the spec and decides whether to transmit
492
// cookies and other identifying information.
493
if (mRequest->GetCredentialsMode() == RequestCredentials::Include) {
494
secFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
495
} else if (mRequest->GetCredentialsMode() == RequestCredentials::Omit) {
496
secFlags |= nsILoadInfo::SEC_COOKIES_OMIT;
497
} else if (mRequest->GetCredentialsMode() ==
498
RequestCredentials::Same_origin) {
499
secFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
500
} else {
501
MOZ_ASSERT_UNREACHABLE("Unexpected credentials mode!");
502
return NS_ERROR_UNEXPECTED;
503
}
504
505
// From here on we create a channel and set its properties with the
506
// information from the InternalRequest. This is an implementation detail.
507
MOZ_ASSERT(mLoadGroup);
508
nsCOMPtr<nsIChannel> chan;
509
510
nsLoadFlags loadFlags = nsIRequest::LOAD_BACKGROUND | bypassFlag;
511
if (mDocument) {
512
MOZ_ASSERT(mDocument->NodePrincipal() == mPrincipal);
513
MOZ_ASSERT(mDocument->CookieSettings() == mCookieSettings);
514
rv = NS_NewChannel(getter_AddRefs(chan), uri, mDocument, secFlags,
515
mRequest->ContentPolicyType(),
516
nullptr, /* aPerformanceStorage */
517
mLoadGroup, nullptr, /* aCallbacks */
518
loadFlags, ios);
519
} else if (mClientInfo.isSome()) {
520
rv = NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, mClientInfo.ref(),
521
mController, secFlags, mRequest->ContentPolicyType(),
522
mCookieSettings, mPerformanceStorage, mLoadGroup,
523
nullptr, /* aCallbacks */
524
loadFlags, ios);
525
} else {
526
rv =
527
NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, secFlags,
528
mRequest->ContentPolicyType(), mCookieSettings,
529
mPerformanceStorage, mLoadGroup, nullptr, /* aCallbacks */
530
loadFlags, ios);
531
}
532
NS_ENSURE_SUCCESS(rv, rv);
533
534
if (mCSPEventListener) {
535
nsCOMPtr<nsILoadInfo> loadInfo = chan->LoadInfo();
536
rv = loadInfo->SetCspEventListener(mCSPEventListener);
537
NS_ENSURE_SUCCESS(rv, rv);
538
}
539
540
// Insert ourselves into the notification callbacks chain so we can set
541
// headers on redirects.
542
#ifdef DEBUG
543
{
544
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
545
chan->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks));
546
MOZ_ASSERT(!notificationCallbacks);
547
}
548
#endif
549
chan->SetNotificationCallbacks(this);
550
551
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(chan));
552
// Mark channel as urgent-start if the Fetch is triggered by user input
553
// events.
554
if (cos && UserActivation::IsHandlingUserInput()) {
555
cos->AddClassFlags(nsIClassOfService::UrgentStart);
556
}
557
558
// Step 3.5 begins "HTTP network or cache fetch".
559
// HTTP network or cache fetch
560
// ---------------------------
561
// Step 1 "Let HTTPRequest..." The channel is the HTTPRequest.
562
nsCOMPtr<nsIHttpChannel> httpChan = do_QueryInterface(chan);
563
if (httpChan) {
564
// Copy the method.
565
nsAutoCString method;
566
mRequest->GetMethod(method);
567
rv = httpChan->SetRequestMethod(method);
568
NS_ENSURE_SUCCESS(rv, rv);
569
570
// Set the same headers.
571
SetRequestHeaders(httpChan);
572
574
// If request's referrer policy is the empty string and request's client is
575
// non-null, then set request's referrer policy to request's client's
576
// associated referrer policy.
577
// Basically, "client" is not in our implementation, we use
578
// EnvironmentReferrerPolicy of the worker or document context
579
ReferrerPolicy referrerPolicy = mRequest->GetEnvironmentReferrerPolicy();
580
if (mRequest->ReferrerPolicy_() == ReferrerPolicy::_empty) {
581
mRequest->SetReferrerPolicy(referrerPolicy);
582
}
584
// If request’s referrer policy is the empty string,
585
// then set request’s referrer policy to the user-set default policy.
586
if (mRequest->ReferrerPolicy_() == ReferrerPolicy::_empty) {
587
nsCOMPtr<nsILoadInfo> loadInfo = httpChan->LoadInfo();
588
bool isPrivate = loadInfo->GetOriginAttributes().mPrivateBrowsingId > 0;
589
referrerPolicy =
590
ReferrerInfo::GetDefaultReferrerPolicy(httpChan, uri, isPrivate);
591
mRequest->SetReferrerPolicy(referrerPolicy);
592
}
593
594
rv = FetchUtil::SetRequestReferrer(mPrincipal, mDocument, httpChan,
595
mRequest);
596
NS_ENSURE_SUCCESS(rv, rv);
597
598
// Bug 1120722 - Authorization will be handled later.
599
// Auth may require prompting, we don't support it yet.
600
// The next patch in this same bug prevents this from aborting the request.
601
// Credentials checks for CORS are handled by nsCORSListenerProxy,
602
603
nsCOMPtr<nsIHttpChannelInternal> internalChan = do_QueryInterface(httpChan);
604
605
// Conversion between enumerations is safe due to static asserts in
606
// dom/workers/ServiceWorkerManager.cpp
607
rv = internalChan->SetCorsMode(static_cast<uint32_t>(mRequest->Mode()));
608
MOZ_ASSERT(NS_SUCCEEDED(rv));
609
rv = internalChan->SetRedirectMode(
610
static_cast<uint32_t>(mRequest->GetRedirectMode()));
611
MOZ_ASSERT(NS_SUCCEEDED(rv));
612
mRequest->MaybeSkipCacheIfPerformingRevalidation();
613
rv = internalChan->SetFetchCacheMode(
614
static_cast<uint32_t>(mRequest->GetCacheMode()));
615
MOZ_ASSERT(NS_SUCCEEDED(rv));
616
rv = internalChan->SetIntegrityMetadata(mRequest->GetIntegrity());
617
MOZ_ASSERT(NS_SUCCEEDED(rv));
618
619
// Set the initiator type
620
nsCOMPtr<nsITimedChannel> timedChannel(do_QueryInterface(httpChan));
621
if (timedChannel) {
622
timedChannel->SetInitiatorType(NS_LITERAL_STRING("fetch"));
623
}
624
}
625
626
// Step 5. Proxy authentication will be handled by Necko.
627
628
// Continue setting up 'HTTPRequest'. Content-Type and body data.
629
nsCOMPtr<nsIUploadChannel2> uploadChan = do_QueryInterface(chan);
630
if (uploadChan) {
631
nsAutoCString contentType;
632
ErrorResult result;
633
mRequest->Headers()->GetFirst(NS_LITERAL_CSTRING("content-type"),
634
contentType, result);
635
// We don't actually expect "result" to have failed here: that only happens
636
// for invalid header names. But if for some reason it did, just propagate
637
// it out.
638
if (result.Failed()) {
639
return result.StealNSResult();
640
}
641
642
// Now contentType is the header that was set in mRequest->Headers(), or a
643
// void string if no header was set.
644
#ifdef DEBUG
645
bool hasContentTypeHeader =
646
mRequest->Headers()->Has(NS_LITERAL_CSTRING("content-type"), result);
647
MOZ_ASSERT(!result.Failed());
648
MOZ_ASSERT_IF(!hasContentTypeHeader, contentType.IsVoid());
649
#endif // DEBUG
650
651
int64_t bodyLength;
652
nsCOMPtr<nsIInputStream> bodyStream;
653
mRequest->GetBody(getter_AddRefs(bodyStream), &bodyLength);
654
if (bodyStream) {
655
nsAutoCString method;
656
mRequest->GetMethod(method);
657
rv = uploadChan->ExplicitSetUploadStream(bodyStream, contentType,
658
bodyLength, method,
659
false /* aStreamHasHeaders */);
660
NS_ENSURE_SUCCESS(rv, rv);
661
}
662
}
663
664
// If preflight is required, start a "CORS preflight fetch"
666
// implementation is handled by the http channel calling into
667
// nsCORSListenerProxy. We just inform it which unsafe headers are included
668
// in the request.
669
if (mRequest->Mode() == RequestMode::Cors) {
670
AutoTArray<nsCString, 5> unsafeHeaders;
671
mRequest->Headers()->GetUnsafeHeaders(unsafeHeaders);
672
nsCOMPtr<nsILoadInfo> loadInfo = chan->LoadInfo();
673
loadInfo->SetCorsPreflightInfo(unsafeHeaders, false);
674
}
675
676
if (mIsTrackingFetch && StaticPrefs::network_http_tailing_enabled() && cos) {
677
cos->AddClassFlags(nsIClassOfService::Throttleable |
678
nsIClassOfService::Tail);
679
}
680
681
if (mIsTrackingFetch &&
682
StaticPrefs::privacy_trackingprotection_lower_network_priority()) {
683
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(chan);
684
if (p) {
685
p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST);
686
}
687
}
688
689
NotifyNetworkMonitorAlternateStack(chan, std::move(mOriginStack));
690
691
// if the preferred alternative data type in InternalRequest is not empty, set
692
// the data type on the created channel and also create a
693
// AlternativeDataStreamListener to be the stream listener of the channel.
694
if (!aPreferredAlternativeDataType.IsEmpty()) {
695
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(chan);
696
if (cic) {
697
cic->PreferAlternativeDataType(aPreferredAlternativeDataType,
698
EmptyCString(), true);
699
MOZ_ASSERT(!mAltDataListener);
700
mAltDataListener = new AlternativeDataStreamListener(
701
this, chan, aPreferredAlternativeDataType);
702
rv = chan->AsyncOpen(mAltDataListener);
703
} else {
704
rv = chan->AsyncOpen(this);
705
}
706
} else {
707
// Integrity check cannot be done on alt-data yet.
708
if (mRequest->GetIntegrity().IsEmpty()) {
709
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(chan);
710
if (cic) {
711
cic->PreferAlternativeDataType(
712
NS_LITERAL_CSTRING(WASM_ALT_DATA_TYPE_V1),
713
NS_LITERAL_CSTRING(WASM_CONTENT_TYPE), false);
714
}
715
}
716
717
rv = chan->AsyncOpen(this);
718
}
719
720
if (NS_FAILED(rv)) {
721
return rv;
722
}
723
724
// Step 4 onwards of "HTTP Fetch" is handled internally by Necko.
725
726
mChannel = chan;
727
return NS_OK;
728
}
729
already_AddRefed<InternalResponse> FetchDriver::BeginAndGetFilteredResponse(
730
InternalResponse* aResponse, bool aFoundOpaqueRedirect) {
731
MOZ_ASSERT(aResponse);
732
AutoTArray<nsCString, 4> reqURLList;
733
mRequest->GetURLListWithoutFragment(reqURLList);
734
MOZ_ASSERT(!reqURLList.IsEmpty());
735
aResponse->SetURLList(reqURLList);
736
RefPtr<InternalResponse> filteredResponse;
737
if (aFoundOpaqueRedirect) {
738
filteredResponse = aResponse->OpaqueRedirectResponse();
739
} else {
740
switch (mRequest->GetResponseTainting()) {
741
case LoadTainting::Basic:
742
filteredResponse = aResponse->BasicResponse();
743
break;
744
case LoadTainting::CORS:
745
filteredResponse = aResponse->CORSResponse();
746
break;
747
case LoadTainting::Opaque: {
748
filteredResponse = aResponse->OpaqueResponse();
749
nsresult rv = filteredResponse->GeneratePaddingInfo();
750
if (NS_WARN_IF(NS_FAILED(rv))) {
751
return nullptr;
752
}
753
break;
754
}
755
default:
756
MOZ_CRASH("Unexpected case");
757
}
758
}
759
760
MOZ_ASSERT(filteredResponse);
761
MOZ_ASSERT(mObserver);
762
if (!ShouldCheckSRI(mRequest, filteredResponse)) {
763
mObserver->OnResponseAvailable(filteredResponse);
764
#ifdef DEBUG
765
mResponseAvailableCalled = true;
766
#endif
767
}
768
769
return filteredResponse.forget();
770
}
771
772
void FetchDriver::FailWithNetworkError(nsresult rv) {
773
AssertIsOnMainThread();
774
RefPtr<InternalResponse> error = InternalResponse::NetworkError(rv);
775
if (mObserver) {
776
mObserver->OnResponseAvailable(error);
777
#ifdef DEBUG
778
mResponseAvailableCalled = true;
779
#endif
780
mObserver->OnResponseEnd(FetchDriverObserver::eByNetworking);
781
mObserver = nullptr;
782
}
783
784
mChannel = nullptr;
785
}
786
787
NS_IMETHODIMP
788
FetchDriver::OnStartRequest(nsIRequest* aRequest) {
789
AssertIsOnMainThread();
790
791
// Note, this can be called multiple times if we are doing an opaqueredirect.
792
// In that case we will get a simulated OnStartRequest() and then the real
793
// channel will call in with an errored OnStartRequest().
794
795
if (!mChannel) {
796
MOZ_ASSERT(!mObserver);
797
return NS_BINDING_ABORTED;
798
}
799
800
nsresult rv;
801
aRequest->GetStatus(&rv);
802
if (NS_FAILED(rv)) {
803
FailWithNetworkError(rv);
804
return rv;
805
}
806
807
// We should only get to the following code once.
808
MOZ_ASSERT(!mPipeOutputStream);
809
810
if (!mObserver) {
811
MOZ_ASSERT(false, "We should have mObserver here.");
812
FailWithNetworkError(NS_ERROR_UNEXPECTED);
813
return NS_ERROR_UNEXPECTED;
814
}
815
816
mNeedToObserveOnDataAvailable = mObserver->NeedOnDataAvailable();
817
818
RefPtr<InternalResponse> response;
819
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
820
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
821
822
// On a successful redirect we perform the following substeps of HTTP Fetch,
823
// step 5, "redirect status", step 11.
824
825
bool foundOpaqueRedirect = false;
826
827
nsAutoCString contentType;
828
channel->GetContentType(contentType);
829
830
int64_t contentLength = InternalResponse::UNKNOWN_BODY_SIZE;
831
rv = channel->GetContentLength(&contentLength);
832
MOZ_ASSERT_IF(NS_FAILED(rv),
833
contentLength == InternalResponse::UNKNOWN_BODY_SIZE);
834
835
if (httpChannel) {
836
uint32_t responseStatus;
837
rv = httpChannel->GetResponseStatus(&responseStatus);
838
MOZ_ASSERT(NS_SUCCEEDED(rv));
839
840
if (mozilla::net::nsHttpChannel::IsRedirectStatus(responseStatus)) {
841
if (mRequest->GetRedirectMode() == RequestRedirect::Error) {
842
FailWithNetworkError(NS_BINDING_ABORTED);
843
return NS_BINDING_FAILED;
844
}
845
if (mRequest->GetRedirectMode() == RequestRedirect::Manual) {
846
foundOpaqueRedirect = true;
847
}
848
}
849
850
nsAutoCString statusText;
851
rv = httpChannel->GetResponseStatusText(statusText);
852
MOZ_ASSERT(NS_SUCCEEDED(rv));
853
854
response = new InternalResponse(responseStatus, statusText,
855
mRequest->GetCredentialsMode());
856
857
UniquePtr<mozilla::ipc::PrincipalInfo> principalInfo(
858
new mozilla::ipc::PrincipalInfo());
859
nsresult rv = PrincipalToPrincipalInfo(mPrincipal, principalInfo.get());
860
if (NS_WARN_IF(NS_FAILED(rv))) {
861
return rv;
862
}
863
864
response->SetPrincipalInfo(std::move(principalInfo));
865
866
response->Headers()->FillResponseHeaders(httpChannel);
867
868
// If Content-Encoding or Transfer-Encoding headers are set, then the actual
869
// Content-Length (which refer to the decoded data) is obscured behind the
870
// encodings.
871
ErrorResult result;
872
if (response->Headers()->Has(NS_LITERAL_CSTRING("content-encoding"),
873
result) ||
874
response->Headers()->Has(NS_LITERAL_CSTRING("transfer-encoding"),
875
result)) {
876
// We cannot trust the content-length when content-encoding or
877
// transfer-encoding are set. There are many servers which just
878
// get this wrong.
879
contentLength = InternalResponse::UNKNOWN_BODY_SIZE;
880
}
881
MOZ_ASSERT(!result.Failed());
882
} else {
883
response = new InternalResponse(200, NS_LITERAL_CSTRING("OK"),
884
mRequest->GetCredentialsMode());
885
886
if (!contentType.IsEmpty()) {
887
nsAutoCString contentCharset;
888
channel->GetContentCharset(contentCharset);
889
if (NS_SUCCEEDED(rv) && !contentCharset.IsEmpty()) {
890
contentType += NS_LITERAL_CSTRING(";charset=") + contentCharset;
891
}
892
893
IgnoredErrorResult result;
894
response->Headers()->Append(NS_LITERAL_CSTRING("Content-Type"),
895
contentType, result);
896
MOZ_ASSERT(!result.Failed());
897
}
898
899
if (contentLength > 0) {
900
nsAutoCString contentLenStr;
901
contentLenStr.AppendInt(contentLength);
902
903
IgnoredErrorResult result;
904
response->Headers()->Append(NS_LITERAL_CSTRING("Content-Length"),
905
contentLenStr, result);
906
MOZ_ASSERT(!result.Failed());
907
}
908
}
909
910
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(aRequest);
911
if (cic) {
912
if (mAltDataListener) {
913
// Skip the case that mAltDataListener->Status() equals to FALLBACK, that
914
// means the opened channel for alternative data loading is reused for
915
// loading the main data.
916
if (mAltDataListener->Status() !=
917
AlternativeDataStreamListener::FALLBACK) {
918
// Verify the cache ID is the same with from alternative data cache.
919
// If the cache ID is different, droping the alternative data loading,
920
// otherwise setup the response's alternative body and cacheInfoChannel.
921
uint64_t cacheEntryId = 0;
922
if (NS_SUCCEEDED(cic->GetCacheEntryId(&cacheEntryId)) &&
923
cacheEntryId !=
924
mAltDataListener->GetAlternativeDataCacheEntryId()) {
925
mAltDataListener->Cancel();
926
} else {
927
// AlternativeDataStreamListener::OnStartRequest had already been
928
// called, the alternative data input stream and cacheInfo channel
929
// must be created.
930
nsCOMPtr<nsICacheInfoChannel> cacheInfo =
931
mAltDataListener->GetCacheInfoChannel();
932
nsCOMPtr<nsIInputStream> altInputStream =
933
mAltDataListener->GetAlternativeInputStream();
934
MOZ_ASSERT(altInputStream && cacheInfo);
935
response->SetAlternativeBody(altInputStream);
936
nsMainThreadPtrHandle<nsICacheInfoChannel> handle(
937
new nsMainThreadPtrHolder<nsICacheInfoChannel>(
938
"nsICacheInfoChannel", cacheInfo, false));
939
response->SetCacheInfoChannel(handle);
940
}
941
} else if (!mAltDataListener->GetAlternativeDataType().IsEmpty()) {
942
// If the status is FALLBACK and the
943
// mAltDataListener::mAlternativeDataType is not empty, that means the
944
// data need to be saved into cache, setup the response's
945
// nsICacheInfoChannel for caching the data after loading.
946
nsMainThreadPtrHandle<nsICacheInfoChannel> handle(
947
new nsMainThreadPtrHolder<nsICacheInfoChannel>(
948
"nsICacheInfoChannel", cic, false));
949
response->SetCacheInfoChannel(handle);
950
}
951
} else if (!cic->PreferredAlternativeDataTypes().IsEmpty()) {
952
MOZ_ASSERT(cic->PreferredAlternativeDataTypes().Length() == 1);
953
MOZ_ASSERT(cic->PreferredAlternativeDataTypes()[0].type().EqualsLiteral(
954
WASM_ALT_DATA_TYPE_V1));
955
MOZ_ASSERT(
956
cic->PreferredAlternativeDataTypes()[0].contentType().EqualsLiteral(
957
WASM_CONTENT_TYPE));
958
959
if (contentType.EqualsLiteral(WASM_CONTENT_TYPE)) {
960
// We want to attach the CacheInfoChannel to the response object such
961
// that we can track its origin when the Response object is manipulated
962
// by JavaScript code. This is important for WebAssembly, which uses
963
// fetch to query its sources in JavaScript and transfer the Response
964
// object to other function responsible for storing the alternate data
965
// using the CacheInfoChannel.
966
nsMainThreadPtrHandle<nsICacheInfoChannel> handle(
967
new nsMainThreadPtrHolder<nsICacheInfoChannel>(
968
"nsICacheInfoChannel", cic, false));
969
response->SetCacheInfoChannel(handle);
970
}
971
}
972
}
973
974
// We open a pipe so that we can immediately set the pipe's read end as the
975
// response's body. Setting the segment size to UINT32_MAX means that the
976
// pipe has infinite space. The nsIChannel will continue to buffer data in
977
// xpcom events even if we block on a fixed size pipe. It might be possible
978
// to suspend the channel and then resume when there is space available, but
979
// for now use an infinite pipe to avoid blocking.
980
nsCOMPtr<nsIInputStream> pipeInputStream;
981
rv = NS_NewPipe(getter_AddRefs(pipeInputStream),
982
getter_AddRefs(mPipeOutputStream),
983
0, /* default segment size */
984
UINT32_MAX /* infinite pipe */,
985
true /* non-blocking input, otherwise you deadlock */,
986
false /* blocking output, since the pipe is 'in'finite */);
987
if (NS_WARN_IF(NS_FAILED(rv))) {
988
FailWithNetworkError(rv);
989
// Cancel request.
990
return rv;
991
}
992
response->SetBody(pipeInputStream, contentLength);
993
994
// If the request is a file channel, then remember the local path to
995
// that file so we can later create File blobs rather than plain ones.
996
nsCOMPtr<nsIFileChannel> fc = do_QueryInterface(aRequest);
997
if (fc) {
998
nsCOMPtr<nsIFile> file;
999
rv = fc->GetFile(getter_AddRefs(file));
1000
if (!NS_WARN_IF(NS_FAILED(rv))) {
1001
nsAutoString path;
1002
file->GetPath(path);
1003
response->SetBodyLocalPath(path);
1004
}
1005
} else {
1006
// If the request is a blob URI, then remember that URI so that we
1007
// can later just use that blob instance instead of cloning it.
1008
nsCString blobURISpec;
1009
GetBlobURISpecFromChannel(aRequest, blobURISpec);
1010
if (!blobURISpec.IsVoid()) {
1011
response->SetBodyBlobURISpec(blobURISpec);
1012
}
1013
}
1014
1015
response->InitChannelInfo(channel);
1016
1017
nsCOMPtr<nsIURI> channelURI;
1018
rv = channel->GetURI(getter_AddRefs(channelURI));
1019
if (NS_WARN_IF(NS_FAILED(rv))) {
1020
FailWithNetworkError(rv);
1021
// Cancel request.
1022
return rv;
1023
}
1024
1025
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
1026
// Propagate any tainting from the channel back to our response here. This
1027
// step is not reflected in the spec because the spec is written such that
1028
// FetchEvent.respondWith() just passes the already-tainted Response back to
1029
// the outer fetch(). In gecko, however, we serialize the Response through
1030
// the channel and must regenerate the tainting from the channel in the
1031
// interception case.
1032
mRequest->MaybeIncreaseResponseTainting(loadInfo->GetTainting());
1033
1034
// Resolves fetch() promise which may trigger code running in a worker. Make
1035
// sure the Response is fully initialized before calling this.
1036
mResponse = BeginAndGetFilteredResponse(response, foundOpaqueRedirect);
1037
if (NS_WARN_IF(!mResponse)) {
1038
// Fail to generate a paddingInfo for opaque response.
1039
MOZ_DIAGNOSTIC_ASSERT(mRequest->GetResponseTainting() ==
1040
LoadTainting::Opaque &&
1041
!foundOpaqueRedirect);
1042
FailWithNetworkError(NS_ERROR_UNEXPECTED);
1043
return NS_ERROR_UNEXPECTED;
1044
}
1045
1046
// From "Main Fetch" step 19: SRI-part1.
1047
if (ShouldCheckSRI(mRequest, mResponse) && mSRIMetadata.IsEmpty()) {
1048
nsIConsoleReportCollector* reporter = nullptr;
1049
if (mObserver) {
1050
reporter = mObserver->GetReporter();
1051
}
1052
1053
nsAutoCString sourceUri;
1054
if (mDocument && mDocument->GetDocumentURI()) {
1055
mDocument->GetDocumentURI()->GetAsciiSpec(sourceUri);
1056
} else if (!mWorkerScript.IsEmpty()) {
1057
sourceUri.Assign(mWorkerScript);
1058
}
1059
SRICheck::IntegrityMetadata(mRequest->GetIntegrity(), sourceUri, reporter,
1060
&mSRIMetadata);
1061
mSRIDataVerifier =
1062
new SRICheckDataVerifier(mSRIMetadata, sourceUri, reporter);
1063
1064
// Do not retarget off main thread when using SRI API.
1065
return NS_OK;
1066
}
1067
1068
nsCOMPtr<nsIEventTarget> sts =
1069
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
1070
if (NS_WARN_IF(NS_FAILED(rv))) {
1071
FailWithNetworkError(rv);
1072
// Cancel request.
1073
return rv;
1074
}
1075
1076
// Try to retarget off main thread.
1077
if (nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(aRequest)) {
1078
Unused << NS_WARN_IF(NS_FAILED(rr->RetargetDeliveryTo(sts)));
1079
}
1080
return NS_OK;
1081
}
1082
1083
namespace {
1084
1085
// Runnable to call the observer OnDataAvailable on the main-thread.
1086
class DataAvailableRunnable final : public Runnable {
1087
RefPtr<FetchDriverObserver> mObserver;
1088
1089
public:
1090
explicit DataAvailableRunnable(FetchDriverObserver* aObserver)
1091
: Runnable("dom::DataAvailableRunnable"), mObserver(aObserver) {
1092
MOZ_ASSERT(aObserver);
1093
}
1094
1095
NS_IMETHOD
1096
Run() override {
1097
mObserver->OnDataAvailable();
1098
mObserver = nullptr;
1099
return NS_OK;
1100
}
1101
};
1102
1103
struct SRIVerifierAndOutputHolder {
1104
SRIVerifierAndOutputHolder(SRICheckDataVerifier* aVerifier,
1105
nsIOutputStream* aOutputStream)
1106
: mVerifier(aVerifier), mOutputStream(aOutputStream) {}
1107
1108
SRICheckDataVerifier* mVerifier;
1109
nsIOutputStream* mOutputStream;
1110
1111
private:
1112
SRIVerifierAndOutputHolder() = delete;
1113
};
1114
1115
// Just like NS_CopySegmentToStream, but also sends the data into an
1116
// SRICheckDataVerifier.
1117
nsresult CopySegmentToStreamAndSRI(nsIInputStream* aInStr, void* aClosure,
1118
const char* aBuffer, uint32_t aOffset,
1119
uint32_t aCount, uint32_t* aCountWritten) {
1120
auto holder = static_cast<SRIVerifierAndOutputHolder*>(aClosure);
1121
MOZ_DIAGNOSTIC_ASSERT(holder && holder->mVerifier && holder->mOutputStream,
1122
"Bogus holder");
1123
nsresult rv = holder->mVerifier->Update(
1124
aCount, reinterpret_cast<const uint8_t*>(aBuffer));
1125
NS_ENSURE_SUCCESS(rv, rv);
1126
1127
// The rest is just like NS_CopySegmentToStream.
1128
*aCountWritten = 0;
1129
while (aCount) {
1130
uint32_t n = 0;
1131
rv = holder->mOutputStream->Write(aBuffer, aCount, &n);
1132
if (NS_FAILED(rv)) {
1133
return rv;
1134
}
1135
aBuffer += n;
1136
aCount -= n;
1137
*aCountWritten += n;
1138
}
1139
return NS_OK;
1140
}
1141
1142
} // anonymous namespace
1143
1144
NS_IMETHODIMP
1145
FetchDriver::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
1146
uint64_t aOffset, uint32_t aCount) {
1147
// NB: This can be called on any thread! But we're guaranteed that it is
1148
// called between OnStartRequest and OnStopRequest, so we don't need to worry
1149
// about races.
1150
1151
if (mNeedToObserveOnDataAvailable) {
1152
mNeedToObserveOnDataAvailable = false;
1153
if (mObserver) {
1154
if (NS_IsMainThread()) {
1155
mObserver->OnDataAvailable();
1156
} else {
1157
RefPtr<Runnable> runnable = new DataAvailableRunnable(mObserver);
1158
nsresult rv = mMainThreadEventTarget->Dispatch(runnable.forget(),
1159
NS_DISPATCH_NORMAL);
1160
if (NS_WARN_IF(NS_FAILED(rv))) {
1161
return rv;
1162
}
1163
}
1164
}
1165
}
1166
1167
// Needs to be initialized to 0 because in some cases nsStringInputStream may
1168
// not write to aRead.
1169
uint32_t aRead = 0;
1170
MOZ_ASSERT(mResponse);
1171
MOZ_ASSERT(mPipeOutputStream);
1172
1173
// From "Main Fetch" step 19: SRI-part2.
1174
// Note: Avoid checking the hidden opaque body.
1175
nsresult rv;
1176
if (mResponse->Type() != ResponseType::Opaque &&
1177
ShouldCheckSRI(mRequest, mResponse)) {
1178
MOZ_ASSERT(mSRIDataVerifier);
1179
1180
SRIVerifierAndOutputHolder holder(mSRIDataVerifier, mPipeOutputStream);
1181
rv = aInputStream->ReadSegments(CopySegmentToStreamAndSRI, &holder, aCount,
1182
&aRead);
1183
} else {
1184
rv = aInputStream->ReadSegments(NS_CopySegmentToStream, mPipeOutputStream,
1185
aCount, &aRead);
1186
}
1187
1188
// If no data was read, it's possible the output stream is closed but the
1189
// ReadSegments call followed its contract of returning NS_OK despite write
1190
// errors. Unfortunately, nsIOutputStream has an ill-conceived contract when
1191
// taken together with ReadSegments' contract, because the pipe will just
1192
// NS_OK if we try and invoke its Write* functions ourselves with a 0 count.
1193
// So we must just assume the pipe is broken.
1194
if (aRead == 0 && aCount != 0) {
1195
return NS_BASE_STREAM_CLOSED;
1196
}
1197
return rv;
1198
}
1199
1200
NS_IMETHODIMP
1201
FetchDriver::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
1202
AssertIsOnMainThread();
1203
1204
MOZ_DIAGNOSTIC_ASSERT(!mOnStopRequestCalled);
1205
mOnStopRequestCalled = true;
1206
1207
// main data loading is going to finish, breaking the reference cycle.
1208
RefPtr<AlternativeDataStreamListener> altDataListener =
1209
mAltDataListener.forget();
1210
1211
// We need to check mObserver, which is nulled by FailWithNetworkError(),
1212
// because in the case of "error" redirect mode, aStatusCode may be NS_OK but
1213
// mResponse will definitely be null so we must not take the else branch.
1214
if (NS_FAILED(aStatusCode) || !mObserver) {
1215
nsCOMPtr<nsIAsyncOutputStream> outputStream =
1216
do_QueryInterface(mPipeOutputStream);
1217
if (outputStream) {
1218
outputStream->CloseWithStatus(NS_FAILED(aStatusCode) ? aStatusCode
1219
: NS_BINDING_FAILED);
1220
}
1221
if (altDataListener) {
1222
altDataListener->Cancel();
1223
}
1224
1225
// We proceed as usual here, since we've already created a successful
1226
// response from OnStartRequest.
1227
} else {
1228
MOZ_ASSERT(mResponse);
1229
MOZ_ASSERT(!mResponse->IsError());
1230
1231
// From "Main Fetch" step 19: SRI-part3.
1232
if (ShouldCheckSRI(mRequest, mResponse)) {
1233
MOZ_ASSERT(mSRIDataVerifier);
1234
1235
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
1236
1237
nsIConsoleReportCollector* reporter = nullptr;
1238
if (mObserver) {
1239
reporter = mObserver->GetReporter();
1240
}
1241
1242
nsAutoCString sourceUri;
1243
if (mDocument && mDocument->GetDocumentURI()) {
1244
mDocument->GetDocumentURI()->GetAsciiSpec(sourceUri);
1245
} else if (!mWorkerScript.IsEmpty()) {
1246
sourceUri.Assign(mWorkerScript);
1247
}
1248
nsresult rv =
1249
mSRIDataVerifier->Verify(mSRIMetadata, channel, sourceUri, reporter);
1250
if (NS_FAILED(rv)) {
1251
if (altDataListener) {
1252
altDataListener->Cancel();
1253
}
1254
FailWithNetworkError(rv);
1255
// Cancel request.
1256
return rv;
1257
}
1258
}
1259
1260
if (mPipeOutputStream) {
1261
mPipeOutputStream->Close();
1262
}
1263
}
1264
1265
FinishOnStopRequest(altDataListener);
1266
return NS_OK;
1267
}
1268
1269
void FetchDriver::FinishOnStopRequest(
1270
AlternativeDataStreamListener* aAltDataListener) {
1271
AssertIsOnMainThread();
1272
// OnStopRequest is not called from channel, that means the main data loading
1273
// does not finish yet. Reaching here since alternative data loading finishes.
1274
if (!mOnStopRequestCalled) {
1275
return;
1276
}
1277
1278
MOZ_DIAGNOSTIC_ASSERT(!mAltDataListener);
1279
// Wait for alternative data loading finish if we needed it.
1280
if (aAltDataListener &&
1281
aAltDataListener->Status() == AlternativeDataStreamListener::LOADING) {
1282
// For LOADING case, channel holds the reference of altDataListener, no need
1283
// to restore it to mAltDataListener.
1284
return;
1285
}
1286
1287
if (mObserver) {
1288
// From "Main Fetch" step 19.1, 19.2: Process response.
1289
if (ShouldCheckSRI(mRequest, mResponse)) {
1290
MOZ_ASSERT(mResponse);
1291
mObserver->OnResponseAvailable(mResponse);
1292
#ifdef DEBUG
1293
mResponseAvailableCalled = true;
1294
#endif
1295
}
1296
1297
mObserver->OnResponseEnd(FetchDriverObserver::eByNetworking);
1298
mObserver = nullptr;
1299
}
1300
1301
mChannel = nullptr;
1302
}
1303
1304
NS_IMETHODIMP
1305
FetchDriver::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
1306
nsIChannel* aNewChannel, uint32_t aFlags,
1307
nsIAsyncVerifyRedirectCallback* aCallback) {
1308
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aNewChannel);
1309
if (httpChannel) {
1310
SetRequestHeaders(httpChannel);
1311
}
1312
1313
// "HTTP-redirect fetch": step 14 "Append locationURL to request's URL list."
1314
// However, ignore internal redirects here. We don't want to flip
1315
// Response.redirected to true if an internal redirect occurs. These
1316
// should be transparent to script.
1317
nsCOMPtr<nsIURI> uri;
1318
MOZ_ALWAYS_SUCCEEDS(aNewChannel->GetURI(getter_AddRefs(uri)));
1319
1320
nsCOMPtr<nsIURI> uriClone;
1321
nsresult rv = NS_GetURIWithoutRef(uri, getter_AddRefs(uriClone));
1322
if (NS_WARN_IF(NS_FAILED(rv))) {
1323
return rv;
1324
}
1325
nsCString spec;
1326
rv = uriClone->GetSpec(spec);
1327
if (NS_WARN_IF(NS_FAILED(rv))) {
1328
return rv;
1329
}
1330
nsCString fragment;
1331
rv = uri->GetRef(fragment);
1332
if (NS_WARN_IF(NS_FAILED(rv))) {
1333
return rv;
1334
}
1335
1336
if (!(aFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) {
1337
mRequest->AddURL(spec, fragment);
1338
} else {
1339
// Overwrite the URL only when the request is redirected by a service
1340
// worker.
1341
mRequest->SetURLForInternalRedirect(aFlags, spec, fragment);
1342
}
1343
1344
// In redirect, httpChannel already took referrer-policy into account, so
1345
// updates request’s associated referrer policy from channel.
1346
if (httpChannel) {
1347
nsAutoString computedReferrerSpec;
1348
nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo();
1349
if (referrerInfo) {
1350
mRequest->SetReferrerPolicy(referrerInfo->ReferrerPolicy());
1351
Unused << referrerInfo->GetComputedReferrerSpec(computedReferrerSpec);
1352
}
1353
1355
// If request’s referrer is not "no-referrer" (empty), set request’s
1356
// referrer to the result of invoking determine request’s referrer.
1357
mRequest->SetReferrer(computedReferrerSpec);
1358
}
1359
1360
aCallback->OnRedirectVerifyCallback(NS_OK);
1361
return NS_OK;
1362
}
1363
1364
NS_IMETHODIMP
1365
FetchDriver::CheckListenerChain() { return NS_OK; }
1366
1367
NS_IMETHODIMP
1368
FetchDriver::GetInterface(const nsIID& aIID, void** aResult) {
1369
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
1370
*aResult = static_cast<nsIChannelEventSink*>(this);
1371
NS_ADDREF_THIS();
1372
return NS_OK;
1373
}
1374
if (aIID.Equals(NS_GET_IID(nsIStreamListener))) {
1375
*aResult = static_cast<nsIStreamListener*>(this);
1376
NS_ADDREF_THIS();
1377
return NS_OK;
1378
}
1379
if (aIID.Equals(NS_GET_IID(nsIRequestObserver))) {
1380
*aResult = static_cast<nsIRequestObserver*>(this);
1381
NS_ADDREF_THIS();
1382
return NS_OK;
1383
}
1384
1385
return QueryInterface(aIID, aResult);
1386
}
1387
1388
void FetchDriver::SetDocument(Document* aDocument) {
1389
// Cannot set document after Fetch() has been called.
1390
MOZ_ASSERT(!mFetchCalled);
1391
mDocument = aDocument;
1392
}
1393
1394
void FetchDriver::SetCSPEventListener(nsICSPEventListener* aCSPEventListener) {
1395
MOZ_ASSERT(!mFetchCalled);
1396
mCSPEventListener = aCSPEventListener;
1397
}
1398
1399
void FetchDriver::SetClientInfo(const ClientInfo& aClientInfo) {
1400
MOZ_ASSERT(!mFetchCalled);
1401
mClientInfo.emplace(aClientInfo);
1402
}
1403
1404
void FetchDriver::SetController(
1405
const Maybe<ServiceWorkerDescriptor>& aController) {
1406
MOZ_ASSERT(!mFetchCalled);
1407
mController = aController;
1408
}
1409
1410
void FetchDriver::SetRequestHeaders(nsIHttpChannel* aChannel) const {
1411
MOZ_ASSERT(aChannel);
1412
1413
// nsIHttpChannel has a set of pre-configured headers (Accept,
1414
// Accept-Languages, ...) and we don't want to merge the Request's headers
1415
// with them. This array is used to know if the current header has been aleady
1416
// set, if yes, we ask necko to merge it with the previous one, otherwise, we
1417
// don't want the merge.
1418
nsTArray<nsCString> headersSet;
1419
1420
AutoTArray<InternalHeaders::Entry, 5> headers;
1421
mRequest->Headers()->GetEntries(headers);
1422
for (uint32_t i = 0; i < headers.Length(); ++i) {
1423
bool alreadySet = headersSet.Contains(headers[i].mName);
1424
if (!alreadySet) {
1425
headersSet.AppendElement(headers[i].mName);
1426
}
1427
1428
if (headers[i].mValue.IsEmpty()) {
1429
DebugOnly<nsresult> rv =
1430
aChannel->SetEmptyRequestHeader(headers[i].mName);
1431
MOZ_ASSERT(NS_SUCCEEDED(rv));
1432
} else {
1433
DebugOnly<nsresult> rv = aChannel->SetRequestHeader(
1434
headers[i].mName, headers[i].mValue, alreadySet /* merge */);
1435
MOZ_ASSERT(NS_SUCCEEDED(rv));
1436
}
1437
}
1438
1439
nsAutoCString method;
1440
mRequest->GetMethod(method);
1441
if (!method.EqualsLiteral("GET") && !method.EqualsLiteral("HEAD")) {
1442
nsAutoString origin;
1443
if (NS_SUCCEEDED(nsContentUtils::GetUTFOrigin(mPrincipal, origin))) {
1444
DebugOnly<nsresult> rv = aChannel->SetRequestHeader(
1445
nsDependentCString(net::nsHttp::Origin),
1446
NS_ConvertUTF16toUTF8(origin), false /* merge */);
1447
MOZ_ASSERT(NS_SUCCEEDED(rv));
1448
}
1449
}
1450
}
1451
1452
void FetchDriver::Abort() {
1453
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
1454
1455
if (mObserver) {
1456
#ifdef DEBUG
1457
mResponseAvailableCalled = true;
1458
#endif
1459
mObserver->OnResponseEnd(FetchDriverObserver::eAborted);
1460
mObserver = nullptr;
1461
}
1462
1463
if (mChannel) {
1464
mChannel->Cancel(NS_BINDING_ABORTED);
1465
mChannel = nullptr;
1466
}
1467
}
1468
1469
} // namespace dom
1470
} // namespace mozilla