Standardize usage of virtual/override/final specifiers in net/.
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser.h
blobc0f97cab6bdb914044b54052674afc5f14fc6ead
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"
15 namespace net {
17 // A handler class for SPDY headers.
18 class SpdyHeadersHandlerInterface {
19 public:
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
24 // the block.
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;
39 namespace test {
41 class SpdyHeadersBlockParserPeer;
43 } // namespace test
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 {
49 public:
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,
66 size_t len);
67 enum ParserError {
68 OK,
69 // Set when parsing failed due to insufficient data.
70 // This error is recoverable, by passing in new data.
71 NEED_MORE_DATA,
72 // Set when a complete block has been read, but unprocessed data remains.
73 TOO_MUCH_DATA,
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 // 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);
87 private:
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.
100 enum ParserState {
101 READING_HEADER_BLOCK_LEN,
102 READING_KEY_LEN,
103 READING_KEY,
104 READING_VALUE_LEN,
105 READING_VALUE,
106 FINISHED_HEADER
108 ParserState state_;
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
120 // headers block.
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_;
140 ParserError error_;
143 } // namespace net
145 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_