Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / net / spdy / hpack / hpack_encoder.h
blobd95ea11845f55e7e43b5d6642f079f226d0435ae
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.
5 #ifndef NET_SPDY_HPACK_ENCODER_H_
6 #define NET_SPDY_HPACK_ENCODER_H_
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/macros.h"
15 #include "base/strings/string_piece.h"
16 #include "net/base/net_export.h"
17 #include "net/spdy/hpack/hpack_header_table.h"
18 #include "net/spdy/hpack/hpack_output_stream.h"
19 #include "net/spdy/spdy_protocol.h"
21 // An HpackEncoder encodes header sets as outlined in
22 // http://tools.ietf.org/html/rfc7541.
24 namespace net {
26 class HpackHuffmanTable;
28 namespace test {
29 class HpackEncoderPeer;
30 } // namespace test
32 class NET_EXPORT_PRIVATE HpackEncoder {
33 public:
34 friend class test::HpackEncoderPeer;
36 // |table| is an initialized HPACK Huffman table, having an
37 // externally-managed lifetime which spans beyond HpackEncoder.
38 explicit HpackEncoder(const HpackHuffmanTable& table);
39 ~HpackEncoder();
41 // Encodes the given header set into the given string. Returns
42 // whether or not the encoding was successful.
43 bool EncodeHeaderSet(const SpdyHeaderBlock& header_set, std::string* output);
45 // Encodes the given header set into the given string. Only non-indexed
46 // literal representations are emitted, bypassing the header table. Huffman
47 // coding is also not used. Returns whether the encoding was successful.
48 bool EncodeHeaderSetWithoutCompression(const SpdyHeaderBlock& header_set,
49 std::string* output);
51 // Called upon a change to SETTINGS_HEADER_TABLE_SIZE. Specifically, this
52 // is to be called after receiving (and sending an acknowledgement for) a
53 // SETTINGS_HEADER_TABLE_SIZE update from the remote decoding endpoint.
54 void ApplyHeaderTableSizeSetting(size_t size_setting) {
55 header_table_.SetSettingsHeaderTableSize(size_setting);
58 // Sets externally-owned storage for aggregating character counts of emitted
59 // literal representations.
60 void SetCharCountsStorage(std::vector<size_t>* char_counts,
61 size_t* total_char_counts);
63 private:
64 typedef std::pair<base::StringPiece, base::StringPiece> Representation;
65 typedef std::vector<Representation> Representations;
67 // Emits a static/dynamic indexed representation (Section 7.1).
68 void EmitIndex(const HpackEntry* entry);
70 // Emits a literal representation (Section 7.2).
71 void EmitIndexedLiteral(const Representation& representation);
72 void EmitNonIndexedLiteral(const Representation& representation);
73 void EmitLiteral(const Representation& representation);
75 // Emits a Huffman or identity string (whichever is smaller).
76 void EmitString(base::StringPiece str);
78 void UpdateCharacterCounts(base::StringPiece str);
80 // Crumbles a cookie header into ";" delimited crumbs.
81 static void CookieToCrumbs(const Representation& cookie,
82 Representations* crumbs_out);
84 // Crumbles other header field values at \0 delimiters.
85 static void DecomposeRepresentation(const Representation& header_field,
86 Representations* out);
88 HpackHeaderTable header_table_;
89 HpackOutputStream output_stream_;
91 bool allow_huffman_compression_;
92 const HpackHuffmanTable& huffman_table_;
94 // Externally-owned, nullable storage for character counts of literals.
95 std::vector<size_t>* char_counts_;
96 size_t* total_char_counts_;
98 DISALLOW_COPY_AND_ASSIGN(HpackEncoder);
101 } // namespace net
103 #endif // NET_SPDY_HPACK_ENCODER_H_