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_
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_piece.h"
14 #include "net/base/net_export.h"
18 // A handler class for SPDY headers block key/value pairs.
19 // TODO(ygi) Modify this stub handler to pass the key-value pair
20 // through the spdy logic.
21 class KeyValueHandler {
23 virtual ~KeyValueHandler() {}
24 virtual void OnKeyValuePair(const base::StringPiece& key,
25 const base::StringPiece& value) = 0;
28 // Helper class which simplifies reading contiguously
29 // from a disjoint buffer prefix & suffix.
30 class NET_EXPORT_PRIVATE SpdyHeadersBlockParserReader {
32 // StringPieces are treated as raw buffers.
33 SpdyHeadersBlockParserReader(base::StringPiece prefix,
34 base::StringPiece suffix);
35 // Number of bytes available to be read.
38 // Reads |count| bytes, copying into |*out|. Returns true on success,
39 // false if not enough bytes were available.
40 bool ReadN(size_t count, char* out);
42 // Returns a new string contiguously holding
43 // the remaining unread prefix and suffix.
44 std::vector<char> Remainder();
47 base::StringPiece prefix_;
48 base::StringPiece suffix_;
53 // This class handles SPDY headers block bytes and parses out key-value pairs
54 // as they arrive. This class is not thread-safe, and assumes that all headers
55 // block bytes are processed in a single thread.
56 class NET_EXPORT_PRIVATE SpdyHeadersBlockParser {
58 // Costructor. The handler's OnKeyValuePair will be called for every key
59 // value pair that we parsed from the headers block.
60 explicit SpdyHeadersBlockParser(KeyValueHandler* handler);
62 virtual ~SpdyHeadersBlockParser();
64 // Handles headers block data as it arrives.
65 void HandleControlFrameHeadersData(const char* headers_data, size_t len);
68 // The state of the parser.
70 READING_HEADER_BLOCK_LEN,
79 typedef SpdyHeadersBlockParserReader Reader;
81 // Parses an unsigned 32 bit integer from the reader. This method assumes that
82 // the integer is in network order and converts to host order. Returns true on
83 // success and false on failure (if there were not enough bytes available).
84 static bool ParseUInt32(Reader* reader, uint32_t* parsed_value);
86 // Resets the state of the parser to prepare it for a headers block of a
90 // Number of key-value pairs until we complete handling the current
92 uint32_t remaining_key_value_pairs_for_frame_;
94 // The length of the next field in the headers block (either a key or
96 uint32_t next_field_len_;
98 // The length of the key of the current header.
101 // Holds unprocessed bytes of the headers block.
102 std::vector<char> headers_block_prefix_;
104 // Handles key-value pairs as we parse them.
105 KeyValueHandler* handler_;
107 // Points to the current key.
108 scoped_ptr<char[]> current_key;
110 // Points to the current value.
111 scoped_ptr<char[]> current_value;
116 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_