-
Notifications
You must be signed in to change notification settings - Fork 2
/
header_encoder.js
267 lines (238 loc) · 9.29 KB
/
header_encoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
function Encoder() {
this.buffer_ = [];
}
Encoder.prototype.encodeOctet = function(o) {
this.buffer_.push(o & 0xff);
};
// Encodes an integer I into the representation described in 4.1.1. N
// is the number of bits of the prefix as described in 4.1.1, and
// opCode is put into the top (8 - N) bytes of the first octet of the
// encoded integer.
Encoder.prototype.encodeInteger = function(opCode, N, I) {
var nextMarker = (1 << N) - 1;
var octets = [];
var origI = I;
if (I < nextMarker) {
octets.push((opCode << N) | I);
this.encodeOctet((opCode << N) | I);
//console.log("Encoding: ", origI, " on ", N, " bits with prefixVal: ", opCode, " output: ", octets);
return;
}
if (N > 0) {
octets.push((opCode << N) | nextMarker);
this.encodeOctet((opCode << N) | nextMarker);
}
I -= nextMarker;
while (I >= 128) {
octets.push(I % 128 | 128);
this.encodeOctet(I % 128 | 128);
// Divide I by 128. (Remember that / in JavaScript is
// floating-point division).
I >>= 7;
}
octets.push(I);
this.encodeOctet(I);
//console.log("Encoding: ", origI, " on ", N, " bits with prefixVal: ", opCode, " output: ", octets);
}
// Encodes the given octet sequence represented by a string as a
// length-prefixed octet sequence.
Encoder.prototype.encodeOctetSequence = function(str) {
var str_to_encode = str;
if (ENCODE_HUFFMAN) {
var code_table = CLIENT_TO_SERVER_CODEBOOK;
if (!IS_REQUEST) {
code_table = SERVER_TO_CLIENT_CODEBOOK;
}
str_to_encode = String.fromCharCode.apply(String, encodeBYTES(str, code_table));
}
//console.log("str: ", str, " len: ", str_to_encode.length, " is request: ", IS_REQUEST, " str_to_encode: ", str_to_encode);
this.encodeInteger(ENCODE_HUFFMAN, 7, str_to_encode.length);
for (var i = 0; i < str_to_encode.length; ++i) {
this.encodeOctet(str_to_encode.charCodeAt(i));
}
}
// All parameters to the encode functions below are assumed to be
// valid.
// Encode an indexed header as described in 4.2.
Encoder.prototype.encodeIndexedHeader = function(index) {
this.encodeInteger(INDEX_VALUE, INDEX_N, index);
}
// Encode a literal header without indexing as described in 4.3.1.
Encoder.prototype.encodeLiteralHeaderWithoutIndexing = function(
indexOrName, value) {
switch (typeof indexOrName) {
case 'number':
this.encodeInteger(LITERAL_NO_INDEX_VALUE, LITERAL_NO_INDEX_N,
indexOrName + 1);
this.encodeOctetSequence(value);
return;
case 'string':
this.encodeInteger(LITERAL_NO_INDEX_VALUE, LITERAL_NO_INDEX_N, 0);
this.encodeOctetSequence(indexOrName);
this.encodeOctetSequence(value);
return;
}
throw new Error('not an index or name: ' + indexOrName);
}
// Encode a literal header with incremental indexing as described in
// 4.3.2.
Encoder.prototype.encodeLiteralHeaderWithIncrementalIndexing = function(
indexOrName, value) {
switch (typeof indexOrName) {
case 'number':
this.encodeInteger(LITERAL_INCREMENTAL_VALUE, LITERAL_INCREMENTAL_N,
indexOrName + 1);
this.encodeOctetSequence(value);
return;
case 'string':
this.encodeInteger(LITERAL_INCREMENTAL_VALUE, LITERAL_INCREMENTAL_N,
0);
this.encodeOctetSequence(indexOrName);
this.encodeOctetSequence(value);
return;
}
throw new Error('not an index or name: ' + indexOrName);
}
Encoder.prototype.flush = function() {
var buffer = this.buffer_;
this.buffer_ = [];
return buffer;
}
// direction can be either REQUEST or RESPONSE, which controls the
// pre-defined header table to use. The higher compressionLevel is,
// the more this encoder tries to exercise the various encoding
// opcodes.
function HeaderEncoder(direction, compressionLevel) {
this.encodingContext_ = new EncodingContext(direction);
this.compressionLevel_ = compressionLevel;
}
HeaderEncoder.prototype.setHeaderTableMaxSize = function(maxSize) {
this.encodingContext_.setHeaderTableMaxSize(maxSize);
};
HeaderEncoder.prototype.encodeHeader_ = function(encoder, name, value) {
if (!isValidHeaderName(name)) {
throw new Error('Invalid header name: ' + name);
}
if (!isValidHeaderValue(value)) {
throw new Error('Invalid header value: ' + value);
}
// Touches are used below to track how many times a header has been
// explicitly encoded.
// Utility function to explicitly emit an entry in the reference
// set. The entry must already be touched.
var explicitlyEmitReferenceIndex = function(referenceIndex) {
if (!this.encodingContext_.isReferenced(referenceIndex)) {
throw new Error('Trying to explicitly emit entry ' + referenceIndex +
' not in reference set');
}
if (this.encodingContext_.getTouchCount(referenceIndex) === null) {
throw new Error('Trying to explicitly emit untouched entry ' +
referenceIndex);
}
for (var i = 0; i < 2; ++i) {
encoder.encodeIndexedHeader(referenceIndex);
this.encodingContext_.processIndexedHeader(referenceIndex);
}
this.encodingContext_.addTouches(referenceIndex, 1);
}.bind(this);
if (this.compressionLevel_ > 1) {
// Check to see if the header is already in the header table, and
// use the indexed header opcode if so.
var nameValueIndex =
this.encodingContext_.findIndexWithNameAndValue(name, value);
if (nameValueIndex >= 0) {
if (this.encodingContext_.isReferenced(nameValueIndex)) {
var emittedCount =
this.encodingContext_.getTouchCount(nameValueIndex);
if (emittedCount === null) {
// Mark that we've encountered this header once but haven't
// explicitly encoded it (since it's in the reference set).
this.encodingContext_.addTouches(nameValueIndex, 0);
} else if (emittedCount == 0) {
// Explicitly emit the entry twice; once for the previous
// time this header was encountered (when it wasn't
// explicitly encoded), and one for this time.
for (var i = 0; i < 2; ++i) {
//console.log("explicitlyEmitReferenceIndex: ", nameValueIndex);
explicitlyEmitReferenceIndex(nameValueIndex);
}
} else {
// We've encoded this header once for each time this was
// encountered previously, so emit the index just once for
// this time.
//console.log("explicitlyEmitReferenceIndex: ", nameValueIndex);
explicitlyEmitReferenceIndex(nameValueIndex);
}
} else {
// Mark that we've encountered this header once and explicitly
// encoded it (since it wasn't in the reference set).
//console.log("encodeIndexedHeader: ", nameValueIndex);
encoder.encodeIndexedHeader(nameValueIndex);
this.encodingContext_.processIndexedHeader(nameValueIndex);
this.encodingContext_.addTouches(nameValueIndex, 1);
}
return;
}
}
var index = -1;
if (this.compressionLevel_ > 0) {
// Check to see if the header name is already in the header table,
// and use its index if so.
index = this.encodingContext_.findIndexWithName(name);
}
// Used below when processing literal headers that may evict entries
// in the reference set.
var onReferenceSetRemoval = function(referenceIndex) { }.bind(this)
// if (this.encodingContext_.getTouchCount(referenceIndex) == 0) {
// // The implicitly emitted entry at referenceIndex will be
// // removed, so explicitly emit it.
// explicitlyEmitReferenceIndex(referenceIndex);
// }
// }.bind(this);
var indexOrName = (index >= 0) ? index : name;
if ((this.compressionLevel_ > 3)) {
// If the header name is not already in the header table, use
// incremental indexing.
//console.log("processLiteralHeaderWithIncrementalIndexing: ", name, value);
var storedIndex =
this.encodingContext_.processLiteralHeaderWithIncrementalIndexing(
name, value, onReferenceSetRemoval);
encoder.encodeLiteralHeaderWithIncrementalIndexing(indexOrName, value);
if (storedIndex >= 0) {
this.encodingContext_.addTouches(storedIndex, 1);
}
return;
}
// Don't index at all.
//console.log("encodeLiteralHeaderWithoutIndexing: ", indexOrName, value);
encoder.encodeLiteralHeaderWithoutIndexing(indexOrName, value);
};
// The given header set is encoded as an array of octets which is then
// returned. An exception will be thrown if an error is encountered.
HeaderEncoder.prototype.encodeHeaderSet = function(headerSet) {
var encoder = new Encoder();
for (var i = 0; i < headerSet.length; ++i) {
var key = headerSet[i][0];
var value = headerSet[i][1];
var values = [value];
if (key == "cookie") {
// TODO: Enable this functionality with a button.
//values = value.split('; ');
}
for (var j = 0; j < values.length; ++j) {
this.encodeHeader_(encoder, key, values[j]);
}
}
// Remove each header not in the just-encoded header set from the
// reference set.
this.encodingContext_.forEachEntry(
function(index, name, value, referenced, touchCount) {
if (referenced && (touchCount === null)) {
encoder.encodeIndexedHeader(index);
this.encodingContext_.processIndexedHeader(index);
}
this.encodingContext_.clearTouches(index);
}.bind(this));
return encoder.flush();
}