admin管理员组文章数量:1415491
I have a socket server listening for all types of data on the same port, if the data is HTTP then I would like to parse it, otherwise I would like to do something else with it...
net.createServer(function(socket){
if(/*socket contains HTTP data*/){
// parse it
}
else{
// do something else with the socket
}
}).listen(999)
How should I parse the HTTP data from the socket?
I have started to write my own HTTP parser for this purpose, but I don't want to reinvent the wheel, maybe there is a much better way.
I have a socket server listening for all types of data on the same port, if the data is HTTP then I would like to parse it, otherwise I would like to do something else with it...
net.createServer(function(socket){
if(/*socket contains HTTP data*/){
// parse it
}
else{
// do something else with the socket
}
}).listen(999)
How should I parse the HTTP data from the socket?
I have started to write my own HTTP parser for this purpose, but I don't want to reinvent the wheel, maybe there is a much better way.
Share Improve this question asked Mar 18, 2014 at 15:15 DrahcirDrahcir 12k25 gold badges88 silver badges128 bronze badges2 Answers
Reset to default 6After looking at the source for http.js, I came up with the following way to do it...
var net = require('net'),
http = require('http'),
events = require('events');
var HTTPParser = process.binding('http_parser').HTTPParser;
function freeParser(parser){
if (parser) {
parser.onIning = null;
parser.socket = null;
http.parsers.free(parser);
parser = null;
}
};
function parse(socket){
var emitter = new events.EventEmitter();
var parser = http.parsers.alloc();
parser.reinitialize(HTTPParser.REQUEST);
parser.socket = socket;
parser.maxHeaderPairs = 2000;
parser.onIning = function(req){
emitter.emit('request', req);
};
socket.on('data', function(buffer){
var ret = parser.execute(buffer, 0, buffer.length);
if(ret instanceof Error){
emitter.emit('error');
freeParser(parser);
}
});
socket.once('close', function(){
freeParser(parser);
});
return emitter;
};
net.createServer(function(socket){
var parser = parse(socket);
parser.on('request', function(req){
// Got parsed HTTP object
});
parser.once('error', function(){
// Not HTTP data
});
}).listen(999);
So the Node.JS core libraries actually implement HTTP handling over a socket. That code is written in Javascript and is visible: https://github./joyent/node/blob/master/lib/http.js
The big challenge here is really that I'm not clear how you're planning to differentiate traffic types? I mean you can listen for "HTTP" on any port or socket, we just generally agree on 80 (and 443 for https).
But it's not clear how you listen for call to both HTTP and say the MySQL protocol within the same socket. How do you know which packets of data belong to which protocol? I've never really heard of anyone implementing such a "multi-purpose" socket, you normally know what type of data to expect before opening the socket. Otherwise you could just be receiving garbage, right?
本文标签: javascriptParse HTTP messages from SocketStack Overflow
版权声明:本文标题:javascript - Parse HTTP messages from Socket - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745183883a2646592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论