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 "base/strings/stringprintf.h"
8 #include "net/quic/quic_spdy_session.h"
10 using base::StringPiece
;
17 const QuicStreamId kInvalidStreamId
= 0;
21 // A SpdyFramer visitor which passed SYN_STREAM and SYN_REPLY frames to
22 // the QuicDataStream, and closes the connection if any unexpected frames
24 class QuicHeadersStream::SpdyFramerVisitor
25 : public SpdyFramerVisitorInterface
,
26 public SpdyFramerDebugVisitorInterface
{
28 SpdyFramerVisitor(QuicHeadersStream
* stream
) : stream_(stream
) {}
30 // SpdyFramerVisitorInterface implementation
31 void OnSynStream(SpdyStreamId stream_id
,
32 SpdyStreamId associated_stream_id
,
33 SpdyPriority priority
,
35 bool unidirectional
) override
{
36 CloseConnection("SPDY SYN_STREAM frame received.");
39 void OnSynReply(SpdyStreamId stream_id
, bool fin
) override
{
40 CloseConnection("SPDY SYN_REPLY frame received.");
43 bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
44 const char* header_data
,
45 size_t len
) override
{
46 if (!stream_
->IsConnected()) {
49 stream_
->OnControlFrameHeaderData(stream_id
, header_data
, len
);
53 void OnStreamFrameData(SpdyStreamId stream_id
,
57 if (fin
&& len
== 0) {
58 // The framer invokes OnStreamFrameData with zero-length data and
59 // fin = true after processing a SYN_STREAM or SYN_REPLY frame
60 // that had the fin bit set.
63 CloseConnection("SPDY DATA frame received.");
66 void OnStreamPadding(SpdyStreamId stream_id
, size_t len
) override
{
67 CloseConnection("SPDY frame padding received.");
70 void OnError(SpdyFramer
* framer
) override
{
71 CloseConnection(base::StringPrintf(
72 "SPDY framing error: %s",
73 SpdyFramer::ErrorCodeToString(framer
->error_code())));
76 void OnDataFrameHeader(SpdyStreamId stream_id
,
79 CloseConnection("SPDY DATA frame received.");
82 void OnRstStream(SpdyStreamId stream_id
,
83 SpdyRstStreamStatus status
) override
{
84 CloseConnection("SPDY RST_STREAM frame received.");
87 void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) override
{
88 CloseConnection("SPDY SETTINGS frame received.");
91 void OnSettingsAck() override
{
92 CloseConnection("SPDY SETTINGS frame received.");
95 void OnSettingsEnd() override
{
96 CloseConnection("SPDY SETTINGS frame received.");
99 void OnPing(SpdyPingId unique_id
, bool is_ack
) override
{
100 CloseConnection("SPDY PING frame received.");
103 void OnGoAway(SpdyStreamId last_accepted_stream_id
,
104 SpdyGoAwayStatus status
) override
{
105 CloseConnection("SPDY GOAWAY frame received.");
108 void OnHeaders(SpdyStreamId stream_id
,
110 SpdyPriority priority
,
111 SpdyStreamId parent_stream_id
,
115 if (!stream_
->IsConnected()) {
119 stream_
->OnSynStream(stream_id
, priority
, fin
);
121 stream_
->OnSynReply(stream_id
, fin
);
125 void OnWindowUpdate(SpdyStreamId stream_id
, int delta_window_size
) override
{
126 CloseConnection("SPDY WINDOW_UPDATE frame received.");
129 void OnPushPromise(SpdyStreamId stream_id
,
130 SpdyStreamId promised_stream_id
,
132 CloseConnection("SPDY PUSH_PROMISE frame received.");
135 void OnContinuation(SpdyStreamId stream_id
, bool end
) override
{
138 bool OnUnknownFrame(SpdyStreamId stream_id
, int frame_type
) override
{
139 CloseConnection("Unknown frame type received.");
143 // SpdyFramerDebugVisitorInterface implementation
144 void OnSendCompressedFrame(SpdyStreamId stream_id
,
147 size_t frame_len
) override
{}
149 void OnReceiveCompressedFrame(SpdyStreamId stream_id
,
151 size_t frame_len
) override
{
152 if (stream_
->IsConnected()) {
153 stream_
->OnCompressedFrameSize(frame_len
);
158 void CloseConnection(const string
& details
) {
159 if (stream_
->IsConnected()) {
160 stream_
->CloseConnectionWithDetails(
161 QUIC_INVALID_HEADERS_STREAM_DATA
, details
);
166 QuicHeadersStream
* stream_
;
168 DISALLOW_COPY_AND_ASSIGN(SpdyFramerVisitor
);
171 QuicHeadersStream::QuicHeadersStream(QuicSpdySession
* session
)
172 : ReliableQuicStream(kHeadersStreamId
, session
),
173 spdy_session_(session
),
174 stream_id_(kInvalidStreamId
),
178 spdy_framer_visitor_(new SpdyFramerVisitor(this)) {
179 spdy_framer_
.set_visitor(spdy_framer_visitor_
.get());
180 spdy_framer_
.set_debug_visitor(spdy_framer_visitor_
.get());
181 // The headers stream is exempt from connection level flow control.
182 DisableConnectionFlowControlForThisStream();
185 QuicHeadersStream::~QuicHeadersStream() {}
187 size_t QuicHeadersStream::WriteHeaders(
188 QuicStreamId stream_id
,
189 const SpdyHeaderBlock
& headers
,
191 QuicPriority priority
,
192 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) {
193 SpdyHeadersIR
headers_frame(stream_id
);
194 headers_frame
.set_name_value_block(headers
);
195 headers_frame
.set_fin(fin
);
196 if (session()->perspective() == Perspective::IS_CLIENT
) {
197 headers_frame
.set_has_priority(true);
198 headers_frame
.set_priority(priority
);
200 scoped_ptr
<SpdySerializedFrame
> frame(
201 spdy_framer_
.SerializeFrame(headers_frame
));
202 WriteOrBufferData(StringPiece(frame
->data(), frame
->size()), false,
203 ack_notifier_delegate
);
204 return frame
->size();
207 uint32
QuicHeadersStream::ProcessRawData(const char* data
,
209 return spdy_framer_
.ProcessInput(data
, data_len
);
212 QuicPriority
QuicHeadersStream::EffectivePriority() const { return 0; }
214 void QuicHeadersStream::OnSynStream(SpdyStreamId stream_id
,
215 SpdyPriority priority
,
217 if (session()->perspective() == Perspective::IS_CLIENT
) {
218 CloseConnectionWithDetails(
219 QUIC_INVALID_HEADERS_STREAM_DATA
,
220 "SPDY SYN_STREAM frame received at the client");
223 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
224 stream_id_
= stream_id
;
226 spdy_session_
->OnStreamHeadersPriority(stream_id
, priority
);
229 void QuicHeadersStream::OnSynReply(SpdyStreamId stream_id
, bool fin
) {
230 if (session()->perspective() == Perspective::IS_SERVER
) {
231 CloseConnectionWithDetails(
232 QUIC_INVALID_HEADERS_STREAM_DATA
,
233 "SPDY SYN_REPLY frame received at the server");
236 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
237 stream_id_
= stream_id
;
241 void QuicHeadersStream::OnControlFrameHeaderData(SpdyStreamId stream_id
,
242 const char* header_data
,
244 DCHECK_EQ(stream_id_
, stream_id
);
246 DCHECK_NE(0u, stream_id_
);
247 DCHECK_NE(0u, frame_len_
);
248 spdy_session_
->OnStreamHeadersComplete(stream_id_
, fin_
, frame_len_
);
249 // Reset state for the next frame.
250 stream_id_
= kInvalidStreamId
;
254 spdy_session_
->OnStreamHeaders(stream_id_
, StringPiece(header_data
, len
));
258 void QuicHeadersStream::OnCompressedFrameSize(size_t frame_len
) {
259 frame_len_
+= frame_len
;
262 bool QuicHeadersStream::IsConnected() {
263 return session()->connection()->connected();