How do I make sure that any invalid or unwanted request that isn't following sockets protocol gets rejected and my socket aren't closed.
Consider a simple example :
var net = require('net');
net.createServer(function(socket) {
socket.on('data', function(data) {
var parsedData = JSON.parse(data.toString());
});
}).listen(5555);
server.listen(1337, '127.0.0.1');
and try visiting that port from browser , and again look at terminal. It produces following error :
undefined:1
GET / HTTP/1.1
^
SyntaxError: Unexpected token G
at Object.parse (native)
at Socket.<anonymous> (/home/agauniyal/projects/watch/watch.js:115:35)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:529:20)
Now I fully agree that visiting that port is a mistake of mine , but closing a socket on different format rather than rejecting that request isn't the ideal way to do it. And what if someone tries to access a webserver at that port only realizing later that there wasn't any at 8080.
So how should I make sure socket isn't closed and rather rejects that request?
EDIT : This is possibily happening because I'm calling JSON.Parse(data.toString())
method on data being received from socket and http headers aren't being parsed by that method.
Aucun commentaire:
Enregistrer un commentaire