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 explicit 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_header_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 void QuicHeadersStream::OnDataAvailable() {
211 iov
.iov_base
= buffer
;
212 iov
.iov_len
= arraysize(buffer
);
213 if (sequencer()->GetReadableRegions(&iov
, 1) != 1) {
214 // No more data to read.
217 if (spdy_framer_
.ProcessInput(static_cast<char*>(iov
.iov_base
),
218 iov
.iov_len
) != iov
.iov_len
) {
219 // Error processing data.
222 sequencer()->MarkConsumed(iov
.iov_len
);
226 QuicPriority
QuicHeadersStream::EffectivePriority() const { return 0; }
228 void QuicHeadersStream::OnSynStream(SpdyStreamId stream_id
,
229 SpdyPriority priority
,
231 if (session()->perspective() == Perspective::IS_CLIENT
) {
232 CloseConnectionWithDetails(
233 QUIC_INVALID_HEADERS_STREAM_DATA
,
234 "SPDY SYN_STREAM frame received at the client");
237 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
238 stream_id_
= stream_id
;
240 spdy_session_
->OnStreamHeadersPriority(stream_id
, priority
);
243 void QuicHeadersStream::OnSynReply(SpdyStreamId stream_id
, bool fin
) {
244 if (session()->perspective() == Perspective::IS_SERVER
) {
245 CloseConnectionWithDetails(
246 QUIC_INVALID_HEADERS_STREAM_DATA
,
247 "SPDY SYN_REPLY frame received at the server");
250 DCHECK_EQ(kInvalidStreamId
, stream_id_
);
251 stream_id_
= stream_id
;
255 void QuicHeadersStream::OnControlFrameHeaderData(SpdyStreamId stream_id
,
256 const char* header_data
,
258 DCHECK_EQ(stream_id_
, stream_id
);
260 DCHECK_NE(0u, stream_id_
);
261 DCHECK_NE(0u, frame_len_
);
262 spdy_session_
->OnStreamHeadersComplete(stream_id_
, fin_
, frame_len_
);
263 // Reset state for the next frame.
264 stream_id_
= kInvalidStreamId
;
268 spdy_session_
->OnStreamHeaders(stream_id_
, StringPiece(header_data
, len
));
272 void QuicHeadersStream::OnCompressedFrameSize(size_t frame_len
) {
273 frame_len_
+= frame_len
;
276 bool QuicHeadersStream::IsConnected() {
277 return session()->connection()->connected();