DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set et cin ts=4 sw=2 sts=2: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsHttpChannel_h__
#define nsHttpChannel_h__

#include "DelayHttpChannelQueue.h"
#include "HttpBaseChannel.h"
#include "nsTArray.h"
#include "nsICachingChannel.h"
#include "nsICacheEntry.h"
#include "nsICacheEntryOpenCallback.h"
#include "nsIDNSListener.h"
#include "nsIApplicationCacheChannel.h"
#include "nsIChannelWithDivertableParentListener.h"
#include "nsIProtocolProxyCallback.h"
#include "nsIStreamTransportService.h"
#include "nsIHttpAuthenticableChannel.h"
#include "nsIAsyncVerifyRedirectCallback.h"
#include "nsIThreadRetargetableRequest.h"
#include "nsIThreadRetargetableStreamListener.h"
#include "nsWeakReference.h"
#include "TimingStruct.h"
#include "ADivertableParentChannel.h"
#include "AutoClose.h"
#include "nsIStreamListener.h"
#include "nsISupportsPrimitives.h"
#include "nsICorsPreflightCallback.h"
#include "AlternateServices.h"
#include "nsIRaceCacheWithNetwork.h"
#include "mozilla/extensions/PStreamFilterParent.h"
#include "mozilla/Mutex.h"
#include "nsIRemoteTab.h"

class nsDNSPrefetch;
class nsICancelable;
class nsIHttpChannelAuthProvider;
class nsInputStreamPump;
class nsITransportSecurityInfo;

namespace mozilla {
namespace net {

class nsChannelClassifier;
class Http2PushedStream;

class HttpChannelSecurityWarningReporter : public nsISupports {
 public:
  virtual MOZ_MUST_USE nsresult ReportSecurityMessage(
      const nsAString& aMessageTag, const nsAString& aMessageCategory) = 0;
  virtual MOZ_MUST_USE nsresult LogBlockedCORSRequest(
      const nsAString& aMessage, const nsACString& aCategory) = 0;
  virtual MOZ_MUST_USE nsresult
  LogMimeTypeMismatch(const nsACString& aMessageName, bool aWarning,
                      const nsAString& aURL, const nsAString& aContentType) = 0;
};

//-----------------------------------------------------------------------------
// nsHttpChannel
//-----------------------------------------------------------------------------

// Use to support QI nsIChannel to nsHttpChannel
#define NS_HTTPCHANNEL_IID                           \
  {                                                  \
    0x301bf95b, 0x7bb3, 0x4ae1, {                    \
      0xa9, 0x71, 0x40, 0xbc, 0xfa, 0x81, 0xde, 0x12 \
    }                                                \
  }

class nsHttpChannel final : public HttpBaseChannel,
                            public HttpAsyncAborter<nsHttpChannel>,
                            public nsIStreamListener,
                            public nsICachingChannel,
                            public nsICacheEntryOpenCallback,
                            public nsITransportEventSink,
                            public nsIProtocolProxyCallback,
                            public nsIHttpAuthenticableChannel,
                            public nsIApplicationCacheChannel,
                            public nsIAsyncVerifyRedirectCallback,
                            public nsIThreadRetargetableRequest,
                            public nsIThreadRetargetableStreamListener,
                            public nsIDNSListener,
                            public nsSupportsWeakReference,
                            public nsICorsPreflightCallback,
                            public nsIChannelWithDivertableParentListener,
                            public nsIRaceCacheWithNetwork,
                            public nsIRequestTailUnblockCallback,
                            public nsITimerCallback {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSIREQUESTOBSERVER
  NS_DECL_NSISTREAMLISTENER
  NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
  NS_DECL_NSICACHEINFOCHANNEL
  NS_DECL_NSICACHINGCHANNEL
  NS_DECL_NSICACHEENTRYOPENCALLBACK
  NS_DECL_NSITRANSPORTEVENTSINK
  NS_DECL_NSIPROTOCOLPROXYCALLBACK
  NS_DECL_NSIPROXIEDCHANNEL
  NS_DECL_NSIAPPLICATIONCACHECONTAINER
  NS_DECL_NSIAPPLICATIONCACHECHANNEL
  NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
  NS_DECL_NSITHREADRETARGETABLEREQUEST
  NS_DECL_NSIDNSLISTENER
  NS_DECL_NSICHANNELWITHDIVERTABLEPARENTLISTENER
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_HTTPCHANNEL_IID)
  NS_DECL_NSIRACECACHEWITHNETWORK
  NS_DECL_NSITIMERCALLBACK
  NS_DECL_NSIREQUESTTAILUNBLOCKCALLBACK

