Created
December 2, 2013 06:51
-
-
Save kaixiang-li/7746016 to your computer and use it in GitHub Desktop.
expose node.js internal http parser.
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 HTTPParser = process.binding('http_parser').HTTPParser; | |
exports.HTTPParser = HTTPParser; | |
HTTPParser.prototype.parse = function(data) { | |
var buffer = this.buffer = this.buffer || ""; | |
var start = buffer.length; | |
buffer += data; | |
this.execute(new Buffer(buffer), start, data.length); | |
}; | |
HTTPParser.prototype.onHeadersComplete = function(info) { | |
this.info = info; | |
} | |
var data = "GET / HTTP/1.0\r\nUser-Agent: Wget/1.11.4\r\nAccept: */*\r\nHost: www.douban.com\r\nConnection: Keep-Alive\r\n\r\n" | |
var parser = new HTTPParser(HTTPParser.REQUEST); | |
// do parse | |
parser.parse(data); | |
// get it! | |
console.log(parser.info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment