| 1 | /*jshint node:true */
|
|---|
| 2 |
|
|---|
| 3 | exports.HTTPParser = HTTPParser;
|
|---|
| 4 | function HTTPParser(type) {
|
|---|
| 5 | if (type !== undefined && type !== HTTPParser.REQUEST && type !== HTTPParser.RESPONSE) {
|
|---|
| 6 | throw new Error('type must be REQUEST or RESPONSE');
|
|---|
| 7 | }
|
|---|
| 8 | if (type === undefined) {
|
|---|
| 9 | // Node v12+
|
|---|
| 10 | } else {
|
|---|
| 11 | this.initialize(type);
|
|---|
| 12 | }
|
|---|
| 13 | this.maxHeaderSize=HTTPParser.maxHeaderSize
|
|---|
| 14 | }
|
|---|
| 15 | HTTPParser.prototype.initialize = function (type, async_resource) {
|
|---|
| 16 | if (type !== HTTPParser.REQUEST && type !== HTTPParser.RESPONSE) {
|
|---|
| 17 | throw new Error('type must be REQUEST or RESPONSE');
|
|---|
| 18 | }
|
|---|
| 19 | this.type = type;
|
|---|
| 20 | this.state = type + '_LINE';
|
|---|
| 21 | this.info = {
|
|---|
| 22 | headers: [],
|
|---|
| 23 | upgrade: false
|
|---|
| 24 | };
|
|---|
| 25 | this.trailers = [];
|
|---|
| 26 | this.line = '';
|
|---|
| 27 | this.isChunked = false;
|
|---|
| 28 | this.connection = '';
|
|---|
| 29 | this.headerSize = 0; // for preventing too big headers
|
|---|
| 30 | this.body_bytes = null;
|
|---|
| 31 | this.isUserCall = false;
|
|---|
| 32 | this.hadError = false;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | HTTPParser.encoding = 'ascii';
|
|---|
| 36 | HTTPParser.maxHeaderSize = 80 * 1024; // maxHeaderSize (in bytes) is configurable, but 80kb by default;
|
|---|
| 37 | HTTPParser.REQUEST = 'REQUEST';
|
|---|
| 38 | HTTPParser.RESPONSE = 'RESPONSE';
|
|---|
| 39 |
|
|---|
| 40 | // Note: *not* starting with kOnHeaders=0 line the Node parser, because any
|
|---|
| 41 | // newly added constants (kOnTimeout in Node v12.19.0) will overwrite 0!
|
|---|
| 42 | var kOnHeaders = HTTPParser.kOnHeaders = 1;
|
|---|
| 43 | var kOnHeadersComplete = HTTPParser.kOnHeadersComplete = 2;
|
|---|
| 44 | var kOnBody = HTTPParser.kOnBody = 3;
|
|---|
| 45 | var kOnMessageComplete = HTTPParser.kOnMessageComplete = 4;
|
|---|
| 46 |
|
|---|
| 47 | // Some handler stubs, needed for compatibility
|
|---|
| 48 | HTTPParser.prototype[kOnHeaders] =
|
|---|
| 49 | HTTPParser.prototype[kOnHeadersComplete] =
|
|---|
| 50 | HTTPParser.prototype[kOnBody] =
|
|---|
| 51 | HTTPParser.prototype[kOnMessageComplete] = function () {};
|
|---|
| 52 |
|
|---|
| 53 | var compatMode0_12 = true;
|
|---|
| 54 | Object.defineProperty(HTTPParser, 'kOnExecute', {
|
|---|
| 55 | get: function () {
|
|---|
| 56 | // hack for backward compatibility
|
|---|
| 57 | compatMode0_12 = false;
|
|---|
| 58 | return 99;
|
|---|
| 59 | }
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | var methods = exports.methods = HTTPParser.methods = [
|
|---|
| 63 | 'DELETE',
|
|---|
| 64 | 'GET',
|
|---|
| 65 | 'HEAD',
|
|---|
| 66 | 'POST',
|
|---|
| 67 | 'PUT',
|
|---|
| 68 | 'CONNECT',
|
|---|
| 69 | 'OPTIONS',
|
|---|
| 70 | 'TRACE',
|
|---|
| 71 | 'COPY',
|
|---|
| 72 | 'LOCK',
|
|---|
| 73 | 'MKCOL',
|
|---|
| 74 | 'MOVE',
|
|---|
| 75 | 'PROPFIND',
|
|---|
| 76 | 'PROPPATCH',
|
|---|
| 77 | 'SEARCH',
|
|---|
| 78 | 'UNLOCK',
|
|---|
| 79 | 'BIND',
|
|---|
| 80 | 'REBIND',
|
|---|
| 81 | 'UNBIND',
|
|---|
| 82 | 'ACL',
|
|---|
| 83 | 'REPORT',
|
|---|
| 84 | 'MKACTIVITY',
|
|---|
| 85 | 'CHECKOUT',
|
|---|
| 86 | 'MERGE',
|
|---|
| 87 | 'M-SEARCH',
|
|---|
| 88 | 'NOTIFY',
|
|---|
| 89 | 'SUBSCRIBE',
|
|---|
| 90 | 'UNSUBSCRIBE',
|
|---|
| 91 | 'PATCH',
|
|---|
| 92 | 'PURGE',
|
|---|
| 93 | 'MKCALENDAR',
|
|---|
| 94 | 'LINK',
|
|---|
| 95 | 'UNLINK',
|
|---|
| 96 | 'SOURCE',
|
|---|
| 97 | ];
|
|---|
| 98 | var method_connect = methods.indexOf('CONNECT');
|
|---|
| 99 | HTTPParser.prototype.reinitialize = HTTPParser;
|
|---|
| 100 | HTTPParser.prototype.close =
|
|---|
| 101 | HTTPParser.prototype.pause =
|
|---|
| 102 | HTTPParser.prototype.resume =
|
|---|
| 103 | HTTPParser.prototype.remove =
|
|---|
| 104 | HTTPParser.prototype.free = function () {};
|
|---|
| 105 | HTTPParser.prototype._compatMode0_11 = false;
|
|---|
| 106 | HTTPParser.prototype.getAsyncId = function() { return 0; };
|
|---|
| 107 |
|
|---|
| 108 | var headerState = {
|
|---|
| 109 | REQUEST_LINE: true,
|
|---|
| 110 | RESPONSE_LINE: true,
|
|---|
| 111 | HEADER: true
|
|---|
| 112 | };
|
|---|
| 113 | HTTPParser.prototype.execute = function (chunk, start, length) {
|
|---|
| 114 | if (!(this instanceof HTTPParser)) {
|
|---|
| 115 | throw new TypeError('not a HTTPParser');
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // backward compat to node < 0.11.4
|
|---|
| 119 | // Note: the start and length params were removed in newer version
|
|---|
| 120 | start = start || 0;
|
|---|
| 121 | length = typeof length === 'number' ? length : chunk.length;
|
|---|
| 122 |
|
|---|
| 123 | this.chunk = chunk;
|
|---|
| 124 | this.offset = start;
|
|---|
| 125 | var end = this.end = start + length;
|
|---|
| 126 | try {
|
|---|
| 127 | while (this.offset < end) {
|
|---|
| 128 | if (this[this.state]()) {
|
|---|
| 129 | break;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | } catch (err) {
|
|---|
| 133 | if (this.isUserCall) {
|
|---|
| 134 | throw err;
|
|---|
| 135 | }
|
|---|
| 136 | this.hadError = true;
|
|---|
| 137 | return err;
|
|---|
| 138 | }
|
|---|
| 139 | this.chunk = null;
|
|---|
| 140 | length = this.offset - start;
|
|---|
| 141 | if (headerState[this.state]) {
|
|---|
| 142 | this.headerSize += length;
|
|---|
| 143 | if (this.headerSize > (this.maxHeaderSize||HTTPParser.maxHeaderSize)) {
|
|---|
| 144 | return new Error('max header size exceeded');
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | return length;
|
|---|
| 148 | };
|
|---|
| 149 |
|
|---|
| 150 | var stateFinishAllowed = {
|
|---|
| 151 | REQUEST_LINE: true,
|
|---|
| 152 | RESPONSE_LINE: true,
|
|---|
| 153 | BODY_RAW: true
|
|---|
| 154 | };
|
|---|
| 155 | HTTPParser.prototype.finish = function () {
|
|---|
| 156 | if (this.hadError) {
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 | if (!stateFinishAllowed[this.state]) {
|
|---|
| 160 | return new Error('invalid state for EOF');
|
|---|
| 161 | }
|
|---|
| 162 | if (this.state === 'BODY_RAW') {
|
|---|
| 163 | this.userCall()(this[kOnMessageComplete]());
|
|---|
| 164 | }
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | // These three methods are used for an internal speed optimization, and it also
|
|---|
| 168 | // works if theses are noops. Basically consume() asks us to read the bytes
|
|---|
| 169 | // ourselves, but if we don't do it we get them through execute().
|
|---|
| 170 | HTTPParser.prototype.consume =
|
|---|
| 171 | HTTPParser.prototype.unconsume =
|
|---|
| 172 | HTTPParser.prototype.getCurrentBuffer = function () {};
|
|---|
| 173 |
|
|---|
| 174 | //For correct error handling - see HTTPParser#execute
|
|---|
| 175 | //Usage: this.userCall()(userFunction('arg'));
|
|---|
| 176 | HTTPParser.prototype.userCall = function () {
|
|---|
| 177 | this.isUserCall = true;
|
|---|
| 178 | var self = this;
|
|---|
| 179 | return function (ret) {
|
|---|
| 180 | self.isUserCall = false;
|
|---|
| 181 | return ret;
|
|---|
| 182 | };
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 185 | HTTPParser.prototype.nextRequest = function () {
|
|---|
| 186 | this.userCall()(this[kOnMessageComplete]());
|
|---|
| 187 | this.reinitialize(this.type);
|
|---|
| 188 | };
|
|---|
| 189 |
|
|---|
| 190 | HTTPParser.prototype.consumeLine = function () {
|
|---|
| 191 | var end = this.end,
|
|---|
| 192 | chunk = this.chunk;
|
|---|
| 193 | for (var i = this.offset; i < end; i++) {
|
|---|
| 194 | if (chunk[i] === 0x0a) { // \n
|
|---|
| 195 | var line = this.line + chunk.toString(HTTPParser.encoding, this.offset, i);
|
|---|
| 196 | if (line.charAt(line.length - 1) === '\r') {
|
|---|
| 197 | line = line.substr(0, line.length - 1);
|
|---|
| 198 | }
|
|---|
| 199 | this.line = '';
|
|---|
| 200 | this.offset = i + 1;
|
|---|
| 201 | return line;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | //line split over multiple chunks
|
|---|
| 205 | this.line += chunk.toString(HTTPParser.encoding, this.offset, this.end);
|
|---|
| 206 | this.offset = this.end;
|
|---|
| 207 | };
|
|---|
| 208 |
|
|---|
| 209 | var headerExp = /^([^: \t]+):[ \t]*((?:.*[^ \t])|)/;
|
|---|
| 210 | var headerContinueExp = /^[ \t]+(.*[^ \t])/;
|
|---|
| 211 | HTTPParser.prototype.parseHeader = function (line, headers) {
|
|---|
| 212 | if (line.indexOf('\r') !== -1) {
|
|---|
| 213 | throw parseErrorCode('HPE_LF_EXPECTED');
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | var match = headerExp.exec(line);
|
|---|
| 217 | var k = match && match[1];
|
|---|
| 218 | if (k) { // skip empty string (malformed header)
|
|---|
| 219 | headers.push(k);
|
|---|
| 220 | headers.push(match[2]);
|
|---|
| 221 | } else {
|
|---|
| 222 | var matchContinue = headerContinueExp.exec(line);
|
|---|
| 223 | if (matchContinue && headers.length) {
|
|---|
| 224 | if (headers[headers.length - 1]) {
|
|---|
| 225 | headers[headers.length - 1] += ' ';
|
|---|
| 226 | }
|
|---|
| 227 | headers[headers.length - 1] += matchContinue[1];
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | var requestExp = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
|
|---|
| 233 | HTTPParser.prototype.REQUEST_LINE = function () {
|
|---|
| 234 | var line = this.consumeLine();
|
|---|
| 235 | if (!line) {
|
|---|
| 236 | return;
|
|---|
| 237 | }
|
|---|
| 238 | var match = requestExp.exec(line);
|
|---|
| 239 | if (match === null) {
|
|---|
| 240 | throw parseErrorCode('HPE_INVALID_CONSTANT');
|
|---|
| 241 | }
|
|---|
| 242 | this.info.method = this._compatMode0_11 ? match[1] : methods.indexOf(match[1]);
|
|---|
| 243 | if (this.info.method === -1) {
|
|---|
| 244 | throw new Error('invalid request method');
|
|---|
| 245 | }
|
|---|
| 246 | this.info.url = match[2];
|
|---|
| 247 | this.info.versionMajor = +match[3];
|
|---|
| 248 | this.info.versionMinor = +match[4];
|
|---|
| 249 | this.body_bytes = 0;
|
|---|
| 250 | this.state = 'HEADER';
|
|---|
| 251 | };
|
|---|
| 252 |
|
|---|
| 253 | var responseExp = /^HTTP\/(\d)\.(\d) (\d{3}) ?(.*)$/;
|
|---|
| 254 | HTTPParser.prototype.RESPONSE_LINE = function () {
|
|---|
| 255 | var line = this.consumeLine();
|
|---|
| 256 | if (!line) {
|
|---|
| 257 | return;
|
|---|
| 258 | }
|
|---|
| 259 | var match = responseExp.exec(line);
|
|---|
| 260 | if (match === null) {
|
|---|
| 261 | throw parseErrorCode('HPE_INVALID_CONSTANT');
|
|---|
| 262 | }
|
|---|
| 263 | this.info.versionMajor = +match[1];
|
|---|
| 264 | this.info.versionMinor = +match[2];
|
|---|
| 265 | var statusCode = this.info.statusCode = +match[3];
|
|---|
| 266 | this.info.statusMessage = match[4];
|
|---|
| 267 | // Implied zero length.
|
|---|
| 268 | if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) {
|
|---|
| 269 | this.body_bytes = 0;
|
|---|
| 270 | }
|
|---|
| 271 | this.state = 'HEADER';
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | HTTPParser.prototype.shouldKeepAlive = function () {
|
|---|
| 275 | if (this.info.versionMajor > 0 && this.info.versionMinor > 0) {
|
|---|
| 276 | if (this.connection.indexOf('close') !== -1) {
|
|---|
| 277 | return false;
|
|---|
| 278 | }
|
|---|
| 279 | } else if (this.connection.indexOf('keep-alive') === -1) {
|
|---|
| 280 | return false;
|
|---|
| 281 | }
|
|---|
| 282 | if (this.body_bytes !== null || this.isChunked) { // || skipBody
|
|---|
| 283 | return true;
|
|---|
| 284 | }
|
|---|
| 285 | return false;
|
|---|
| 286 | };
|
|---|
| 287 |
|
|---|
| 288 | HTTPParser.prototype.HEADER = function () {
|
|---|
| 289 | var line = this.consumeLine();
|
|---|
| 290 | if (line === undefined) {
|
|---|
| 291 | return;
|
|---|
| 292 | }
|
|---|
| 293 | var info = this.info;
|
|---|
| 294 | if (line) {
|
|---|
| 295 | this.parseHeader(line, info.headers);
|
|---|
| 296 | } else {
|
|---|
| 297 | var headers = info.headers;
|
|---|
| 298 | var hasContentLength = false;
|
|---|
| 299 | var currentContentLengthValue;
|
|---|
| 300 | var hasUpgradeHeader = false;
|
|---|
| 301 | for (var i = 0; i < headers.length; i += 2) {
|
|---|
| 302 | switch (headers[i].toLowerCase()) {
|
|---|
| 303 | case 'transfer-encoding':
|
|---|
| 304 | this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
|
|---|
| 305 | break;
|
|---|
| 306 | case 'content-length':
|
|---|
| 307 | currentContentLengthValue = +headers[i + 1];
|
|---|
| 308 | if (hasContentLength) {
|
|---|
| 309 | // Fix duplicate Content-Length header with same values.
|
|---|
| 310 | // Throw error only if values are different.
|
|---|
| 311 | // Known issues:
|
|---|
| 312 | // https://github.com/request/request/issues/2091#issuecomment-328715113
|
|---|
| 313 | // https://github.com/nodejs/node/issues/6517#issuecomment-216263771
|
|---|
| 314 | if (currentContentLengthValue !== this.body_bytes) {
|
|---|
| 315 | throw parseErrorCode('HPE_UNEXPECTED_CONTENT_LENGTH');
|
|---|
| 316 | }
|
|---|
| 317 | } else {
|
|---|
| 318 | hasContentLength = true;
|
|---|
| 319 | this.body_bytes = currentContentLengthValue;
|
|---|
| 320 | }
|
|---|
| 321 | break;
|
|---|
| 322 | case 'connection':
|
|---|
| 323 | this.connection += headers[i + 1].toLowerCase();
|
|---|
| 324 | break;
|
|---|
| 325 | case 'upgrade':
|
|---|
| 326 | hasUpgradeHeader = true;
|
|---|
| 327 | break;
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | // if both isChunked and hasContentLength, isChunked wins
|
|---|
| 332 | // This is required so the body is parsed using the chunked method, and matches
|
|---|
| 333 | // Chrome's behavior. We could, maybe, ignore them both (would get chunked
|
|---|
| 334 | // encoding into the body), and/or disable shouldKeepAlive to be more
|
|---|
| 335 | // resilient.
|
|---|
| 336 | if (this.isChunked && hasContentLength) {
|
|---|
| 337 | hasContentLength = false;
|
|---|
| 338 | this.body_bytes = null;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | // Logic from https://github.com/nodejs/http-parser/blob/921d5585515a153fa00e411cf144280c59b41f90/http_parser.c#L1727-L1737
|
|---|
| 342 | // "For responses, "Upgrade: foo" and "Connection: upgrade" are
|
|---|
| 343 | // mandatory only when it is a 101 Switching Protocols response,
|
|---|
| 344 | // otherwise it is purely informational, to announce support.
|
|---|
| 345 | if (hasUpgradeHeader && this.connection.indexOf('upgrade') != -1) {
|
|---|
| 346 | info.upgrade = this.type === HTTPParser.REQUEST || info.statusCode === 101;
|
|---|
| 347 | } else {
|
|---|
| 348 | info.upgrade = info.method === method_connect;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if (this.isChunked && info.upgrade) {
|
|---|
| 352 | this.isChunked = false;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | info.shouldKeepAlive = this.shouldKeepAlive();
|
|---|
| 356 | //problem which also exists in original node: we should know skipBody before calling onHeadersComplete
|
|---|
| 357 | var skipBody;
|
|---|
| 358 | if (compatMode0_12) {
|
|---|
| 359 | skipBody = this.userCall()(this[kOnHeadersComplete](info));
|
|---|
| 360 | } else {
|
|---|
| 361 | skipBody = this.userCall()(this[kOnHeadersComplete](info.versionMajor,
|
|---|
| 362 | info.versionMinor, info.headers, info.method, info.url, info.statusCode,
|
|---|
| 363 | info.statusMessage, info.upgrade, info.shouldKeepAlive));
|
|---|
| 364 | }
|
|---|
| 365 | if (skipBody === 2) {
|
|---|
| 366 | this.nextRequest();
|
|---|
| 367 | return true;
|
|---|
| 368 | } else if (this.isChunked && !skipBody) {
|
|---|
| 369 | this.state = 'BODY_CHUNKHEAD';
|
|---|
| 370 | } else if (skipBody || this.body_bytes === 0) {
|
|---|
| 371 | this.nextRequest();
|
|---|
| 372 | // For older versions of node (v6.x and older?), that return skipBody=1 or skipBody=true,
|
|---|
| 373 | // need this "return true;" if it's an upgrade request.
|
|---|
| 374 | return info.upgrade;
|
|---|
| 375 | } else if (this.body_bytes === null) {
|
|---|
| 376 | this.state = 'BODY_RAW';
|
|---|
| 377 | } else {
|
|---|
| 378 | this.state = 'BODY_SIZED';
|
|---|
| 379 | }
|
|---|
| 380 | }
|
|---|
| 381 | };
|
|---|
| 382 |
|
|---|
| 383 | HTTPParser.prototype.BODY_CHUNKHEAD = function () {
|
|---|
| 384 | var line = this.consumeLine();
|
|---|
| 385 | if (line === undefined) {
|
|---|
| 386 | return;
|
|---|
| 387 | }
|
|---|
| 388 | this.body_bytes = parseInt(line, 16);
|
|---|
| 389 | if (!this.body_bytes) {
|
|---|
| 390 | this.state = 'BODY_CHUNKTRAILERS';
|
|---|
| 391 | } else {
|
|---|
| 392 | this.state = 'BODY_CHUNK';
|
|---|
| 393 | }
|
|---|
| 394 | };
|
|---|
| 395 |
|
|---|
| 396 | HTTPParser.prototype.BODY_CHUNK = function () {
|
|---|
| 397 | var length = Math.min(this.end - this.offset, this.body_bytes);
|
|---|
| 398 | // 0, length are for backwards compatibility. See: https://github.com/creationix/http-parser-js/pull/98
|
|---|
| 399 | this.userCall()(this[kOnBody](this.chunk.slice(this.offset, this.offset + length), 0, length));
|
|---|
| 400 | this.offset += length;
|
|---|
| 401 | this.body_bytes -= length;
|
|---|
| 402 | if (!this.body_bytes) {
|
|---|
| 403 | this.state = 'BODY_CHUNKEMPTYLINE';
|
|---|
| 404 | }
|
|---|
| 405 | };
|
|---|
| 406 |
|
|---|
| 407 | HTTPParser.prototype.BODY_CHUNKEMPTYLINE = function () {
|
|---|
| 408 | var line = this.consumeLine();
|
|---|
| 409 | if (line === undefined) {
|
|---|
| 410 | return;
|
|---|
| 411 | }
|
|---|
| 412 | if (line !== '') {
|
|---|
| 413 | throw new Error('Expected empty line');
|
|---|
| 414 | }
|
|---|
| 415 | this.state = 'BODY_CHUNKHEAD';
|
|---|
| 416 | };
|
|---|
| 417 |
|
|---|
| 418 | HTTPParser.prototype.BODY_CHUNKTRAILERS = function () {
|
|---|
| 419 | var line = this.consumeLine();
|
|---|
| 420 | if (line === undefined) {
|
|---|
| 421 | return;
|
|---|
| 422 | }
|
|---|
| 423 | if (line) {
|
|---|
| 424 | this.parseHeader(line, this.trailers);
|
|---|
| 425 | } else {
|
|---|
| 426 | if (this.trailers.length) {
|
|---|
| 427 | this.userCall()(this[kOnHeaders](this.trailers, ''));
|
|---|
| 428 | }
|
|---|
| 429 | this.nextRequest();
|
|---|
| 430 | }
|
|---|
| 431 | };
|
|---|
| 432 |
|
|---|
| 433 | HTTPParser.prototype.BODY_RAW = function () {
|
|---|
| 434 | // 0, length are for backwards compatibility. See: https://github.com/creationix/http-parser-js/pull/98
|
|---|
| 435 | this.userCall()(this[kOnBody](this.chunk.slice(this.offset, this.end), 0, this.end - this.offset));
|
|---|
| 436 | this.offset = this.end;
|
|---|
| 437 | };
|
|---|
| 438 |
|
|---|
| 439 | HTTPParser.prototype.BODY_SIZED = function () {
|
|---|
| 440 | var length = Math.min(this.end - this.offset, this.body_bytes);
|
|---|
| 441 | // 0, length are for backwards compatibility. See: https://github.com/creationix/http-parser-js/pull/98
|
|---|
| 442 | this.userCall()(this[kOnBody](this.chunk.slice(this.offset, this.offset + length), 0, length));
|
|---|
| 443 | this.offset += length;
|
|---|
| 444 | this.body_bytes -= length;
|
|---|
| 445 | if (!this.body_bytes) {
|
|---|
| 446 | this.nextRequest();
|
|---|
| 447 | }
|
|---|
| 448 | };
|
|---|
| 449 |
|
|---|
| 450 | // backward compat to node < 0.11.6
|
|---|
| 451 | ['Headers', 'HeadersComplete', 'Body', 'MessageComplete'].forEach(function (name) {
|
|---|
| 452 | var k = HTTPParser['kOn' + name];
|
|---|
| 453 | Object.defineProperty(HTTPParser.prototype, 'on' + name, {
|
|---|
| 454 | get: function () {
|
|---|
| 455 | return this[k];
|
|---|
| 456 | },
|
|---|
| 457 | set: function (to) {
|
|---|
| 458 | // hack for backward compatibility
|
|---|
| 459 | this._compatMode0_11 = true;
|
|---|
| 460 | method_connect = 'CONNECT';
|
|---|
| 461 | return (this[k] = to);
|
|---|
| 462 | }
|
|---|
| 463 | });
|
|---|
| 464 | });
|
|---|
| 465 |
|
|---|
| 466 | function parseErrorCode(code) {
|
|---|
| 467 | var err = new Error('Parse Error');
|
|---|
| 468 | err.code = code;
|
|---|
| 469 | return err;
|
|---|
| 470 | }
|
|---|