Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/net/HttpBaseChannel.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 ts=8 et tw=80 : */
3
4
/* This Source Code Form is subject to the terms of the Mozilla Public
5
 * License, v. 2.0. If a copy of the MPL was not distributed with this
6
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8
#ifndef mozilla_net_HttpBaseChannel_h
9
#define mozilla_net_HttpBaseChannel_h
10
11
#include "mozilla/Atomics.h"
12
#include "nsHttp.h"
13
#include "nsAutoPtr.h"
14
#include "nsHashPropertyBag.h"
15
#include "nsProxyInfo.h"
16
#include "nsHttpRequestHead.h"
17
#include "nsHttpResponseHead.h"
18
#include "nsHttpConnectionInfo.h"
19
#include "nsIConsoleReportCollector.h"
20
#include "nsIEncodedChannel.h"
21
#include "nsIHttpChannel.h"
22
#include "nsHttpHandler.h"
23
#include "nsIHttpChannelInternal.h"
24
#include "nsIForcePendingChannel.h"
25
#include "nsIFormPOSTActionChannel.h"
26
#include "nsIUploadChannel2.h"
27
#include "nsIProgressEventSink.h"
28
#include "nsIURI.h"
29
#include "nsIEffectiveTLDService.h"
30
#include "nsIStringEnumerator.h"
31
#include "nsISupportsPriority.h"
32
#include "nsIClassOfService.h"
33
#include "nsIClassifiedChannel.h"
34
#include "nsIApplicationCache.h"
35
#include "nsIResumableChannel.h"
36
#include "nsITraceableChannel.h"
37
#include "nsILoadContext.h"
38
#include "nsILoadInfo.h"
39
#include "mozilla/net/NeckoCommon.h"
40
#include "nsThreadUtils.h"
41
#include "PrivateBrowsingChannel.h"
42
#include "mozilla/net/DNS.h"
43
#include "nsITimedChannel.h"
44
#include "nsIHttpChannel.h"
45
#include "nsISecurityConsoleMessage.h"
46
#include "nsCOMArray.h"
47
#include "mozilla/net/ChannelEventQueue.h"
48
#include "mozilla/Move.h"
49
#include "nsIThrottledInputChannel.h"
50
#include "nsTArray.h"
51
#include "nsCOMPtr.h"
52
#include "mozilla/IntegerPrintfMacros.h"
53
#include "nsStringEnumerator.h"
54
55
#define HTTP_BASE_CHANNEL_IID \
56
{ 0x9d5cde03, 0xe6e9, 0x4612, \
57
    { 0xbf, 0xef, 0xbb, 0x66, 0xf3, 0xbb, 0x74, 0x46 } }
58
59
60
class nsISecurityConsoleMessage;
61
class nsIPrincipal;
62
63
namespace mozilla {
64
65
namespace dom {
66
class PerformanceStorage;
67
}
68
69
class LogCollector;
70
71
namespace net {
72
extern mozilla::LazyLogModule gHttpLog;
73
74
/*
75
 * This class is a partial implementation of nsIHttpChannel.  It contains code
76
 * shared by nsHttpChannel and HttpChannelChild.
77
 * - Note that this class has nothing to do with nsBaseChannel, which is an
78
 *   earlier effort at a base class for channels that somehow never made it all
79
 *   the way to the HTTP channel.
80
 */
81
class HttpBaseChannel : public nsHashPropertyBag
82
                      , public nsIEncodedChannel
83
                      , public nsIHttpChannel
84
                      , public nsIHttpChannelInternal
85
                      , public nsIFormPOSTActionChannel
86
                      , public nsIUploadChannel2
87
                      , public nsISupportsPriority
88
                      , public nsIClassOfService
89
                      , public nsIResumableChannel
90
                      , public nsITraceableChannel
91
                      , public PrivateBrowsingChannel<HttpBaseChannel>
92
                      , public nsITimedChannel
93
                      , public nsIForcePendingChannel
94
                      , public nsIConsoleReportCollector
95
                      , public nsIThrottledInputChannel
96
                      , public nsIClassifiedChannel