  // nsIHttpAuthenticableChannel. We can't use
  // NS_DECL_NSIHTTPAUTHENTICABLECHANNEL because it duplicates cancel() and
  // others.
  NS_IMETHOD GetIsSSL(bool* aIsSSL) override;
  NS_IMETHOD GetProxyMethodIsConnect(bool* aProxyMethodIsConnect) override;
  NS_IMETHOD GetServerResponseHeader(
      nsACString& aServerResponseHeader) override;
  NS_IMETHOD GetProxyChallenges(nsACString& aChallenges) override;
  NS_IMETHOD GetWWWChallenges(nsACString& aChallenges) override;
  NS_IMETHOD SetProxyCredentials(const nsACString& aCredentials) override;
  NS_IMETHOD SetWWWCredentials(const nsACString& aCredentials) override;
  NS_IMETHOD OnAuthAvailable() override;
  NS_IMETHOD OnAuthCancelled(bool userCancel) override;
  NS_IMETHOD CloseStickyConnection() override;
  NS_IMETHOD ConnectionRestartable(bool) override;
  // Functions we implement from nsIHttpAuthenticableChannel but are
  // declared in HttpBaseChannel must be implemented in this class. We
  // just call the HttpBaseChannel:: impls.
  NS_IMETHOD GetLoadFlags(nsLoadFlags* aLoadFlags) override;
  NS_IMETHOD GetURI(nsIURI** aURI) override;
  NS_IMETHOD GetNotificationCallbacks(
      nsIInterfaceRequestor** aCallbacks) override;
  NS_IMETHOD GetLoadGroup(nsILoadGroup** aLoadGroup) override;
  NS_IMETHOD GetRequestMethod(nsACString& aMethod) override;

  nsHttpChannel();

  virtual MOZ_MUST_USE nsresult
  Init(nsIURI* aURI, uint32_t aCaps, nsProxyInfo* aProxyInfo,
       uint32_t aProxyResolveFlags, nsIURI* aProxyURI, uint64_t aChannelId,
       nsContentPolicyType aContentPolicyType) override;

  MOZ_MUST_USE nsresult OnPush(const nsACString& uri,
                               Http2PushedStreamWrapper* pushedStream);

  static bool IsRedirectStatus(uint32_t status);
  static bool WillRedirect(nsHttpResponseHead* response);

  // Methods HttpBaseChannel didn't implement for us or that we override.
  //
  // nsIRequest
  NS_IMETHOD Cancel(nsresult status) override;
  NS_IMETHOD Suspend() override;
  NS_IMETHOD Resume() override;
  // nsIChannel
  NS_IMETHOD GetSecurityInfo(nsISupports** aSecurityInfo) override;
  NS_IMETHOD AsyncOpen(nsIStreamListener* aListener) override;
  // nsIHttpChannel
  NS_IMETHOD GetEncodedBodySize(uint64_t* aEncodedBodySize) override;
  NS_IMETHOD SwitchProcessTo(mozilla::dom::Promise* aBrowserParent,
                             uint64_t aIdentifier) override;
  NS_IMETHOD HasCrossOriginOpenerPolicyMismatch(bool* aMismatch) override;
  // nsIHttpChannelInternal
  NS_IMETHOD SetupFallbackChannel(const char* aFallbackKey) override;
  NS_IMETHOD SetChannelIsForDownload(bool aChannelIsForDownload) override;
  NS_IMETHOD GetNavigationStartTimeStamp(TimeStamp* aTimeStamp) override;
  NS_IMETHOD SetNavigationStartTimeStamp(TimeStamp aTimeStamp) override;
  NS_IMETHOD CancelByURLClassifier(nsresult aErrorCode) override;
  // nsISupportsPriority
  NS_IMETHOD SetPriority(int32_t value) override;
  // nsIClassOfService
  NS_IMETHOD SetClassFlags(uint32_t inFlags) override;
  NS_IMETHOD AddClassFlags(uint32_t inFlags) override;
  NS_IMETHOD ClearClassFlags(uint32_t inFlags) override;

  // nsIResumableChannel
  NS_IMETHOD ResumeAt(uint64_t startPos, const nsACString& entityID) override;

  NS_IMETHOD SetNotificationCallbacks(
      nsIInterfaceRequestor* aCallbacks) override;
  NS_IMETHOD SetLoadGroup(nsILoadGroup* aLoadGroup) override;
  // nsITimedChannel
  NS_IMETHOD GetDomainLookupStart(
      mozilla::TimeStamp* aDomainLookupStart) override;
  NS_IMETHOD GetDomainLookupEnd(mozilla::TimeStamp* aDomainLookupEnd) override;
  NS_IMETHOD GetConnectStart(mozilla::TimeStamp* aConnectStart) override;
  NS_IMETHOD GetTcpConnectEnd(mozilla::TimeStamp* aTcpConnectEnd) override;
  NS_IMETHOD GetSecureConnectionStart(
      mozilla::TimeStamp* aSecureConnectionStart) override;
  NS_IMETHOD GetConnectEnd(mozilla::TimeStamp* aConnectEnd) override;
  NS_IMETHOD GetRequestStart(mozilla::TimeStamp* aRequestStart) override;
  NS_IMETHOD GetResponseStart(mozilla::TimeStamp* aResponseStart) override;
  NS_IMETHOD GetResponseEnd(mozilla::TimeStamp* aResponseEnd) override;
  // nsICorsPreflightCallback
  NS_IMETHOD OnPreflightSucceeded() override;
  NS_IMETHOD OnPreflightFailed(nsresult aError) override;

