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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/quic_headers_stream.h"
#include "net/quic/quic_session.h"
#include "net/quic/quic_spdy_decompressor.h"
using base::StringPiece;
namespace net {
namespace {
const QuicStreamId kInvalidStreamId = 0;
} // namespace
// A SpdyFramer visitor which passed SYN_STREAM and SYN_REPLY frames to
// the QuicDataStream, and closes the connection if any unexpected frames
// are received.
class QuicHeadersStream::SpdyFramerVisitor
: public SpdyFramerVisitorInterface,
public SpdyFramerDebugVisitorInterface {
public:
explicit SpdyFramerVisitor(QuicHeadersStream* stream) : stream_(stream) {}
// SpdyFramerVisitorInterface implementation
virtual void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
bool fin,
bool unidirectional) OVERRIDE {
if (!stream_->IsConnected()) {
return;
}
if (associated_stream_id != 0) {
CloseConnection("associated_stream_id != 0");
return;
}
if (unidirectional != 0) {
CloseConnection("unidirectional != 0");
return;
}
stream_->OnSynStream(stream_id, priority, fin);
}
virtual void OnSynReply(SpdyStreamId stream_id, bool fin) OVERRIDE {
if (!stream_->IsConnected()) {
return;
}
stream_->OnSynReply(stream_id, fin);
}
virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id,
const char* header_data,
size_t len) OVERRIDE {
if (!stream_->IsConnected()) {
return false;
}
stream_->OnControlFrameHeaderData(stream_id, header_data, len);
return true;
}
virtual void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) OVERRIDE {
if (fin && len == 0) {
// The framer invokes OnStreamFrameData with zero-length data and
// fin = true after processing a SYN_STREAM or SYN_REPLY frame
// that had the fin bit set.
return;
}
CloseConnection("SPDY DATA frame recevied.");
}
virtual void OnError(SpdyFramer* framer) OVERRIDE {
CloseConnection("SPDY framing error.");
}
virtual void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) OVERRIDE {
CloseConnection("SPDY DATA frame recevied.");
}
virtual void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) OVERRIDE {
CloseConnection("SPDY RST_STREAM frame recevied.");
}
virtual void OnSetting(SpdySettingsIds id,
uint8 flags,
uint32 value) OVERRIDE {
CloseConnection("SPDY SETTINGS frame recevied.");
}
virtual void OnPing(uint32 unique_id) OVERRIDE {
CloseConnection("SPDY PING frame recevied.");
}
virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) OVERRIDE {
CloseConnection("SPDY GOAWAY frame recevied.");
}
virtual void OnHeaders(SpdyStreamId stream_id, bool fin) OVERRIDE {
CloseConnection("SPDY HEADERS frame recevied.");
}
virtual void OnWindowUpdate(SpdyStreamId stream_id,
uint32 delta_window_size) OVERRIDE {
CloseConnection("SPDY WINDOW_UPDATE frame recevied.");
}
virtual void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id) OVERRIDE {
LOG(DFATAL) << "PUSH_PROMISE frame received from a SPDY/3 framer";
CloseConnection("SPDY PUSH_PROMISE frame recevied.");
}
// SpdyFramerDebugVisitorInterface implementation
virtual void OnSendCompressedFrame(SpdyStreamId stream_id,
SpdyFrameType type,
size_t payload_len,
size_t frame_len) OVERRIDE {}
virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id,
SpdyFrameType type,
size_t frame_len) OVERRIDE {
if (stream_->IsConnected()) {
stream_->OnCompressedFrameSize(frame_len);
}
}
private:
void CloseConnection(const string& details) {
if (stream_->IsConnected()) {
stream_->CloseConnectionWithDetails(
QUIC_INVALID_HEADERS_STREAM_DATA, details);
}
}
private:
QuicHeadersStream* stream_;
DISALLOW_COPY_AND_ASSIGN(SpdyFramerVisitor);
};
QuicHeadersStream::QuicHeadersStream(QuicSession* session)
: ReliableQuicStream(kHeadersStreamId, session),
stream_id_(kInvalidStreamId),
fin_(false),
frame_len_(0),
spdy_framer_(SPDY3),
spdy_framer_visitor_(new SpdyFramerVisitor(this)) {
spdy_framer_.set_visitor(spdy_framer_visitor_.get());
spdy_framer_.set_debug_visitor(spdy_framer_visitor_.get());
}
QuicHeadersStream::~QuicHeadersStream() {}
size_t QuicHeadersStream::WriteHeaders(QuicStreamId stream_id,
const SpdyHeaderBlock& headers,
bool fin) {
scoped_ptr<SpdySerializedFrame> frame;
if (session()->is_server()) {
SpdySynReplyIR syn_reply(stream_id);
syn_reply.set_name_value_block(headers);
syn_reply.set_fin(fin);
frame.reset(spdy_framer_.SerializeFrame(syn_reply));
} else {
SpdySynStreamIR syn_stream(stream_id);
syn_stream.set_name_value_block(headers);
syn_stream.set_fin(fin);
frame.reset(spdy_framer_.SerializeFrame(syn_stream));
}
WriteOrBufferData(StringPiece(frame->data(), frame->size()), false);
return frame->size();
}
uint32 QuicHeadersStream::ProcessRawData(const char* data,
uint32 data_len) {
return spdy_framer_.ProcessInput(data, data_len);
}
QuicPriority QuicHeadersStream::EffectivePriority() const { return 0; }
void QuicHeadersStream::OnSynStream(SpdyStreamId stream_id,
SpdyPriority priority,
bool fin) {
if (!session()->is_server()) {
CloseConnectionWithDetails(
QUIC_INVALID_HEADERS_STREAM_DATA,
"SPDY SYN_STREAM frame recevied at the client");
return;
}
DCHECK_EQ(kInvalidStreamId, stream_id_);
stream_id_ = stream_id;
fin_ = fin;
session()->OnStreamHeadersPriority(stream_id, priority);
}
void QuicHeadersStream::OnSynReply(SpdyStreamId stream_id, bool fin) {
if (session()->is_server()) {
CloseConnectionWithDetails(
QUIC_INVALID_HEADERS_STREAM_DATA,
"SPDY SYN_REPLY frame recevied at the server");
return;
}
DCHECK_EQ(kInvalidStreamId, stream_id_);
stream_id_ = stream_id;
fin_ = fin;
}
void QuicHeadersStream::OnControlFrameHeaderData(SpdyStreamId stream_id,
const char* header_data,
size_t len) {
DCHECK_EQ(stream_id_, stream_id);
if (len == 0) {
DCHECK_NE(0u, stream_id_);
DCHECK_NE(0u, frame_len_);
session()->OnStreamHeadersComplete(stream_id_, fin_, frame_len_);
// Reset state for the next frame.
stream_id_ = kInvalidStreamId;
fin_ = false;
frame_len_ = 0;
} else {
session()->OnStreamHeaders(stream_id_, StringPiece(header_data, len));
}
}
void QuicHeadersStream::OnCompressedFrameSize(size_t frame_len) {
DCHECK_EQ(kInvalidStreamId, stream_id_);
DCHECK_EQ(0u, frame_len_);
frame_len_ = frame_len;
}
bool QuicHeadersStream::IsConnected() {
return session()->connection()->connected();
}
} // namespace net
|