9b8ec8c9c3e69b87ba1ba4e4e1fe88ddb97aa174
[platform/framework/web/crosswalk.git] / src / net / spdy / spdy_headers_block_parser.h
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.
4
5 #ifndef NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_
6 #define NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_
7
8 #include <memory>
9 #include <vector>
10
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"
15
16 namespace net {
17
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 {
22  public:
23   virtual ~KeyValueHandler() {}
24   virtual void OnKeyValuePair(const base::StringPiece& key,
25                               const base::StringPiece& value) = 0;
26 };
27
28 // Helper class which simplifies reading contiguously
29 // from a disjoint buffer prefix & suffix.
30 class NET_EXPORT_PRIVATE SpdyHeadersBlockParserReader {
31  public:
32   // StringPieces are treated as raw buffers.
33   SpdyHeadersBlockParserReader(base::StringPiece prefix,
34                                base::StringPiece suffix);
35   // Number of bytes available to be read.
36   size_t Available();
37
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);
41
42   // Returns a new string contiguously holding
43   // the remaining unread prefix and suffix.
44   std::vector<char> Remainder();
45
46  private:
47   base::StringPiece prefix_;
48   base::StringPiece suffix_;
49   bool in_suffix_;
50   size_t offset_;
51 };
52
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 {
57  public:
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);
61
62   virtual ~SpdyHeadersBlockParser();
63
64   // Handles headers block data as it arrives.
65   void HandleControlFrameHeadersData(const char* headers_data, size_t len);
66
67  private:
68   // The state of the parser.
69   enum ParserState {
70     READING_HEADER_BLOCK_LEN,
71     READING_KEY_LEN,
72     READING_KEY,
73     READING_VALUE_LEN,
74     READING_VALUE
75   };
76
77   ParserState state_;
78
79   typedef SpdyHeadersBlockParserReader Reader;
80
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);
85
86   // Resets the state of the parser to prepare it for a headers block of a
87   // new frame.
88   void Reset();
89
90   // Number of key-value pairs until we complete handling the current
91   // headers block.
92   uint32_t remaining_key_value_pairs_for_frame_;
93
94   // The length of the next field in the headers block (either a key or
95   // a value).
96   uint32_t next_field_len_;
97
98   // The length of the key of the current header.
99   uint32_t key_len_;
100
101   // Holds unprocessed bytes of the headers block.
102   std::vector<char> headers_block_prefix_;
103
104   // Handles key-value pairs as we parse them.
105   KeyValueHandler* handler_;
106
107   // Points to the current key.
108   scoped_ptr<char[]> current_key;
109
110   // Points to the current value.
111   scoped_ptr<char[]> current_value;
112 };
113
114 }  // namespace net
115
116 #endif  // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_