  MOZ_MUST_USE nsresult AddSecurityMessage(
      const nsAString& aMessageTag, const nsAString& aMessageCategory) override;
  NS_IMETHOD LogBlockedCORSRequest(const nsAString& aMessage,
                                   const nsACString& aCategory) override;
  NS_IMETHOD LogMimeTypeMismatch(const nsACString& aMessageName, bool aWarning,
                                 const nsAString& aURL,
                                 const nsAString& aContentType) override;

  void SetWarningReporter(HttpChannelSecurityWarningReporter* aReporter);
  HttpChannelSecurityWarningReporter* GetWarningReporter();

 public: /* internal necko use only */
  uint32_t GetRequestTime() const { return mRequestTime; }

  nsresult AsyncOpenFinal(TimeStamp aTimeStamp);

  MOZ_MUST_USE nsresult OpenCacheEntry(bool usingSSL);
  MOZ_MUST_USE nsresult OpenCacheEntryInternal(
      bool isHttps, nsIApplicationCache* applicationCache, bool noAppCache);
  MOZ_MUST_USE nsresult ContinueConnect();

  MOZ_MUST_USE nsresult StartRedirectChannelToURI(nsIURI*, uint32_t);

  // This allows cache entry to be marked as foreign even after channel itself
  // is gone.  Needed for e10s (see
  // HttpChannelParent::RecvDocumentChannelCleanup)
  class OfflineCacheEntryAsForeignMarker {
    nsCOMPtr<nsIApplicationCache> mApplicationCache;
    nsCOMPtr<nsIURI> mCacheURI;

   public:
    OfflineCacheEntryAsForeignMarker(nsIApplicationCache* appCache,
                                     nsIURI* aURI)
        : mApplicationCache(appCache), mCacheURI(aURI) {}

    nsresult MarkAsForeign();
  };

  OfflineCacheEntryAsForeignMarker* GetOfflineCacheEntryAsForeignMarker();

  // Helper to keep cache callbacks wait flags consistent
  class AutoCacheWaitFlags {
   public:
    explicit AutoCacheWaitFlags(nsHttpChannel* channel)
        : mChannel(channel), mKeep(0) {
      // Flags must be set before entering any AsyncOpenCacheEntry call.
      mChannel->mCacheEntriesToWaitFor =
          nsHttpChannel::WAIT_FOR_CACHE_ENTRY |
          nsHttpChannel::WAIT_FOR_OFFLINE_CACHE_ENTRY;
    }

    void Keep(uint32_t flags) {
      // Called after successful call to appropriate AsyncOpenCacheEntry call.
      mKeep |= flags;
    }

    ~AutoCacheWaitFlags() {
      // Keep only flags those are left to be wait for.
      mChannel->mCacheEntriesToWaitFor &= mKeep;
    }

   private:
    nsHttpChannel* mChannel;
    uint32_t mKeep : 2;
  };

  bool AwaitingCacheCallbacks();
  void SetCouldBeSynthesized();

  // Return true if the latest ODA is invoked by mCachePump.
  // Should only be called on the same thread as ODA.
  bool IsReadingFromCache() const { return mIsReadingFromCache; }

  base::ProcessId ProcessId();

  MOZ_MUST_USE bool AttachStreamFilter(
      ipc::Endpoint<extensions::PStreamFilterParent>&& aEndpoint);

 private:  // used for alternate service validation
  RefPtr<TransactionObserver> mTransactionObserver;

 public:
  void SetConnectionInfo(nsHttpConnectionInfo*);  // clones the argument
  void SetTransactionObserver(TransactionObserver* arg) {
    mTransactionObserver = arg;
  }
  TransactionObserver* GetTransactionObserver() { return mTransactionObserver; }

  typedef MozPromise<uint64_t, nsresult, false> ContentProcessIdPromise;
  already_AddRefed<ContentProcessIdPromise>
  TakeRedirectContentProcessIdPromise() {
    return mRedirectContentProcessIdPromise.forget();
  }
  uint64_t CrossProcessRedirectIdentifier() {
    return mCrossProcessRedirectIdentifier;
  }

  CacheDisposition mCacheDisposition;

 protected:
  virtual ~nsHttpChannel();

 private:
  typedef nsresult (nsHttpChannel::*nsContinueRedirectionFunc)(nsresult result);

  // Directly call |aFunc| if the channel is not canceled and not suspended.
  // Otherwise, set |aFunc| to |mCallOnResume| and wait until the channel
  // resumes.
  nsresult CallOrWaitForResume(
      const std::function<nsresult(nsHttpChannel*)>& aFunc);

  bool RequestIsConditional();
  void HandleContinueCancellingByURLClassifier(nsresult aErrorCode);
  nsresult CancelInternal(nsresult status);
  void ContinueCancellingByURLClassifier(nsresult aErrorCode);

  // Connections will only be established in this function.
  // (including DNS prefetch and speculative connection.)
  nsresult BeginConnectActual();
  void MaybeStartDNSPrefetch();

