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.
19 class SpdyHeadersHandlerInterface {
21 virtual ~SpdyHeadersHandlerInterface() {}
23 // A callback method which notifies when the parser starts handling a new
24 // SPDY headers block, this method also notifies on the number of headers in
26 virtual void OnHeaderBlock(uint32_t num_of_headers) = 0;
28 // A callback method which notifies when the parser finishes handling a SPDY
30 virtual void OnHeaderBlockEnd() = 0;
32 // A callback method which notifies on a SPDY header key value pair.
33 virtual void OnKeyValuePair(const base::StringPiece& key,
34 const base::StringPiece& value) = 0;
37 // Helper class which simplifies reading contiguously
38 // from a disjoint buffer prefix & suffix.
39 class NET_EXPORT_PRIVATE SpdyHeadersBlockParserReader {
41 // StringPieces are treated as raw buffers.
42 SpdyHeadersBlockParserReader(base::StringPiece prefix,
43 base::StringPiece suffix);
44 // Number of bytes available to be read.
47 // Reads |count| bytes, copying into |*out|. Returns true on success,
48 // false if not enough bytes were available.
49 bool ReadN(size_t count, char* out);
51 // Returns a new string contiguously holding
52 // the remaining unread prefix and suffix.
53 std::vector<char> Remainder();
56 base::StringPiece prefix_;
57 base::StringPiece suffix_;
62 // This class handles SPDY headers block bytes and parses out key-value pairs
63 // as they arrive. This class is not thread-safe, and assumes that all headers
64 // block bytes are processed in a single thread.
65 class NET_EXPORT_PRIVATE SpdyHeadersBlockParser {
67 // Costructor. The handler's OnKeyValuePair will be called for every key
68 // value pair that we parsed from the headers block.
69 explicit SpdyHeadersBlockParser(SpdyHeadersHandlerInterface* handler);
71 virtual ~SpdyHeadersBlockParser();
73 // Handles headers block data as it arrives.
74 void HandleControlFrameHeadersData(const char* headers_data, size_t len);
77 // The state of the parser.
79 READING_HEADER_BLOCK_LEN,
88 typedef SpdyHeadersBlockParserReader Reader;
90 // Parses an unsigned 32 bit integer from the reader. This method assumes that
91 // the integer is in network order and converts to host order. Returns true on
92 // success and false on failure (if there were not enough bytes available).
93 static bool ParseUInt32(Reader* reader, uint32_t* parsed_value);
95 // Resets the state of the parser to prepare it for a headers block of a
99 // Number of key-value pairs until we complete handling the current
101 uint32_t remaining_key_value_pairs_for_frame_;
103 // The length of the next field in the headers block (either a key or
105 uint32_t next_field_len_;
107 // The length of the key of the current header.
110 // Holds unprocessed bytes of the headers block.
111 std::vector<char> headers_block_prefix_;
113 // Handles key-value pairs as we parse them.
114 SpdyHeadersHandlerInterface* handler_;
116 // Points to the current key.
117 scoped_ptr<char[]> current_key;
119 // Points to the current value.
120 scoped_ptr<char[]> current_value;
125 #endif // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_