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_ENCODER_H_ | 5 #ifndef NET_SPDY_HPACK_ENCODER_H_ |
6 #define NET_SPDY_HPACK_ENCODER_H_ | 6 #define NET_SPDY_HPACK_ENCODER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
15 #include "net/spdy/hpack_encoding_context.h" | 16 #include "net/spdy/hpack_header_table.h" |
| 17 #include "net/spdy/hpack_output_stream.h" |
16 | 18 |
17 // An HpackEncoder encodes header sets as outlined in | 19 // An HpackEncoder encodes header sets as outlined in |
18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 | 20 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 |
19 | 21 |
20 namespace net { | 22 namespace net { |
21 | 23 |
| 24 class HpackHuffmanTable; |
| 25 |
22 namespace test { | 26 namespace test { |
23 class HpackEncoderPeer; | 27 class HpackEncoderPeer; |
24 } // namespace test | 28 } // namespace test |
25 | 29 |
26 class NET_EXPORT_PRIVATE HpackEncoder { | 30 class NET_EXPORT_PRIVATE HpackEncoder { |
27 public: | 31 public: |
28 friend class test::HpackEncoderPeer; | 32 friend class test::HpackEncoderPeer; |
29 | 33 |
30 explicit HpackEncoder(); | 34 // |table| is an initialized HPACK Huffman table, having an |
| 35 // externally-managed lifetime which spans beyond HpackEncoder. |
| 36 explicit HpackEncoder(const HpackHuffmanTable& table); |
31 ~HpackEncoder(); | 37 ~HpackEncoder(); |
32 | 38 |
33 // Encodes the given header set into the given string. Returns | 39 // Encodes the given header set into the given string. Returns |
34 // whether or not the encoding was successful. | 40 // whether or not the encoding was successful. |
35 bool EncodeHeaderSet(const std::map<std::string, std::string>& header_set, | 41 bool EncodeHeaderSet(const std::map<std::string, std::string>& header_set, |
36 std::string* output); | 42 std::string* output); |
37 | 43 |
| 44 // Encodes the given header set into the given string. Only non-indexed |
| 45 // literal representations are emitted, bypassing the header table. Huffman |
| 46 // coding is also not used. Returns whether the encoding was successful. |
| 47 // TODO(jgraettinger): Enable Huffman coding once the table as stablized. |
| 48 bool EncodeHeaderSetWithoutCompression( |
| 49 const std::map<std::string, std::string>& header_set, |
| 50 std::string* output); |
| 51 |
| 52 // Called upon a change to SETTINGS_HEADER_TABLE_SIZE. Specifically, this |
| 53 // is to be called after receiving (and sending an acknowledgement for) a |
| 54 // SETTINGS_HEADER_TABLE_SIZE update from the remote decoding endpoint. |
| 55 void ApplyHeaderTableSizeSetting(size_t size_setting) { |
| 56 header_table_.SetSettingsHeaderTableSize(size_setting); |
| 57 } |
| 58 |
38 private: | 59 private: |
39 static void CookieToCrumbs(base::StringPiece cookie, | 60 typedef std::pair<base::StringPiece, base::StringPiece> Representation; |
40 std::vector<base::StringPiece>* out); | 61 typedef std::vector<Representation> Representations; |
41 | 62 |
42 uint32 max_string_literal_size_; | 63 // Emits a static/dynamic indexed representation (Section 4.2). |
43 HpackEncodingContext context_; | 64 void EmitDynamicIndex(HpackEntry* entry); |
| 65 void EmitStaticIndex(HpackEntry* entry); |
| 66 |
| 67 // Emits a literal representation (Section 4.3). |
| 68 void EmitIndexedLiteral(const Representation& representation); |
| 69 void EmitNonIndexedLiteral(const Representation& representation); |
| 70 void EmitLiteral(const Representation& representation); |
| 71 |
| 72 // Emits a Huffman or identity string (whichever is smaller). |
| 73 void EmitString(base::StringPiece str); |
| 74 |
| 75 // Determines the representation delta required to encode |header_set| in |
| 76 // the current header table context. Entries in the reference set are |
| 77 // enumerated and marked with membership in the current |header_set|. |
| 78 // Representations which must be explicitly emitted are returned. |
| 79 Representations DetermineEncodingDelta( |
| 80 const std::map<std::string, std::string>& header_set); |
| 81 |
| 82 static void CookieToCrumbs(const Representation& cookie, |
| 83 Representations* crumbs_out); |
| 84 |
| 85 HpackHeaderTable header_table_; |
| 86 HpackOutputStream output_stream_; |
| 87 |
| 88 bool allow_huffman_compression_; |
| 89 const HpackHuffmanTable& huffman_table_; |
44 | 90 |
45 DISALLOW_COPY_AND_ASSIGN(HpackEncoder); | 91 DISALLOW_COPY_AND_ASSIGN(HpackEncoder); |
46 }; | 92 }; |
47 | 93 |
48 } // namespace net | 94 } // namespace net |
49 | 95 |
50 #endif // NET_SPDY_HPACK_ENCODER_H_ | 96 #endif // NET_SPDY_HPACK_ENCODER_H_ |
OLD | NEW |