  // We might synchronously or asynchronously call BeginConnectActual,
  // which includes DNS prefetch and speculative connection, according to
  // whether an async tracker lookup is required. If the tracker lookup
  // is required, this funciton will just return NS_OK and BeginConnectActual()
  // will be called when callback. See Bug 1325054 for more information.
  nsresult BeginConnect();
  MOZ_MUST_USE nsresult ContinueBeginConnectWithResult();
  void ContinueBeginConnect();
  MOZ_MUST_USE nsresult PrepareToConnect();
  void HandleOnBeforeConnect();
  MOZ_MUST_USE nsresult OnBeforeConnect();
  MOZ_MUST_USE nsresult ContinueOnBeforeConnect(bool aShouldUpgrade,
                                                nsresult aStatus);
  void OnBeforeConnectContinue();
  MOZ_MUST_USE nsresult Connect();
  void SpeculativeConnect();
  MOZ_MUST_USE nsresult SetupTransaction();
  MOZ_MUST_USE nsresult CallOnStartRequest();
  MOZ_MUST_USE nsresult ProcessResponse();
  void AsyncContinueProcessResponse();
  MOZ_MUST_USE nsresult ContinueProcessResponse1();
  MOZ_MUST_USE nsresult ContinueProcessResponse2(nsresult);
  void UpdateCacheDisposition(bool aSuccessfulReval, bool aPartialContentUsed);
  MOZ_MUST_USE nsresult ContinueProcessResponse3(nsresult);
  MOZ_MUST_USE nsresult ContinueProcessResponse4(nsresult);
  MOZ_MUST_USE nsresult ProcessNormal();
  MOZ_MUST_USE nsresult ContinueProcessNormal(nsresult);
  void ProcessAltService();
  bool ShouldBypassProcessNotModified();
  MOZ_MUST_USE nsresult ProcessNotModified(
      const std::function<nsresult(nsHttpChannel*, nsresult)>&
          aContinueProcessResponseFunc);
  MOZ_MUST_USE nsresult ContinueProcessResponseAfterNotModified(nsresult aRv);

  MOZ_MUST_USE nsresult AsyncProcessRedirection(uint32_t httpStatus);
  MOZ_MUST_USE nsresult ContinueProcessRedirection(nsresult);
  MOZ_MUST_USE nsresult ContinueProcessRedirectionAfterFallback(nsresult);
  MOZ_MUST_USE nsresult ProcessFailedProxyConnect(uint32_t httpStatus);
  MOZ_MUST_USE nsresult ProcessFallback(bool* waitingForRedirectCallback);
  MOZ_MUST_USE nsresult ContinueProcessFallback(nsresult);
  void HandleAsyncAbort();
  MOZ_MUST_USE nsresult EnsureAssocReq();
  void ProcessSSLInformation();
  bool IsHTTPS();

  MOZ_MUST_USE nsresult ContinueOnStartRequest1(nsresult);
  MOZ_MUST_USE nsresult ContinueOnStartRequest2(nsresult);
  MOZ_MUST_USE nsresult ContinueOnStartRequest3(nsresult);
  MOZ_MUST_USE nsresult ContinueOnStartRequest4(nsresult);

  void OnClassOfServiceUpdated();

  // redirection specific methods
  void HandleAsyncRedirect();
  void HandleAsyncAPIRedirect();
  MOZ_MUST_USE nsresult ContinueHandleAsyncRedirect(nsresult);
  void HandleAsyncNotModified();
  void HandleAsyncFallback();
  MOZ_MUST_USE nsresult ContinueHandleAsyncFallback(nsresult);
  MOZ_MUST_USE nsresult PromptTempRedirect();
  virtual MOZ_MUST_USE nsresult
  SetupReplacementChannel(nsIURI*, nsIChannel*, bool preserveMethod,
                          uint32_t redirectFlags) override;
  nsresult StartCrossProcessRedirect();

  // proxy specific methods
  MOZ_MUST_USE nsresult ProxyFailover();
  MOZ_MUST_USE nsresult AsyncDoReplaceWithProxy(nsIProxyInfo*);
  MOZ_MUST_USE nsresult ContinueDoReplaceWithProxy(nsresult);
  MOZ_MUST_USE nsresult ResolveProxy();

  // cache specific methods
  MOZ_MUST_USE nsresult OnOfflineCacheEntryAvailable(
      nsICacheEntry* aEntry, bool aNew, nsIApplicationCache* aAppCache,
      nsresult aResult);
  MOZ_MUST_USE nsresult OnNormalCacheEntryAvailable(nsICacheEntry* aEntry,
                                                    bool aNew,
                                                    nsresult aResult);
  MOZ_MUST_USE nsresult OpenOfflineCacheEntryForWriting();
  MOZ_MUST_USE nsresult OnOfflineCacheEntryForWritingAvailable(
      nsICacheEntry* aEntry, nsIApplicationCache* aAppCache, nsresult aResult);
  MOZ_MUST_USE nsresult OnCacheEntryAvailableInternal(
      nsICacheEntry* entry, bool aNew, nsIApplicationCache* aAppCache,
      nsresult status);
  MOZ_MUST_USE nsresult GenerateCacheKey(uint32_t postID, nsACString& key);
  MOZ_MUST_USE nsresult UpdateExpirationTime();
  MOZ_MUST_USE nsresult CheckPartial(nsICacheEntry* aEntry, int64_t* aSize,
                                     int64_t* aContentLength);
  bool ShouldUpdateOfflineCacheEntry();
  MOZ_MUST_USE nsresult ReadFromCache(bool alreadyMarkedValid);
  void CloseCacheEntry(bool doomOnFailure);
  void CloseOfflineCacheEntry();
  MOZ_MUST_USE nsresult InitCacheEntry();
  void UpdateInhibitPersistentCachingFlag();
  MOZ_MUST_USE nsresult InitOfflineCacheEntry();
  MOZ_MUST_USE nsresult AddCacheEntryHeaders(nsICacheEntry* entry);
  MOZ_MUST_USE nsresult FinalizeCacheEntry();
  MOZ_MUST_USE nsresult InstallCacheListener(int64_t offset = 0);
  MOZ_MUST_USE nsresult InstallOfflineCacheListener(int64_t offset = 0);
  void MaybeInvalidateCacheEntryForSubsequentGet();
  void AsyncOnExamineCachedResponse();

