QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / spdy / spdy_headers_block_parser.h
blob66a6b810c1593f032063ee6056984899ca9bef7f
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_headers_handler_interface.h"
13 #include "net/spdy/spdy_prefixed_buffer_reader.h"
14 #include "net/spdy/spdy_protocol.h"
16 namespace net {
18 namespace test {
20 class SpdyHeadersBlockParserPeer;
22 } // namespace test
24 // This class handles SPDY headers block bytes and parses out key-value pairs
25 // as they arrive. This class is not thread-safe, and assumes that all headers
26 // block bytes are processed in a single thread.
27 class NET_EXPORT_PRIVATE SpdyHeadersBlockParser {
28 public:
29 // Bound on acceptable header name or value length.
30 static const size_t kMaximumFieldLength; // = 16 * 1024
32 // Constructor. The handler's OnHeader will be called for every key
33 // value pair that we parsed from the headers block.
34 SpdyHeadersBlockParser(SpdyMajorVersion spdy_version,
35 SpdyHeadersHandlerInterface* handler);
37 virtual ~SpdyHeadersBlockParser();
39 // Handles headers block data as it arrives. Returns false if an error has
40 // been set, which can include the recoverable error NEED_MORE_DATA. Returns
41 // true if the invocation completes the parse of the entire headers block,
42 // in which case the parser is ready for a new headers block.
43 bool HandleControlFrameHeadersData(SpdyStreamId stream_id,
44 const char* headers_data,
45 size_t len);
46 enum ParserError {
47 NO_PARSER_ERROR,
48 // Set when parsing failed due to insufficient data.
49 // This error is recoverable, by passing in new data.
50 NEED_MORE_DATA,
51 // Set when a complete block has been read, but unprocessed data remains.
52 TOO_MUCH_DATA,
53 // Set when a block exceeds |MaxNumberOfHeadersForVersion| headers.
54 HEADER_BLOCK_TOO_LARGE,
55 // Set when a header key or value exceeds |kMaximumFieldLength|.
56 HEADER_FIELD_TOO_LARGE,
57 // Set when the parser is given an unexpected stream ID.
58 UNEXPECTED_STREAM_ID,
60 ParserError get_error() const { return error_; }
62 SpdyMajorVersion spdy_version() const { return spdy_version_; }
64 // Returns the size in bytes of a length field in a SPDY header.
65 static size_t LengthFieldSizeForVersion(SpdyMajorVersion spdy_version);
67 // Returns the maximal number of headers in a SPDY headers block.
68 static size_t MaxNumberOfHeadersForVersion(SpdyMajorVersion spdy_version);
70 private:
71 typedef SpdyPrefixedBufferReader Reader;
73 // Parses and sanity-checks header block length.
74 void ParseBlockLength(Reader* reader);
76 // Parses and sanity-checks header field length.
77 void ParseFieldLength(Reader* reader);
79 // Parses and decodes network-order lengths into |parsed_length|.
80 void ParseLength(Reader* reader, uint32_t* parsed_length);
82 // The state of the parser.
83 enum ParserState {
84 READING_HEADER_BLOCK_LEN,
85 READING_KEY_LEN,
86 READING_KEY,
87 READING_VALUE_LEN,
88 READING_VALUE,
89 FINISHED_HEADER
91 ParserState state_;
93 // Size in bytes of a length field in the spdy header.
94 const size_t length_field_size_;
96 // The maximal number of headers in a SPDY headers block.
97 const size_t max_headers_in_block_;
99 // A running total of the bytes parsed since the last call to Reset().
100 size_t total_bytes_received_;
102 // Number of key-value pairs until we complete handling the current
103 // headers block.
104 uint32_t remaining_key_value_pairs_for_frame_;
106 // The length of the next header field to be read (either key or value).
107 uint32_t next_field_length_;
109 // Handles key-value pairs as we parse them.
110 SpdyHeadersHandlerInterface* handler_;
112 // Holds unprocessed buffer remainders between calls to
113 // |HandleControlFrameHeadersData|.
114 SpdyPinnableBufferPiece headers_block_prefix_;
116 // Holds the key of a partially processed header between calls to
117 // |HandleControlFrameHeadersData|.
118 SpdyPinnableBufferPiece key_;
120 // The current header block stream identifier.
121 SpdyStreamId stream_id_;
123 ParserError error_;
125 const SpdyMajorVersion spdy_version_;
128 } // namespace net
130 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_