Skip to content

Instantly share code, notes, and snippets.

@nathanaschbacher
Created October 9, 2012 22:23
Show Gist options
  • Save nathanaschbacher/3861830 to your computer and use it in GitHub Desktop.
Save nathanaschbacher/3861830 to your computer and use it in GitHub Desktop.
Using the Node.js core HTTP parser for my whims
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.
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