HTTP Sniffer – listening to the HTTP conversation

The events emitted by the HTTPServer object can be used for additional purposes beyond the immediate task of delivering a web application. The following code demonstrates a useful module that listens to all the HTTP Server events. It could be a useful debugging tool, which also demonstrates how HTTP server objects operate.

Node.js's HTTP Server object is an EventEmitter and the HTTP Sniffer simply listens to every server event, printing out information pertinent to each event.

Create a file named httpsniffer.js containing the following code:

var util = require('util');
var url  = require('url');

exports.sniffOn = function(server) {
  server.on('request', (req, res) => {
    util.log('e_request');
    util.log(reqToString(req));
  });
  server.on('close', errno => { util.log('e_close errno='+ errno);  });
  server.on('checkContinue', (req, res) => {
    util.log('e_checkContinue');
    util.log(reqToString(req));
    res.writeContinue();
  });
  server.on('upgrade', (req, socket, head) => {
    util.log('e_upgrade');
    util.log(reqToString(req));
  });
  server.on('clientError', () => { util.log('e_clientError'); });
};

var reqToString = exports.reqToString = function(req) {
  var ret=`req ${req.method} ${req.httpVersion} ${req.url}` +'
';
  ret += JSON.stringify(url.parse(req.url, true)) +'
';
  var keys = Object.keys(req.headers);
  for (var i = 0, l = keys.length; i < l; i++) {
    var key = keys[i];
    ret += `${i} ${key}: ${req.headers[key]}` +'
';
  }
  if (req.trailers)
    ret += req.trailers +'
';
    return ret;
};

That was a lot of code! But the key to it is the sniffOn function. When given an HTTP Server function, it uses the .on function to attach listener functions that print data about each event emitted by the HTTP Server object. It gives a fairly detailed trace of HTTP traffic on an application.

In order to use it, simply insert this code just before the listen function in server.js:

require('./httpsniffer').sniffOn(server);
server.listen(8124);
console.log('listening to http://localhost:8124');

With this in place, run the server as we did earlier. You can visit http://localhost:8124/ in your browser and see the following console output:

$ node server.js 
listening to http://localhost:8124
30 Jan 16:32:39 - request
30 Jan 16:32:39 - request GET 1.1 /
{"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":"","query":{},"pathname":"/","path":"/","href":"/"}
0 host: localhost:8124
1 connection: keep-alive
2 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
3 upgrade-insecure-requests: 1
4 user-agent: Mozilla/5.0 (X11; CrOS x86_64 7520.67.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.110 Safari/537.36
5 accept-encoding: gzip, deflate, sdch
6 accept-language: en-US,en;q=0.8
[object Object]

30 Jan 16:32:49 - request
30 Jan 16:32:49 - request GET 1.1 /osinfo
{"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":"","query":{},"pathname":"/osinfo","path":"/osinfo","href":"/osinfo"}
0 host: localhost:8124
1 connection: keep-alive
2 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
3 upgrade-insecure-requests: 1
4 user-agent: Mozilla/5.0 (X11; CrOS x86_64 7520.67.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.110 Safari/537.36
5 referer: http://localhost:8124/
6 accept-encoding: gzip, deflate, sdch
7 accept-language: en-US,en;q=0.8
[object Object]

You now have a tool for snooping on HTTPServer events. This simple technique prints a detailed log of event data. The pattern can be used for any EventEmitter object. You can use this technique as a way to inspect the actual behavior of EventEmitter objects in your program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset