Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / spdy / hpack_encoder.h
blob752f2f57ba53ad06523fb6dc7add3d5e4e9c3dfb
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_header_table.h"
18 #include "net/spdy/hpack_output_stream.h"
20 // An HpackEncoder encodes header sets as outlined in
21 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
23 namespace net {
25 class HpackHuffmanTable;
27 namespace test {
28 class HpackEncoderPeer;
29 } // namespace test
31 class NET_EXPORT_PRIVATE HpackEncoder {
32 public:
33 friend class test::HpackEncoderPeer;
35 // |table| is an initialized HPACK Huffman table, having an
36 // externally-managed lifetime which spans beyond HpackEncoder.
37 explicit HpackEncoder(const HpackHuffmanTable& table);
38 ~HpackEncoder();
40 // Encodes the given header set into the given string. Returns
41 // whether or not the encoding was successful.
42 bool EncodeHeaderSet(const std::map<std::string, std::string>& header_set,
43 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(
49 const std::map<std::string, std::string>& header_set,
50 std::string* output);
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);
59 // Sets externally-owned storage for aggregating character counts of emitted
60 // literal representations.
61 void SetCharCountsStorage(std::vector<size_t>* char_counts,
62 size_t* total_char_counts);
64 private:
65 typedef std::pair<base::StringPiece, base::StringPiece> Representation;
66 typedef std::vector<Representation> Representations;
68 // Emits a static/dynamic indexed representation (Section 7.1).
69 void EmitIndex(const HpackEntry* entry);
71 // Emits a literal representation (Section 7.2).
72 void EmitIndexedLiteral(const Representation& representation);
73 void EmitNonIndexedLiteral(const Representation& representation);
74 void EmitLiteral(const Representation& representation);
76 // Emits a Huffman or identity string (whichever is smaller).
77 void EmitString(base::StringPiece str);
79 void UpdateCharacterCounts(base::StringPiece str);
81 // Crumbles a cookie header into sorted, de-duplicated crumbs.
82 static void CookieToCrumbs(const Representation& cookie,
83 Representations* crumbs_out);
85 // Crumbles other header field values at \0 delimiters.
86 static void DecomposeRepresentation(const Representation& header_field,
87 Representations* out);
89 HpackHeaderTable header_table_;
90 HpackOutputStream output_stream_;
92 bool allow_huffman_compression_;
93 const HpackHuffmanTable& huffman_table_;
95 // Externally-owned, nullable storage for character counts of literals.
96 std::vector<size_t>* char_counts_;
97 size_t* total_char_counts_;
99 DISALLOW_COPY_AND_ASSIGN(HpackEncoder);
102 } // namespace net
104 #endif // NET_SPDY_HPACK_ENCODER_H_