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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: -*- */
/* vim:set expandtab ts=2 sw=2 sts=2 cin: */
/* 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 "HttpLog.h"
#include "InterceptedChannel.h"
#include "nsInputStreamPump.h"
#include "nsITimedChannel.h"
#include "nsHttpChannel.h"
#include "HttpChannelChild.h"
#include "nsHttpResponseHead.h"
#include "nsNetUtil.h"
#include "mozilla/ConsoleReportCollector.h"
#include "mozilla/dom/ChannelInfo.h"
#include "nsThreadUtils.h"
namespace mozilla {
namespace net {
extern nsresult DoUpdateExpirationTime(nsHttpChannel* aSelf,
nsICacheEntry* aCacheEntry,
nsHttpResponseHead* aResponseHead,
uint32_t& aExpirationTime);
extern nsresult DoAddCacheEntryHeaders(nsHttpChannel* self,
nsICacheEntry* entry,
nsHttpRequestHead* requestHead,
nsHttpResponseHead* responseHead,
nsISupports* securityInfo);
NS_IMPL_ISUPPORTS(InterceptedChannelBase, nsIInterceptedChannel)
InterceptedChannelBase::InterceptedChannelBase(
nsINetworkInterceptController* aController)
: mController(aController),
mReportCollector(new ConsoleReportCollector()),
mClosed(false),
mSynthesizedOrReset(Invalid) {}
void InterceptedChannelBase::EnsureSynthesizedResponse() {
if (mSynthesizedResponseHead.isNothing()) {
mSynthesizedResponseHead.emplace(new nsHttpResponseHead());
}
}
void InterceptedChannelBase::DoNotifyController() {
nsresult rv = NS_OK;
if (NS_WARN_IF(!mController)) {
rv = ResetInterception();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to resume intercepted network request");
CancelInterception(rv);
}
return;
}
rv = mController->ChannelIntercepted(this);
if (NS_WARN_IF(NS_FAILED(rv))) {
rv = ResetInterception();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to resume intercepted network request");
CancelInterception(rv);
}
}
mController = nullptr;
}
void InterceptedChannelBase::DoSynthesizeStatus(uint16_t aStatus,
const nsACString& aReason) {
EnsureSynthesizedResponse();
// Always assume HTTP 1.1 for synthesized responses.
nsAutoCString statusLine;
statusLine.AppendLiteral("HTTP/1.1 ");
statusLine.AppendInt(aStatus);
statusLine.AppendLiteral(" ");
statusLine.Append(aReason);
(*mSynthesizedResponseHead)->ParseStatusLine(statusLine);
}
nsresult InterceptedChannelBase::DoSynthesizeHeader(const nsACString& aName,
const nsACString& aValue) {
EnsureSynthesizedResponse();
nsAutoCString header = aName + ": "_ns + aValue;
// Overwrite any existing header.
return (*mSynthesizedResponseHead)->ParseHeaderLine(header);
}
NS_IMETHODIMP
InterceptedChannelBase::GetConsoleReportCollector(
nsIConsoleReportCollector** aCollectorOut) {
MOZ_ASSERT(aCollectorOut);
nsCOMPtr<nsIConsoleReportCollector> ref = mReportCollector;
ref.forget(aCollectorOut);
return NS_OK;
}
NS_IMETHODIMP
InterceptedChannelBase::SetReleaseHandle(nsISupports* aHandle) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mReleaseHandle);
MOZ_ASSERT(aHandle);
// We need to keep it and mChannel alive until destructor clear it up.
mReleaseHandle = aHandle;
return NS_OK;
}
NS_IMETHODIMP
InterceptedChannelBase::SaveTimeStamps() {
MOZ_ASSERT(NS_IsMainThread());
// If we were not able to start the fetch event for some reason (like
// corrupted scripts), then just do nothing here.
if (mHandleFetchEventStart.IsNull()) {
return NS_OK;
}
nsCOMPtr<nsIChannel> underlyingChannel;
nsresult rv = GetChannel(getter_AddRefs(underlyingChannel));
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(underlyingChannel);
MOZ_ASSERT(timedChannel);
rv = timedChannel->SetLaunchServiceWorkerStart(mLaunchServiceWorkerStart);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = timedChannel->SetLaunchServiceWorkerEnd(mLaunchServiceWorkerEnd);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = timedChannel->SetDispatchFetchEventStart(mDispatchFetchEventStart);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = timedChannel->SetDispatchFetchEventEnd(mDispatchFetchEventEnd);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = timedChannel->SetHandleFetchEventStart(mHandleFetchEventStart);
MOZ_ASSERT(NS_SUCCEEDED(rv));
rv = timedChannel->SetHandleFetchEventEnd(mHandleFetchEventEnd);
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsCOMPtr<nsIChannel> channel;
GetChannel(getter_AddRefs(channel));
if (NS_WARN_IF(!channel)) {
return NS_ERROR_FAILURE;
}
bool isNonSubresourceRequest =
nsContentUtils::IsNonSubresourceRequest(channel);
nsCString navigationOrSubresource =
isNonSubresourceRequest ? "navigation"_ns : "subresource"_ns;
nsAutoCString subresourceKey(""_ns);
GetSubresourceTimeStampKey(channel, subresourceKey);
// We may have null timestamps if the fetch dispatch runnable was cancelled
// and we defaulted to resuming the request.
if (!mFinishResponseStart.IsNull() && !mFinishResponseEnd.IsNull()) {
MOZ_ASSERT(mSynthesizedOrReset != Invalid);
Telemetry::HistogramID id =
(mSynthesizedOrReset == Synthesized)
? Telemetry::
SERVICE_WORKER_FETCH_EVENT_FINISH_SYNTHESIZED_RESPONSE_MS
: Telemetry::SERVICE_WORKER_FETCH_EVENT_CHANNEL_RESET_MS;
Telemetry::Accumulate(
id, navigationOrSubresource,
static_cast<uint32_t>(
(mFinishResponseEnd - mFinishResponseStart).ToMilliseconds()));
if (!isNonSubresourceRequest && !subresourceKey.IsEmpty()) {
Telemetry::Accumulate(
id, subresourceKey,
static_cast<uint32_t>(
(mFinishResponseEnd - mFinishResponseStart).ToMilliseconds()));
}
}
Telemetry::Accumulate(
Telemetry::SERVICE_WORKER_FETCH_EVENT_DISPATCH_MS,
navigationOrSubresource,
static_cast<uint32_t>((mHandleFetchEventStart - mDispatchFetchEventStart)
.ToMilliseconds()));
if (!isNonSubresourceRequest && !subresourceKey.IsEmpty()) {
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_FETCH_EVENT_DISPATCH_MS,
subresourceKey,
static_cast<uint32_t>((mHandleFetchEventStart -
mDispatchFetchEventStart)
.ToMilliseconds()));
}
if (!mFinishResponseEnd.IsNull()) {
Telemetry::Accumulate(
Telemetry::SERVICE_WORKER_FETCH_INTERCEPTION_DURATION_MS,
navigationOrSubresource,
static_cast<uint32_t>(
(mFinishResponseEnd - mDispatchFetchEventStart).ToMilliseconds()));
if (!isNonSubresourceRequest && !subresourceKey.IsEmpty()) {
Telemetry::Accumulate(
Telemetry::SERVICE_WORKER_FETCH_INTERCEPTION_DURATION_MS,
subresourceKey,
static_cast<uint32_t>((mFinishResponseEnd - mDispatchFetchEventStart)
.ToMilliseconds()));
}
}
return rv;
}
/* static */
already_AddRefed<nsIURI> InterceptedChannelBase::SecureUpgradeChannelURI(
nsIChannel* aChannel) {
nsCOMPtr<nsIURI> uri;
nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, nullptr);
nsCOMPtr<nsIURI> upgradedURI;
rv = NS_GetSecureUpgradedURI(uri, getter_AddRefs(upgradedURI));
NS_ENSURE_SUCCESS(rv, nullptr);
return upgradedURI.forget();
}
} // namespace net
} // namespace mozilla
|