97
{
98
protected:
99
  virtual ~HttpBaseChannel();
100
101
public:
102
  NS_DECL_ISUPPORTS_INHERITED
103
  NS_DECL_NSIUPLOADCHANNEL
104
  NS_DECL_NSIFORMPOSTACTIONCHANNEL
105
  NS_DECL_NSIUPLOADCHANNEL2
106
  NS_DECL_NSITRACEABLECHANNEL
107
  NS_DECL_NSITIMEDCHANNEL
108
  NS_DECL_NSITHROTTLEDINPUTCHANNEL
109
  NS_DECL_NSICLASSIFIEDCHANNEL
110
111
  NS_DECLARE_STATIC_IID_ACCESSOR(HTTP_BASE_CHANNEL_IID)
112
113
  HttpBaseChannel();
114
115
  virtual MOZ_MUST_USE nsresult Init(nsIURI *aURI, uint32_t aCaps,
116
                                     nsProxyInfo *aProxyInfo,
117
                                     uint32_t aProxyResolveFlags,
118
                                     nsIURI *aProxyURI,
119
                                     uint64_t aChannelId);
120
121
  // nsIRequest
122
  NS_IMETHOD GetName(nsACString& aName) override;
123
  NS_IMETHOD IsPending(bool *aIsPending) override;
124
  NS_IMETHOD GetStatus(nsresult *aStatus) override;
125
  NS_IMETHOD GetLoadGroup(nsILoadGroup **aLoadGroup) override;
126
  NS_IMETHOD SetLoadGroup(nsILoadGroup *aLoadGroup) override;
127
  NS_IMETHOD GetLoadFlags(nsLoadFlags *aLoadFlags) override;
128
  NS_IMETHOD SetLoadFlags(nsLoadFlags aLoadFlags) override;
129
  NS_IMETHOD SetDocshellUserAgentOverride();
130
131
  // nsIChannel
132
  NS_IMETHOD GetOriginalURI(nsIURI **aOriginalURI) override;
133
  NS_IMETHOD SetOriginalURI(nsIURI *aOriginalURI) override;
134
  NS_IMETHOD GetURI(nsIURI **aURI) override;
135
  NS_IMETHOD GetOwner(nsISupports **aOwner) override;
136
  NS_IMETHOD SetOwner(nsISupports *aOwner) override;
137
  NS_IMETHOD GetLoadInfo(nsILoadInfo **aLoadInfo) override;
138
  NS_IMETHOD SetLoadInfo(nsILoadInfo *aLoadInfo) override;
139
  NS_IMETHOD GetIsDocument(bool *aIsDocument) override;
140
  NS_IMETHOD GetNotificationCallbacks(nsIInterfaceRequestor **aCallbacks) override;
141
  NS_IMETHOD SetNotificationCallbacks(nsIInterfaceRequestor *aCallbacks) override;
142
  NS_IMETHOD GetContentType(nsACString& aContentType) override;
143
  NS_IMETHOD SetContentType(const nsACString& aContentType) override;
144
  NS_IMETHOD GetContentCharset(nsACString& aContentCharset) override;
145
  NS_IMETHOD SetContentCharset(const nsACString& aContentCharset) override;
146
  NS_IMETHOD GetContentDisposition(uint32_t *aContentDisposition) override;
147
  NS_IMETHOD SetContentDisposition(uint32_t aContentDisposition) override;
148
  NS_IMETHOD GetContentDispositionFilename(nsAString& aContentDispositionFilename) override;
149
  NS_IMETHOD SetContentDispositionFilename(const nsAString& aContentDispositionFilename) override;
150
  NS_IMETHOD GetContentDispositionHeader(nsACString& aContentDispositionHeader) override;
151
  NS_IMETHOD GetContentLength(int64_t *aContentLength) override;
152
  NS_IMETHOD SetContentLength(int64_t aContentLength) override;
153
  NS_IMETHOD Open(nsIInputStream **aResult) override;
154
  NS_IMETHOD Open2(nsIInputStream **aResult) override;
155
  NS_IMETHOD GetBlockAuthPrompt(bool* aValue) override;
156
  NS_IMETHOD SetBlockAuthPrompt(bool aValue) override;
157
158
  // nsIEncodedChannel
159
  NS_IMETHOD GetApplyConversion(bool *value) override;
160
  NS_IMETHOD SetApplyConversion(bool value) override;
161
  NS_IMETHOD GetContentEncodings(nsIUTF8StringEnumerator** aEncodings) override;
162
  NS_IMETHOD DoApplyContentConversions(nsIStreamListener *aNextListener,
163
                                       nsIStreamListener **aNewNextListener,
164
                                       nsISupports *aCtxt) override;
165
166
  // HttpBaseChannel::nsIHttpChannel
167
  NS_IMETHOD GetRequestMethod(nsACString& aMethod) override;
168
  NS_IMETHOD SetRequestMethod(const nsACString& aMethod) override;
169
  NS_IMETHOD GetReferrer(nsIURI **referrer) override;
170
  NS_IMETHOD SetReferrer(nsIURI *referrer) override;
171
  NS_IMETHOD GetReferrerPolicy(uint32_t *referrerPolicy) override;
172
  NS_IMETHOD SetReferrerWithPolicy(nsIURI *referrer, uint32_t referrerPolicy) override;
173
  NS_IMETHOD GetRequestHeader(const nsACString& aHeader, nsACString& aValue) override;
174
  NS_IMETHOD SetRequestHeader(const nsACString& aHeader,
175
                              const nsACString& aValue, bool aMerge) override;
176
  NS_IMETHOD SetEmptyRequestHeader(const nsACString& aHeader) override;
177
  NS_IMETHOD VisitRequestHeaders(nsIHttpHeaderVisitor *visitor) override;
178
  NS_IMETHOD VisitNonDefaultRequestHeaders(nsIHttpHeaderVisitor *visitor) override;
179
  NS_IMETHOD GetResponseHeader(const nsACString &header, nsACString &value) override;
180
  NS_IMETHOD SetResponseHeader(const nsACString& header,
181
                               const nsACString& value, bool merge) override;
182
  NS_IMETHOD VisitResponseHeaders(nsIHttpHeaderVisitor *visitor) override;
183
  NS_IMETHOD GetOriginalResponseHeader(const nsACString &aHeader,
184
                                       nsIHttpHeaderVisitor *aVisitor) override;
185
  NS_IMETHOD VisitOriginalResponseHeaders(nsIHttpHeaderVisitor *aVisitor) override;
186
  NS_IMETHOD GetAllowPipelining(bool *value) override; // deprecated
187
  NS_IMETHOD SetAllowPipelining(bool value) override;  // deprecated
188
  NS_IMETHOD GetAllowSTS(bool *value) override;
189
  NS_IMETHOD SetAllowSTS(bool value) override;
190
  NS_IMETHOD GetRedirectionLimit(uint32_t *value) override;
191
  NS_IMETHOD SetRedirectionLimit(uint32_t value) override;
192
  NS_IMETHOD IsNoStoreResponse(bool *value) override;
193
  NS_IMETHOD IsNoCacheResponse(bool *value) override;
194
  NS_IMETHOD IsPrivateResponse(bool *value) override;
195
  NS_IMETHOD GetResponseStatus(uint32_t *aValue) override;
196
  NS_IMETHOD GetResponseStatusText(nsACString& aValue) override;
197
  NS_IMETHOD GetRequestSucceeded(bool *aValue) override;
198
  NS_IMETHOD RedirectTo(nsIURI *newURI) override;
199
  NS_IMETHOD UpgradeToSecure() override;
200
  NS_IMETHOD GetRequestContextID(uint64_t *aRCID) override;
201
  NS_IMETHOD GetTransferSize(uint64_t *aTransferSize) override;
202
  NS_IMETHOD GetDecodedBodySize(uint64_t *aDecodedBodySize) override;
203
  NS_IMETHOD GetEncodedBodySize(uint64_t *aEncodedBodySize) override;
204
  NS_IMETHOD SetRequestContextID(uint64_t aRCID) override;
205
  NS_IMETHOD GetIsMainDocumentChannel(bool* aValue) override;
206
  NS_IMETHOD SetIsMainDocumentChannel(bool aValue) override;
207
  NS_IMETHOD GetProtocolVersion(nsACString & aProtocolVersion) override;
208
  NS_IMETHOD GetChannelId(uint64_t *aChannelId) override;
209
  NS_IMETHOD SetChannelId(uint64_t aChannelId) override;
210
  NS_IMETHOD GetTopLevelContentWindowId(uint64_t *aContentWindowId) override;
211
  NS_IMETHOD SetTopLevelContentWindowId(uint64_t aContentWindowId) override;
212
  NS_IMETHOD GetTopLevelOuterContentWindowId(uint64_t *aWindowId) override;
213
  NS_IMETHOD SetTopLevelOuterContentWindowId(uint64_t aWindowId) override;
214
  NS_IMETHOD GetIsTrackingResource(bool* aIsTrackingResource) override;
215
  NS_IMETHOD GetIsThirdPartyTrackingResource(bool* aIsTrackingResource) override;
216
  NS_IMETHOD OverrideTrackingFlagsForDocumentCookieAccessor(nsIHttpChannel* aDocumentChannel) override;
217
218
  // nsIHttpChannelInternal
219
  NS_IMETHOD GetDocumentURI(nsIURI **aDocumentURI) override;
220
  NS_IMETHOD SetDocumentURI(nsIURI *aDocumentURI) override;
221
  NS_IMETHOD GetRequestVersion(uint32_t *major, uint32_t *minor) override;
222
  NS_IMETHOD GetResponseVersion(uint32_t *major, uint32_t *minor) override;
223
  NS_IMETHOD SetCookie(const char *aCookieHeader) override;
224
  NS_IMETHOD GetThirdPartyFlags(uint32_t *aForce) override;
225
  NS_IMETHOD SetThirdPartyFlags(uint32_t aForce) override;
226
  NS_IMETHOD GetForceAllowThirdPartyCookie(bool *aForce) override;
227
  NS_IMETHOD SetForceAllowThirdPartyCookie(bool aForce) override;
228
  NS_IMETHOD GetCanceled(bool *aCanceled) override;
229
  NS_IMETHOD GetChannelIsForDownload(bool *aChannelIsForDownload) override;
230
  NS_IMETHOD SetChannelIsForDownload(bool aChannelIsForDownload) override;
231
  NS_IMETHOD SetCacheKeysRedirectChain(nsTArray<nsCString> *cacheKeys) override;
232
  NS_IMETHOD GetLocalAddress(nsACString& addr) override;
233
  NS_IMETHOD GetLocalPort(int32_t* port) override;
234
  NS_IMETHOD GetRemoteAddress(nsACString& addr) override;
235
  NS_IMETHOD GetRemotePort(int32_t* port) override;
236
  NS_IMETHOD GetAllowSpdy(bool *aAllowSpdy) override;
237
  NS_IMETHOD SetAllowSpdy(bool aAllowSpdy) override;
238
  NS_IMETHOD GetAllowAltSvc(bool *aAllowAltSvc) override;
239
  NS_IMETHOD SetAllowAltSvc(bool aAllowAltSvc) override;
240
  NS_IMETHOD GetBeConservative(bool *aBeConservative) override;
241
  NS_IMETHOD SetBeConservative(bool aBeConservative) override;
242
  NS_IMETHOD GetTrr(bool *aTRR) override;
243
  NS_IMETHOD SetTrr(bool aTRR) override;
244
  NS_IMETHOD GetTlsFlags(uint32_t *aTlsFlags) override;
245
  NS_IMETHOD SetTlsFlags(uint32_t aTlsFlags) override;
246
  NS_IMETHOD GetApiRedirectToURI(nsIURI * *aApiRedirectToURI) override;
247
  virtual MOZ_MUST_USE nsresult AddSecurityMessage(const nsAString &aMessageTag, const nsAString &aMessageCategory);
248
  NS_IMETHOD TakeAllSecurityMessages(nsCOMArray<nsISecurityConsoleMessage> &aMessages) override;
249
  NS_IMETHOD GetResponseTimeoutEnabled(bool *aEnable) override;
250
  NS_IMETHOD SetResponseTimeoutEnabled(bool aEnable) override;
251
  NS_IMETHOD GetInitialRwin(uint32_t* aRwin) override;
252
  NS_IMETHOD SetInitialRwin(uint32_t aRwin) override;
253
  NS_IMETHOD ForcePending(bool aForcePending) override;
254
  NS_IMETHOD GetLastModifiedTime(PRTime* lastModifiedTime) override;
255
  NS_IMETHOD GetCorsIncludeCredentials(bool* aInclude) override;
256
  NS_IMETHOD SetCorsIncludeCredentials(bool aInclude) override;
257
  NS_IMETHOD GetCorsMode(uint32_t* aCorsMode) override;
258
  NS_IMETHOD SetCorsMode(uint32_t aCorsMode) override;
259
  NS_IMETHOD GetRedirectMode(uint32_t* aRedirectMode) override;
260
  NS_IMETHOD SetRedirectMode(uint32_t aRedirectMode) override;
261
  NS_IMETHOD GetFetchCacheMode(uint32_t* aFetchCacheMode) override;
262
  NS_IMETHOD SetFetchCacheMode(uint32_t aFetchCacheMode) override;
263
  NS_IMETHOD GetTopWindowURI(nsIURI **aTopWindowURI) override;
264
  NS_IMETHOD SetTopWindowURIIfUnknown(nsIURI *aTopWindowURI) override;
265
  NS_IMETHOD GetProxyURI(nsIURI **proxyURI) override;
266
  virtual void SetCorsPreflightParameters(const nsTArray<nsCString>& unsafeHeaders) override;
267
  virtual void SetAltDataForChild(bool aIsForChild) override;
268
  NS_IMETHOD GetConnectionInfoHashKey(nsACString& aConnectionInfoHashKey) override;
269
  NS_IMETHOD GetIntegrityMetadata(nsAString& aIntegrityMetadata) override;
270
  NS_IMETHOD SetIntegrityMetadata(const nsAString& aIntegrityMetadata) override;
271
  NS_IMETHOD GetLastRedirectFlags(uint32_t *aValue) override;
272
  NS_IMETHOD SetLastRedirectFlags(uint32_t aValue) override;
273
  NS_IMETHOD GetNavigationStartTimeStamp(TimeStamp* aTimeStamp) override;
274
  NS_IMETHOD SetNavigationStartTimeStamp(TimeStamp aTimeStamp) override;
275
  NS_IMETHOD CancelForTrackingProtection() override;
276
277
  inline void CleanRedirectCacheChainIfNecessary()
278
0
  {
279
0
      mRedirectedCachekeys = nullptr;
280
0
  }
281
  NS_IMETHOD HTTPUpgrade(const nsACString & aProtocolName,
282
                         nsIHttpUpgradeListener *aListener) override;
283
284
  // nsISupportsPriority
285
  NS_IMETHOD GetPriority(int32_t *value) override;
286
  NS_IMETHOD AdjustPriority(int32_t delta) override;
287
288
  // nsIClassOfService
289
0
  NS_IMETHOD GetClassFlags(uint32_t *outFlags) override { *outFlags = mClassOfService; return NS_OK; }
290
291
  // nsIResumableChannel
292
  NS_IMETHOD GetEntityID(nsACString& aEntityID) override;
293
294
295
  // nsIConsoleReportCollector
296
  void
297
  AddConsoleReport(uint32_t aErrorFlags, const nsACString& aCategory,
298
                   nsContentUtils::PropertiesFile aPropertiesFile,
299
                   const nsACString& aSourceFileURI,
300
                   uint32_t aLineNumber, uint32_t aColumnNumber,
301
                   const nsACString& aMessageName,
302
                   const nsTArray<nsString>& aStringParams) override;
303
304
  void
305
  FlushReportsToConsole(uint64_t aInnerWindowID,
306
                        ReportAction aAction = ReportAction::Forget) override;
307
308
  void
309
  FlushReportsToConsoleForServiceWorkerScope(const nsACString& aScope,
310
                                             ReportAction aAction = ReportAction::Forget) override;
311
312
  void
313
  FlushConsoleReports(nsIDocument* aDocument,
314
                      ReportAction aAction = ReportAction::Forget) override;
315
316
  void
317
  FlushConsoleReports(nsILoadGroup* aLoadGroup,
318
                      ReportAction aAction = ReportAction::Forget) override;
319
320
  void
321
  FlushConsoleReports(nsIConsoleReportCollector* aCollector) override;
322
323
  void
324
  ClearConsoleReports() override;
325
326
  class nsContentEncodings : public nsStringEnumeratorBase
327
    {
328
    public:
329
        NS_DECL_ISUPPORTS
330
        NS_DECL_NSIUTF8STRINGENUMERATOR
331
332
        using nsStringEnumeratorBase::GetNext;
333
334
        nsContentEncodings(nsIHttpChannel* aChannel, const char* aEncodingHeader);
335
336
    private:
337
0
        virtual ~nsContentEncodings() = default;
338
339
        MOZ_MUST_USE nsresult PrepareForNext(void);
340
341
        // We do not own the buffer.  The channel owns it.
342
        const char* mEncodingHeader;
343
        const char* mCurStart;  // points to start of current header
344
        const char* mCurEnd;  // points to end of current header
345
346
        // Hold a ref to our channel so that it can't go away and take the
347
        // header with it.
348
        nsCOMPtr<nsIHttpChannel> mChannel;
349
350
        bool mReady;
351
    };
352
353
0
    nsHttpResponseHead * GetResponseHead() const { return mResponseHead; }
354
0
    nsHttpRequestHead * GetRequestHead() { return &mRequestHead; }
355
0
    nsHttpHeaderArray * GetResponseTrailers() const { return mResponseTrailers; }
356
357
0
    const NetAddr& GetSelfAddr() { return mSelfAddr; }
358
0
    const NetAddr& GetPeerAddr() { return mPeerAddr; }
359
360
    MOZ_MUST_USE nsresult OverrideSecurityInfo(nsISupports* aSecurityInfo);
361
362
public: /* Necko internal use only... */
363
0
    int64_t GetAltDataLength() { return mAltDataLength; }
364
    bool IsNavigation();
365
366
    static bool IsReferrerSchemeAllowed(nsIURI *aReferrer);
367
368
    static void PropagateReferenceIfNeeded(nsIURI *aURI, nsCOMPtr<nsIURI>& aRedirectURI);
369
370
    // Return whether upon a redirect code of httpStatus for method, the
371
    // request method should be rewritten to GET.
372
    static bool ShouldRewriteRedirectToGET(uint32_t httpStatus,
373
                                           nsHttpRequestHead::ParsedMethodType method);
374
375
    // Like nsIEncodedChannel::DoApplyConversions except context is set to
376
    // mListenerContext.
377
    MOZ_MUST_USE nsresult
378
    DoApplyContentConversions(nsIStreamListener *aNextListener,
379
                              nsIStreamListener **aNewNextListener);
380
381
    // Callback on STS thread called by CopyComplete when NS_AsyncCopy()
382
    // is finished. This function works as a proxy function to dispatch
383
    // |EnsureUploadStreamIsCloneableComplete| to main thread.
384
    virtual void OnCopyComplete(nsresult aStatus);
385
386
    void SetIsTrackingResource(bool aIsThirdParty);
387
388
    const uint64_t& ChannelId() const
389
0
    {
390
0
      return mChannelId;
391
0
    }
392
393
    void InternalSetUploadStream(nsIInputStream *uploadStream)
394
0
    {
395
0
      mUploadStream = uploadStream;
396
0
    }
397
398
    void InternalSetUploadStreamLength(uint64_t aLength)
399
0
    {
400
0
      mReqContentLength = aLength;
401
0
    }
402
403
    void SetUploadStreamHasHeaders(bool hasHeaders)
404
0
    {
405
0
      mUploadStreamHasHeaders = hasHeaders;
406
0
    }
407
408
    MOZ_MUST_USE nsresult
409
    SetReferrerWithPolicyInternal(nsIURI *referrer, uint32_t referrerPolicy)
410
0
    {
411
0
      nsAutoCString spec;
412
0
      nsresult rv = referrer->GetAsciiSpec(spec);
413
0
      if (NS_FAILED(rv)) {
414
0
        return rv;
415
0
      }
416
0
      mReferrer = referrer;
417
0
      mReferrerPolicy = referrerPolicy;
418
0
      rv = mRequestHead.SetHeader(nsHttp::Referer, spec);
419
0
      return rv;
420
0
    }
421
422
    MOZ_MUST_USE nsresult SetTopWindowURI(nsIURI* aTopWindowURI)
423
0
    {
424
0
      mTopWindowURI = aTopWindowURI;
425
0
      return NS_OK;
426
0
    }
427
428
protected:
429
  // Handle notifying listener, removing from loadgroup if request failed.
430
  void     DoNotifyListener();
431
  virtual void DoNotifyListenerCleanup() = 0;
432
433
  // drop reference to listener, its callbacks, and the progress sink
434
  virtual void ReleaseListeners();
435
436
  // Call AsyncAbort().
437
  virtual void
438
  DoAsyncAbort(nsresult aStatus) = 0;
439
440
  // This is fired only when a cookie is created due to the presence of
441
  // Set-Cookie header in the response header of any network request.
442
  // This notification will come only after the "http-on-examine-response"
443
  // was fired.
444
  void NotifySetCookie(char const *aCookie);
445
446
  mozilla::dom::PerformanceStorage* GetPerformanceStorage();
447
  void MaybeReportTimingData();
448
  nsIURI* GetReferringPage();
449
  nsPIDOMWindowInner* GetInnerDOMWindow();
450
451
  void AddCookiesToRequest();
452
  virtual MOZ_MUST_USE nsresult
453
  SetupReplacementChannel(nsIURI *, nsIChannel *, bool preserveMethod,
454
                          uint32_t redirectFlags);
455
456
  // bundle calling OMR observers and marking flag into one function
457
0
  inline void CallOnModifyRequestObservers() {
458
0
    gHttpHandler->OnModifyRequest(this);
459
0
    MOZ_ASSERT(!mRequestObserversCalled);
460
0
    mRequestObserversCalled = true;
461
0
  }
462
463
  // Helper function to simplify getting notification callbacks.
464
  template <class T>
465
  void GetCallback(nsCOMPtr<T> &aResult)
466
0
  {
467
0
    NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup,
468
0
                                  NS_GET_TEMPLATE_IID(T),
469
0
                                  getter_AddRefs(aResult));
470
0
  }
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsILoadContext>(nsCOMPtr<nsILoadContext>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsINetworkInterceptController>(nsCOMPtr<nsINetworkInterceptController>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsIProgressEventSink>(nsCOMPtr<nsIProgressEventSink>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsITabChild>(nsCOMPtr<nsITabChild>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsIApplicationCacheContainer>(nsCOMPtr<nsIApplicationCacheContainer>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsIDeprecationWarner>(nsCOMPtr<nsIDeprecationWarner>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsIRedirectResultListener>(nsCOMPtr<nsIRedirectResultListener>&)
Unexecuted instantiation: void mozilla::net::HttpBaseChannel::GetCallback<nsIPrompt>(nsCOMPtr<nsIPrompt>&)
471
472
  // Redirect tracking
473
  // Checks whether or not aURI and mOriginalURI share the same domain.
474
  bool SameOriginWithOriginalUri(nsIURI *aURI);
475
476
  // GetPrincipal Returns the channel's URI principal.
477
  nsIPrincipal *GetURIPrincipal();
478
479
  MOZ_MUST_USE bool BypassServiceWorker() const;
480
481
  // Returns true if this channel should intercept the network request and prepare
482
  // for a possible synthesized response instead.
483
  bool ShouldIntercept(nsIURI* aURI = nullptr);
484
485
  // Callback on main thread when NS_AsyncCopy() is finished populating
486
  // the new mUploadStream.
487
  void EnsureUploadStreamIsCloneableComplete(nsresult aStatus);
488
489
#ifdef DEBUG
490
  // Check if mPrivateBrowsingId matches between LoadInfo and LoadContext.
491
  void AssertPrivateBrowsingId();
492
#endif
493
494
  // Called before we create the redirect target channel.
495
  already_AddRefed<nsILoadInfo> CloneLoadInfoForRedirect(nsIURI *newURI, uint32_t redirectFlags);
496
497
  static void CallTypeSniffers(void *aClosure, const uint8_t *aData,
498
                               uint32_t aCount);
499
500
  nsresult
501
  CheckRedirectLimit(uint32_t aRedirectFlags) const;
502
503
  bool
504
  MaybeWaitForUploadStreamLength(nsIStreamListener *aListener,
505
                                 nsISupports *aContext);
506
507
  friend class PrivateBrowsingChannel<HttpBaseChannel>;
508
  friend class InterceptFailedOnStop;
509
510
protected:
511
  // this section is for main-thread-only object
512
  // all the references need to be proxy released on main thread.
513
  nsCOMPtr<nsIURI> mURI;
514
  nsCOMPtr<nsIURI> mOriginalURI;
515
  nsCOMPtr<nsIURI> mDocumentURI;
516
  nsCOMPtr<nsILoadGroup> mLoadGroup;
517
  nsCOMPtr<nsILoadInfo> mLoadInfo;
518
  nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
519
  nsCOMPtr<nsIProgressEventSink> mProgressSink;
520
  nsCOMPtr<nsIURI> mReferrer;
521
  nsCOMPtr<nsIApplicationCache> mApplicationCache;
522
  nsCOMPtr<nsIURI> mAPIRedirectToURI;
523
  nsCOMPtr<nsIURI> mProxyURI;
524
  nsCOMPtr<nsIPrincipal> mPrincipal;
525
  nsCOMPtr<nsIURI> mTopWindowURI;
526
  nsCOMPtr<nsIStreamListener> mListener;
527
  nsCOMPtr<nsISupports> mListenerContext;
528
  // An instance of nsHTTPCompressConv
529
  nsCOMPtr<nsIStreamListener> mCompressListener;
530
531
private:
532
  // Proxy release all members above on main thread.
533
  void ReleaseMainThreadOnlyReferences();
534
535
  bool IsCrossOriginWithReferrer();
536
537
  nsresult
538
  ExplicitSetUploadStreamLength(uint64_t aContentLength,
539
                                bool aStreamHasHeaders);
540
541
  void
542
  MaybeResumeAsyncOpen();
543
544
protected:
545
  nsCString mSpec; // ASCII encoded URL spec
546
  nsCString mContentTypeHint;
547
  nsCString mContentCharsetHint;
548
  nsCString mUserSetCookieHeader;
549
  // HTTP Upgrade Data
550
  nsCString mUpgradeProtocol;
551
  // Resumable channel specific data
552
  nsCString mEntityID;
553
  // The initiator type (for this resource) - how was the resource referenced in
554
  // the HTML file.
555
  nsString mInitiatorType;
556
  // Holds the name of the preferred alt-data type.
557
  nsCString mPreferredCachedAltDataType;
558
  // Holds the name of the alternative data type the channel returned.
559
  nsCString mAvailableCachedAltDataType;
560
  nsString mIntegrityMetadata;
561
562
  // Classified channel's matched information
563
  nsCString mMatchedList;
564
  nsCString mMatchedProvider;
565
  nsCString mMatchedFullHash;
566
567
568
  nsCOMPtr<nsISupports>             mOwner;
569
570
  nsHttpRequestHead                 mRequestHead;
571
  // Upload throttling.
572
  nsCOMPtr<nsIInputChannelThrottleQueue> mThrottleQueue;
573
  nsCOMPtr<nsIInputStream>          mUploadStream;
574
  nsCOMPtr<nsIRunnable>             mUploadCloneableCallback;
575
  nsAutoPtr<nsHttpResponseHead>     mResponseHead;
576
  nsAutoPtr<nsHttpHeaderArray>      mResponseTrailers;
577
  RefPtr<nsHttpConnectionInfo>      mConnectionInfo;
578
  nsCOMPtr<nsIProxyInfo>            mProxyInfo;
579
  nsCOMPtr<nsISupports>             mSecurityInfo;
580
  nsCOMPtr<nsIHttpUpgradeListener> mUpgradeProtocolCallback;
581
  nsAutoPtr<nsString> mContentDispositionFilename;
582
  nsCOMPtr<nsIConsoleReportCollector> mReportCollector;
583
584
  RefPtr<nsHttpHandler> mHttpHandler;  // keep gHttpHandler alive
585
  nsAutoPtr<nsTArray<nsCString> > mRedirectedCachekeys;
586
  nsCOMPtr<nsIRequestContext> mRequestContext;
587
588
  NetAddr                           mSelfAddr;
589
  NetAddr                           mPeerAddr;
590
591
  nsTArray<Pair<nsString, nsString>> mSecurityConsoleMessages;
592
  nsTArray<nsCString>               mUnsafeHeaders;
593
594
  // A time value equal to the starting time of the fetch that initiates the
595
  // redirect.
596
  mozilla::TimeStamp                mRedirectStartTimeStamp;
597
  // A time value equal to the time immediately after receiving the last byte of
598
  // the response of the last redirect.
599
  mozilla::TimeStamp                mRedirectEndTimeStamp;
600
601
  PRTime                            mChannelCreationTime;
602
  TimeStamp                         mChannelCreationTimestamp;
603
  TimeStamp                         mAsyncOpenTime;
604
  TimeStamp                         mCacheReadStart;
605
  TimeStamp                         mCacheReadEnd;
606
  TimeStamp                         mLaunchServiceWorkerStart;
607
  TimeStamp                         mLaunchServiceWorkerEnd;
608
  TimeStamp                         mDispatchFetchEventStart;
609
  TimeStamp                         mDispatchFetchEventEnd;
610
  TimeStamp                         mHandleFetchEventStart;
611
  TimeStamp                         mHandleFetchEventEnd;
612
  // copied from the transaction before we null out mTransaction
613
  // so that the timing can still be queried from OnStopRequest
614
  TimingStruct                      mTransactionTimings;
615
616
  uint64_t                          mStartPos;
617
  uint64_t mTransferSize;
618
  uint64_t mDecodedBodySize;
619
  uint64_t mEncodedBodySize;
620
  uint64_t mRequestContextID;
621
  // ID of the top-level document's inner window this channel is being
622
  // originated from.
623
  uint64_t mContentWindowId;
624
  uint64_t mTopLevelOuterContentWindowId;
625
  int64_t mAltDataLength;
626
  uint64_t mChannelId;
627
  uint64_t mReqContentLength;
628
629
  Atomic<nsresult, ReleaseAcquire>  mStatus;
630
631
  // Use Release-Acquire ordering to ensure the OMT ODA is ignored while channel
632
  // is canceled on main thread.
633
  Atomic<bool, ReleaseAcquire> mCanceled;
634
  Atomic<bool, ReleaseAcquire> mIsFirstPartyTrackingResource;
635
  Atomic<bool, ReleaseAcquire> mIsThirdPartyTrackingResource;
636
637
  uint32_t                          mLoadFlags;
638
  uint32_t                          mCaps;
639
  uint32_t                          mClassOfService;
640
641
  uint32_t                          mUpgradeToSecure            : 1;
642
  uint32_t                          mApplyConversion            : 1;
643
  uint32_t                          mIsPending                  : 1;
644
  uint32_t                          mWasOpened                  : 1;
645
  // if 1 all "http-on-{opening|modify|etc}-request" observers have been called
646
  uint32_t                          mRequestObserversCalled     : 1;
647
  uint32_t                          mResponseHeadersModified    : 1;
648
  uint32_t                          mAllowSTS                   : 1;
649
  uint32_t                          mThirdPartyFlags            : 3;
650
  uint32_t                          mUploadStreamHasHeaders     : 1;
651
  uint32_t                          mInheritApplicationCache    : 1;
652
  uint32_t                          mChooseApplicationCache     : 1;
653
  uint32_t                          mLoadedFromApplicationCache : 1;
654
  uint32_t                          mChannelIsForDownload       : 1;
655
  uint32_t                          mTracingEnabled             : 1;
656
  // True if timing collection is enabled
657
  uint32_t                          mTimingEnabled              : 1;
658
  uint32_t                          mReportTiming               : 1;
659
  uint32_t                          mAllowSpdy                  : 1;
660
  uint32_t                          mAllowAltSvc                : 1;
661
  uint32_t                          mBeConservative             : 1;
662
  uint32_t                          mTRR                        : 1;
663
  uint32_t                          mResponseTimeoutEnabled     : 1;
664
  // A flag that should be false only if a cross-domain redirect occurred
665
  uint32_t                          mAllRedirectsSameOrigin     : 1;
666
667
  // Is 1 if no redirects have occured or if all redirects
668
  // pass the Resource Timing timing-allow-check
669
  uint32_t                          mAllRedirectsPassTimingAllowCheck : 1;
670
671
  // True if this channel was intercepted and could receive a synthesized response.
672
  uint32_t                          mResponseCouldBeSynthesized : 1;
673
674
  uint32_t                          mBlockAuthPrompt : 1;
675
676
  // If true, we behave as if the LOAD_FROM_CACHE flag has been set.
677
  // Used to enforce that flag's behavior but not expose it externally.
678
  uint32_t                          mAllowStaleCacheContent : 1;
679
680
  // True iff this request has been calculated in its request context as
681
  // a non tail request.  We must remove it again when this channel is done.
682
  uint32_t                          mAddedAsNonTailRequest : 1;
683
684
  // True if AsyncOpen() is called when the stream length is still unknown.
685
  // AsyncOpen() will be retriggered when InputStreamLengthHelper execs the
686
  // callback, passing the stream length value.
687
  uint32_t                          mAsyncOpenWaitingForStreamLength : 1;
688
689
  // Defaults to true.  This is set to false when it is no longer possible
690
  // to upgrade the request to a secure channel.
691
  uint32_t                          mUpgradableToSecure : 1;
692
693
694
  // An opaque flags for non-standard behavior of the TLS system.
695
  // It is unlikely this will need to be set outside of telemetry studies
696
  // relating to the TLS implementation.
697
  uint32_t                          mTlsFlags;
698
699
  // Current suspension depth for this channel object
700
  uint32_t                          mSuspendCount;
701
702
  // Per channel transport window override (0 means no override)
703
  uint32_t                          mInitialRwin;
704
705
  uint32_t                          mProxyResolveFlags;
706
707
  uint32_t                          mContentDispositionHint;
708
  uint32_t                          mReferrerPolicy;
709
710
  uint32_t mCorsMode;
711
  uint32_t mRedirectMode;
712
713
  // If this channel was created as the result of a redirect, then this value
714
  // will reflect the redirect flags passed to the SetupReplacementChannel()
715
  // method.
716
  uint32_t mLastRedirectFlags;
717
718
  int16_t                           mPriority;
719
  uint8_t                           mRedirectionLimit;
720
721
  // Performance tracking
722
  // Number of redirects that has occurred.
723
  int8_t                            mRedirectCount;
724
  // Number of internal redirects that has occurred.
725
  int8_t                            mInternalRedirectCount;
726
727
  bool                              mAsyncOpenTimeOverriden;
728
  bool                              mForcePending;
729
730
  bool mCorsIncludeCredentials;
731
732
  // These parameters are used to ensure that we do not call OnStartRequest and
733
  // OnStopRequest more than once.
734
  bool mOnStartRequestCalled;
735
  bool mOnStopRequestCalled;
736
737
  // Defaults to false. Is set to true at the begining of OnStartRequest.
738
  // Used to ensure methods can't be called before OnStartRequest.
739
  bool mAfterOnStartRequestBegun;
740
741
  bool mRequireCORSPreflight;
742
743
  // This flag will be true if the consumer is requesting alt-data AND the
744
  // consumer is in the child process.
745
  bool mAltDataForChild;
746
747
  bool mForceMainDocumentChannel;
748
  // This is set true if the channel is waiting for the
749
  // InputStreamLengthHelper::GetAsyncLength callback.
750
  bool mPendingInputStreamLengthOperation;
751
752
  bool EnsureRequestContextID();
753
  bool EnsureRequestContext();
754
755
  // Adds/removes this channel as a non-tailed request in its request context
756
  // these helpers ensure we add it only once and remove it only when added
757
  // via mAddedAsNonTailRequest member tracking.
758
  void AddAsNonTailRequest();
759
  void RemoveAsNonTailRequest();
760
761
  void EnsureTopLevelOuterContentWindowId();
762
};
763
764
NS_DEFINE_STATIC_IID_ACCESSOR(HttpBaseChannel, HTTP_BASE_CHANNEL_IID)
765
766
// Share some code while working around C++'s absurd inability to handle casting
767
// of member functions between base/derived types.
768
// - We want to store member function pointer to call at resume time, but one
769
//   such function--HandleAsyncAbort--we want to share between the
770
//   nsHttpChannel/HttpChannelChild.  Can't define it in base class, because
771
//   then we'd have to cast member function ptr between base/derived class
772
//   types.  Sigh...
773
template <class T>
774
class HttpAsyncAborter
775
{
776
public:
777
0
  explicit HttpAsyncAborter(T *derived) : mThis(derived), mCallOnResume(0) {}
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::HttpChannelChild>::HttpAsyncAborter(mozilla::net::HttpChannelChild*)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::InterceptedHttpChannel>::HttpAsyncAborter(mozilla::net::InterceptedHttpChannel*)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::nsHttpChannel>::HttpAsyncAborter(mozilla::net::nsHttpChannel*)
778
779
  // Aborts channel: calls OnStart/Stop with provided status, removes channel
780
  // from loadGroup.
781
  MOZ_MUST_USE nsresult AsyncAbort(nsresult status);
782
783
  // Does most the actual work.
784
  void HandleAsyncAbort();
785
786
  // AsyncCall calls a member function asynchronously (via an event).
787
  // retval isn't refcounted and is set only when event was successfully
788
  // posted, the event is returned for the purpose of cancelling when needed
789
  MOZ_MUST_USE virtual nsresult AsyncCall(void (T::*funcPtr)(),
790
                                          nsRunnableMethod<T> **retval = nullptr);
791
private:
792
  T *mThis;
793
794
protected:
795
  // Function to be called at resume time
796
  void (T::* mCallOnResume)(void);
797
};
798
799
template <class T>
800
MOZ_MUST_USE nsresult HttpAsyncAborter<T>::AsyncAbort(nsresult status)
801
0
{
802
0
  MOZ_LOG(gHttpLog, LogLevel::Debug,
803
0
         ("HttpAsyncAborter::AsyncAbort [this=%p status=%" PRIx32 "]\n",
804
0
          mThis, static_cast<uint32_t>(status)));
805
0
806
0
  mThis->mStatus = status;
807
0
808
0
  // if this fails?  Callers ignore our return value anyway....
809
0
  return AsyncCall(&T::HandleAsyncAbort);
810
0
}
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::HttpChannelChild>::AsyncAbort(nsresult)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::InterceptedHttpChannel>::AsyncAbort(nsresult)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::nsHttpChannel>::AsyncAbort(nsresult)
811
812
// Each subclass needs to define its own version of this (which just calls this
813
// base version), else we wind up casting base/derived member function ptrs
814
template <class T>
815
inline void HttpAsyncAborter<T>::HandleAsyncAbort()
816
0
{
817
0
  MOZ_ASSERT(!mCallOnResume, "How did that happen?");
818
0
819
0
  if (mThis->mSuspendCount) {
820
0
    MOZ_LOG(gHttpLog, LogLevel::Debug,
821
0
           ("Waiting until resume to do async notification [this=%p]\n", mThis));
822
0
    mCallOnResume = &T::HandleAsyncAbort;
823
0
    return;
824
0
  }
825
0
826
0
  mThis->DoNotifyListener();
827
0
828
0
  // finally remove ourselves from the load group.
829
0
  if (mThis->mLoadGroup)
830
0
    mThis->mLoadGroup->RemoveRequest(mThis, nullptr, mThis->mStatus);
831
0
}
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::HttpChannelChild>::HandleAsyncAbort()
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::InterceptedHttpChannel>::HandleAsyncAbort()
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::nsHttpChannel>::HandleAsyncAbort()
832
833
template <class T>
834
nsresult HttpAsyncAborter<T>::AsyncCall(void (T::*funcPtr)(),
835
                                        nsRunnableMethod<T> **retval)
836
0
{
837
0
  nsresult rv;
838
0
839
0
  RefPtr<nsRunnableMethod<T>> event =
840
0
    NewRunnableMethod("net::HttpAsyncAborter::AsyncCall", mThis, funcPtr);
841
0
  rv = NS_DispatchToCurrentThread(event);
842
0
  if (NS_SUCCEEDED(rv) && retval) {
843
0
    *retval = event;
844
0
  }
845
0
846
0
  return rv;
847
0
}
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::HttpChannelChild>::AsyncCall(void (mozilla::net::HttpChannelChild::*)(), nsRunnableMethod<mozilla::net::HttpChannelChild, void, true, (mozilla::RunnableKind)0>**)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::nsHttpChannel>::AsyncCall(void (mozilla::net::nsHttpChannel::*)(), nsRunnableMethod<mozilla::net::nsHttpChannel, void, true, (mozilla::RunnableKind)0>**)
Unexecuted instantiation: mozilla::net::HttpAsyncAborter<mozilla::net::InterceptedHttpChannel>::AsyncCall(void (mozilla::net::InterceptedHttpChannel::*)(), nsRunnableMethod<mozilla::net::InterceptedHttpChannel, void, true, (mozilla::RunnableKind)0>**)
848
849
class ProxyReleaseRunnable final : public mozilla::Runnable
850
{
851
public:
852
  explicit ProxyReleaseRunnable(nsTArray<nsCOMPtr<nsISupports>>&& aDoomed)
853
    : Runnable("ProxyReleaseRunnable")
854
    , mDoomed(std::move(aDoomed))
855
0
  {}
856
857
  NS_IMETHOD
858
  Run() override
859
0
  {
860
0
    mDoomed.Clear();
861
0
    return NS_OK;
862
0
  }
863
864
private:
865
0
  virtual ~ProxyReleaseRunnable() = default;
866
867
  nsTArray<nsCOMPtr<nsISupports>> mDoomed;
868
};
869
870
} // namespace net
871
} // namespace mozilla
872
873
#endif // mozilla_net_HttpBaseChannel_h