  // Handle the bogus Content-Encoding Apache sometimes sends
  void ClearBogusContentEncodingIfNeeded();

  // byte range request specific methods
  MOZ_MUST_USE nsresult ProcessPartialContent(
      const std::function<nsresult(nsHttpChannel*, nsresult)>&
          aContinueProcessResponseFunc);
  MOZ_MUST_USE nsresult
  ContinueProcessResponseAfterPartialContent(nsresult aRv);
  MOZ_MUST_USE nsresult OnDoneReadingPartialCacheEntry(bool* streamDone);

  MOZ_MUST_USE nsresult
  DoAuthRetry(nsHttpTransaction* aTransWithStickyConn,
              const std::function<nsresult(nsHttpChannel*, nsresult)>&
                  aContinueOnStopRequestFunc);
  MOZ_MUST_USE nsresult
  ContinueDoAuthRetry(nsHttpTransaction* aTransWithStickyConn,
                      const std::function<nsresult(nsHttpChannel*, nsresult)>&
                          aContinueOnStopRequestFunc);
  MOZ_MUST_USE nsresult
  DoConnect(nsHttpTransaction* aTransWithStickyConn = nullptr);
  MOZ_MUST_USE nsresult ContinueOnStopRequestAfterAuthRetry(
      nsresult aStatus, bool aAuthRetry, bool aIsFromNet, bool aContentComplete,
      nsHttpTransaction* aTransWithStickyConn);
  MOZ_MUST_USE nsresult ContinueOnStopRequest(nsresult status, bool aIsFromNet,
                                              bool aContentComplete);

  void HandleAsyncRedirectChannelToHttps();
  MOZ_MUST_USE nsresult StartRedirectChannelToHttps();
  MOZ_MUST_USE nsresult ContinueAsyncRedirectChannelToURI(nsresult rv);
  MOZ_MUST_USE nsresult OpenRedirectChannel(nsresult rv);

  HttpTrafficCategory CreateTrafficCategory();

  /**
   * A function that takes care of reading STS and PKP headers and enforcing
   * STS and PKP load rules. After a secure channel is erected, STS and PKP
   * requires the channel to be trusted or any STS or PKP header data on
   * the channel is ignored. This is called from ProcessResponse.
   */
  MOZ_MUST_USE nsresult ProcessSecurityHeaders();

  /**
   * Taking care of the Content-Signature header and fail the channel if
   * the signature verification fails or is required but the header is not
   * present.
   * This sets mListener to ContentVerifier, which buffers the entire response
   * before verifying the Content-Signature header. If the verification is
   * successful, the load proceeds as usual. If the verification fails, a
   * NS_ERROR_INVALID_SIGNATURE is thrown and a fallback loaded in nsDocShell
   */
  MOZ_MUST_USE nsresult
  ProcessContentSignatureHeader(nsHttpResponseHead* aResponseHead);

  /**
   * A function that will, if the feature is enabled, send security reports.
   */
  void ProcessSecurityReport(nsresult status);

  nsresult GetResponseCrossOriginPolicy(
      nsILoadInfo::CrossOriginPolicy* aResponseCrossOriginPolicy);
  nsresult ProcessCrossOriginHeader();
  nsresult ProcessCrossOriginResourcePolicyHeader();

  nsresult ComputeCrossOriginOpenerPolicyMismatch();

  /**
   * A function to process a single security header (STS or PKP), assumes
   * some basic sanity checks have been applied to the channel. Called
   * from ProcessSecurityHeaders.
   */
  MOZ_MUST_USE nsresult ProcessSingleSecurityHeader(
      uint32_t aType, nsITransportSecurityInfo* aSecInfo, uint32_t aFlags);

  void InvalidateCacheEntryForLocation(const char* location);
  void AssembleCacheKey(const char* spec, uint32_t postID, nsACString& key);
  MOZ_MUST_USE nsresult CreateNewURI(const char* loc, nsIURI** newURI);
  void DoInvalidateCacheEntry(nsIURI* aURI);

