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 virtual void OnHeaders(SpdyStreamId stream_id
,
228 SpdyPriority priority
,
232 // Called when a WINDOW_UPDATE frame has been parsed.
233 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
234 uint32 delta_window_size
) = 0;
236 // Called when a goaway frame opaque data is available.
237 // |goaway_data| A buffer containing the opaque GOAWAY data chunk received.
238 // |len| The length of the header data buffer. A length of zero indicates
239 // that the header data block has been completely sent.
240 // When this function returns true the visitor indicates that it accepted
241 // all of the data. Returning false indicates that that an error has
242 // occurred while processing the data. Default implementation returns true.
243 virtual bool OnGoAwayFrameData(const char* goaway_data
, size_t len
);
245 // Called when rst_stream frame opaque data is available.
246 // |rst_stream_data| A buffer containing the opaque RST_STREAM
247 // data chunk received.
248 // |len| The length of the header data buffer. A length of zero indicates
249 // that the opaque data has been completely sent.
250 // When this function returns true the visitor indicates that it accepted
251 // all of the data. Returning false indicates that that an error has
252 // occurred while processing the data. Default implementation returns true.
253 virtual bool OnRstStreamFrameData(const char* rst_stream_data
, size_t len
);
255 // Called when a BLOCKED frame has been parsed.
256 virtual void OnBlocked(SpdyStreamId stream_id
) {}
258 // Called when a PUSH_PROMISE frame is received.
259 // Note that header block data is not included. See
260 // OnControlFrameHeaderData().
261 virtual void OnPushPromise(SpdyStreamId stream_id
,
262 SpdyStreamId promised_stream_id
,
265 // Called when a CONTINUATION frame is received.
266 // Note that header block data is not included. See
267 // OnControlFrameHeaderData().
268 virtual void OnContinuation(SpdyStreamId stream_id
, bool end
) = 0;
270 // Called when an ALTSVC frame has been parsed.
271 virtual void OnAltSvc(
272 SpdyStreamId stream_id
,
273 base::StringPiece origin
,
274 const SpdyAltSvcWireFormat::AlternativeService
& altsvc
) {}
276 // Called when a PRIORITY frame is received.
277 virtual void OnPriority(SpdyStreamId stream_id
,
278 SpdyStreamId parent_stream_id
,
282 // Called when a frame type we don't recognize is received.
283 // Return true if this appears to be a valid extension frame, false otherwise.
284 // We distinguish between extension frames and nonsense by checking
285 // whether the stream id is valid.
286 virtual bool OnUnknownFrame(SpdyStreamId stream_id
, int frame_type
) = 0;
289 // Optionally, and in addition to SpdyFramerVisitorInterface, a class supporting
290 // SpdyFramerDebugVisitorInterface may be used in conjunction with SpdyFramer in
291 // order to extract debug/internal information about the SpdyFramer as it
294 // Most SPDY implementations need not bother with this interface at all.
295 class NET_EXPORT_PRIVATE SpdyFramerDebugVisitorInterface
{
297 virtual ~SpdyFramerDebugVisitorInterface() {}
299 // Called after compressing a frame with a payload of
300 // a list of name-value pairs.
301 // |payload_len| is the uncompressed payload size.
302 // |frame_len| is the compressed frame size.
303 virtual void OnSendCompressedFrame(SpdyStreamId stream_id
,
308 // Called when a frame containing a compressed payload of
309 // name-value pairs is received.
310 // |frame_len| is the compressed frame size.
311 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id
,
316 class NET_EXPORT_PRIVATE SpdyFramer
{
319 // TODO(mbelshe): Can we move these into the implementation
320 // and avoid exposing through the header. (Needed for test)
325 SPDY_READING_COMMON_HEADER
,
326 SPDY_CONTROL_FRAME_PAYLOAD
,
327 SPDY_READ_DATA_FRAME_PADDING_LENGTH
,
328 SPDY_CONSUME_PADDING
,
329 SPDY_IGNORE_REMAINING_PAYLOAD
,
330 SPDY_FORWARD_STREAM_FRAME
,
331 SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK
,
332 SPDY_CONTROL_FRAME_HEADER_BLOCK
,
333 SPDY_GOAWAY_FRAME_PAYLOAD
,
334 SPDY_RST_STREAM_FRAME_PAYLOAD
,
335 SPDY_SETTINGS_FRAME_PAYLOAD
,
336 SPDY_ALTSVC_FRAME_PAYLOAD
,
342 SPDY_INVALID_CONTROL_FRAME
, // Control frame is mal-formatted.
343 SPDY_CONTROL_PAYLOAD_TOO_LARGE
, // Control frame payload was too large.
344 SPDY_ZLIB_INIT_FAILURE
, // The Zlib library could not initialize.
345 SPDY_UNSUPPORTED_VERSION
, // Control frame has unsupported version.
346 SPDY_DECOMPRESS_FAILURE
, // There was an error decompressing.
347 SPDY_COMPRESS_FAILURE
, // There was an error compressing.
348 SPDY_GOAWAY_FRAME_CORRUPT
, // GOAWAY frame could not be parsed.
349 SPDY_RST_STREAM_FRAME_CORRUPT
, // RST_STREAM frame could not be parsed.
350 SPDY_INVALID_DATA_FRAME_FLAGS
, // Data frame has invalid flags.
351 SPDY_INVALID_CONTROL_FRAME_FLAGS
, // Control frame has invalid flags.
352 SPDY_UNEXPECTED_FRAME
, // Frame received out of order.
354 LAST_ERROR
, // Must be the last entry in the enum.
357 // Constant for invalid (or unknown) stream IDs.
358 static const SpdyStreamId kInvalidStream
;
360 // The maximum size of header data chunks delivered to the framer visitor
361 // through OnControlFrameHeaderData. (It is exposed here for unit test
363 static const size_t kHeaderDataChunkMaxSize
;
365 // Serializes a SpdyHeaderBlock.
366 static void WriteHeaderBlock(SpdyFrameBuilder
* frame
,
367 const SpdyMajorVersion spdy_version
,
368 const SpdyHeaderBlock
* headers
);
370 // Retrieve serialized length of SpdyHeaderBlock.
371 // TODO(hkhalil): Remove, or move to quic code.
372 static size_t GetSerializedLength(
373 const SpdyMajorVersion spdy_version
,
374 const SpdyHeaderBlock
* headers
);
376 // Create a new Framer, provided a SPDY version.
377 explicit SpdyFramer(SpdyMajorVersion version
);
378 virtual ~SpdyFramer();
380 // Set callbacks to be called from the framer. A visitor must be set, or
381 // else the framer will likely crash. It is acceptable for the visitor
382 // to do nothing. If this is called multiple times, only the last visitor
384 void set_visitor(SpdyFramerVisitorInterface
* visitor
) {
388 // Set debug callbacks to be called from the framer. The debug visitor is
389 // completely optional and need not be set in order for normal operation.
390 // If this is called multiple times, only the last visitor will be used.
391 void set_debug_visitor(SpdyFramerDebugVisitorInterface
* debug_visitor
) {
392 debug_visitor_
= debug_visitor
;
395 // Pass data into the framer for parsing.
396 // Returns the number of bytes consumed. It is safe to pass more bytes in
397 // than may be consumed.
398 size_t ProcessInput(const char* data
, size_t len
);
400 // Resets the framer state after a frame has been successfully decoded.
401 // TODO(mbelshe): can we make this private?
404 // Check the state of the framer.
405 SpdyError
error_code() const { return error_code_
; }
406 SpdyState
state() const { return state_
; }
407 bool HasError() const { return state_
== SPDY_ERROR
; }
409 // Given a buffer containing a decompressed header block in SPDY
410 // serialized format, parse out a SpdyHeaderBlock, putting the results
411 // in the given header block.
412 // Returns number of bytes consumed if successfully parsed, 0 otherwise.
413 size_t ParseHeaderBlockInBuffer(const char* header_data
,
414 size_t header_length
,
415 SpdyHeaderBlock
* block
) const;
417 // Serialize a data frame.
418 SpdySerializedFrame
* SerializeData(const SpdyDataIR
& data
) const;
419 // Serializes the data frame header and optionally padding length fields,
420 // excluding actual data payload and padding.
421 SpdySerializedFrame
* SerializeDataFrameHeaderWithPaddingLengthField(
422 const SpdyDataIR
& data
) const;
424 // Serializes a SYN_STREAM frame.
425 SpdySerializedFrame
* SerializeSynStream(const SpdySynStreamIR
& syn_stream
);
427 // Serialize a SYN_REPLY SpdyFrame.
428 SpdySerializedFrame
* SerializeSynReply(const SpdySynReplyIR
& syn_reply
);
430 SpdySerializedFrame
* SerializeRstStream(
431 const SpdyRstStreamIR
& rst_stream
) const;
433 // Serializes a SETTINGS frame. The SETTINGS frame is
434 // used to communicate name/value pairs relevant to the communication channel.
435 SpdySerializedFrame
* SerializeSettings(const SpdySettingsIR
& settings
) const;
437 // Serializes a PING frame. The unique_id is used to
438 // identify the ping request/response.
439 SpdySerializedFrame
* SerializePing(const SpdyPingIR
& ping
) const;
441 // Serializes a GOAWAY frame. The GOAWAY frame is used
442 // prior to the shutting down of the TCP connection, and includes the
443 // stream_id of the last stream the sender of the frame is willing to process
445 SpdySerializedFrame
* SerializeGoAway(const SpdyGoAwayIR
& goaway
) const;
447 // Serializes a HEADERS frame. The HEADERS frame is used
448 // for sending additional headers outside of a SYN_STREAM/SYN_REPLY.
449 SpdySerializedFrame
* SerializeHeaders(const SpdyHeadersIR
& headers
);
451 // Serializes a WINDOW_UPDATE frame. The WINDOW_UPDATE
452 // frame is used to implement per stream flow control in SPDY.
453 SpdySerializedFrame
* SerializeWindowUpdate(
454 const SpdyWindowUpdateIR
& window_update
) const;
456 // Serializes a BLOCKED frame. The BLOCKED frame is used to
457 // indicate to the remote endpoint that this endpoint believes itself to be
458 // flow-control blocked but otherwise ready to send data. The BLOCKED frame
459 // is purely advisory and optional.
460 SpdySerializedFrame
* SerializeBlocked(const SpdyBlockedIR
& blocked
) const;
462 // Serializes a PUSH_PROMISE frame. The PUSH_PROMISE frame is used
463 // to inform the client that it will be receiving an additional stream
464 // in response to the original request. The frame includes synthesized
465 // headers to explain the upcoming data.
466 SpdySerializedFrame
* SerializePushPromise(
467 const SpdyPushPromiseIR
& push_promise
);
469 // Serializes a CONTINUATION frame. The CONTINUATION frame is used
470 // to continue a sequence of header block fragments.
471 // TODO(jgraettinger): This implementation is incorrect. The continuation
472 // frame continues a previously-begun HPACK encoding; it doesn't begin a
473 // new one. Figure out whether it makes sense to keep SerializeContinuation().
474 SpdySerializedFrame
* SerializeContinuation(
475 const SpdyContinuationIR
& continuation
);
477 // Serializes an ALTSVC frame. The ALTSVC frame advertises the
478 // availability of an alternative service to the client.
479 SpdySerializedFrame
* SerializeAltSvc(const SpdyAltSvcIR
& altsvc
);
481 // Serializes a PRIORITY frame. The PRIORITY frame advises a change in
482 // the relative priority of the given stream.
483 SpdySerializedFrame
* SerializePriority(const SpdyPriorityIR
& priority
) const;
485 // Serialize a frame of unknown type.
486 SpdySerializedFrame
* SerializeFrame(const SpdyFrameIR
& frame
);
488 // NOTES about frame compression.
489 // We want spdy to compress headers across the entire session. As long as
490 // the session is over TCP, frames are sent serially. The client & server
491 // can each compress frames in the same order and then compress them in that
492 // order, and the remote can do the reverse. However, we ultimately want
493 // the creation of frames to be less sensitive to order so that they can be
494 // placed over a UDP based protocol and yet still benefit from some
495 // compression. We don't know of any good compression protocol which does
496 // not build its state in a serial (stream based) manner.... For now, we're
497 // using zlib anyway.
499 // Compresses a SpdyFrame.
500 // On success, returns a new SpdyFrame with the payload compressed.
501 // Compression state is maintained as part of the SpdyFramer.
502 // Returned frame must be freed with "delete".
503 // On failure, returns NULL.
504 SpdyFrame
* CompressFrame(const SpdyFrame
& frame
);
506 // For ease of testing and experimentation we can tweak compression on/off.
507 void set_enable_compression(bool value
) {
508 enable_compression_
= value
;
511 // Used only in log messages.
512 void set_display_protocol(const std::string
& protocol
) {
513 display_protocol_
= protocol
;
516 // Returns the (minimum) size of frames (sans variable-length portions).
517 size_t GetDataFrameMinimumSize() const;
518 size_t GetControlFrameHeaderSize() const;
519 size_t GetSynStreamMinimumSize() const;
520 size_t GetSynReplyMinimumSize() const;
521 size_t GetRstStreamMinimumSize() const;
522 size_t GetSettingsMinimumSize() const;
523 size_t GetPingSize() const;
524 size_t GetGoAwayMinimumSize() const;
525 size_t GetHeadersMinimumSize() const;
526 size_t GetWindowUpdateSize() const;
527 size_t GetBlockedSize() const;
528 size_t GetPushPromiseMinimumSize() const;
529 size_t GetContinuationMinimumSize() const;
530 size_t GetAltSvcMinimumSize() const;
531 size_t GetPrioritySize() const;
533 // Returns the minimum size a frame can be (data or control).
534 size_t GetFrameMinimumSize() const;
536 // Returns the maximum size a frame can be (data or control).
537 size_t GetFrameMaximumSize() const;
539 // Returns the maximum payload size of a DATA frame.
540 size_t GetDataFrameMaximumPayload() const;
542 // Returns the prefix length for the given frame type.
543 size_t GetPrefixLength(SpdyFrameType type
) const;
546 static const char* StateToString(int state
);
547 static const char* ErrorCodeToString(int error_code
);
548 static const char* StatusCodeToString(int status_code
);
549 static const char* FrameTypeToString(SpdyFrameType type
);
551 SpdyMajorVersion
protocol_version() const { return protocol_version_
; }
553 bool probable_http_response() const { return probable_http_response_
; }
555 SpdyPriority
GetLowestPriority() const {
556 return protocol_version_
< SPDY3
? 3 : 7;
559 SpdyPriority
GetHighestPriority() const { return 0; }
561 // Interpolates SpdyPriority values into SPDY4/HTTP2 priority weights,
563 static uint8
MapPriorityToWeight(SpdyPriority priority
);
564 static SpdyPriority
MapWeightToPriority(uint8 weight
);
566 // Deliver the given control frame's compressed headers block to the visitor
567 // in decompressed form, in chunks. Returns true if the visitor has
568 // accepted all of the chunks.
569 bool IncrementallyDecompressControlFrameHeaderData(
570 SpdyStreamId stream_id
,
574 // Updates the maximum size of header compression table.
575 void UpdateHeaderTableSizeSetting(uint32 value
);
577 // Returns bound of header compression table size.
578 size_t header_table_size_bound() const;
581 friend class HttpNetworkLayer
; // This is temporary for the server.
582 friend class HttpNetworkTransactionTest
;
583 friend class HttpProxyClientSocketPoolTest
;
584 friend class SpdyHttpStreamTest
;
585 friend class SpdyNetworkTransactionTest
;
586 friend class SpdyProxyClientSocketTest
;
587 friend class SpdySessionTest
;
588 friend class SpdyStreamTest
;
589 friend class test::TestSpdyVisitor
;
590 friend class test::SpdyFramerPeer
;
593 // Internal breakouts from ProcessInput. Each returns the number of bytes
594 // consumed from the data.
595 size_t ProcessCommonHeader(const char* data
, size_t len
);
596 size_t ProcessControlFramePayload(const char* data
, size_t len
);
597 size_t ProcessControlFrameBeforeHeaderBlock(const char* data
, size_t len
);
598 // HPACK data is re-encoded as SPDY3 and re-entrantly delivered through
599 // |ProcessControlFrameHeaderBlock()|. |is_hpack_header_block| controls
600 // whether data is treated as HPACK- vs SPDY3-encoded.
601 size_t ProcessControlFrameHeaderBlock(const char* data
,
603 bool is_hpack_header_block
);
604 size_t ProcessDataFramePaddingLength(const char* data
, size_t len
);
605 size_t ProcessFramePadding(const char* data
, size_t len
);
606 size_t ProcessDataFramePayload(const char* data
, size_t len
);
607 size_t ProcessGoAwayFramePayload(const char* data
, size_t len
);
608 size_t ProcessRstStreamFramePayload(const char* data
, size_t len
);
609 size_t ProcessSettingsFramePayload(const char* data
, size_t len
);
610 size_t ProcessAltSvcFramePayload(const char* data
, size_t len
);
611 size_t ProcessIgnoredControlFramePayload(/*const char* data,*/ size_t len
);
613 // TODO(jgraettinger): To be removed with migration to
614 // SpdyHeadersHandlerInterface.
615 // Serializes the last-processed header block of |hpack_decoder_| as
616 // a SPDY3 format block, and delivers it to the visitor via reentrant
617 // call to ProcessControlFrameHeaderBlock().
618 void DeliverHpackBlockAsSpdy3Block();
620 // Helpers for above internal breakouts from ProcessInput.
621 void ProcessControlFrameHeader(int control_frame_type_field
);
622 // Always passed exactly 1 setting's worth of data.
623 bool ProcessSetting(const char* data
);
625 // Retrieve serialized length of SpdyHeaderBlock. If compression is enabled, a
626 // maximum estimate is returned.
627 size_t GetSerializedLength(const SpdyHeaderBlock
& headers
);
629 // Get (and lazily initialize) the ZLib state.
630 z_stream
* GetHeaderCompressor();
631 z_stream
* GetHeaderDecompressor();
633 // Get (and lazily initialize) the HPACK state.
634 HpackEncoder
* GetHpackEncoder();
635 HpackDecoder
* GetHpackDecoder();
637 size_t GetNumberRequiredContinuationFrames(size_t size
);
639 void WritePayloadWithContinuation(SpdyFrameBuilder
* builder
,
640 const std::string
& hpack_encoding
,
641 SpdyStreamId stream_id
,
643 int padding_payload_len
);
645 // Deliver the given control frame's uncompressed headers block to the
646 // visitor in chunks. Returns true if the visitor has accepted all of the
648 bool IncrementallyDeliverControlFrameHeaderData(SpdyStreamId stream_id
,
652 // Utility to copy the given data block to the current frame buffer, up
653 // to the given maximum number of bytes, and update the buffer
654 // data (pointer and length). Returns the number of bytes
656 // *data is advanced the number of bytes read.
657 // *len is reduced by the number of bytes read.
658 size_t UpdateCurrentFrameBuffer(const char** data
, size_t* len
,
661 void WriteHeaderBlockToZ(const SpdyHeaderBlock
* headers
,
662 z_stream
* out
) const;
664 void SerializeNameValueBlockWithoutCompression(
665 SpdyFrameBuilder
* builder
,
666 const SpdyNameValueBlock
& name_value_block
) const;
668 // Compresses automatically according to enable_compression_.
669 void SerializeNameValueBlock(
670 SpdyFrameBuilder
* builder
,
671 const SpdyFrameWithNameValueBlockIR
& frame
);
673 // Set the error code and moves the framer into the error state.
674 void set_error(SpdyError error
);
676 // The size of the control frame buffer.
677 // Since this is only used for control frame headers, the maximum control
678 // frame header size (SYN_STREAM) is sufficient; all remaining control
679 // frame data is streamed to the visitor.
680 static const size_t kControlFrameBufferSize
;
682 // The maximum size of the control frames that we support.
683 // This limit is arbitrary. We can enforce it here or at the application
684 // layer. We chose the framing layer, but this can be changed (or removed)
685 // if necessary later down the line.
686 static const size_t kMaxControlFrameSize
;
689 SpdyState previous_state_
;
690 SpdyError error_code_
;
692 // Note that for DATA frame, remaining_data_length_ is sum of lengths of
693 // frame header, padding length field (optional), data payload (optional) and
694 // padding payload (optional).
695 size_t remaining_data_length_
;
697 // The length (in bytes) of the padding payload to be processed.
698 size_t remaining_padding_payload_length_
;
700 // The number of bytes remaining to read from the current control frame's
701 // headers. Note that header data blocks (for control types that have them)
702 // are part of the frame's payload, and not the frame's headers.
703 size_t remaining_control_header_
;
705 scoped_ptr
<char[]> current_frame_buffer_
;
706 // Number of bytes read into the current_frame_buffer_.
707 size_t current_frame_buffer_length_
;
709 // The type of the frame currently being read.
710 SpdyFrameType current_frame_type_
;
712 // The total length of the frame currently being read, including frame header.
713 uint32 current_frame_length_
;
715 // The stream ID field of the frame currently being read, if applicable.
716 SpdyStreamId current_frame_stream_id_
;
718 // Set this to the current stream when we receive a HEADERS, PUSH_PROMISE, or
719 // CONTINUATION frame without the END_HEADERS(0x4) bit set. These frames must
720 // be followed by a CONTINUATION frame, or else we throw a PROTOCOL_ERROR.
721 // A value of 0 indicates that we are not expecting a CONTINUATION frame.
722 SpdyStreamId expect_continuation_
;
724 // Scratch space for handling SETTINGS frames.
725 // TODO(hkhalil): Unify memory for this scratch space with
726 // current_frame_buffer_.
727 SpdySettingsScratch settings_scratch_
;
729 SpdyAltSvcScratch altsvc_scratch_
;
731 // SPDY header compressors.
732 scoped_ptr
<z_stream
> header_compressor_
;
733 scoped_ptr
<z_stream
> header_decompressor_
;
735 scoped_ptr
<HpackEncoder
> hpack_encoder_
;
736 scoped_ptr
<HpackDecoder
> hpack_decoder_
;
738 SpdyFramerVisitorInterface
* visitor_
;
739 SpdyFramerDebugVisitorInterface
* debug_visitor_
;
741 std::string display_protocol_
;
743 // The protocol version to be spoken/understood by this framer.
744 const SpdyMajorVersion protocol_version_
;
746 // The flags field of the frame currently being read.
747 uint8 current_frame_flags_
;
749 // Determines whether HPACK or gzip compression is used.
750 bool enable_compression_
;
752 // Tracks if we've ever gotten far enough in framing to see a control frame of
753 // type SYN_STREAM or SYN_REPLY.
755 // If we ever get something which looks like a data frame before we've had a
756 // SYN, we explicitly check to see if it looks like we got an HTTP response
757 // to a SPDY request. This boolean lets us do that.
758 bool syn_frame_processed_
;
760 // If we ever get a data frame before a SYN frame, we check to see if it
761 // starts with HTTP. If it does, we likely have an HTTP response. This
762 // isn't guaranteed though: we could have gotten a settings frame and then
763 // corrupt data that just looks like HTTP, but deterministic checking requires
765 bool probable_http_response_
;
767 // If a HEADERS frame is followed by a CONTINUATION frame, the FIN/END_STREAM
768 // flag is still carried in the HEADERS frame. If it's set, flip this so that
769 // we know to terminate the stream when the entire header block has been
771 bool end_stream_when_done_
;
773 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE.
774 size_t header_table_size_bound_
;
779 #endif // NET_SPDY_SPDY_FRAMER_H_