summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/EarlyHintPreconnect.cpp
blob: e4d05c073f8493339a033e63ffd92d956b0c446c (plain)
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
/* 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/. */

#include "EarlyHintPreconnect.h"

#include "mozilla/CORSMode.h"
#include "mozilla/dom/Element.h"
#include "mozilla/StaticPrefs_network.h"
#include "nsIOService.h"
#include "nsIURI.h"

namespace mozilla::net {

void EarlyHintPreconnect::MaybePreconnect(const LinkHeader& aHeader,
                                          nsIURI* aBaseURI,
                                          nsIPrincipal* aPrincipal) {
  if (!StaticPrefs::network_early_hints_preconnect_enabled()) {
    return;
  }

  if (!gIOService) {
    return;
  }

  nsCOMPtr<nsIURI> uri;
  if (NS_FAILED(aHeader.NewResolveHref(getter_AddRefs(uri), aBaseURI))) {
    return;
  }

  // only preconnect secure context urls
  if (!uri->SchemeIs("https")) {
    return;
  }

  // Note that the http connection manager will limit the number of connections
  // we can make, so it should be fine we don't check duplicate preconnect
  // attempts here.
  CORSMode corsMode = dom::Element::StringToCORSMode(aHeader.mCrossOrigin);
  if (corsMode == CORS_ANONYMOUS) {
    gIOService->SpeculativeAnonymousConnect(uri, aPrincipal, nullptr);
  } else {
    gIOService->SpeculativeConnect(uri, aPrincipal, nullptr);
  }
}

}  // namespace mozilla::net