-
Notifications
You must be signed in to change notification settings - Fork 2
/
header_decoder.js
289 lines (261 loc) · 9.25 KB
/
header_decoder.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
// emitFunction will be called with the name and value of each header
// encountered while decoding.
function Decoder(buffer, encodingContext, emitFunction) {
this.buffer_ = buffer;
this.i_ = 0;
this.encodingContext_ = encodingContext;
this.emitFunction_ = emitFunction;
this.opcodeStack_ = [];
this.currentOpcode_ = [];
}
Decoder.prototype.pushOntoCurrentOpcode = function(x) {
this.currentOpcode_.push(x);
}
function octetsToHex(octets, use_spaces) {
var str = '';
for (var i = 0; i < octets.length; ++i) {
var octet = octets[i];
if (octet < 16) {
str += '0';
}
str += '' + octet.toString(16);
if (use_spaces) str += ' ';
}
return str;
}
function formatOpcode(opFields) {
// opFields, [operation, ... operation]
// operation, {name: 'blah', data: {key1: foo, key2: ...} }
var output = '';
var cols = 0;
function addSpaces(numSpaces) {
for (var k = 0; k < numSpaces; ++k) output += ' ';
cols += numSpaces;
}
//console.log(opFields);
var tabLen = 0;
for (var i = 0; i < opFields.length; ++i) {
var name = opFields[i].name;
var data = opFields[i].data;
addSpaces(tabLen);
output += name + ':\n';
tabLen = 4;
for (var key in data) {
var entry = data[key];
if (key == "encoded") {
entry = '"' + octetsToHex(entry) + '"';
} else {
entry = entry.toString();
}
addSpaces(tabLen + 4);
output += key + ': ' + entry + '\n';
}
tabLen = 4;
}
return output;
}
Decoder.prototype.finishedCurrentOpcode = function() {
//console.log(formatOpcode(this.currentOpcode_));
this.opcodeStack_.push(this.currentOpcode_);
this.currentOpcode_ = [];
}
Decoder.prototype.getFormattedOpcodeList = function() {
var output = "";
for (var i = 0; i < this.opcodeStack_.length; ++i) {
output += formatOpcode(this.opcodeStack_[i]) + '\n';
}
return output;
}
Decoder.prototype.hasMoreData = function() {
return this.i_ < this.buffer_.length;
};
Decoder.prototype.peekNextOctet_ = function() {
if (!this.hasMoreData()) {
throw new Error('Unexpected end of buffer');
}
return this.buffer_[this.i_] & 0xff;
};
Decoder.prototype.decodeNextOctet_ = function() {
var nextOctet = this.peekNextOctet_();
++this.i_;
return nextOctet;
};
// Decodes the next integer based on the representation described in
// 4.1.1. N is the number of bits of the prefix as described in 4.1.1.
Decoder.prototype.decodeNextInteger_ = function(N, description) {
var I = 0;
var hasMore = true;
var R = 0;
var shift = 0;
var start = this.i_;
if (!description) description = "int";
if (N > 0) {
var nextMarker = (1 << N) - 1;
var nextOctet = this.decodeNextOctet_();
I = nextOctet & nextMarker;
hasMore = (I == nextMarker);
}
while (hasMore) {
var nextOctet = this.decodeNextOctet_();
// Check the high bit. (Remember that / in JavaScript is
// floating-point division).
hasMore = ((nextOctet & 0x80) != 0);
R += (nextOctet % 128) << shift;
shift += 7;
}
I += R;
var data = this.buffer_.slice(start, this.i_);
this.pushOntoCurrentOpcode( {name: description,
data: {
//prefixLen: N,
encoded: data,
decoded: I}} );
//console.log("Decoded", description, ": ", I, "from: ", this.buffer_.slice(start, this.i_));
return I;
};
// Decodes the next length-prefixed octet sequence and returns it as a
// string with character codes representing the octets.
Decoder.prototype.decodeNextOctetSequence_ = function(description) {
var is_huffman_encoded = this.peekNextOctet_() >> 7 & 1;
var length = this.decodeNextInteger_(7, description + "_length");
var data = this.buffer_.slice(this.i_, this.i_ + length);
var str = '';
if (is_huffman_encoded) {
var inv_code_table = INVERSE_CLIENT_TO_SERVER_CODEBOOK;
if (!IS_REQUEST) {
inv_code_table = INVERSE_SERVER_TO_CLIENT_CODEBOOK;
}
//console.log("decoding huffman data:", data);
str = decodeBYTES(data, 0, inv_code_table).str;
this.i_ += length;
} else {
for (var i = 0; i < length; ++i) {
var nextOctet = this.decodeNextOctet_();
str += String.fromCharCode(nextOctet);
}
}
this.pushOntoCurrentOpcode( { name: description,
data: {
is_huffman_encoded: is_huffman_encoded,
encoded: data,
decoded: '"' + str + '"'}} );
//console.log("Decoded str: ", str, " len: ", length,
// "is_huffman_encoded: ", is_huffman_encoded,
// "is_request: ", IS_REQUEST, "from: ", data);
return str;
};
// Decodes the next header name based on the representation described
// in 4.1.2. N is the number of bits of the prefix of the length of
// the header name as described in 4.1.1.
Decoder.prototype.decodeNextName_ = function(N) {
var indexPlusOneOrZero = this.decodeNextInteger_(N, "name_index");
var name = null;
if (indexPlusOneOrZero == 0) {
name = this.decodeNextOctetSequence_("name_data");
} else {
var index = indexPlusOneOrZero - 1;
name = this.encodingContext_.getIndexedHeaderName(index);
}
if (!isValidHeaderName(name)) {
throw new Error('Invalid header name: ' + name);
}
return name;
};
// Decodes the next header value based on the representation described
// in 4.1.3.
Decoder.prototype.decodeNextValue_ = function() {
var value = this.decodeNextOctetSequence_("value_data");
if (!isValidHeaderValue(value)) {
throw new Error('Invalid header value: ' + value);
}
return value;
};
function determineOpcode(nextOctet) {
var opcode = OPCODES.UNKNOWN_OPCODE;
if ((nextOctet >> INDEX_N) == INDEX_VALUE) {
opcode = OPCODES.INDEX_OPCODE;
} else if ((nextOctet >> LITERAL_NO_INDEX_N) == LITERAL_NO_INDEX_VALUE) {
opcode = OPCODES.LITERAL_NO_INDEX_OPCODE;
} else if((nextOctet >> LITERAL_INCREMENTAL_N) == LITERAL_INCREMENTAL_VALUE) {
opcode = OPCODES.LITERAL_INCREMENTAL_OPCODE;
}
return opcode;
}
// Processes the next header representation as described in 3.2.1.
Decoder.prototype.processNextHeaderRepresentation = function() {
var nextOctet = this.peekNextOctet_();
var opcodeStartIndex = this.i_;
var opcode = determineOpcode(nextOctet);
this.pushOntoCurrentOpcode({name: opcode.name,
data: {
opcodeLengthInBits: opcode.opcode_len,
discoveredFromPeekingAtByte:
"'" + octetsToHex([nextOctet]) + "'"}});
// Touches are used below to track which headers have been emitted.
switch (opcode) {
case OPCODES.INDEX_OPCODE:
var index = this.decodeNextInteger_(7, "entry_index");
this.encodingContext_.processIndexedHeader(index);
if (!this.encodingContext_.isReferenced(index)) {
break;
}
this.encodingContext_.addTouches(index, 0);
var name = this.encodingContext_.getIndexedHeaderName(index);
var value = this.encodingContext_.getIndexedHeaderValue(index);
this.emitFunction_(name, value);
break;
case OPCODES.LITERAL_INCREMENTAL_OPCODE:
// Literal header with incremental indexing (4.3.2).
var name = this.decodeNextName_(LITERAL_INCREMENTAL_N);
var value = this.decodeNextValue_();
var index =
this.encodingContext_.processLiteralHeaderWithIncrementalIndexing(
name, value, function(referenceIndex) { /* Do nothing */ });
if (index >= 0) {
this.encodingContext_.addTouches(index, 0);
}
this.emitFunction_(name, value);
break;
case OPCODES.LITERAL_NO_INDEX_OPCODE:
// Literal header without indexing (4.3.1).
var name = this.decodeNextName_(LITERAL_NO_INDEX_N);
var value = this.decodeNextValue_();
this.emitFunction_(name, value);
break;
default:
throw new Error('Could not decode opcode from ' + nextOctet);
}
this.finishedCurrentOpcode();
};
// direction can be either REQUEST or RESPONSE, which controls the
// pre-defined header table to use.
function HeaderDecoder(direction) {
this.encodingContext_ = new EncodingContext(direction);
}
HeaderDecoder.prototype.setHeaderTableMaxSize = function(maxSize) {
this.encodingContext_.setHeaderTableMaxSize(maxSize);
};
// encodedHeaderSet must be the complete encoding of an header set,
// represented as an array of octets. emitFunction will be called with
// the name and value of each header in the header set. An exception
// will be thrown if an error is encountered.
HeaderDecoder.prototype.decodeHeaderSet = function(
encodedHeaderSet, emitFunction) {
var decoder =
new Decoder(encodedHeaderSet, this.encodingContext_, emitFunction);
while (decoder.hasMoreData()) {
decoder.processNextHeaderRepresentation();
}
var formattedOpcodes = decoder.getFormattedOpcodeList();
// Emits each header contained in the reference set that has not
// already been emitted as described in 3.2.2.
this.encodingContext_.forEachEntry(
function(index, name, value, referenced, touchCount) {
if (referenced && (touchCount === null)) {
emitFunction(name, value);
}
this.encodingContext_.clearTouches(index);
}.bind(this));
return formattedOpcodes;
};