  // Ref RFC2616 13.10: "invalidation... MUST only be performed if
  // the host part is the same as in the Request-URI"
  inline bool HostPartIsTheSame(nsIURI* uri) {
    nsAutoCString tmpHost1, tmpHost2;
    return (NS_SUCCEEDED(mURI->GetAsciiHost(tmpHost1)) &&
            NS_SUCCEEDED(uri->GetAsciiHost(tmpHost2)) &&
            (tmpHost1 == tmpHost2));
  }

  inline static bool DoNotRender3xxBody(nsresult rv) {
    return rv == NS_ERROR_REDIRECT_LOOP || rv == NS_ERROR_CORRUPTED_CONTENT ||
           rv == NS_ERROR_UNKNOWN_PROTOCOL || rv == NS_ERROR_MALFORMED_URI ||
           rv == NS_ERROR_PORT_ACCESS_NOT_ALLOWED;
  }

  // Report net vs cache time telemetry
  void ReportNetVSCacheTelemetry();
  int64_t ComputeTelemetryBucketNumber(int64_t difftime_ms);

  // Report telemetry and stats to about:networking
  void ReportRcwnStats(bool isFromNet);

  // Create a aggregate set of the current notification callbacks
  // and ensure the transaction is updated to use it.
  void UpdateAggregateCallbacks();

  static bool HasQueryString(nsHttpRequestHead::ParsedMethodType method,
                             nsIURI* uri);
  bool ResponseWouldVary(nsICacheEntry* entry);
  bool IsResumable(int64_t partialLen, int64_t contentLength,
                   bool ignoreMissingPartialLen = false) const;
  MOZ_MUST_USE nsresult
  MaybeSetupByteRangeRequest(int64_t partialLen, int64_t contentLength,
                             bool ignoreMissingPartialLen = false);
  MOZ_MUST_USE nsresult SetupByteRangeRequest(int64_t partialLen);
  void UntieByteRangeRequest();
  void UntieValidationRequest();
  MOZ_MUST_USE nsresult OpenCacheInputStream(nsICacheEntry* cacheEntry,
                                             bool startBuffering,
                                             bool checkingAppCacheEntry);

  void SetPushedStream(Http2PushedStreamWrapper* stream);

  void MaybeWarnAboutAppCache();

  void SetLoadGroupUserAgentOverride();

  void SetOriginHeader();
  void SetDoNotTrack();

  bool IsIsolated();

  const nsCString& GetTopWindowOrigin();

  already_AddRefed<nsChannelClassifier> GetOrCreateChannelClassifier();

  // Start an internal redirect to a new InterceptedHttpChannel which will
  // resolve in firing a ServiceWorker FetchEvent.
  MOZ_MUST_USE nsresult RedirectToInterceptedChannel();

  // Determines and sets content type in the cache entry. It's called when
  // writing a new entry. The content type is used in cache internally only.
  void SetCachedContentType();

  // Stores information about access from eTLD+1 of the top level document to
  // the cache entry.
  void StoreSiteAccessToCacheEntry();

 private:
  // this section is for main-thread-only object
  // all the references need to be proxy released on main thread.
  nsCOMPtr<nsIApplicationCache> mApplicationCacheForWrite;
  // auth specific data
  nsCOMPtr<nsIHttpChannelAuthProvider> mAuthProvider;
  nsCOMPtr<nsIURI> mRedirectURI;
  nsCOMPtr<nsIChannel> mRedirectChannel;
  nsCOMPtr<nsIChannel> mPreflightChannel;

  // The associated childChannel is getting relocated to another process.
  // This promise will be resolved when that process is set up.
  RefPtr<ContentProcessIdPromise> mRedirectContentProcessIdPromise;
  // This identifier is passed to the childChannel in order to identify it.
  uint64_t mCrossProcessRedirectIdentifier = 0;

  // nsChannelClassifier checks this channel's URI against
  // the URI classifier service.
  // nsChannelClassifier will be invoked twice in InitLocalBlockList() and
  // BeginConnectActual(), so save the nsChannelClassifier here to keep the
  // state of whether tracking protection is enabled or not.
  RefPtr<nsChannelClassifier> mChannelClassifier;

  // Proxy release all members above on main thread.
  void ReleaseMainThreadOnlyReferences();

  // Called after the channel is made aware of its tracking status in order
  // to readjust the referrer if needed according to the referrer default
  // policy preferences.
  void ReEvaluateReferrerAfterTrackingStatusIsKnown();

  // Create a dummy channel for the same principal, out of the load group
  // just to revalidate the cache entry.  We don't care if this fails.
  // This method can be called on any thread, and creates an idle task
  // to perform the revalidation with delay.
  void PerformBackgroundCacheRevalidation();
  // This method can only be called on the main thread.
  void PerformBackgroundCacheRevalidationNow();

 private:
  nsCOMPtr<nsICancelable> mProxyRequest;

  RefPtr<nsInputStreamPump> mTransactionPump;
  RefPtr<nsHttpTransaction> mTransaction;

  uint64_t mLogicalOffset;

