senchalabs/connect — pull request #506 by stephank (2012-03-09 20:05:25+00:00)
Title: A proposal for handling upgrade requests.

This is a working implementation, with tests, for handling upgrade requests with connect middleware.

The basic premise is to reuse the existing middleware stack, but have a separate handler for these requests. With this pull, things work as follows:

var blog = function(req, res, next) {
next();
}
blog.upgrade = function(req, sock, head, next) {
next();
}

var app = connect();
app.use('/blog', blog);

Middleware without an upgrade handler is skipped. If no upgrade handler is found for an upgrade request, the request socket is closed. (This follows `http.Server` behavior, without an `upgrade` event listener.)

Possible issues:

- Middleware that already has an `upgrade` attribute will do funky stuff when an upgrade request is sent there.
- Applications that used to handle `upgrade` events on the `http.Server` themselves will fail, because Connect is always the first listener, and will close the socket.