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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* 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 "Http2Push.h"
#include "Http2Stream.h"
#include "nsHttp.h"
#include "nsHttpConnectionInfo.h"
#include "nsHttpRequestHead.h"
#include "nsISocketTransport.h"
#include "Http2Session.h"
#include "PSpdyPush.h"
#include "nsIRequestContext.h"
#include "nsHttpTransaction.h"
#include "nsSocketTransportService2.h"
namespace mozilla::net {
Http2Stream::Http2Stream(nsAHttpTransaction* httpTransaction,
Http2Session* session, int32_t priority, uint64_t bcId)
: Http2StreamBase((httpTransaction->QueryHttpTransaction())
? httpTransaction->QueryHttpTransaction()->BrowserId()
: 0,
session, priority, bcId),
mTransaction(httpTransaction) {
LOG1(("Http2Stream::Http2Stream %p trans=%p", this, httpTransaction));
}
Http2Stream::~Http2Stream() { ClearPushSource(); }
void Http2Stream::CloseStream(nsresult reason) {
// In case we are connected to a push, make sure the push knows we are closed,
// so it doesn't try to give us any more DATA that comes on it after our
// close.
ClearPushSource();
mTransaction->Close(reason);
mSession = nullptr;
}
void Http2Stream::ClearPushSource() {
if (mPushSource) {
mPushSource->SetConsumerStream(nullptr);
mPushSource = nullptr;
}
}
nsresult Http2Stream::CheckPushCache() {
nsHttpRequestHead* head = mTransaction->RequestHead();
// check the push cache for GET
if (!head->IsGet()) {
return NS_OK;
}
RefPtr<Http2Session> session = Session();
nsAutoCString authorityHeader;
nsAutoCString hashkey;
nsresult rv = head->GetHeader(nsHttp::Host, authorityHeader);
if (NS_FAILED(rv)) {
MOZ_ASSERT(false);
return rv;
}
nsAutoCString requestURI;
head->RequestURI(requestURI);
mozilla::OriginAttributes originAttributes;
mSocketTransport->GetOriginAttributes(&originAttributes);
CreatePushHashKey(nsDependentCString(head->IsHTTPS() ? "https" : "http"),
authorityHeader, originAttributes, session->Serial(),
requestURI, mOrigin, hashkey);
// from :scheme, :authority, :path
nsIRequestContext* requestContext = mTransaction->RequestContext();
SpdyPushCache* cache = nullptr;
if (requestContext) {
cache = requestContext->GetSpdyPushCache();
}
RefPtr<Http2PushedStreamWrapper> pushedStreamWrapper;
Http2PushedStream* pushedStream = nullptr;
// If a push stream is attached to the transaction via onPush, match only
// with that one. This occurs when a push was made with in conjunction with
// a nsIHttpPushListener
nsHttpTransaction* trans = mTransaction->QueryHttpTransaction();
if (trans && (pushedStreamWrapper = trans->TakePushedStream()) &&
(pushedStream = pushedStreamWrapper->GetStream())) {
RefPtr<Http2Session> pushSession = pushedStream->Session();
if (pushSession == session) {
LOG3(
("Pushed Stream match based on OnPush correlation %p", pushedStream));
} else {
LOG3(("Pushed Stream match failed due to stream mismatch %p %" PRId64
" %" PRId64 "\n",
pushedStream, pushSession->Serial(), session->Serial()));
pushedStream->OnPushFailed();
pushedStream = nullptr;
}
}
// we remove the pushedstream from the push cache so that
// it will not be used for another GET. This does not destroy the
// stream itself - that is done when the transactionhash is done with it.
if (cache && !pushedStream) {
pushedStream = cache->RemovePushedStreamHttp2(hashkey);
}
LOG3(
("Pushed Stream Lookup "
"session=%p key=%s requestcontext=%p cache=%p hit=%p\n",
session.get(), hashkey.get(), requestContext, cache, pushedStream));
if (pushedStream) {
LOG3(("Pushed Stream Match located %p id=0x%X key=%s\n", pushedStream,
pushedStream->StreamID(), hashkey.get()));
pushedStream->SetConsumerStream(this);
mPushSource = pushedStream;
SetSentFin(true);
AdjustPushedPriority();
// There is probably pushed data buffered so trigger a read manually
// as we can't rely on future network events to do it
session->ConnectPushedStream(this);
mOpenGenerated = 1;
// if the "mother stream" had TRR, this one is a TRR stream too!
RefPtr<nsHttpConnectionInfo> ci(Transaction()->ConnectionInfo());
if (ci && ci->GetIsTrrServiceChannel()) {
session->IncrementTrrCounter();
}
}
return NS_OK;
}
uint32_t Http2Stream::GetWireStreamId() {
// >0 even numbered IDs are pushed streams.
// odd numbered IDs are pulled streams.
// 0 is the sink for a pushed stream.
if (!mStreamID) {
MOZ_ASSERT(mPushSource);
if (!mPushSource) {
return 0;
}
MOZ_ASSERT(mPushSource->StreamID());
MOZ_ASSERT(!(mPushSource->StreamID() & 1)); // is a push stream
// If the pushed stream has recvd a FIN, there is no reason to update
// the window
if (mPushSource->RecvdFin() || mPushSource->RecvdReset() ||
(mPushSource->HTTPState() == RESERVED_BY_REMOTE)) {
return 0;
}
return mPushSource->StreamID();
}
if (mState == RESERVED_BY_REMOTE) {
// h2-14 prevents sending a window update in this state
return 0;
}
return mStreamID;
}
void Http2Stream::AdjustPushedPriority() {
// >0 even numbered IDs are pushed streams. odd numbered IDs are pulled
// streams. 0 is the sink for a pushed stream.
if (mStreamID || !mPushSource) return;
MOZ_ASSERT(mPushSource->StreamID() && !(mPushSource->StreamID() & 1));
// If the pushed stream has recvd a FIN, there is no reason to update
// the window
if (mPushSource->RecvdFin() || mPushSource->RecvdReset()) return;
// Ensure we pick up the right dependency to place the pushed stream under.
UpdatePriorityDependency();
EnsureBuffer(mTxInlineFrame,
mTxInlineFrameUsed + Http2Session::kFrameHeaderBytes + 5,
mTxInlineFrameUsed, mTxInlineFrameSize);
uint8_t* packet = mTxInlineFrame.get() + mTxInlineFrameUsed;
mTxInlineFrameUsed += Http2Session::kFrameHeaderBytes + 5;
RefPtr<Http2Session> session = Session();
session->CreateFrameHeader(packet, 5, Http2Session::FRAME_TYPE_PRIORITY, 0,
mPushSource->StreamID());
mPushSource->SetPriorityDependency(mPriority, mPriorityDependency);
uint32_t wireDep = PR_htonl(mPriorityDependency);
memcpy(packet + Http2Session::kFrameHeaderBytes, &wireDep, 4);
memcpy(packet + Http2Session::kFrameHeaderBytes + 4, &mPriorityWeight, 1);
LOG3(("AdjustPushedPriority %p id 0x%X to dep %X weight %X\n", this,
mPushSource->StreamID(), mPriorityDependency, mPriorityWeight));
}
bool Http2Stream::IsReadingFromPushStream() { return !!mPushSource; }
nsresult Http2Stream::OnWriteSegment(char* buf, uint32_t count,
uint32_t* countWritten) {
LOG3(("Http2Stream::OnWriteSegment %p count=%d state=%x 0x%X\n", this, count,
mUpstreamState, mStreamID));
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(mSegmentWriter);
if (mPushSource) {
nsresult rv;
rv = mPushSource->GetBufferedData(buf, count, countWritten);
if (NS_FAILED(rv)) return rv;
RefPtr<Http2Session> session = Session();
session->ConnectPushedStream(this);
return NS_OK;
}
return Http2StreamBase::OnWriteSegment(buf, count, countWritten);
}
nsresult Http2Stream::CallToReadData(uint32_t count, uint32_t* countRead) {
return mTransaction->ReadSegments(this, count, countRead);
}
nsresult Http2Stream::CallToWriteData(uint32_t count, uint32_t* countWritten) {
return mTransaction->WriteSegments(this, count, countWritten);
}
// This is really a headers frame, but open is pretty clear from a workflow pov
nsresult Http2Stream::GenerateHeaders(nsCString& aCompressedData,
uint8_t& firstFrameFlags) {
nsHttpRequestHead* head = mTransaction->RequestHead();
nsAutoCString requestURI;
head->RequestURI(requestURI);
RefPtr<Http2Session> session = Session();
LOG3(("Http2Stream %p Stream ID 0x%X [session=%p] for URI %s\n", this,
mStreamID, session.get(), requestURI.get()));
nsAutoCString authorityHeader;
nsresult rv = head->GetHeader(nsHttp::Host, authorityHeader);
if (NS_FAILED(rv)) {
MOZ_ASSERT(false);
return rv;
}
nsDependentCString scheme(head->IsHTTPS() ? "https" : "http");
nsAutoCString method;
nsAutoCString path;
head->Method(method);
head->Path(path);
rv = session->Compressor()->EncodeHeaderBlock(
mFlatHttpRequestHeaders, method, path, authorityHeader, scheme,
EmptyCString(), false, aCompressedData);
NS_ENSURE_SUCCESS(rv, rv);
int64_t clVal = session->Compressor()->GetParsedContentLength();
if (clVal != -1) {
mRequestBodyLenRemaining = clVal;
}
// Determine whether to put the fin bit on the header frame or whether
// to wait for a data packet to put it on.
if (head->IsGet() || head->IsHead()) {
// for GET and HEAD place the fin bit right on the
// header packet
firstFrameFlags |= Http2Session::kFlag_END_STREAM;
} else if (head->IsPost() || head->IsPut() || head->IsConnect()) {
// place fin in a data frame even for 0 length messages for iterop
} else if (!mRequestBodyLenRemaining) {
// for other HTTP extension methods, rely on the content-length
// to determine whether or not to put fin on headers
firstFrameFlags |= Http2Session::kFlag_END_STREAM;
}
// The size of the input headers is approximate
uint32_t ratio =
aCompressedData.Length() * 100 /
(11 + requestURI.Length() + mFlatHttpRequestHeaders.Length());
Telemetry::Accumulate(Telemetry::SPDY_SYN_RATIO, ratio);
return NS_OK;
}
} // namespace mozilla::net
|