  // cache specific data
  nsCOMPtr<nsICacheEntry> mCacheEntry;
  // This will be set during OnStopRequest() before calling CloseCacheEntry(),
  // but only if the listener wants to use alt-data (signaled by
  // HttpBaseChannel::mPreferredCachedAltDataType being not empty)
  // Needed because calling openAlternativeOutputStream needs a reference
  // to the cache entry.
  nsCOMPtr<nsICacheEntry> mAltDataCacheEntry;
  // We must close mCacheInputStream explicitly to avoid leaks.
  AutoClose<nsIInputStream> mCacheInputStream;
  RefPtr<nsInputStreamPump> mCachePump;
  nsAutoPtr<nsHttpResponseHead> mCachedResponseHead;
  nsCOMPtr<nsISupports> mCachedSecurityInfo;
  uint32_t mPostID;
  uint32_t mRequestTime;

  nsCOMPtr<nsICacheEntry> mOfflineCacheEntry;
  uint32_t mOfflineCacheLastModifiedTime;

  mozilla::TimeStamp mOnStartRequestTimestamp;
  // Timestamp of the time the channel was suspended.
  mozilla::TimeStamp mSuspendTimestamp;
  mozilla::TimeStamp mOnCacheEntryCheckTimestamp;
#ifdef MOZ_GECKO_PROFILER
  // For the profiler markers
  mozilla::TimeStamp mLastStatusReported;
#endif
  // Total time the channel spent suspended. This value is reported to
  // telemetry in nsHttpChannel::OnStartRequest().
  uint32_t mSuspendTotalTime;

  // If the channel is associated with a cache, and the URI matched
  // a fallback namespace, this will hold the key for the fallback
  // cache entry.
  nsCString mFallbackKey;

  friend class AutoRedirectVetoNotifier;
  friend class HttpAsyncAborter<nsHttpChannel>;

  uint32_t mRedirectType;

  static const uint32_t WAIT_FOR_CACHE_ENTRY = 1;
  static const uint32_t WAIT_FOR_OFFLINE_CACHE_ENTRY = 2;

  bool mCacheOpenWithPriority;
  uint32_t mCacheQueueSizeWhenOpen;

  // state flags
  uint32_t mCachedContentIsValid : 1;
  uint32_t mCachedContentIsPartial : 1;
  uint32_t mCacheOnlyMetadata : 1;
  uint32_t mTransactionReplaced : 1;
  uint32_t mAuthRetryPending : 1;
  uint32_t mProxyAuthPending : 1;
  // Set if before the first authentication attempt a custom authorization
  // header has been set on the channel.  This will make that custom header
  // go to the server instead of any cached credentials.
  uint32_t mCustomAuthHeader : 1;
  uint32_t mResuming : 1;
  uint32_t mInitedCacheEntry : 1;
  // True if we are loading a fallback cache entry from the
  // application cache.
  uint32_t mFallbackChannel : 1;
  // True if consumer added its own If-None-Match or If-Modified-Since
  // headers. In such a case we must not override them in the cache code
  // and also we want to pass possible 304 code response through.
  uint32_t mCustomConditionalRequest : 1;
  uint32_t mFallingBack : 1;
  uint32_t mWaitingForRedirectCallback : 1;
  // True if mRequestTime has been set. In such a case it is safe to update
  // the cache entry's expiration time. Otherwise, it is not(see bug 567360).
  uint32_t mRequestTimeInitialized : 1;
  uint32_t mCacheEntryIsReadOnly : 1;
  uint32_t mCacheEntryIsWriteOnly : 1;
  // see WAIT_FOR_* constants above
  uint32_t mCacheEntriesToWaitFor : 2;
  // whether cache entry data write was in progress during cache entry check
  // when true, after we finish read from cache we must check all data
  // had been loaded from cache. If not, then an error has to be propagated
  // to the consumer.
  uint32_t mConcurrentCacheAccess : 1;
  // whether the request is setup be byte-range
  uint32_t mIsPartialRequest : 1;
  // true iff there is AutoRedirectVetoNotifier on the stack
  uint32_t mHasAutoRedirectVetoNotifier : 1;
  // consumers set this to true to use cache pinning, this has effect
  // only when the channel is in an app context
  uint32_t mPinCacheContent : 1;
  // True if CORS preflight has been performed
  uint32_t mIsCorsPreflightDone : 1;

  // if the http transaction was performed (i.e. not cached) and
  // the result in OnStopRequest was known to be correctly delimited
  // by chunking, content-length, or h2 end-stream framing
  uint32_t mStronglyFramed : 1;

  // true if an HTTP transaction is created for the socket thread
  uint32_t mUsedNetwork : 1;

  // the next authentication request can be sent on a whole new connection
  uint32_t mAuthConnectionRestartable : 1;

  // True if the channel classifier has marked the channel to be cancelled due
  // to the safe-browsing classifier rules, but the asynchronous cancellation
  // process hasn't finished yet.
  uint32_t mChannelClassifierCancellationPending : 1;

  // True only when we are between Resume and async fire of mCallOnResume.
  // Used to suspend any newly created pumps in mCallOnResume handler.
  uint32_t mAsyncResumePending : 1;

  // True only when we have checked whether this channel has been isolated for
  // anti-tracking purposes.
  uint32_t mHasBeenIsolatedChecked : 1;
  // True only when we have determined this channel should be isolated for
  // anti-tracking purposes.  Can never ben true unless mHasBeenIsolatedChecked
  // is true.
  uint32_t mIsIsolated : 1;

