1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_headers_stream.h"
7 #include "net/quic/quic_session.h"
9 using base::StringPiece
;
15 const QuicStreamId kInvalidStreamId
= 0;
19 // A SpdyFramer visitor which passed SYN_STREAM and SYN_REPLY frames to
20 // the QuicDataStream, and closes the connection if any unexpected frames
22 class QuicHeadersStream::SpdyFramerVisitor
23 : public SpdyFramerVisitorInterface
,
24 public SpdyFramerDebugVisitorInterface
{
26 explicit SpdyFramerVisitor(QuicHeadersStream
* stream
) : stream_(stream
) {}
28 // SpdyFramerVisitorInterface implementation
29 virtual void OnSynStream(SpdyStreamId stream_id
,
30 SpdyStreamId associated_stream_id
,
31 SpdyPriority priority
,
33 bool unidirectional
) OVERRIDE
{
34 if (!stream_
->IsConnected()) {
38 if (associated_stream_id
!= 0) {
39 CloseConnection("associated_stream_id != 0");
43 if (unidirectional
!= 0) {
44 CloseConnection("unidirectional != 0");
48 stream_
->OnSynStream(stream_id
, priority
, fin
);
51 virtual void OnSynReply(SpdyStreamId stream_id
, bool fin
) OVERRIDE
{
52 if (!stream_
->IsConnected()) {
56 stream_
->OnSynReply(stream_id
, fin
);
59 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
60 const char* header_data
,
61 size_t len
) OVERRIDE
{
62 if (!stream_
->IsConnected()) {
65 stream_
->OnControlFrameHeaderData(stream_id
, header_data
, len
);
69 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
73 if (fin
&& len
== 0) {
74 // The framer invokes OnStreamFrameData with zero-length data and
75 // fin = true after processing a SYN_STREAM or SYN_REPLY frame
76 // that had the fin bit set.
79 CloseConnection("SPDY DATA frame received.");
82 virtual void OnError(SpdyFramer
* framer
) OVERRIDE
{
83 CloseConnection("SPDY framing error.");
86 virtual void OnDataFrameHeader(SpdyStreamId stream_id
,
89 CloseConnection("SPDY DATA frame received.");
92 virtual void OnRstStream(SpdyStreamId stream_id
,
93 SpdyRstStreamStatus status
) OVERRIDE
{
94 CloseConnection("SPDY RST_STREAM frame received.");
97 virtual void OnSetting(SpdySettingsIds id
,
99 uint32 value
) OVERRIDE
{
100 CloseConnection("SPDY SETTINGS frame received.");
103 virtual void OnSettingsAck() OVERRIDE
{
104 CloseConnection("SPDY SETTINGS frame received.");
107 virtual void OnSettingsEnd() OVERRIDE
{
108 CloseConnection("SPDY SETTINGS frame received.");
111 virtual void OnPing(SpdyPingId unique_id
, bool is_ack
) OVERRIDE
{
112 CloseConnection("SPDY PING frame received.");
115 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id
,
116 SpdyGoAwayStatus status
) OVERRIDE
{
117 CloseConnection("SPDY GOAWAY frame received.");
120 virtual void OnHeaders(SpdyStreamId stream_id
, bool fin
, bool end
) OVERRIDE
{
121 CloseConnection("SPDY HEADERS frame received.");
124 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
125 uint32 delta_window_size
) OVERRIDE
{
126 CloseConnection("SPDY WINDOW_UPDATE frame received.");
129 virtual void OnPushPromise(SpdyStreamId stream_id
,
130 SpdyStreamId promised_stream_id
,
132 LOG(DFATAL
) << "PUSH_PROMISE frame received from a SPDY/3 framer";
133 CloseConnection("SPDY PUSH_PROMISE frame received.");
136 virtual void OnContinuation(SpdyStreamId stream_id
, bool end
) OVERRIDE
{
137 CloseConnection("SPDY CONTINUATION frame received.");
140 // SpdyFramerDebugVisitorInterface implementation
141 virtual void OnSendCompressedFrame(SpdyStreamId stream_id
,
144 size_t frame_len
) OVERRIDE
{}
146 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id
,
148 size_t frame_len
) OVERRIDE
{
149 if (stream_
->IsConnected()) {
150 stream_
->OnCompressedFrameSize(frame_len
);
155 void CloseConnection(const string
& details
) {
156 if (stream_
->IsConnected()) {
157 stream_
->CloseConnectionWithDetails(
158 QUIC_INVALID_HEADERS_STREAM_DATA
, details
);
163 QuicHeadersStream
* stream_
;
165 DISALLOW_COPY_AND_ASSIGN(SpdyFramerVisitor
);
168 QuicHeadersStream::QuicHeadersStream(QuicSession
* session
)
169 : ReliableQuicStream(kHeadersStreamId
, session
),
170 stream_id_(kInvalidStreamId
),
174 spdy_framer_visitor_(new SpdyFramerVisitor(this)) {
175 spdy_framer_
.set_visitor(spdy_framer_visitor_
.get());
176 spdy_framer_
.set_debug_visitor(spdy_framer_visitor_
.get());
177 if (version() <= QUIC_VERSION_20
) {
178 // Prior to QUIC_VERSION_21 the headers stream is not subject to any flow
180 DisableFlowControl();
182 // The headers stream is exempt from connection level flow control.
183 DisableConnectionFlowControlForThisStream();
186 QuicHeadersStream::~QuicHeadersStream() {}
188 size_t QuicHeadersStream::WriteHeaders(
189 QuicStreamId stream_id
,
190 const SpdyHeaderBlock
& headers
,
192 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) {
193 scoped_ptr
<SpdySerializedFrame
> frame
;
194 if (session()->is_server()) {
195 SpdySynReplyIR
syn_reply(stream_id
);
196 syn_reply
.set_name_value_block(headers
);
197 syn_reply
.set_fin(fin
);
198 frame
.reset(spdy_framer_
.SerializeFrame(syn_reply
));
200 SpdySynStreamIR
syn_stream(stream_id
);
201 syn_stream
.set_name_value_block(headers
);
202 syn_stream
.set_fin(fin
);
203 frame
.reset(spdy_framer_
.SerializeFrame(syn_stream
));
205 WriteOrBufferData(StringPiece(frame
->data(), frame
->size()), false,
206 ack_notifier_delegate
);
207 return frame
->size();
210 uint32
QuicHeadersStream::ProcessRawData(const char* data
,
212 return spdy_framer_
.ProcessInput(data
, data_len
);
215 QuicPriority
QuicHeadersStream::EffectivePriority() const { return 0; }
217 void QuicHeadersStream::OnSynStream(SpdyStreamId stream_id
,
218 SpdyPriority priority
,
220 if (!session()->is_server()) {
221 CloseConnectionWithDetails(
222 QUIC_INVALID_HEADERS_STREAM_DATA
,
223 "SPDY SYN_STREAM frame received at the client");
226 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
227 stream_id_
= stream_id
;
229 session()->OnStreamHeadersPriority(stream_id
, priority
);
232 void QuicHeadersStream::OnSynReply(SpdyStreamId stream_id
, bool fin
) {
233 if (session()->is_server()) {
234 CloseConnectionWithDetails(
235 QUIC_INVALID_HEADERS_STREAM_DATA
,
236 "SPDY SYN_REPLY frame received at the server");
239 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
240 stream_id_
= stream_id
;
244 void QuicHeadersStream::OnControlFrameHeaderData(SpdyStreamId stream_id
,
245 const char* header_data
,
247 DCHECK_EQ(stream_id_
, stream_id
);
249 DCHECK_NE(0u, stream_id_
);
250 DCHECK_NE(0u, frame_len_
);
251 session()->OnStreamHeadersComplete(stream_id_
, fin_
, frame_len_
);
252 // Reset state for the next frame.
253 stream_id_
= kInvalidStreamId
;
257 session()->OnStreamHeaders(stream_id_
, StringPiece(header_data
, len
));
261 void QuicHeadersStream::OnCompressedFrameSize(size_t frame_len
) {
262 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
263 DCHECK_EQ(0u, frame_len_
);
264 frame_len_
= frame_len
;
267 bool QuicHeadersStream::IsConnected() {
268 return session()->connection()->connected();