File: HTTPSRecordResolver.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (204 lines) | stat: -rw-r--r-- 6,125 bytes parent folder | download
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */

// HttpLog.h should generally be included first
#include "HttpLog.h"

#include "HTTPSRecordResolver.h"
#include "mozilla/Components.h"
#include "mozilla/StaticPrefs_network.h"
#include "nsIDNSByTypeRecord.h"
#include "nsIDNSAdditionalInfo.h"
#include "nsIDNSService.h"
#include "nsHttpConnectionInfo.h"
#include "nsNetCID.h"
#include "nsAHttpTransaction.h"
#include "nsServiceManagerUtils.h"

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS(HTTPSRecordResolver, nsIDNSListener)

HTTPSRecordResolver::HTTPSRecordResolver(nsAHttpTransaction* aTransaction)
    : mTransaction(aTransaction), mConnInfo(aTransaction->ConnectionInfo()) {}

HTTPSRecordResolver::~HTTPSRecordResolver() = default;

nsresult HTTPSRecordResolver::FetchHTTPSRRInternal(
    nsIEventTarget* aTarget, nsICancelable** aDNSRequest) {
  NS_ENSURE_ARG_POINTER(aTarget);

  // Only fetch HTTPS RR for https.
  if (!mConnInfo->FirstHopSSL()) {
    return NS_ERROR_FAILURE;
  }

  nsCOMPtr<nsIDNSService> dns = mozilla::components::DNS::Service();
  if (!dns) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsIDNSService::DNSFlags flags =
      nsIDNSService::GetFlagsFromTRRMode(mConnInfo->GetTRRMode());
  if (mTransaction->Caps() & NS_HTTP_REFRESH_DNS) {
    flags |= nsIDNSService::RESOLVE_BYPASS_CACHE;
  }

  nsCOMPtr<nsIDNSAdditionalInfo> info;
  if (mConnInfo->OriginPort() != NS_HTTPS_DEFAULT_PORT) {
    dns->NewAdditionalInfo(""_ns, mConnInfo->OriginPort(),
                           getter_AddRefs(info));
  }

  MutexAutoLock lock(mMutex);

  nsresult rv = dns->AsyncResolveNative(
      mConnInfo->GetOrigin(), nsIDNSService::RESOLVE_TYPE_HTTPSSVC, flags, info,
      this, aTarget, mConnInfo->GetOriginAttributes(),
      getter_AddRefs(mHTTPSRecordRequest));

  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsICancelable> request = mHTTPSRecordRequest;
  request.forget(aDNSRequest);

  if (!StaticPrefs::network_dns_https_rr_check_record_with_cname()) {
    return rv;
  }

  rv = dns->AsyncResolveNative(
      mConnInfo->GetOrigin(), nsIDNSService::RESOLVE_TYPE_DEFAULT,
      flags | nsIDNSService::RESOLVE_CANONICAL_NAME, nullptr, this, aTarget,
      mConnInfo->GetOriginAttributes(), getter_AddRefs(mCnameRequest));
  return rv;
}

NS_IMETHODIMP HTTPSRecordResolver::OnLookupComplete(nsICancelable* aRequest,
                                                    nsIDNSRecord* aRecord,
                                                    nsresult aStatus) {
  MutexAutoLock lock(mMutex);
  if (!mTransaction || mDone) {
    // The transaction is not interesed in a response anymore.
    mCnameRequest = nullptr;
    mHTTPSRecordRequest = nullptr;
    return NS_OK;
  }

  if (aRequest == mHTTPSRecordRequest) {
    nsCOMPtr<nsIDNSHTTPSSVCRecord> record = do_QueryInterface(aRecord);
    mHTTPSRecordRequest = nullptr;
    if (!record || NS_FAILED(aStatus)) {
      // When failed, we don't want to wait for the CNAME.
      mCnameRequest = nullptr;
      MutexAutoUnlock unlock(mMutex);
      return InvokeCallback();
    }

    mHTTPSRecord = record;

    // Waiting for the address record.
    if (mCnameRequest) {
      return NS_OK;
    }

    MutexAutoUnlock unlock(mMutex);
    return InvokeCallback();
  }

  // Having mCnameRequest indicates that we are interested in the address
  // record.
  if (mCnameRequest && aRequest == mCnameRequest) {
    nsCOMPtr<nsIDNSAddrRecord> addrRecord = do_QueryInterface(aRecord);
    mCnameRequest = nullptr;
    if (!addrRecord || NS_FAILED(aStatus)) {
      // If we are still waiting for HTTPS RR, don't invoke the callback.
      if (mHTTPSRecordRequest) {
        return NS_OK;
      }

      MutexAutoUnlock unlock(mMutex);
      return InvokeCallback();
    }

    mAddrRecord = addrRecord;
    // Waiting for the HTTPS record.
    if (mHTTPSRecordRequest) {
      return NS_OK;
    }

    MutexAutoUnlock unlock(mMutex);
    return InvokeCallback();
  }

  return NS_OK;
}

nsresult HTTPSRecordResolver::InvokeCallback() {
  MOZ_ASSERT(!mDone);

  mDone = true;
  if (!mHTTPSRecord) {
    return mTransaction->OnHTTPSRRAvailable(nullptr, nullptr, ""_ns);
  }

  nsCString cname;
  if (mAddrRecord) {
    (void)mAddrRecord->GetCanonicalName(cname);
  }

  // Make sure we use the updated caps from the transaction.
  uint32_t caps = mTransaction->Caps();
  nsCOMPtr<nsISVCBRecord> svcbRecord;
  if (NS_FAILED(mHTTPSRecord->GetServiceModeRecordWithCname(
          caps & NS_HTTP_DISALLOW_SPDY, caps & NS_HTTP_DISALLOW_HTTP3, cname,
          getter_AddRefs(svcbRecord)))) {
    return mTransaction->OnHTTPSRRAvailable(mHTTPSRecord, nullptr, cname);
  }

  return mTransaction->OnHTTPSRRAvailable(mHTTPSRecord, svcbRecord, cname);
}

void HTTPSRecordResolver::PrefetchAddrRecord(const nsACString& aTargetName,
                                             bool aRefreshDNS) {
  MOZ_ASSERT(mTransaction);
  nsCOMPtr<nsIDNSService> dns = mozilla::components::DNS::Service();
  if (!dns) {
    return;
  }

  nsIDNSService::DNSFlags flags = nsIDNSService::GetFlagsFromTRRMode(
      mTransaction->ConnectionInfo()->GetTRRMode());
  if (aRefreshDNS) {
    flags |= nsIDNSService::RESOLVE_BYPASS_CACHE;
  }

  nsCOMPtr<nsICancelable> tmpOutstanding;

  (void)dns->AsyncResolveNative(
      aTargetName, nsIDNSService::RESOLVE_TYPE_DEFAULT,
      flags | nsIDNSService::RESOLVE_SPECULATE, nullptr, this,
      GetCurrentSerialEventTarget(),
      mTransaction->ConnectionInfo()->GetOriginAttributes(),
      getter_AddRefs(tmpOutstanding));
}

void HTTPSRecordResolver::Close() {
  mTransaction = nullptr;
  MutexAutoLock lock(mMutex);
  if (mCnameRequest) {
    mCnameRequest->Cancel(NS_ERROR_ABORT);
    mCnameRequest = nullptr;
  }
  if (mHTTPSRecordRequest) {
    mHTTPSRecordRequest->Cancel(NS_ERROR_ABORT);
    mHTTPSRecordRequest = nullptr;
  }
}

}  // namespace net
}  // namespace mozilla