1 // Copyright (c) 2012 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 #ifndef NET_SPDY_SPDY_FRAMER_H_
6 #define NET_SPDY_SPDY_FRAMER_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string_piece.h"
16 #include "base/sys_byteorder.h"
17 #include "net/base/net_export.h"
18 #include "net/spdy/hpack/hpack_decoder.h"
19 #include "net/spdy/hpack/hpack_encoder.h"
20 #include "net/spdy/spdy_alt_svc_wire_format.h"
21 #include "net/spdy/spdy_header_block.h"
22 #include "net/spdy/spdy_protocol.h"
24 // TODO(akalin): Remove support for CREDENTIAL frames.
26 typedef struct z_stream_s z_stream
; // Forward declaration for zlib.
30 class HttpProxyClientSocketPoolTest
;
31 class HttpNetworkLayer
;
32 class HttpNetworkTransactionTest
;
33 class SpdyHttpStreamTest
;
34 class SpdyNetworkTransactionTest
;
35 class SpdyProxyClientSocketTest
;
36 class SpdySessionTest
;
40 class SpdyFrameBuilder
;
44 class TestSpdyVisitor
;
49 // A datastructure for holding the ID and flag fields for SETTINGS.
50 // Conveniently handles converstion to/from wire format.
51 class NET_EXPORT_PRIVATE SettingsFlagsAndId
{
53 static SettingsFlagsAndId
FromWireFormat(SpdyMajorVersion version
,
56 SettingsFlagsAndId() : flags_(0), id_(0) {}
58 // TODO(hkhalil): restrict to enums instead of free-form ints.
59 SettingsFlagsAndId(uint8 flags
, uint32 id
);
61 uint32
GetWireFormat(SpdyMajorVersion version
) const;
63 uint32
id() const { return id_
; }
64 uint8
flags() const { return flags_
; }
67 static void ConvertFlagsAndIdForSpdy2(uint32
* val
);
73 // SettingsMap has unique (flags, value) pair for given SpdySettingsIds ID.
74 typedef std::pair
<SpdySettingsFlags
, uint32
> SettingsFlagsAndValue
;
75 typedef std::map
<SpdySettingsIds
, SettingsFlagsAndValue
> SettingsMap
;
77 // SpdyFramerVisitorInterface is a set of callbacks for the SpdyFramer.
78 // Implement this interface to receive event callbacks as frames are
79 // decoded from the framer.
81 // Control frames that contain SPDY header blocks (SYN_STREAM, SYN_REPLY,
82 // HEADER, and PUSH_PROMISE) are processed in fashion that allows the
83 // decompressed header block to be delivered in chunks to the visitor.
84 // The following steps are followed:
85 // 1. OnSynStream, OnSynReply, OnHeaders, or OnPushPromise is called.
86 // 2. Repeated: OnControlFrameHeaderData is called with chunks of the
87 // decompressed header block. In each call the len parameter is greater
89 // 3. OnControlFrameHeaderData is called with len set to zero, indicating
90 // that the full header block has been delivered for the control frame.
91 // During step 2 the visitor may return false, indicating that the chunk of
92 // header data could not be handled by the visitor (typically this indicates
93 // resource exhaustion). If this occurs the framer will discontinue
94 // delivering chunks to the visitor, set a SPDY_CONTROL_PAYLOAD_TOO_LARGE
95 // error, and clean up appropriately. Note that this will cause the header
96 // decompressor to lose synchronization with the sender's header compressor,
97 // making the SPDY session unusable for future work. The visitor's OnError
98 // function should deal with this condition by closing the SPDY connection.
99 class NET_EXPORT_PRIVATE SpdyFramerVisitorInterface
{
101 virtual ~SpdyFramerVisitorInterface() {}
103 // Called if an error is detected in the SpdyFrame protocol.
104 virtual void OnError(SpdyFramer
* framer
) = 0;
106 // Called when a data frame header is received. The frame's data
107 // payload will be provided via subsequent calls to
108 // OnStreamFrameData().
109 virtual void OnDataFrameHeader(SpdyStreamId stream_id
,
113 // Called when data is received.
114 // |stream_id| The stream receiving data.
115 // |data| A buffer containing the data received.
116 // |len| The length of the data buffer.
117 // When the other side has finished sending data on this stream,
118 // this method will be called with a zero-length buffer.
119 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
124 // Called when padding is received (padding length field or padding octets).
125 // |stream_id| The stream receiving data.
126 // |len| The number of padding octets.
127 virtual void OnStreamPadding(SpdyStreamId stream_id
, size_t len
) = 0;
129 // Called when a chunk of header data is available. This is called
130 // after OnSynStream, OnSynReply, OnHeaders(), or OnPushPromise.
131 // |stream_id| The stream receiving the header data.
132 // |header_data| A buffer containing the header data chunk received.
133 // |len| The length of the header data buffer. A length of zero indicates
134 // that the header data block has been completely sent.
135 // When this function returns true the visitor indicates that it accepted
136 // all of the data. Returning false indicates that that an unrecoverable
137 // error has occurred, such as bad header data or resource exhaustion.
138 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
139 const char* header_data
,
142 // Called when a SYN_STREAM frame is received.
143 // Note that header block data is not included. See
144 // OnControlFrameHeaderData().
145 virtual void OnSynStream(SpdyStreamId stream_id
,
146 SpdyStreamId associated_stream_id
,
147 SpdyPriority priority
,
149 bool unidirectional
) = 0;
151 // Called when a SYN_REPLY frame is received.
152 // Note that header block data is not included. See
153 // OnControlFrameHeaderData().
154 virtual void OnSynReply(SpdyStreamId stream_id
, bool fin
) = 0;
156 // Called when a RST_STREAM frame has been parsed.
157 virtual void OnRstStream(SpdyStreamId stream_id
,
158 SpdyRstStreamStatus status
) = 0;
160 // Called when a SETTINGS frame is received.
161 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
162 virtual void OnSettings(bool clear_persisted
) {}
164 // Called when a complete setting within a SETTINGS frame has been parsed and
166 virtual void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) = 0;
168 // Called when a SETTINGS frame is received with the ACK flag set.
169 virtual void OnSettingsAck() {}
171 // Called before and after parsing SETTINGS id and value tuples.
172 virtual void OnSettingsEnd() = 0;
174 // Called when a PING frame has been parsed.
175 virtual void OnPing(SpdyPingId unique_id
, bool is_ack
) = 0;
177 // Called when a GOAWAY frame has been parsed.
178 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id
,
179 SpdyGoAwayStatus status
) = 0;
181 // Called when a HEADERS frame is received.
182 // Note that header block data is not included. See
183 // OnControlFrameHeaderData().
184 // |stream_id| The stream receiving the header.
185 // |has_priority| Whether or not the headers frame included a priority value,
186 // and, if protocol version >= HTTP2, stream dependency info.
187 // |priority| If |has_priority| is true and protocol version > SPDY3,
188 // priority value for the receiving stream, else 0.
189 // |parent_stream_id| If |has_priority| is true and protocol
190 // version >= HTTP2, the parent stream of the receiving stream, else 0.
191 // |exclusive| If |has_priority| is true and protocol
192 // version >= HTTP2, the exclusivity of dependence on the parent stream,
194 // |fin| Whether FIN flag is set in frame headers.
195 // |end| False if HEADERs frame is to be followed by a CONTINUATION frame,
197 virtual void OnHeaders(SpdyStreamId stream_id
,
199 SpdyPriority priority
,
200 SpdyStreamId parent_stream_id
,
205 // Called when a WINDOW_UPDATE frame has been parsed.
206 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
207 int delta_window_size
) = 0;
209 // Called when a goaway frame opaque data is available.
210 // |goaway_data| A buffer containing the opaque GOAWAY data chunk received.
211 // |len| The length of the header data buffer. A length of zero indicates
212 // that the header data block has been completely sent.
213 // When this function returns true the visitor indicates that it accepted
214 // all of the data. Returning false indicates that that an error has
215 // occurred while processing the data. Default implementation returns true.
216 virtual bool OnGoAwayFrameData(const char* goaway_data
, size_t len
);
218 // Called when rst_stream frame opaque data is available.
219 // |rst_stream_data| A buffer containing the opaque RST_STREAM
220 // data chunk received.
221 // |len| The length of the header data buffer. A length of zero indicates
222 // that the opaque data has been completely sent.
223 // When this function returns true the visitor indicates that it accepted
224 // all of the data. Returning false indicates that that an error has
225 // occurred while processing the data. Default implementation returns true.
226 virtual bool OnRstStreamFrameData(const char* rst_stream_data
, size_t len
);
228 // Called when a BLOCKED frame has been parsed.
229 virtual void OnBlocked(SpdyStreamId stream_id
) {}
231 // Called when a PUSH_PROMISE frame is received.
232 // Note that header block data is not included. See
233 // OnControlFrameHeaderData().
234 virtual void OnPushPromise(SpdyStreamId stream_id
,
235 SpdyStreamId promised_stream_id
,
238 // Called when a CONTINUATION frame is received.
239 // Note that header block data is not included. See
240 // OnControlFrameHeaderData().
241 virtual void OnContinuation(SpdyStreamId stream_id
, bool end
) = 0;
243 // Called when an ALTSVC frame has been parsed.
244 virtual void OnAltSvc(
245 SpdyStreamId stream_id
,
246 base::StringPiece origin
,
247 const SpdyAltSvcWireFormat::AlternativeServiceVector
& altsvc_vector
) {}
249 // Called when a PRIORITY frame is received.
250 virtual void OnPriority(SpdyStreamId stream_id
,
251 SpdyStreamId parent_stream_id
,
255 // Called when a frame type we don't recognize is received.
256 // Return true if this appears to be a valid extension frame, false otherwise.
257 // We distinguish between extension frames and nonsense by checking
258 // whether the stream id is valid.
259 virtual bool OnUnknownFrame(SpdyStreamId stream_id
, int frame_type
) = 0;
262 // Optionally, and in addition to SpdyFramerVisitorInterface, a class supporting
263 // SpdyFramerDebugVisitorInterface may be used in conjunction with SpdyFramer in
264 // order to extract debug/internal information about the SpdyFramer as it
267 // Most SPDY implementations need not bother with this interface at all.
268 class NET_EXPORT_PRIVATE SpdyFramerDebugVisitorInterface
{
270 virtual ~SpdyFramerDebugVisitorInterface() {}
272 // Called after compressing a frame with a payload of
273 // a list of name-value pairs.
274 // |payload_len| is the uncompressed payload size.
275 // |frame_len| is the compressed frame size.
276 virtual void OnSendCompressedFrame(SpdyStreamId stream_id
,
281 // Called when a frame containing a compressed payload of
282 // name-value pairs is received.
283 // |frame_len| is the compressed frame size.
284 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id
,
289 class NET_EXPORT_PRIVATE SpdyFramer
{
292 // TODO(mbelshe): Can we move these into the implementation
293 // and avoid exposing through the header. (Needed for test)
296 SPDY_READY_FOR_FRAME
, // Framer is ready for reading the next frame.
297 SPDY_FRAME_COMPLETE
, // Framer has finished reading a frame, need to reset.
298 SPDY_READING_COMMON_HEADER
,
299 SPDY_CONTROL_FRAME_PAYLOAD
,
300 SPDY_READ_DATA_FRAME_PADDING_LENGTH
,
301 SPDY_CONSUME_PADDING
,
302 SPDY_IGNORE_REMAINING_PAYLOAD
,
303 SPDY_FORWARD_STREAM_FRAME
,
304 SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK
,
305 SPDY_CONTROL_FRAME_HEADER_BLOCK
,
306 SPDY_GOAWAY_FRAME_PAYLOAD
,
307 SPDY_RST_STREAM_FRAME_PAYLOAD
,
308 SPDY_SETTINGS_FRAME_PAYLOAD
,
309 SPDY_ALTSVC_FRAME_PAYLOAD
,
315 SPDY_INVALID_CONTROL_FRAME
, // Control frame is mal-formatted.
316 SPDY_CONTROL_PAYLOAD_TOO_LARGE
, // Control frame payload was too large.
317 SPDY_ZLIB_INIT_FAILURE
, // The Zlib library could not initialize.
318 SPDY_UNSUPPORTED_VERSION
, // Control frame has unsupported version.
319 SPDY_DECOMPRESS_FAILURE
, // There was an error decompressing.
320 SPDY_COMPRESS_FAILURE
, // There was an error compressing.
321 SPDY_GOAWAY_FRAME_CORRUPT
, // GOAWAY frame could not be parsed.
322 SPDY_RST_STREAM_FRAME_CORRUPT
, // RST_STREAM frame could not be parsed.
323 SPDY_INVALID_DATA_FRAME_FLAGS
, // Data frame has invalid flags.
324 SPDY_INVALID_CONTROL_FRAME_FLAGS
, // Control frame has invalid flags.
325 SPDY_UNEXPECTED_FRAME
, // Frame received out of order.
327 LAST_ERROR
, // Must be the last entry in the enum.
330 // Constant for invalid (or unknown) stream IDs.
331 static const SpdyStreamId kInvalidStream
;
333 // The maximum size of header data chunks delivered to the framer visitor
334 // through OnControlFrameHeaderData. (It is exposed here for unit test
336 static const size_t kHeaderDataChunkMaxSize
;
338 // Serializes a SpdyHeaderBlock.
339 static void WriteHeaderBlock(SpdyFrameBuilder
* frame
,
340 const SpdyMajorVersion spdy_version
,
341 const SpdyHeaderBlock
* headers
);
343 // Retrieve serialized length of SpdyHeaderBlock.
344 // TODO(hkhalil): Remove, or move to quic code.
345 static size_t GetSerializedLength(
346 const SpdyMajorVersion spdy_version
,
347 const SpdyHeaderBlock
* headers
);
349 // Create a new Framer, provided a SPDY version.
350 explicit SpdyFramer(SpdyMajorVersion version
);
351 virtual ~SpdyFramer();
353 // Set callbacks to be called from the framer. A visitor must be set, or
354 // else the framer will likely crash. It is acceptable for the visitor
355 // to do nothing. If this is called multiple times, only the last visitor
357 void set_visitor(SpdyFramerVisitorInterface
* visitor
) {
361 // Set debug callbacks to be called from the framer. The debug visitor is
362 // completely optional and need not be set in order for normal operation.
363 // If this is called multiple times, only the last visitor will be used.
364 void set_debug_visitor(SpdyFramerDebugVisitorInterface
* debug_visitor
) {
365 debug_visitor_
= debug_visitor
;
368 // Sets whether or not ProcessInput returns after finishing a frame, or
369 // continues processing additional frames. Normally ProcessInput processes
370 // all input, but this method enables the caller (and visitor) to work with
371 // a single frame at a time (or that portion of the frame which is provided
372 // as input). Reset() does not change the value of this flag.
373 void set_process_single_input_frame(bool v
) {
374 process_single_input_frame_
= v
;
377 // Pass data into the framer for parsing.
378 // Returns the number of bytes consumed. It is safe to pass more bytes in
379 // than may be consumed.
380 size_t ProcessInput(const char* data
, size_t len
);
382 // Resets the framer state after a frame has been successfully decoded.
383 // TODO(mbelshe): can we make this private?
386 // Check the state of the framer.
387 SpdyError
error_code() const { return error_code_
; }
388 SpdyState
state() const { return state_
; }
389 bool HasError() const { return state_
== SPDY_ERROR
; }
391 // Given a buffer containing a decompressed header block in SPDY
392 // serialized format, parse out a SpdyHeaderBlock, putting the results
393 // in the given header block.
394 // Returns number of bytes consumed if successfully parsed, 0 otherwise.
395 size_t ParseHeaderBlockInBuffer(const char* header_data
,
396 size_t header_length
,
397 SpdyHeaderBlock
* block
) const;
399 // Serialize a data frame.
400 SpdySerializedFrame
* SerializeData(const SpdyDataIR
& data
) const;
401 // Serializes the data frame header and optionally padding length fields,
402 // excluding actual data payload and padding.
403 SpdySerializedFrame
* SerializeDataFrameHeaderWithPaddingLengthField(
404 const SpdyDataIR
& data
) const;
406 // Serializes a SYN_STREAM frame.
407 SpdySerializedFrame
* SerializeSynStream(const SpdySynStreamIR
& syn_stream
);
409 // Serialize a SYN_REPLY SpdyFrame.
410 SpdySerializedFrame
* SerializeSynReply(const SpdySynReplyIR
& syn_reply
);
412 SpdySerializedFrame
* SerializeRstStream(
413 const SpdyRstStreamIR
& rst_stream
) const;
415 // Serializes a SETTINGS frame. The SETTINGS frame is
416 // used to communicate name/value pairs relevant to the communication channel.
417 SpdySerializedFrame
* SerializeSettings(const SpdySettingsIR
& settings
) const;
419 // Serializes a PING frame. The unique_id is used to
420 // identify the ping request/response.
421 SpdySerializedFrame
* SerializePing(const SpdyPingIR
& ping
) const;
423 // Serializes a GOAWAY frame. The GOAWAY frame is used
424 // prior to the shutting down of the TCP connection, and includes the
425 // stream_id of the last stream the sender of the frame is willing to process
427 SpdySerializedFrame
* SerializeGoAway(const SpdyGoAwayIR
& goaway
) const;
429 // Serializes a HEADERS frame. The HEADERS frame is used
430 // for sending additional headers outside of a SYN_STREAM/SYN_REPLY.
431 SpdySerializedFrame
* SerializeHeaders(const SpdyHeadersIR
& headers
);
433 // Serializes a WINDOW_UPDATE frame. The WINDOW_UPDATE
434 // frame is used to implement per stream flow control in SPDY.
435 SpdySerializedFrame
* SerializeWindowUpdate(
436 const SpdyWindowUpdateIR
& window_update
) const;
438 // Serializes a BLOCKED frame. The BLOCKED frame is used to
439 // indicate to the remote endpoint that this endpoint believes itself to be
440 // flow-control blocked but otherwise ready to send data. The BLOCKED frame
441 // is purely advisory and optional.
442 SpdySerializedFrame
* SerializeBlocked(const SpdyBlockedIR
& blocked
) const;
444 // Serializes a PUSH_PROMISE frame. The PUSH_PROMISE frame is used
445 // to inform the client that it will be receiving an additional stream
446 // in response to the original request. The frame includes synthesized
447 // headers to explain the upcoming data.
448 SpdySerializedFrame
* SerializePushPromise(
449 const SpdyPushPromiseIR
& push_promise
);
451 // Serializes a CONTINUATION frame. The CONTINUATION frame is used
452 // to continue a sequence of header block fragments.
453 // TODO(jgraettinger): This implementation is incorrect. The continuation
454 // frame continues a previously-begun HPACK encoding; it doesn't begin a
455 // new one. Figure out whether it makes sense to keep SerializeContinuation().
456 SpdySerializedFrame
* SerializeContinuation(
457 const SpdyContinuationIR
& continuation
);
459 // Serializes an ALTSVC frame. The ALTSVC frame advertises the
460 // availability of an alternative service to the client.
461 SpdySerializedFrame
* SerializeAltSvc(const SpdyAltSvcIR
& altsvc
);
463 // Serializes a PRIORITY frame. The PRIORITY frame advises a change in
464 // the relative priority of the given stream.
465 SpdySerializedFrame
* SerializePriority(const SpdyPriorityIR
& priority
) const;
467 // Serialize a frame of unknown type.
468 SpdySerializedFrame
* SerializeFrame(const SpdyFrameIR
& frame
);
470 // NOTES about frame compression.
471 // We want spdy to compress headers across the entire session. As long as
472 // the session is over TCP, frames are sent serially. The client & server
473 // can each compress frames in the same order and then compress them in that
474 // order, and the remote can do the reverse. However, we ultimately want
475 // the creation of frames to be less sensitive to order so that they can be
476 // placed over a UDP based protocol and yet still benefit from some
477 // compression. We don't know of any good compression protocol which does
478 // not build its state in a serial (stream based) manner.... For now, we're
479 // using zlib anyway.
481 // Compresses a SpdyFrame.
482 // On success, returns a new SpdyFrame with the payload compressed.
483 // Compression state is maintained as part of the SpdyFramer.
484 // Returned frame must be freed with "delete".
485 // On failure, returns NULL.
486 SpdyFrame
* CompressFrame(const SpdyFrame
& frame
);
488 // For ease of testing and experimentation we can tweak compression on/off.
489 void set_enable_compression(bool value
) {
490 enable_compression_
= value
;
493 // Used only in log messages.
494 void set_display_protocol(const std::string
& protocol
) {
495 display_protocol_
= protocol
;
498 // Returns the (minimum) size of frames (sans variable-length portions).
499 size_t GetDataFrameMinimumSize() const;
500 size_t GetControlFrameHeaderSize() const;
501 size_t GetSynStreamMinimumSize() const;
502 size_t GetSynReplyMinimumSize() const;
503 size_t GetRstStreamMinimumSize() const;
504 size_t GetSettingsMinimumSize() const;
505 size_t GetPingSize() const;
506 size_t GetGoAwayMinimumSize() const;
507 size_t GetHeadersMinimumSize() const;
508 size_t GetWindowUpdateSize() const;
509 size_t GetBlockedSize() const;
510 size_t GetPushPromiseMinimumSize() const;
511 size_t GetContinuationMinimumSize() const;
512 size_t GetAltSvcMinimumSize() const;
513 size_t GetPrioritySize() const;
515 // Returns the minimum size a frame can be (data or control).
516 size_t GetFrameMinimumSize() const;
518 // Returns the maximum size a frame can be (data or control).
519 size_t GetFrameMaximumSize() const;
521 // Returns the maximum payload size of a DATA frame.
522 size_t GetDataFrameMaximumPayload() const;
524 // Returns the prefix length for the given frame type.
525 size_t GetPrefixLength(SpdyFrameType type
) const;
528 static const char* StateToString(int state
);
529 static const char* ErrorCodeToString(int error_code
);
530 static const char* StatusCodeToString(int status_code
);
531 static const char* FrameTypeToString(SpdyFrameType type
);
533 SpdyMajorVersion
protocol_version() const { return protocol_version_
; }
535 bool probable_http_response() const { return probable_http_response_
; }
537 SpdyPriority
GetLowestPriority() const {
538 return protocol_version_
< SPDY3
? 3 : 7;
541 SpdyPriority
GetHighestPriority() const { return 0; }
543 // Interpolates SpdyPriority values into SPDY4/HTTP2 priority weights,
545 static uint8
MapPriorityToWeight(SpdyPriority priority
);
546 static SpdyPriority
MapWeightToPriority(uint8 weight
);
548 // Deliver the given control frame's compressed headers block to the visitor
549 // in decompressed form, in chunks. Returns true if the visitor has
550 // accepted all of the chunks.
551 bool IncrementallyDecompressControlFrameHeaderData(
552 SpdyStreamId stream_id
,
556 // Updates the maximum size of header compression table.
557 void UpdateHeaderTableSizeSetting(uint32 value
);
559 // Returns bound of header compression table size.
560 size_t header_table_size_bound() const;
563 friend class HttpNetworkLayer
; // This is temporary for the server.
564 friend class HttpNetworkTransactionTest
;
565 friend class HttpProxyClientSocketPoolTest
;
566 friend class SpdyHttpStreamTest
;
567 friend class SpdyNetworkTransactionTest
;
568 friend class SpdyProxyClientSocketTest
;
569 friend class SpdySessionTest
;
570 friend class SpdyStreamTest
;
571 friend class test::TestSpdyVisitor
;
572 friend class test::SpdyFramerPeer
;
577 explicit CharBuffer(size_t capacity
);
580 void CopyFrom(const char* data
, size_t size
);
583 const char* data() const { return buffer_
.get(); }
584 size_t len() const { return len_
; }
587 scoped_ptr
<char[]> buffer_
;
592 // Scratch space necessary for processing SETTINGS frames.
593 struct SpdySettingsScratch
{
594 SpdySettingsScratch();
597 // Buffer contains up to one complete key/value pair.
600 // The ID of the last setting that was processed in the current SETTINGS
601 // frame. Used for detecting out-of-order or duplicate keys within a
602 // settings frame. Set to -1 before first key/value pair is processed.
606 // Internal breakouts from ProcessInput. Each returns the number of bytes
607 // consumed from the data.
608 size_t ProcessCommonHeader(const char* data
, size_t len
);
609 size_t ProcessControlFramePayload(const char* data
, size_t len
);
610 size_t ProcessControlFrameBeforeHeaderBlock(const char* data
, size_t len
);
611 // HPACK data is re-encoded as SPDY3 and re-entrantly delivered through
612 // |ProcessControlFrameHeaderBlock()|. |is_hpack_header_block| controls
613 // whether data is treated as HPACK- vs SPDY3-encoded.
614 size_t ProcessControlFrameHeaderBlock(const char* data
,
616 bool is_hpack_header_block
);
617 size_t ProcessDataFramePaddingLength(const char* data
, size_t len
);
618 size_t ProcessFramePadding(const char* data
, size_t len
);
619 size_t ProcessDataFramePayload(const char* data
, size_t len
);
620 size_t ProcessGoAwayFramePayload(const char* data
, size_t len
);
621 size_t ProcessRstStreamFramePayload(const char* data
, size_t len
);
622 size_t ProcessSettingsFramePayload(const char* data
, size_t len
);
623 size_t ProcessAltSvcFramePayload(const char* data
, size_t len
);
624 size_t ProcessIgnoredControlFramePayload(/*const char* data,*/ size_t len
);
626 // TODO(jgraettinger): To be removed with migration to
627 // SpdyHeadersHandlerInterface. Serializes the last-processed
628 // header block of |hpack_decoder_| as a SPDY3 format block, and
629 // delivers it to the visitor via reentrant call to
630 // ProcessControlFrameHeaderBlock(). |compressed_len| is used for
631 // logging compression percentage.
632 void DeliverHpackBlockAsSpdy3Block(size_t compressed_len
);
634 // Helpers for above internal breakouts from ProcessInput.
635 void ProcessControlFrameHeader(int control_frame_type_field
);
636 // Always passed exactly 1 setting's worth of data.
637 bool ProcessSetting(const char* data
);
639 // Retrieve serialized length of SpdyHeaderBlock. If compression is enabled, a
640 // maximum estimate is returned.
641 size_t GetSerializedLength(const SpdyHeaderBlock
& headers
);
643 // Get (and lazily initialize) the ZLib state.
644 z_stream
* GetHeaderCompressor();
645 z_stream
* GetHeaderDecompressor();
647 // Get (and lazily initialize) the HPACK state.
648 HpackEncoder
* GetHpackEncoder();
649 HpackDecoder
* GetHpackDecoder();
651 size_t GetNumberRequiredContinuationFrames(size_t size
);
653 void WritePayloadWithContinuation(SpdyFrameBuilder
* builder
,
654 const std::string
& hpack_encoding
,
655 SpdyStreamId stream_id
,
657 int padding_payload_len
);
659 // Deliver the given control frame's uncompressed headers block to the
660 // visitor in chunks. Returns true if the visitor has accepted all of the
662 bool IncrementallyDeliverControlFrameHeaderData(SpdyStreamId stream_id
,
666 // Utility to copy the given data block to the current frame buffer, up
667 // to the given maximum number of bytes, and update the buffer
668 // data (pointer and length). Returns the number of bytes
670 // *data is advanced the number of bytes read.
671 // *len is reduced by the number of bytes read.
672 size_t UpdateCurrentFrameBuffer(const char** data
, size_t* len
,
675 void WriteHeaderBlockToZ(const SpdyHeaderBlock
* headers
,
676 z_stream
* out
) const;
678 void SerializeHeaderBlockWithoutCompression(
679 SpdyFrameBuilder
* builder
,
680 const SpdyHeaderBlock
& header_block
) const;
682 // Compresses automatically according to enable_compression_.
683 void SerializeHeaderBlock(SpdyFrameBuilder
* builder
,
684 const SpdyFrameWithHeaderBlockIR
& frame
);
686 // Set the error code and moves the framer into the error state.
687 void set_error(SpdyError error
);
689 // The size of the control frame buffer.
690 // Since this is only used for control frame headers, the maximum control
691 // frame header size (SYN_STREAM) is sufficient; all remaining control
692 // frame data is streamed to the visitor.
693 static const size_t kControlFrameBufferSize
;
695 // The maximum size of the control frames that we support.
696 // This limit is arbitrary. We can enforce it here or at the application
697 // layer. We chose the framing layer, but this can be changed (or removed)
698 // if necessary later down the line.
699 static const size_t kMaxControlFrameSize
;
702 SpdyState previous_state_
;
703 SpdyError error_code_
;
705 // Note that for DATA frame, remaining_data_length_ is sum of lengths of
706 // frame header, padding length field (optional), data payload (optional) and
707 // padding payload (optional).
708 size_t remaining_data_length_
;
710 // The length (in bytes) of the padding payload to be processed.
711 size_t remaining_padding_payload_length_
;
713 // The number of bytes remaining to read from the current control frame's
714 // headers. Note that header data blocks (for control types that have them)
715 // are part of the frame's payload, and not the frame's headers.
716 size_t remaining_control_header_
;
718 CharBuffer current_frame_buffer_
;
720 // The type of the frame currently being read.
721 SpdyFrameType current_frame_type_
;
723 // The total length of the frame currently being read, including frame header.
724 uint32 current_frame_length_
;
726 // The stream ID field of the frame currently being read, if applicable.
727 SpdyStreamId current_frame_stream_id_
;
729 // Set this to the current stream when we receive a HEADERS, PUSH_PROMISE, or
730 // CONTINUATION frame without the END_HEADERS(0x4) bit set. These frames must
731 // be followed by a CONTINUATION frame, or else we throw a PROTOCOL_ERROR.
732 // A value of 0 indicates that we are not expecting a CONTINUATION frame.
733 SpdyStreamId expect_continuation_
;
735 // Scratch space for handling SETTINGS frames.
736 // TODO(hkhalil): Unify memory for this scratch space with
737 // current_frame_buffer_.
738 SpdySettingsScratch settings_scratch_
;
740 scoped_ptr
<CharBuffer
> altsvc_scratch_
;
742 // SPDY header compressors.
743 scoped_ptr
<z_stream
> header_compressor_
;
744 scoped_ptr
<z_stream
> header_decompressor_
;
746 scoped_ptr
<HpackEncoder
> hpack_encoder_
;
747 scoped_ptr
<HpackDecoder
> hpack_decoder_
;
749 SpdyFramerVisitorInterface
* visitor_
;
750 SpdyFramerDebugVisitorInterface
* debug_visitor_
;
752 std::string display_protocol_
;
754 // The protocol version to be spoken/understood by this framer.
755 const SpdyMajorVersion protocol_version_
;
757 // The flags field of the frame currently being read.
758 uint8 current_frame_flags_
;
760 // Determines whether HPACK or gzip compression is used.
761 bool enable_compression_
;
763 // Tracks if we've ever gotten far enough in framing to see a control frame of
764 // type SYN_STREAM or SYN_REPLY.
766 // If we ever get something which looks like a data frame before we've had a
767 // SYN, we explicitly check to see if it looks like we got an HTTP response
768 // to a SPDY request. This boolean lets us do that.
769 bool syn_frame_processed_
;
771 // If we ever get a data frame before a SYN frame, we check to see if it
772 // starts with HTTP. If it does, we likely have an HTTP response. This
773 // isn't guaranteed though: we could have gotten a settings frame and then
774 // corrupt data that just looks like HTTP, but deterministic checking requires
776 bool probable_http_response_
;
778 // If a HEADERS frame is followed by a CONTINUATION frame, the FIN/END_STREAM
779 // flag is still carried in the HEADERS frame. If it's set, flip this so that
780 // we know to terminate the stream when the entire header block has been
782 bool end_stream_when_done_
;
784 // If true, then ProcessInput returns after processing a full frame,
785 // rather than reading all available input.
786 bool process_single_input_frame_
= false;
788 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE.
789 size_t header_table_size_bound_
;
794 #endif // NET_SPDY_SPDY_FRAMER_H_