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_
15 #include "base/basictypes.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string_piece.h"
19 #include "base/sys_byteorder.h"
20 #include "net/base/net_export.h"
21 #include "net/spdy/hpack_decoder.h"
22 #include "net/spdy/hpack_encoder.h"
23 #include "net/spdy/spdy_alt_svc_wire_format.h"
24 #include "net/spdy/spdy_header_block.h"
25 #include "net/spdy/spdy_protocol.h"
27 // TODO(akalin): Remove support for CREDENTIAL frames.
29 typedef struct z_stream_s z_stream
; // Forward declaration for zlib.
33 class HttpProxyClientSocketPoolTest
;
34 class HttpNetworkLayer
;
35 class HttpNetworkTransactionTest
;
36 class SpdyHttpStreamTest
;
37 class SpdyNetworkTransactionTest
;
38 class SpdyProxyClientSocketTest
;
39 class SpdySessionTest
;
43 class SpdyFrameBuilder
;
47 class TestSpdyVisitor
;
52 // A datastructure for holding a set of headers from a HEADERS, PUSH_PROMISE,
53 // SYN_STREAM, or SYN_REPLY frame.
54 typedef std::map
<std::string
, std::string
> SpdyHeaderBlock
;
56 // A datastructure for holding the ID and flag fields for SETTINGS.
57 // Conveniently handles converstion to/from wire format.
58 class NET_EXPORT_PRIVATE SettingsFlagsAndId
{
60 static SettingsFlagsAndId
FromWireFormat(SpdyMajorVersion version
,
63 SettingsFlagsAndId() : flags_(0), id_(0) {}
65 // TODO(hkhalil): restrict to enums instead of free-form ints.
66 SettingsFlagsAndId(uint8 flags
, uint32 id
);
68 uint32
GetWireFormat(SpdyMajorVersion version
) const;
70 uint32
id() const { return id_
; }
71 uint8
flags() const { return flags_
; }
74 static void ConvertFlagsAndIdForSpdy2(uint32
* val
);
80 // SettingsMap has unique (flags, value) pair for given SpdySettingsIds ID.
81 typedef std::pair
<SpdySettingsFlags
, uint32
> SettingsFlagsAndValue
;
82 typedef std::map
<SpdySettingsIds
, SettingsFlagsAndValue
> SettingsMap
;
84 // Scratch space necessary for processing SETTINGS frames.
85 struct NET_EXPORT_PRIVATE SpdySettingsScratch
{
86 SpdySettingsScratch() { Reset(); }
93 // Buffer contains up to one complete key/value pair.
96 // The amount of the buffer that is filled with valid data.
97 size_t setting_buf_len
;
99 // The ID of the last setting that was processed in the current SETTINGS
100 // frame. Used for detecting out-of-order or duplicate keys within a settings
101 // frame. Set to -1 before first key/value pair is processed.
105 // Scratch space necessary for processing ALTSVC frames.
106 struct NET_EXPORT_PRIVATE SpdyAltSvcScratch
{
108 ~SpdyAltSvcScratch();
115 scoped_ptr
<char[]> buffer
;
116 size_t buffer_length
= 0;
119 // SpdyFramerVisitorInterface is a set of callbacks for the SpdyFramer.
120 // Implement this interface to receive event callbacks as frames are
121 // decoded from the framer.
123 // Control frames that contain SPDY header blocks (SYN_STREAM, SYN_REPLY,
124 // HEADER, and PUSH_PROMISE) are processed in fashion that allows the
125 // decompressed header block to be delivered in chunks to the visitor.
126 // The following steps are followed:
127 // 1. OnSynStream, OnSynReply, OnHeaders, or OnPushPromise is called.
128 // 2. Repeated: OnControlFrameHeaderData is called with chunks of the
129 // decompressed header block. In each call the len parameter is greater
131 // 3. OnControlFrameHeaderData is called with len set to zero, indicating
132 // that the full header block has been delivered for the control frame.
133 // During step 2 the visitor may return false, indicating that the chunk of
134 // header data could not be handled by the visitor (typically this indicates
135 // resource exhaustion). If this occurs the framer will discontinue
136 // delivering chunks to the visitor, set a SPDY_CONTROL_PAYLOAD_TOO_LARGE
137 // error, and clean up appropriately. Note that this will cause the header
138 // decompressor to lose synchronization with the sender's header compressor,
139 // making the SPDY session unusable for future work. The visitor's OnError
140 // function should deal with this condition by closing the SPDY connection.
141 class NET_EXPORT_PRIVATE SpdyFramerVisitorInterface
{
143 virtual ~SpdyFramerVisitorInterface() {}
145 // Called if an error is detected in the SpdyFrame protocol.
146 virtual void OnError(SpdyFramer
* framer
) = 0;
148 // Called when a data frame header is received. The frame's data
149 // payload will be provided via subsequent calls to
150 // OnStreamFrameData().
151 virtual void OnDataFrameHeader(SpdyStreamId stream_id
,
155 // Called when data is received.
156 // |stream_id| The stream receiving data.
157 // |data| A buffer containing the data received.
158 // |len| The length of the data buffer.
159 // When the other side has finished sending data on this stream,
160 // this method will be called with a zero-length buffer.
161 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
166 // Called when padding is received (padding length field or padding octets).
167 // |stream_id| The stream receiving data.
168 // |len| The number of padding octets.
169 virtual void OnStreamPadding(SpdyStreamId stream_id
, size_t len
) = 0;
171 // Called when a chunk of header data is available. This is called
172 // after OnSynStream, OnSynReply, OnHeaders(), or OnPushPromise.
173 // |stream_id| The stream receiving the header data.
174 // |header_data| A buffer containing the header data chunk received.
175 // |len| The length of the header data buffer. A length of zero indicates
176 // that the header data block has been completely sent.
177 // When this function returns true the visitor indicates that it accepted
178 // all of the data. Returning false indicates that that an unrecoverable
179 // error has occurred, such as bad header data or resource exhaustion.
180 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
181 const char* header_data
,
184 // Called when a SYN_STREAM frame is received.
185 // Note that header block data is not included. See
186 // OnControlFrameHeaderData().
187 virtual void OnSynStream(SpdyStreamId stream_id
,
188 SpdyStreamId associated_stream_id
,
189 SpdyPriority priority
,
191 bool unidirectional
) = 0;
193 // Called when a SYN_REPLY frame is received.
194 // Note that header block data is not included. See
195 // OnControlFrameHeaderData().
196 virtual void OnSynReply(SpdyStreamId stream_id
, bool fin
) = 0;
198 // Called when a RST_STREAM frame has been parsed.
199 virtual void OnRstStream(SpdyStreamId stream_id
,
200 SpdyRstStreamStatus status
) = 0;
202 // Called when a SETTINGS frame is received.
203 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
204 virtual void OnSettings(bool clear_persisted
) {}
206 // Called when a complete setting within a SETTINGS frame has been parsed and
208 virtual void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) = 0;
210 // Called when a SETTINGS frame is received with the ACK flag set.
211 virtual void OnSettingsAck() {}
213 // Called before and after parsing SETTINGS id and value tuples.
214 virtual void OnSettingsEnd() = 0;
216 // Called when a PING frame has been parsed.
217 virtual void OnPing(SpdyPingId unique_id
, bool is_ack
) = 0;
219 // Called when a GOAWAY frame has been parsed.
220 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id
,
221 SpdyGoAwayStatus status
) = 0;
223 // Called when a HEADERS frame is received.
224 // Note that header block data is not included. See
225 // OnControlFrameHeaderData().
226 // |stream_id| The stream receiving the header.
227 // |has_priority| Whether or not the headers frame included a priority value,
228 // and, if protocol version >= HTTP2, stream dependency info.
229 // |priority| If |has_priority| is true and protocol version > SPDY3,
230 // priority value for the receiving stream, else 0.
231 // |parent_stream_id| If |has_priority| is true and protocol
232 // version >= HTTP2, the parent stream of the receiving stream, else 0.
233 // |exclusive| If |has_priority| is true and protocol
234 // version >= HTTP2, the exclusivity of dependence on the parent stream,
236 // |fin| Whether FIN flag is set in frame headers.
237 // |end| False if HEADERs frame is to be followed by a CONTINUATION frame,
239 virtual void OnHeaders(SpdyStreamId stream_id
,
241 SpdyPriority priority
,
242 SpdyStreamId parent_stream_id
,
247 // Called when a WINDOW_UPDATE frame has been parsed.
248 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
249 int delta_window_size
) = 0;
251 // Called when a goaway frame opaque data is available.
252 // |goaway_data| A buffer containing the opaque GOAWAY data chunk received.
253 // |len| The length of the header data buffer. A length of zero indicates
254 // that the header data block has been completely sent.
255 // When this function returns true the visitor indicates that it accepted
256 // all of the data. Returning false indicates that that an error has
257 // occurred while processing the data. Default implementation returns true.
258 virtual bool OnGoAwayFrameData(const char* goaway_data
, size_t len
);
260 // Called when rst_stream frame opaque data is available.
261 // |rst_stream_data| A buffer containing the opaque RST_STREAM
262 // data chunk received.
263 // |len| The length of the header data buffer. A length of zero indicates
264 // that the opaque data has been completely sent.
265 // When this function returns true the visitor indicates that it accepted
266 // all of the data. Returning false indicates that that an error has
267 // occurred while processing the data. Default implementation returns true.
268 virtual bool OnRstStreamFrameData(const char* rst_stream_data
, size_t len
);
270 // Called when a BLOCKED frame has been parsed.
271 virtual void OnBlocked(SpdyStreamId stream_id
) {}
273 // Called when a PUSH_PROMISE frame is received.
274 // Note that header block data is not included. See
275 // OnControlFrameHeaderData().
276 virtual void OnPushPromise(SpdyStreamId stream_id
,
277 SpdyStreamId promised_stream_id
,
280 // Called when a CONTINUATION frame is received.
281 // Note that header block data is not included. See
282 // OnControlFrameHeaderData().
283 virtual void OnContinuation(SpdyStreamId stream_id
, bool end
) = 0;
285 // Called when an ALTSVC frame has been parsed.
286 virtual void OnAltSvc(
287 SpdyStreamId stream_id
,
288 base::StringPiece origin
,
289 const SpdyAltSvcWireFormat::AlternativeServiceVector
& altsvc_vector
) {}
291 // Called when a PRIORITY frame is received.
292 virtual void OnPriority(SpdyStreamId stream_id
,
293 SpdyStreamId parent_stream_id
,
297 // Called when a frame type we don't recognize is received.
298 // Return true if this appears to be a valid extension frame, false otherwise.
299 // We distinguish between extension frames and nonsense by checking
300 // whether the stream id is valid.
301 virtual bool OnUnknownFrame(SpdyStreamId stream_id
, int frame_type
) = 0;
304 // Optionally, and in addition to SpdyFramerVisitorInterface, a class supporting
305 // SpdyFramerDebugVisitorInterface may be used in conjunction with SpdyFramer in
306 // order to extract debug/internal information about the SpdyFramer as it
309 // Most SPDY implementations need not bother with this interface at all.
310 class NET_EXPORT_PRIVATE SpdyFramerDebugVisitorInterface
{
312 virtual ~SpdyFramerDebugVisitorInterface() {}
314 // Called after compressing a frame with a payload of
315 // a list of name-value pairs.
316 // |payload_len| is the uncompressed payload size.
317 // |frame_len| is the compressed frame size.
318 virtual void OnSendCompressedFrame(SpdyStreamId stream_id
,
323 // Called when a frame containing a compressed payload of
324 // name-value pairs is received.
325 // |frame_len| is the compressed frame size.
326 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id
,
331 class NET_EXPORT_PRIVATE SpdyFramer
{
334 // TODO(mbelshe): Can we move these into the implementation
335 // and avoid exposing through the header. (Needed for test)
340 SPDY_READING_COMMON_HEADER
,
341 SPDY_CONTROL_FRAME_PAYLOAD
,
342 SPDY_READ_DATA_FRAME_PADDING_LENGTH
,
343 SPDY_CONSUME_PADDING
,
344 SPDY_IGNORE_REMAINING_PAYLOAD
,
345 SPDY_FORWARD_STREAM_FRAME
,
346 SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK
,
347 SPDY_CONTROL_FRAME_HEADER_BLOCK
,
348 SPDY_GOAWAY_FRAME_PAYLOAD
,
349 SPDY_RST_STREAM_FRAME_PAYLOAD
,
350 SPDY_SETTINGS_FRAME_PAYLOAD
,
351 SPDY_ALTSVC_FRAME_PAYLOAD
,
357 SPDY_INVALID_CONTROL_FRAME
, // Control frame is mal-formatted.
358 SPDY_CONTROL_PAYLOAD_TOO_LARGE
, // Control frame payload was too large.
359 SPDY_ZLIB_INIT_FAILURE
, // The Zlib library could not initialize.
360 SPDY_UNSUPPORTED_VERSION
, // Control frame has unsupported version.
361 SPDY_DECOMPRESS_FAILURE
, // There was an error decompressing.
362 SPDY_COMPRESS_FAILURE
, // There was an error compressing.
363 SPDY_GOAWAY_FRAME_CORRUPT
, // GOAWAY frame could not be parsed.
364 SPDY_RST_STREAM_FRAME_CORRUPT
, // RST_STREAM frame could not be parsed.
365 SPDY_INVALID_DATA_FRAME_FLAGS
, // Data frame has invalid flags.
366 SPDY_INVALID_CONTROL_FRAME_FLAGS
, // Control frame has invalid flags.
367 SPDY_UNEXPECTED_FRAME
, // Frame received out of order.
369 LAST_ERROR
, // Must be the last entry in the enum.
372 // Constant for invalid (or unknown) stream IDs.
373 static const SpdyStreamId kInvalidStream
;
375 // The maximum size of header data chunks delivered to the framer visitor
376 // through OnControlFrameHeaderData. (It is exposed here for unit test
378 static const size_t kHeaderDataChunkMaxSize
;
380 // Serializes a SpdyHeaderBlock.
381 static void WriteHeaderBlock(SpdyFrameBuilder
* frame
,
382 const SpdyMajorVersion spdy_version
,
383 const SpdyHeaderBlock
* headers
);
385 // Retrieve serialized length of SpdyHeaderBlock.
386 // TODO(hkhalil): Remove, or move to quic code.
387 static size_t GetSerializedLength(
388 const SpdyMajorVersion spdy_version
,
389 const SpdyHeaderBlock
* headers
);
391 // Create a new Framer, provided a SPDY version.
392 explicit SpdyFramer(SpdyMajorVersion version
);
393 virtual ~SpdyFramer();
395 // Set callbacks to be called from the framer. A visitor must be set, or
396 // else the framer will likely crash. It is acceptable for the visitor
397 // to do nothing. If this is called multiple times, only the last visitor
399 void set_visitor(SpdyFramerVisitorInterface
* visitor
) {
403 // Set debug callbacks to be called from the framer. The debug visitor is
404 // completely optional and need not be set in order for normal operation.
405 // If this is called multiple times, only the last visitor will be used.
406 void set_debug_visitor(SpdyFramerDebugVisitorInterface
* debug_visitor
) {
407 debug_visitor_
= debug_visitor
;
410 // Pass data into the framer for parsing.
411 // Returns the number of bytes consumed. It is safe to pass more bytes in
412 // than may be consumed.
413 size_t ProcessInput(const char* data
, size_t len
);
415 // Resets the framer state after a frame has been successfully decoded.
416 // TODO(mbelshe): can we make this private?
419 // Check the state of the framer.
420 SpdyError
error_code() const { return error_code_
; }
421 SpdyState
state() const { return state_
; }
422 bool HasError() const { return state_
== SPDY_ERROR
; }
424 // Given a buffer containing a decompressed header block in SPDY
425 // serialized format, parse out a SpdyHeaderBlock, putting the results
426 // in the given header block.
427 // Returns number of bytes consumed if successfully parsed, 0 otherwise.
428 size_t ParseHeaderBlockInBuffer(const char* header_data
,
429 size_t header_length
,
430 SpdyHeaderBlock
* block
) const;
432 // Serialize a data frame.
433 SpdySerializedFrame
* SerializeData(const SpdyDataIR
& data
) const;
434 // Serializes the data frame header and optionally padding length fields,
435 // excluding actual data payload and padding.
436 SpdySerializedFrame
* SerializeDataFrameHeaderWithPaddingLengthField(
437 const SpdyDataIR
& data
) const;
439 // Serializes a SYN_STREAM frame.
440 SpdySerializedFrame
* SerializeSynStream(const SpdySynStreamIR
& syn_stream
);
442 // Serialize a SYN_REPLY SpdyFrame.
443 SpdySerializedFrame
* SerializeSynReply(const SpdySynReplyIR
& syn_reply
);
445 SpdySerializedFrame
* SerializeRstStream(
446 const SpdyRstStreamIR
& rst_stream
) const;
448 // Serializes a SETTINGS frame. The SETTINGS frame is
449 // used to communicate name/value pairs relevant to the communication channel.
450 SpdySerializedFrame
* SerializeSettings(const SpdySettingsIR
& settings
) const;
452 // Serializes a PING frame. The unique_id is used to
453 // identify the ping request/response.
454 SpdySerializedFrame
* SerializePing(const SpdyPingIR
& ping
) const;
456 // Serializes a GOAWAY frame. The GOAWAY frame is used
457 // prior to the shutting down of the TCP connection, and includes the
458 // stream_id of the last stream the sender of the frame is willing to process
460 SpdySerializedFrame
* SerializeGoAway(const SpdyGoAwayIR
& goaway
) const;
462 // Serializes a HEADERS frame. The HEADERS frame is used
463 // for sending additional headers outside of a SYN_STREAM/SYN_REPLY.
464 SpdySerializedFrame
* SerializeHeaders(const SpdyHeadersIR
& headers
);
466 // Serializes a WINDOW_UPDATE frame. The WINDOW_UPDATE
467 // frame is used to implement per stream flow control in SPDY.
468 SpdySerializedFrame
* SerializeWindowUpdate(
469 const SpdyWindowUpdateIR
& window_update
) const;
471 // Serializes a BLOCKED frame. The BLOCKED frame is used to
472 // indicate to the remote endpoint that this endpoint believes itself to be
473 // flow-control blocked but otherwise ready to send data. The BLOCKED frame
474 // is purely advisory and optional.
475 SpdySerializedFrame
* SerializeBlocked(const SpdyBlockedIR
& blocked
) const;
477 // Serializes a PUSH_PROMISE frame. The PUSH_PROMISE frame is used
478 // to inform the client that it will be receiving an additional stream
479 // in response to the original request. The frame includes synthesized
480 // headers to explain the upcoming data.
481 SpdySerializedFrame
* SerializePushPromise(
482 const SpdyPushPromiseIR
& push_promise
);
484 // Serializes a CONTINUATION frame. The CONTINUATION frame is used
485 // to continue a sequence of header block fragments.
486 // TODO(jgraettinger): This implementation is incorrect. The continuation
487 // frame continues a previously-begun HPACK encoding; it doesn't begin a
488 // new one. Figure out whether it makes sense to keep SerializeContinuation().
489 SpdySerializedFrame
* SerializeContinuation(
490 const SpdyContinuationIR
& continuation
);
492 // Serializes an ALTSVC frame. The ALTSVC frame advertises the
493 // availability of an alternative service to the client.
494 SpdySerializedFrame
* SerializeAltSvc(const SpdyAltSvcIR
& altsvc
);
496 // Serializes a PRIORITY frame. The PRIORITY frame advises a change in
497 // the relative priority of the given stream.
498 SpdySerializedFrame
* SerializePriority(const SpdyPriorityIR
& priority
) const;
500 // Serialize a frame of unknown type.
501 SpdySerializedFrame
* SerializeFrame(const SpdyFrameIR
& frame
);
503 // NOTES about frame compression.
504 // We want spdy to compress headers across the entire session. As long as
505 // the session is over TCP, frames are sent serially. The client & server
506 // can each compress frames in the same order and then compress them in that
507 // order, and the remote can do the reverse. However, we ultimately want
508 // the creation of frames to be less sensitive to order so that they can be
509 // placed over a UDP based protocol and yet still benefit from some
510 // compression. We don't know of any good compression protocol which does
511 // not build its state in a serial (stream based) manner.... For now, we're
512 // using zlib anyway.
514 // Compresses a SpdyFrame.
515 // On success, returns a new SpdyFrame with the payload compressed.
516 // Compression state is maintained as part of the SpdyFramer.
517 // Returned frame must be freed with "delete".
518 // On failure, returns NULL.
519 SpdyFrame
* CompressFrame(const SpdyFrame
& frame
);
521 // For ease of testing and experimentation we can tweak compression on/off.
522 void set_enable_compression(bool value
) {
523 enable_compression_
= value
;
526 // Used only in log messages.
527 void set_display_protocol(const std::string
& protocol
) {
528 display_protocol_
= protocol
;
531 // Returns the (minimum) size of frames (sans variable-length portions).
532 size_t GetDataFrameMinimumSize() const;
533 size_t GetControlFrameHeaderSize() const;
534 size_t GetSynStreamMinimumSize() const;
535 size_t GetSynReplyMinimumSize() const;
536 size_t GetRstStreamMinimumSize() const;
537 size_t GetSettingsMinimumSize() const;
538 size_t GetPingSize() const;
539 size_t GetGoAwayMinimumSize() const;
540 size_t GetHeadersMinimumSize() const;
541 size_t GetWindowUpdateSize() const;
542 size_t GetBlockedSize() const;
543 size_t GetPushPromiseMinimumSize() const;
544 size_t GetContinuationMinimumSize() const;
545 size_t GetAltSvcMinimumSize() const;
546 size_t GetPrioritySize() const;
548 // Returns the minimum size a frame can be (data or control).
549 size_t GetFrameMinimumSize() const;
551 // Returns the maximum size a frame can be (data or control).
552 size_t GetFrameMaximumSize() const;
554 // Returns the maximum payload size of a DATA frame.
555 size_t GetDataFrameMaximumPayload() const;
557 // Returns the prefix length for the given frame type.
558 size_t GetPrefixLength(SpdyFrameType type
) const;
561 static const char* StateToString(int state
);
562 static const char* ErrorCodeToString(int error_code
);
563 static const char* StatusCodeToString(int status_code
);
564 static const char* FrameTypeToString(SpdyFrameType type
);
566 SpdyMajorVersion
protocol_version() const { return protocol_version_
; }
568 bool probable_http_response() const { return probable_http_response_
; }
570 SpdyPriority
GetLowestPriority() const {
571 return protocol_version_
< SPDY3
? 3 : 7;
574 SpdyPriority
GetHighestPriority() const { return 0; }
576 // Interpolates SpdyPriority values into SPDY4/HTTP2 priority weights,
578 static uint8
MapPriorityToWeight(SpdyPriority priority
);
579 static SpdyPriority
MapWeightToPriority(uint8 weight
);
581 // Deliver the given control frame's compressed headers block to the visitor
582 // in decompressed form, in chunks. Returns true if the visitor has
583 // accepted all of the chunks.
584 bool IncrementallyDecompressControlFrameHeaderData(
585 SpdyStreamId stream_id
,
589 // Updates the maximum size of header compression table.
590 void UpdateHeaderTableSizeSetting(uint32 value
);
592 // Returns bound of header compression table size.
593 size_t header_table_size_bound() const;
596 friend class HttpNetworkLayer
; // This is temporary for the server.
597 friend class HttpNetworkTransactionTest
;
598 friend class HttpProxyClientSocketPoolTest
;
599 friend class SpdyHttpStreamTest
;
600 friend class SpdyNetworkTransactionTest
;
601 friend class SpdyProxyClientSocketTest
;
602 friend class SpdySessionTest
;
603 friend class SpdyStreamTest
;
604 friend class test::TestSpdyVisitor
;
605 friend class test::SpdyFramerPeer
;
608 // Internal breakouts from ProcessInput. Each returns the number of bytes
609 // consumed from the data.
610 size_t ProcessCommonHeader(const char* data
, size_t len
);
611 size_t ProcessControlFramePayload(const char* data
, size_t len
);
612 size_t ProcessControlFrameBeforeHeaderBlock(const char* data
, size_t len
);
613 // HPACK data is re-encoded as SPDY3 and re-entrantly delivered through
614 // |ProcessControlFrameHeaderBlock()|. |is_hpack_header_block| controls
615 // whether data is treated as HPACK- vs SPDY3-encoded.
616 size_t ProcessControlFrameHeaderBlock(const char* data
,
618 bool is_hpack_header_block
);
619 size_t ProcessDataFramePaddingLength(const char* data
, size_t len
);
620 size_t ProcessFramePadding(const char* data
, size_t len
);
621 size_t ProcessDataFramePayload(const char* data
, size_t len
);
622 size_t ProcessGoAwayFramePayload(const char* data
, size_t len
);
623 size_t ProcessRstStreamFramePayload(const char* data
, size_t len
);
624 size_t ProcessSettingsFramePayload(const char* data
, size_t len
);
625 size_t ProcessAltSvcFramePayload(const char* data
, size_t len
);
626 size_t ProcessIgnoredControlFramePayload(/*const char* data,*/ size_t len
);
628 // TODO(jgraettinger): To be removed with migration to
629 // SpdyHeadersHandlerInterface.
630 // Serializes the last-processed header block of |hpack_decoder_| as
631 // a SPDY3 format block, and delivers it to the visitor via reentrant
632 // call to ProcessControlFrameHeaderBlock().
633 void DeliverHpackBlockAsSpdy3Block();
635 // Helpers for above internal breakouts from ProcessInput.
636 void ProcessControlFrameHeader(int control_frame_type_field
);
637 // Always passed exactly 1 setting's worth of data.
638 bool ProcessSetting(const char* data
);
640 // Retrieve serialized length of SpdyHeaderBlock. If compression is enabled, a
641 // maximum estimate is returned.
642 size_t GetSerializedLength(const SpdyHeaderBlock
& headers
);
644 // Get (and lazily initialize) the ZLib state.
645 z_stream
* GetHeaderCompressor();
646 z_stream
* GetHeaderDecompressor();
648 // Get (and lazily initialize) the HPACK state.
649 HpackEncoder
* GetHpackEncoder();
650 HpackDecoder
* GetHpackDecoder();
652 size_t GetNumberRequiredContinuationFrames(size_t size
);
654 void WritePayloadWithContinuation(SpdyFrameBuilder
* builder
,
655 const std::string
& hpack_encoding
,
656 SpdyStreamId stream_id
,
658 int padding_payload_len
);
660 // Deliver the given control frame's uncompressed headers block to the
661 // visitor in chunks. Returns true if the visitor has accepted all of the
663 bool IncrementallyDeliverControlFrameHeaderData(SpdyStreamId stream_id
,
667 // Utility to copy the given data block to the current frame buffer, up
668 // to the given maximum number of bytes, and update the buffer
669 // data (pointer and length). Returns the number of bytes
671 // *data is advanced the number of bytes read.
672 // *len is reduced by the number of bytes read.
673 size_t UpdateCurrentFrameBuffer(const char** data
, size_t* len
,
676 void WriteHeaderBlockToZ(const SpdyHeaderBlock
* headers
,
677 z_stream
* out
) const;
679 void SerializeNameValueBlockWithoutCompression(
680 SpdyFrameBuilder
* builder
,
681 const SpdyNameValueBlock
& name_value_block
) const;
683 // Compresses automatically according to enable_compression_.
684 void SerializeNameValueBlock(
685 SpdyFrameBuilder
* builder
,
686 const SpdyFrameWithNameValueBlockIR
& frame
);
688 // Set the error code and moves the framer into the error state.
689 void set_error(SpdyError error
);
691 // The size of the control frame buffer.
692 // Since this is only used for control frame headers, the maximum control
693 // frame header size (SYN_STREAM) is sufficient; all remaining control
694 // frame data is streamed to the visitor.
695 static const size_t kControlFrameBufferSize
;
697 // The maximum size of the control frames that we support.
698 // This limit is arbitrary. We can enforce it here or at the application
699 // layer. We chose the framing layer, but this can be changed (or removed)
700 // if necessary later down the line.
701 static const size_t kMaxControlFrameSize
;
704 SpdyState previous_state_
;
705 SpdyError error_code_
;
707 // Note that for DATA frame, remaining_data_length_ is sum of lengths of
708 // frame header, padding length field (optional), data payload (optional) and
709 // padding payload (optional).
710 size_t remaining_data_length_
;
712 // The length (in bytes) of the padding payload to be processed.
713 size_t remaining_padding_payload_length_
;
715 // The number of bytes remaining to read from the current control frame's
716 // headers. Note that header data blocks (for control types that have them)
717 // are part of the frame's payload, and not the frame's headers.
718 size_t remaining_control_header_
;
720 scoped_ptr
<char[]> current_frame_buffer_
;
721 // Number of bytes read into the current_frame_buffer_.
722 size_t current_frame_buffer_length_
;
724 // The type of the frame currently being read.
725 SpdyFrameType current_frame_type_
;
727 // The total length of the frame currently being read, including frame header.
728 uint32 current_frame_length_
;
730 // The stream ID field of the frame currently being read, if applicable.
731 SpdyStreamId current_frame_stream_id_
;
733 // Set this to the current stream when we receive a HEADERS, PUSH_PROMISE, or
734 // CONTINUATION frame without the END_HEADERS(0x4) bit set. These frames must
735 // be followed by a CONTINUATION frame, or else we throw a PROTOCOL_ERROR.
736 // A value of 0 indicates that we are not expecting a CONTINUATION frame.
737 SpdyStreamId expect_continuation_
;
739 // Scratch space for handling SETTINGS frames.
740 // TODO(hkhalil): Unify memory for this scratch space with
741 // current_frame_buffer_.
742 SpdySettingsScratch settings_scratch_
;
744 SpdyAltSvcScratch altsvc_scratch_
;
746 // SPDY header compressors.
747 scoped_ptr
<z_stream
> header_compressor_
;
748 scoped_ptr
<z_stream
> header_decompressor_
;
750 scoped_ptr
<HpackEncoder
> hpack_encoder_
;
751 scoped_ptr
<HpackDecoder
> hpack_decoder_
;
753 SpdyFramerVisitorInterface
* visitor_
;
754 SpdyFramerDebugVisitorInterface
* debug_visitor_
;
756 std::string display_protocol_
;
758 // The protocol version to be spoken/understood by this framer.
759 const SpdyMajorVersion protocol_version_
;
761 // The flags field of the frame currently being read.
762 uint8 current_frame_flags_
;
764 // Determines whether HPACK or gzip compression is used.
765 bool enable_compression_
;
767 // Tracks if we've ever gotten far enough in framing to see a control frame of
768 // type SYN_STREAM or SYN_REPLY.
770 // If we ever get something which looks like a data frame before we've had a
771 // SYN, we explicitly check to see if it looks like we got an HTTP response
772 // to a SPDY request. This boolean lets us do that.
773 bool syn_frame_processed_
;
775 // If we ever get a data frame before a SYN frame, we check to see if it
776 // starts with HTTP. If it does, we likely have an HTTP response. This
777 // isn't guaranteed though: we could have gotten a settings frame and then
778 // corrupt data that just looks like HTTP, but deterministic checking requires
780 bool probable_http_response_
;
782 // If a HEADERS frame is followed by a CONTINUATION frame, the FIN/END_STREAM
783 // flag is still carried in the HEADERS frame. If it's set, flip this so that
784 // we know to terminate the stream when the entire header block has been
786 bool end_stream_when_done_
;
788 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE.
789 size_t header_table_size_bound_
;
794 #endif // NET_SPDY_SPDY_FRAMER_H_