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(SpdyStreamId stream_id
,
26 uint32_t num_of_headers
) = 0;
28 // A callback method which notifies on a SPDY header key value pair.
29 virtual void OnHeader(SpdyStreamId stream_id
,
30 base::StringPiece key
,
31 base::StringPiece value
) = 0;
33 // A callback method which notifies when the parser finishes handling a SPDY
34 // headers block. Also notifies on the total number of bytes in this block.
35 virtual void OnHeaderBlockEnd(SpdyStreamId stream_id
,
36 size_t header_bytes_parsed
) = 0;
41 class SpdyHeadersBlockParserPeer
;
45 // This class handles SPDY headers block bytes and parses out key-value pairs
46 // as they arrive. This class is not thread-safe, and assumes that all headers
47 // block bytes are processed in a single thread.
48 class NET_EXPORT_PRIVATE SpdyHeadersBlockParser
{
50 // Bound on acceptable header name or value length.
51 static const size_t kMaximumFieldLength
; // = 16 * 1024
53 // Constructor. The handler's OnHeader will be called for every key
54 // value pair that we parsed from the headers block.
55 SpdyHeadersBlockParser(SpdyMajorVersion spdy_version
,
56 SpdyHeadersHandlerInterface
* handler
);
58 virtual ~SpdyHeadersBlockParser();
60 // Handles headers block data as it arrives. Returns false if an error has
61 // been set, which can include the recoverable error NEED_MORE_DATA. Returns
62 // true if the invocation completes the parse of the entire headers block,
63 // in which case the parser is ready for a new headers block.
64 bool HandleControlFrameHeadersData(SpdyStreamId stream_id
,
65 const char* headers_data
,
69 // Set when parsing failed due to insufficient data.
70 // This error is recoverable, by passing in new data.
72 // Set when a complete block has been read, but unprocessed data remains.
74 // Set when a block exceeds |MaxNumberOfHeadersForVersion| headers.
75 HEADER_BLOCK_TOO_LARGE
,
76 // Set when a header key or value exceeds |kMaximumFieldLength|.
77 HEADER_FIELD_TOO_LARGE
,
79 ParserError
get_error() const { return error_
; }
81 // Resets the state of the parser to prepare it for a headers block of a
85 // Returns the size in bytes of a length field in a SPDY header.
86 static size_t LengthFieldSizeForVersion(SpdyMajorVersion spdy_version
);
88 // Returns the maximal number of headers in a SPDY headers block.
89 static size_t MaxNumberOfHeadersForVersion(SpdyMajorVersion spdy_version
);
92 typedef SpdyPrefixedBufferReader Reader
;
94 // Parses and sanity-checks header block length.
95 void ParseBlockLength(Reader
* reader
);
97 // Parses and sanity-checks header field length.
98 void ParseFieldLength(Reader
* reader
);
100 // Parses and decodes network-order lengths into |parsed_length|.
101 void ParseLength(Reader
* reader
, uint32_t* parsed_length
);
103 // The state of the parser.
105 READING_HEADER_BLOCK_LEN
,
114 // Size in bytes of a length field in the spdy header.
115 const size_t length_field_size_
;
117 // The maximal number of headers in a SPDY headers block.
118 const size_t max_headers_in_block_
;
120 // A running total of the bytes parsed since the last call to Reset().
121 size_t total_bytes_received_
;
123 // Number of key-value pairs until we complete handling the current
125 uint32_t remaining_key_value_pairs_for_frame_
;
127 // The length of the next header field to be read (either key or value).
128 uint32_t next_field_length_
;
130 // Handles key-value pairs as we parse them.
131 SpdyHeadersHandlerInterface
* handler_
;
133 // Holds unprocessed buffer remainders between calls to
134 // |HandleControlFrameHeadersData|.
135 SpdyPinnableBufferPiece headers_block_prefix_
;
137 // Holds the key of a partially processed header between calls to
138 // |HandleControlFrameHeadersData|.
139 SpdyPinnableBufferPiece key_
;
141 // The current header block stream identifier.
142 SpdyStreamId stream_id_
;
149 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_