I have a raw HTTP request string from which I need to create an object representation.
Instead of reinvent the wheel I was thinking about use the internal http parser to get an instance of http.IncomingMessage
Is it possible?
I think so because string is not so different from a complete stream.
How to do it?
I had a look on source code and they get a request parser as follow
var HTTPParser = process.binding('http_parser').HTTPParser;
var parser = new HTTPParser(HTTPParser.REQUEST)
Edit
Some progress from a node.js test
var request = Buffer(raw);
var parser = new HTTPParser(HTTPParser.REQUEST);
parser.execute(request, 0, request.length);
Edit 2
Some eventHandlers were missing (all of them)
parser.onHeadersComplete = function(res) {
console.log('onHeadersComplete');
console.log(res);
};
parser.onBody = function(body) {
console.log('body done');
console.log(body.toString());
}
parser.onMessageComplete = function(res) {
console.log('done');
};
Thanks
httpmodule?require('http').createServer(function(req,res){ ... })The function gets called each time there is a request.reqis already an instance of http.IncomingMessage