OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SPDY_HPACK_OUTPUT_STREAM_H_ | 5 #ifndef NET_SPDY_HPACK_OUTPUT_STREAM_H_ |
6 #define NET_SPDY_HPACK_OUTPUT_STREAM_H_ | 6 #define NET_SPDY_HPACK_OUTPUT_STREAM_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
14 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
15 #include "net/spdy/hpack_constants.h" // For HpackPrefix. | 15 #include "net/spdy/hpack_constants.h" |
16 | 16 |
17 // All section references below are to | 17 // All section references below are to |
18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 | 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 // An HpackOutputStream handles all the low-level details of encoding | 22 // An HpackOutputStream handles all the low-level details of encoding |
23 // header fields. | 23 // header fields. |
24 class NET_EXPORT_PRIVATE HpackOutputStream { | 24 class NET_EXPORT_PRIVATE HpackOutputStream { |
25 public: | 25 public: |
26 // |max_string_literal_size| is the largest that any one string | 26 explicit HpackOutputStream(); |
27 // |literal (header name or header value) can be. | |
28 explicit HpackOutputStream(uint32 max_string_literal_size); | |
29 ~HpackOutputStream(); | 27 ~HpackOutputStream(); |
30 | 28 |
31 // Corresponds to 4.2. | |
32 void AppendIndexedHeader(uint32 index_or_zero); | |
33 | |
34 // Corresponds to 4.3.1 (second form). Returns whether or not the | |
35 // append was successful; if the append was unsuccessful, no other | |
36 // member function may be called. | |
37 bool AppendLiteralHeaderNoIndexingWithName(base::StringPiece name, | |
38 base::StringPiece value); | |
39 | |
40 // Moves the internal buffer to the given string and clears all | |
41 // internal state. | |
42 void TakeString(std::string* output); | |
43 | |
44 // Appends the lower |bit_size| bits of |bits| to the internal buffer. | 29 // Appends the lower |bit_size| bits of |bits| to the internal buffer. |
45 // | 30 // |
46 // |bit_size| must be > 0 and <= 8. |bits| must not have any bits | 31 // |bit_size| must be > 0 and <= 8. |bits| must not have any bits |
47 // set other than the lower |bit_size| bits. | 32 // set other than the lower |bit_size| bits. |
48 void AppendBits(uint8 bits, size_t bit_size); | 33 void AppendBits(uint8 bits, size_t bit_size); |
49 | 34 |
50 // Accessors for testing. | |
51 | |
52 void AppendBitsForTest(uint8 bits, size_t size) { | |
53 AppendBits(bits, size); | |
54 } | |
55 | |
56 void AppendUint32ForTest(uint32 I) { | |
57 AppendUint32(I); | |
58 } | |
59 | |
60 bool AppendStringLiteralForTest(base::StringPiece str) { | |
61 return AppendStringLiteral(str); | |
62 } | |
63 | |
64 private: | |
65 // Simply forwards to AppendBits(prefix.bits, prefix.bit-size). | 35 // Simply forwards to AppendBits(prefix.bits, prefix.bit-size). |
66 void AppendPrefix(HpackPrefix prefix); | 36 void AppendPrefix(HpackPrefix prefix); |
67 | 37 |
| 38 // Directly appends |buffer|. |
| 39 void AppendBytes(base::StringPiece buffer); |
| 40 |
68 // Appends the given integer using the representation described in | 41 // Appends the given integer using the representation described in |
69 // 4.1.1. If the internal buffer ends on a byte boundary, the prefix | 42 // 4.1.1. If the internal buffer ends on a byte boundary, the prefix |
70 // length N is taken to be 8; otherwise, it is taken to be the | 43 // length N is taken to be 8; otherwise, it is taken to be the |
71 // number of bits to the next byte boundary. | 44 // number of bits to the next byte boundary. |
72 // | 45 // |
73 // It is guaranteed that the internal buffer will end on a byte | 46 // It is guaranteed that the internal buffer will end on a byte |
74 // boundary after this function is called. | 47 // boundary after this function is called. |
75 void AppendUint32(uint32 I); | 48 void AppendUint32(uint32 I); |
76 | 49 |
77 // Appends the given string using the representation described in | 50 // Swaps the interal buffer with |output|. |
78 // 4.1.2. The internal buffer must end on a byte boundary, and it is | 51 void TakeString(std::string* output); |
79 // guaranteed that the internal buffer will end on a byte boundary | |
80 // after this function is called. Returns whether or not the append | |
81 // was successful; if the append was unsuccessful, no other member | |
82 // function may be called. | |
83 bool AppendStringLiteral(base::StringPiece str); | |
84 | 52 |
85 const uint32 max_string_literal_size_; | 53 private: |
86 | |
87 // The internal bit buffer. | 54 // The internal bit buffer. |
88 std::string buffer_; | 55 std::string buffer_; |
89 | 56 |
90 // If 0, the buffer ends on a byte boundary. If non-zero, the buffer | 57 // If 0, the buffer ends on a byte boundary. If non-zero, the buffer |
91 // ends on the most significant nth bit. Guaranteed to be < 8. | 58 // ends on the most significant nth bit. Guaranteed to be < 8. |
92 size_t bit_offset_; | 59 size_t bit_offset_; |
93 | 60 |
94 DISALLOW_COPY_AND_ASSIGN(HpackOutputStream); | 61 DISALLOW_COPY_AND_ASSIGN(HpackOutputStream); |
95 }; | 62 }; |
96 | 63 |
97 } // namespace net | 64 } // namespace net |
98 | 65 |
99 #endif // NET_SPDY_HPACK_OUTPUT_STREAM_H_ | 66 #endif // NET_SPDY_HPACK_OUTPUT_STREAM_H_ |
OLD | NEW |