Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / spdy / hpack_input_stream.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_HPACK_INPUT_STREAM_H_
6 #define NET_SPDY_HPACK_INPUT_STREAM_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/macros.h"
13 #include "base/strings/string_piece.h"
14 #include "net/base/net_export.h"
15 #include "net/spdy/hpack_constants.h"  // For HpackPrefix.
16
17 // All section references below are to
18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-05
19 // .
20
21 namespace net {
22
23 // TODO(akalin): When we use a callback/delegate instead of a vector,
24 // use StringPiece instead of string.
25 typedef std::pair<std::string, std::string> HpackHeaderPair;
26 typedef std::vector<HpackHeaderPair> HpackHeaderPairVector;
27
28 // An HpackInputStream handles all the low-level details of decoding
29 // header fields.
30 class NET_EXPORT_PRIVATE HpackInputStream {
31  public:
32   // |max_string_literal_size| is the largest that any one string
33   // literal (header name or header value) can be.
34   HpackInputStream(uint32 max_string_literal_size, base::StringPiece buffer);
35   ~HpackInputStream();
36
37   // Returns whether or not there is more data to process.
38   bool HasMoreData() const;
39
40   // If the next octet has the top |size| bits equal to |bits|,
41   // consumes it and returns true. Otherwise, consumes nothing and
42   // returns false.
43   bool MatchPrefixAndConsume(HpackPrefix prefix);
44
45   // The Decode* functions return true and fill in their arguments if
46   // decoding was successful, or false if an error was encountered.
47
48   bool DecodeNextUint32(uint32* I);
49   bool DecodeNextStringLiteral(base::StringPiece* str);
50
51   // Accessors for testing.
52
53   void SetBitOffsetForTest(size_t bit_offset) {
54     bit_offset_ = bit_offset;
55   }
56
57   bool DecodeNextUint32ForTest(uint32* I) {
58     return DecodeNextUint32(I);
59   }
60
61   bool DecodeNextStringLiteralForTest(base::StringPiece *str) {
62     return DecodeNextStringLiteral(str);
63   }
64
65  private:
66   const uint32 max_string_literal_size_;
67   base::StringPiece buffer_;
68   size_t bit_offset_;
69
70   bool PeekNextOctet(uint8* next_octet);
71
72   bool DecodeNextOctet(uint8* next_octet);
73
74   DISALLOW_COPY_AND_ASSIGN(HpackInputStream);
75 };
76
77 }  // namespace net
78
79 #endif  // NET_SPDY_HPACK_INPUT_STREAM_H_