Skip to content

Instantly share code, notes, and snippets.

@nazarhussain
Forked from threez/node-http-parser.js
Created February 29, 2016 07:51
Show Gist options
  • Save nazarhussain/50f3bd73f4eb8f7c3a4d to your computer and use it in GitHub Desktop.
Save nazarhussain/50f3bd73f4eb8f7c3a4d to your computer and use it in GitHub Desktop.
In this example I played with the internal node http parser. This might be useful to folks that have to parse HTTP manually. For more info look at: https://github.com/joyent/node/blob/master/lib/_http_client.js
var HTTPParser = process.binding('http_parser').HTTPParser;
var parser = require("http").parsers.alloc();
parser.reinitialize(HTTPParser.RESPONSE);
parser.onBody = function(body) {
console.log(body.toString());
}
parser.onIncoming = function(response) {
console.log(response.headers);
console.log(response.statusCode);
};
function parse(data) {
var buffer = new Buffer(data);
parser.execute(buffer, 0, buffer.length);
}
parse("HTTP/1.1 200 OK\r\n");
parse("Date: Fri, 13 Jan 2006 15:12:48 GMT\r\n");
parse("Last-Modified: Tue, 1");
parse("0 Jan 2006 11:18:20 GMT\r\n");
parse("Content-");
parse("Language: de\r\n");
parse("Content-Type: text/html; charset=utf-8\r\n");
parse("Content-Length: 24\r\n");
parse("\r\n");
parse("search=Katzen&go=Artikel");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment