Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle socket errors, add onConnectionError opt.

  • Loading branch information...
commit 89e45987e2b8d52487a420d58428e289039fd03c 1 parent 31e425d
@sethml sethml authored
Showing with 24 additions and 11 deletions.
  1. +6 −1 README.markdown
  2. +18 −10 index.js
View
7 README.markdown
@@ -64,7 +64,12 @@ bouncy(opts={}, cb)
If you specify `opts.key` and `opts.cert`, the connection will be set to secure
mode using tls. Do this if you want to make an https router.
-Your callback `cb` will get these arguments:
+You can specify a handler `opts.onConnectionError` which will be called
+when a connection from a client has an error. Your handler will be passed
+the error and the socket to the client.
+
+You may specify your callback either as an argument, or as `opts.callback`.
+Your callback will get these arguments:
req
---
View
28 index.js
@@ -11,20 +11,28 @@ var net = require('net');
var tls = require('tls');
var bouncy = module.exports = function (opts, cb) {
- if (typeof opts === 'function') {
- cb = opts;
- opts = {};
- }
+ if (typeof opts === 'function')
+ opts = { callback: opts };
+ opts = opts || {};
+ if (typeof cb === 'function')
+ opts.callback = cb;
- if (opts && opts.key && opts.cert) {
- return tls.createServer(opts, handler.bind(null, cb));
+ if (opts.key && opts.cert) {
+ return tls.createServer(opts, handler.bind(null, opts));
}
else {
- return net.createServer(handler.bind(null, cb));
+ return net.createServer(handler.bind(null, opts));
}
};
-var handler = bouncy.handler = function (cb, c) {
+var handler = bouncy.handler = function (opts, c) {
+ // If we don't handle socket errors, the server will die when a
+ // connection times out.
+ c.on('error', function (err) {
+ if (opts.onConnectionError)
+ opts.onConnectionError(err, c);
+ });
+
var parser = parsley(c, function (req) {
c.setMaxListeners(0);
@@ -52,14 +60,14 @@ var handler = bouncy.handler = function (cb, c) {
// don't kill the server on subsequent request errors
req.on('error', function () {});
var bounce = makeBounce(stream, c, req, parser);
- cb(req, bounce);
+ opts.callback(req, bounce);
}
req.on('headers', onHeaders);
function onError (err) {
req.removeListener('headers', onHeaders);
var bounce = makeBounce(stream, c, req, parser);
- cb(req, bounce);
+ opts.callback(req, bounce);
req.emit('error', err);
}
req.once('error', onError);
Please sign in to comment.
Something went wrong with that request. Please try again.