1 // Copyright 2014 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_HEADERS_BLOCK_PARSER_H_
6 #define NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_piece.h"
11 #include "net/base/net_export.h"
12 #include "net/spdy/spdy_prefixed_buffer_reader.h"
13 #include "net/spdy/spdy_protocol.h"
17 // A handler class for SPDY headers.
18 class SpdyHeadersHandlerInterface
{
20 virtual ~SpdyHeadersHandlerInterface() {}
22 // A callback method which notifies when the parser starts handling a new
23 // SPDY headers block, this method also notifies on the number of headers in
25 virtual void OnHeaderBlock(uint32_t num_of_headers
) = 0;
27 // A callback method which notifies on a SPDY header key value pair.
28 virtual void OnHeader(base::StringPiece key
, base::StringPiece value
) = 0;
30 // A callback method which notifies when the parser finishes handling a SPDY
31 // headers block. Also notifies on the total number of bytes in this block.
32 virtual void OnHeaderBlockEnd(size_t header_bytes_parsed
) = 0;
37 class SpdyHeadersBlockParserPeer
;
41 // This class handles SPDY headers block bytes and parses out key-value pairs
42 // as they arrive. This class is not thread-safe, and assumes that all headers
43 // block bytes are processed in a single thread.
44 class NET_EXPORT_PRIVATE SpdyHeadersBlockParser
{
46 // Bound on acceptable header name or value length.
47 static const size_t kMaximumFieldLength
; // = 16 * 1024
49 // Constructor. The handler's OnHeader will be called for every key
50 // value pair that we parsed from the headers block.
51 SpdyHeadersBlockParser(SpdyMajorVersion spdy_version
,
52 SpdyHeadersHandlerInterface
* handler
);
54 virtual ~SpdyHeadersBlockParser();
56 // Handles headers block data as it arrives. Returns false if an error has
57 // been set, which can include the recoverable error NEED_MORE_DATA. Returns
58 // true if the invocation completes the parse of the entire headers block,
59 // in which case the parser is ready for a new headers block.
60 bool HandleControlFrameHeadersData(SpdyStreamId stream_id
,
61 const char* headers_data
,
65 // Set when parsing failed due to insufficient data.
66 // This error is recoverable, by passing in new data.
68 // Set when a complete block has been read, but unprocessed data remains.
70 // Set when a block exceeds |MaxNumberOfHeadersForVersion| headers.
71 HEADER_BLOCK_TOO_LARGE
,
72 // Set when a header key or value exceeds |kMaximumFieldLength|.
73 HEADER_FIELD_TOO_LARGE
,
74 // Set when the parser is given an unexpected stream ID.
77 ParserError
get_error() const { return error_
; }
79 SpdyMajorVersion
spdy_version() const { return spdy_version_
; }
81 // Returns the size in bytes of a length field in a SPDY header.
82 static size_t LengthFieldSizeForVersion(SpdyMajorVersion spdy_version
);
84 // Returns the maximal number of headers in a SPDY headers block.
85 static size_t MaxNumberOfHeadersForVersion(SpdyMajorVersion spdy_version
);
88 typedef SpdyPrefixedBufferReader Reader
;
90 // Parses and sanity-checks header block length.
91 void ParseBlockLength(Reader
* reader
);
93 // Parses and sanity-checks header field length.
94 void ParseFieldLength(Reader
* reader
);
96 // Parses and decodes network-order lengths into |parsed_length|.
97 void ParseLength(Reader
* reader
, uint32_t* parsed_length
);
99 // The state of the parser.
101 READING_HEADER_BLOCK_LEN
,
110 // Size in bytes of a length field in the spdy header.
111 const size_t length_field_size_
;
113 // The maximal number of headers in a SPDY headers block.
114 const size_t max_headers_in_block_
;
116 // A running total of the bytes parsed since the last call to Reset().
117 size_t total_bytes_received_
;
119 // Number of key-value pairs until we complete handling the current
121 uint32_t remaining_key_value_pairs_for_frame_
;
123 // The length of the next header field to be read (either key or value).
124 uint32_t next_field_length_
;
126 // Handles key-value pairs as we parse them.
127 SpdyHeadersHandlerInterface
* handler_
;
129 // Holds unprocessed buffer remainders between calls to
130 // |HandleControlFrameHeadersData|.
131 SpdyPinnableBufferPiece headers_block_prefix_
;
133 // Holds the key of a partially processed header between calls to
134 // |HandleControlFrameHeadersData|.
135 SpdyPinnableBufferPiece key_
;
137 // The current header block stream identifier.
138 SpdyStreamId stream_id_
;
142 const SpdyMajorVersion spdy_version_
;
147 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_