http-parser-js
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -23,2 +23,5 @@ /*jshint node:true */ | ||
this.encoding = null; | ||
this.connection = null; | ||
this.body_bytes = null; | ||
this.headResponse = null; | ||
} | ||
@@ -32,2 +35,3 @@ HTTPParser.REQUEST = "REQUEST"; | ||
BODY_RAW: true, | ||
BODY_SIZED: true, | ||
BODY_CHUNK: true | ||
@@ -45,3 +49,3 @@ }; | ||
this.end = offset + length; | ||
while (this.offset < this.end) { | ||
while (this.offset < this.end && this.state !== "UNINITIALIZED") { | ||
var state = this.state; | ||
@@ -91,3 +95,3 @@ this[state](); | ||
var responseExp = /^HTTP\/([0-9]).([0-9]) (\d+) (\w+)$/; | ||
var responseExp = /^HTTP\/([0-9]).([0-9]) (\d+) ([^\n\r]+)$/; | ||
HTTPParser.prototype.RESPONSE_LINE = function () { | ||
@@ -99,6 +103,13 @@ var line = this.consumeLine(); | ||
var match = responseExp.exec(line); | ||
this.info.versionMajor = match[1]; | ||
this.info.versionMinor = match[2]; | ||
this.info.statusCode = Number(match[3]); | ||
var versionMajor = this.info.versionMajor = match[1]; | ||
var versionMinor = this.info.versionMinor = match[2]; | ||
var statusCode = this.info.statusCode = Number(match[3]); | ||
this.info.statusMsg = match[4]; | ||
// Implied zero length. | ||
if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) { | ||
this.body_bytes = 0; | ||
} | ||
if (versionMajor === '1' && versionMinor === '0') { | ||
this.connection = 'close'; | ||
} | ||
this.state = "HEADER"; | ||
@@ -114,9 +125,19 @@ }; | ||
var match = headerExp.exec(line); | ||
var k = match && match[1].toLowerCase(); | ||
var k = match && match[1]; | ||
var v = match && match[2]; | ||
if (k) { // skip empty string (malformed header) | ||
if (!this.preserveCase) { | ||
k = k.toLowerCase(); | ||
} | ||
this.info.headers.push(k); | ||
this.info.headers.push(v); | ||
if (this.preserveCase) { | ||
k = k.toLowerCase(); | ||
} | ||
if (k === 'transfer-encoding') { | ||
this.encoding = v; | ||
} else if (k === 'content-length') { | ||
this.body_bytes = parseInt(v, 10); | ||
} else if (k === 'connection') { | ||
this.connection = v; | ||
} | ||
@@ -128,6 +149,13 @@ } | ||
this.onHeadersComplete(this.info); | ||
if (this.encoding === 'chunked') { | ||
// Set ``this.headResponse = true;`` to ignore Content-Length. | ||
if (this.headResponse) { | ||
this.onMessageComplete(); | ||
this.state = 'UNINITIALIZED'; | ||
} else if (this.encoding === 'chunked') { | ||
this.state = "BODY_CHUNKHEAD"; | ||
} else if (this.body_bytes === null) { | ||
//if (this.connection !== 'close') throw new Error('Unkown body length'); | ||
this.state = "BODY_RAW"; | ||
} else { | ||
this.state = "BODY_RAW"; | ||
this.state = "BODY_SIZED"; | ||
} | ||
@@ -189,1 +217,12 @@ } | ||
}; | ||
HTTPParser.prototype.BODY_SIZED = function () { | ||
var length = Math.min(this.end - this.offset, this.body_bytes); | ||
this.onBody(this.chunk, this.offset, length); | ||
this.offset += length; | ||
this.body_bytes -= length; | ||
if (!this.body_bytes) { | ||
this.onMessageComplete(); | ||
this.state = 'UNINITIALIZED'; | ||
} | ||
}; |
{ | ||
"name": "http-parser-js", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A pure JS HTTP parser for node.", | ||
@@ -16,4 +16,8 @@ "main": "http-parser.js", | ||
], | ||
"author": "Tim Caswell", | ||
"author": "Tim Caswell (https://github.com/creationix)", | ||
"contributors": [ | ||
"Jimb Esser (https://github.com/Jimbly)", | ||
"Lawrence Rowe (https://github.com/lrowe)" | ||
], | ||
"license": "MIT" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
65797
1972
3