  // True only when we have computed the value of the top window origin.
  uint32_t mTopWindowOriginComputed : 1;

  // True if this is a navigation to a page with a different cross origin
  // opener policy ( see ComputeCrossOriginOpenerPolicyMismatch )
  uint32_t mHasCrossOriginOpenerPolicyMismatch : 1;

  // The origin of the top window, only valid when mTopWindowOriginComputed is
  // true.
  nsCString mTopWindowOrigin;

  nsTArray<nsContinueRedirectionFunc> mRedirectFuncStack;

  // Needed for accurate DNS timing
  RefPtr<nsDNSPrefetch> mDNSPrefetch;

  RefPtr<Http2PushedStreamWrapper> mPushedStream;
  // True if the channel's principal was found on a phishing, malware, or
  // tracking (if tracking protection is enabled) blocklist
  bool mLocalBlocklist;

  MOZ_MUST_USE nsresult WaitForRedirectCallback();
  void PushRedirectAsyncFunc(nsContinueRedirectionFunc func);
  void PopRedirectAsyncFunc(nsContinueRedirectionFunc func);

  // If this resource is eligible for tailing based on class-of-service flags
  // and load flags.  We don't tail Leaders/Unblocked/UrgentStart and top-level
  // loads.
  bool EligibleForTailing();

  // Called exclusively only from AsyncOpen or after all classification
  // callbacks. If this channel is 1) Tail, 2) assigned a request context, 3)
  // the context is still in the tail-blocked phase, then the method will queue
  // this channel. OnTailUnblock will be called after the context is
  // tail-unblocked or canceled.
  bool WaitingForTailUnblock();

  // A function we trigger when untail callback is triggered by our request
  // context in case this channel was tail-blocked.
  nsresult (nsHttpChannel::*mOnTailUnblock)();
  // Called on untail when tailed during AsyncOpen execution.
  nsresult AsyncOpenOnTailUnblock();
  // Called on untail when tailed because of being a tracking resource.
  nsresult ConnectOnTailUnblock();

  nsCString mUsername;

  // If non-null, warnings should be reported to this object.
  RefPtr<HttpChannelSecurityWarningReporter> mWarningReporter;

  RefPtr<ADivertableParentChannel> mParentChannel;

  // True if the channel is reading from cache.
  Atomic<bool> mIsReadingFromCache;

  // These next members are only used in unit tests to delay the call to
  // cache->AsyncOpenURI in order to race the cache with the network.
  nsCOMPtr<nsITimer> mCacheOpenTimer;
  std::function<void(nsHttpChannel*)> mCacheOpenFunc;
  uint32_t mCacheOpenDelay = 0;

  // We need to remember which is the source of the response we are using.
  enum ResponseSource {
    RESPONSE_PENDING = 0,      // response is pending
    RESPONSE_FROM_CACHE = 1,   // response coming from cache. no network.
    RESPONSE_FROM_NETWORK = 2  // response coming from the network
  };
  Atomic<ResponseSource, Relaxed> mFirstResponseSource;

  // Determines if it's possible and advisable to race the network request
  // with the cache fetch, and proceeds to do so.
  nsresult MaybeRaceCacheWithNetwork();

  nsresult TriggerNetworkWithDelay(uint32_t aDelay);
  nsresult TriggerNetwork();
  void CancelNetworkRequest(nsresult aStatus);
  // Timer used to delay the network request, or to trigger the network
  // request if retrieving the cache entry takes too long.
  nsCOMPtr<nsITimer> mNetworkTriggerTimer;
  // Is true if the network request has been triggered.
  bool mNetworkTriggered = false;
  bool mWaitingForProxy = false;
  // Will be true if the onCacheEntryAvailable callback is not called by the
  // time we send the network request
  Atomic<bool> mRaceCacheWithNetwork;
  uint32_t mRaceDelay;
  // If true then OnCacheEntryAvailable should ignore the entry, because
  // SetupTransaction removed conditional headers and decisions made in
  // OnCacheEntryCheck are no longer valid.
  bool mIgnoreCacheEntry;
  // Lock preventing OnCacheEntryCheck and SetupTransaction being called at
  // the same time.
  mozilla::Mutex mRCWNLock;

  TimeStamp mNavigationStartTimeStamp;

 protected:
  virtual void DoNotifyListenerCleanup() override;

  // Override ReleaseListeners() because mChannelClassifier only exists
  // in nsHttpChannel and it will be released in ReleaseListeners().
  virtual void ReleaseListeners() override;

  virtual void DoAsyncAbort(nsresult aStatus) override;

 private:  // cache telemetry
  bool mDidReval;
};

NS_DEFINE_STATIC_IID_ACCESSOR(nsHttpChannel, NS_HTTPCHANNEL_IID)
}  // namespace net
}  // namespace mozilla

inline nsISupports* ToSupports(mozilla::net::nsHttpChannel* aChannel) {
  return static_cast<nsIHttpChannel*>(aChannel);
}

#endif  // nsHttpChannel_h__