Upstream version 5.34.104.0
[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.
19 class SpdyHeadersHandlerInterface {
20  public:
21   virtual ~SpdyHeadersHandlerInterface() {}
22
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
25   // the block.
26   virtual void OnHeaderBlock(uint32_t num_of_headers) = 0;
27
28   // A callback method which notifies when the parser finishes handling a SPDY
29   // headers block.
30   virtual void OnHeaderBlockEnd() = 0;
31
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;
35 };
36
37 // Helper class which simplifies reading contiguously
38 // from a disjoint buffer prefix & suffix.
39 class NET_EXPORT_PRIVATE SpdyHeadersBlockParserReader {
40  public:
41   // StringPieces are treated as raw buffers.
42   SpdyHeadersBlockParserReader(base::StringPiece prefix,
43                                base::StringPiece suffix);
44   // Number of bytes available to be read.
45   size_t Available();
46
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);
50
51   // Returns a new string contiguously holding
52   // the remaining unread prefix and suffix.
53   std::vector<char> Remainder();
54
55  private:
56   base::StringPiece prefix_;
57   base::StringPiece suffix_;
58   bool in_suffix_;
59   size_t offset_;
60 };
61
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 {
66  public:
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);
70
71   virtual ~SpdyHeadersBlockParser();
72
73   // Handles headers block data as it arrives.
74   void HandleControlFrameHeadersData(const char* headers_data, size_t len);
75
76  private:
77   // The state of the parser.
78   enum ParserState {
79     READING_HEADER_BLOCK_LEN,
80     READING_KEY_LEN,
81     READING_KEY,
82     READING_VALUE_LEN,
83     READING_VALUE
84   };
85
86   ParserState state_;
87
88   typedef SpdyHeadersBlockParserReader Reader;
89
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);
94
95   // Resets the state of the parser to prepare it for a headers block of a
96   // new frame.
97   void Reset();
98
99   // Number of key-value pairs until we complete handling the current
100   // headers block.
101   uint32_t remaining_key_value_pairs_for_frame_;
102
103   // The length of the next field in the headers block (either a key or
104   // a value).
105   uint32_t next_field_len_;
106
107   // The length of the key of the current header.
108   uint32_t key_len_;
109
110   // Holds unprocessed bytes of the headers block.
111   std::vector<char> headers_block_prefix_;
112
113   // Handles key-value pairs as we parse them.
114   SpdyHeadersHandlerInterface* handler_;
115
116   // Points to the current key.
117   scoped_ptr<char[]> current_key;
118
119   // Points to the current value.
120   scoped_ptr<char[]> current_value;
121 };
122
123 }  // namespace net
124
125 #endif  // NET_SPDY_SPDY_HEADERS_BLOCK_PARSER_H_