The thing is, once there is an UPGRADE, it's not HTTP anymore. For all intents and purposes websockets are NOT HTTP and can't be handled as such.
Node doesn't offer any core API objects for parsing and encoding to websockets, it pushes all of that to higher level libraries like socket.io, so the current API is "good enough" for them. I don't think it's a good idea to emit "request" and create a normal HTTP ServerRequest object for websockets. I'm open to hearing alternative APIs for dealing with them but what is the goal of such an API? If it's to add encoder/decoder objects for websockets in core we might need to push back. On Apr 12, 2012, at April 12, 20122:13 PM, Isaac Schlueter wrote: > This is definitely a wart in the API, but not one that we're likely to > fix in v0.8. It may require rethinking a few things, and we are not > prepared to make major changes to the HTTP layer right now. > > Can you write up the feature request as an issue? Let's get it on the > list for v0.9. > > 2012/3/29 Stéphan Kochen <step...@angrybytes.com>: >> Here's the example from before as a test case: >> >> https://github.com/stephank/node/commit/0339fc0 >> >> I went a step further, and experimented with providing a response object >> for upgrade and CONNECT requests, also queuing them properly. This is on >> a branch at: >> >> https://github.com/stephank/node/compare/upgrade-response >> >> Notably: >> >> - The `upgrade` and `connect` event listeners have the same signature >> as regular `request` events, namely `(req, res)`. >> >> - The response can be treated like any other, and `res.end` will treat >> it as if with `Connection: close`. (Ie. `shouldKeepAlive = false`). >> >> - To successfully upgrade, call `res.switchProtocols`, rather than >> `res.end`, which takes a callback with the familiar `(sock, head)`. >> >> Example: >> >> var http = require('http'); >> >> var server = http.createServer(); >> >> server.on('request', function(req, res) { >> res.writeHead(200, {'Content-Type': 'text/plain'}); >> res.end('Hello World\n'); >> }); >> >> server.on('upgrade', function(req, res) { >> res.writeHead(101, { >> 'Upgrade': 'dummy', >> 'Connection': 'Upgrade' >> }); >> res.switchProtocols(function(sock, head) { >> /* ... */ >> }); >> }); >> >> server.listen(3000);