javascript - Parse HTTP messages from Socket - Stack Overflow

I have a socket server listening for all types of data on the same port, if the data is HTTP then I wou

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

After 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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745183884a4615545.html

相关推荐

  • javascript - Parse HTTP messages from Socket - Stack Overflow

    I have a socket server listening for all types of data on the same port, if the data is HTTP then I wou

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信