Created
October 9, 2012 22:23
-
-
Save nathanaschbacher/3861830 to your computer and use it in GitHub Desktop.
Using the Node.js core HTTP parser for my whims
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTTP/1.1 200 OK | |
Date: Mon, 19 Jul 2004 16:18:20 GMT | |
Server: Apache | |
Last-Modified: Sat, 10 Jul 2004 17:29:19 GMT | |
ETag: "1d0325-2470-40f0276f" | |
Accept-Ranges: bytes | |
Content-Length: 9328 | |
Connection: close | |
Content-Type: text/html | |
<HTML> | |
<HEAD> | |
</HEAD> | |
</HTML> | |
// DELETE THIS LINE AFTER YOU'VE DONE A FIND AND REPLACE ON \n to \r\n because Gist messes with line endings. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var HTTPParser = process.binding('http_parser').HTTPParser; | |
var raw_http_res = fs.readFileSync("raw_http_res.txt"); | |
var parser = new HTTPParser(HTTPParser.RESPONSE); | |
parser.onHeaders = function() { | |
console.log("onHeaders"); | |
console.log(arguments); | |
}; | |
parser.onHeadersComplete = function() { | |
console.log("onHeadersComplete"); | |
console.log(arguments); | |
}; | |
parser.onBody = function() { | |
console.log("onBody"); | |
console.log(arguments); | |
}; | |
parser.onMessageComplete = function() { | |
console.log("onMessageComplete"); | |
console.log(arguments); | |
}; | |
parser.execute(raw_http_res, 0, raw_http_res.length); | |
var final_result = parser.finish(); | |
parser = null; | |
return final_result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment