Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the upgradeReq property #1099

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ app.use(function (req, res) {
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {
const location = url.parse(ws.upgradeReq.url, true);
wss.on('connection', function connection(ws, req) {
const location = url.parse(req.url, true);
// You might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
// or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312)

ws.on('message', function incoming(message) {
console.log('received: %s', message);
Expand Down Expand Up @@ -280,17 +280,17 @@ const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
const ip = ws.upgradeReq.connection.remoteAddress;
wss.on('connection', function connection(ws, req) {
const ip = req.connection.remoteAddress;
});
```

When the server runs behing a proxy like NGINX, the de-facto standard is to use
the `X-Forwarded-For` header.

```js
wss.on('connection', function connection(ws) {
const ip = ws.upgradeReq.headers['x-forwarded-for'];
wss.on('connection', function connection(ws, req) {
const ip = req.headers['x-forwarded-for'];
});
```

Expand Down
12 changes: 4 additions & 8 deletions doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ provided.
### Event: 'connection'

- `socket` {WebSocket}
- `request` {http.IncomingMessage}

Emitted when the handshake is complete. `socket` is an instance of `WebSocket`.
Emitted when the handshake is complete. `request` is the http GET request sent
by the client. Useful for parsing authority headers, cookie headers, and other
information.

### Event: 'error'

Expand Down Expand Up @@ -423,13 +426,6 @@ Send `data` through the connection.

Forcibly close the connection.

### websocket.upgradeReq

- {http.IncomingMessage}

The http GET request sent by the client. Useful for parsing authority headers,
cookie headers, and other information. This is only available for server clients.

### websocket.url

- {String}
Expand Down
6 changes: 2 additions & 4 deletions examples/express-session-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ const wss = new WebSocket.Server({
server
});

wss.on('connection', (ws) => {
wss.on('connection', (ws, req) => {
ws.on('message', (message) => {
const session = ws.upgradeReq.session;

//
// Here we can now use session parameters.
//
console.log(`WS message ${message} from user ${session.userId}`);
console.log(`WS message ${message} from user ${req.session.userId}`);
});
});

Expand Down
5 changes: 2 additions & 3 deletions lib/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class WebSocket extends EventEmitter {
this._ultron = null;

if (Array.isArray(address)) {
initAsServerClient.call(this, address[0], address[1], address[2], options);
initAsServerClient.call(this, address[0], address[1], options);
} else {
initAsClient.call(this, address, protocols, options);
}
Expand Down Expand Up @@ -455,13 +455,12 @@ module.exports = WebSocket;
* @param {String} options.protocol The chosen subprotocol
* @private
*/
function initAsServerClient (req, socket, head, options) {
function initAsServerClient (socket, head, options) {
this.protocolVersion = options.protocolVersion;
this.extensions = options.extensions;
this.maxPayload = options.maxPayload;
this.protocol = options.protocol;

this.upgradeReq = req;
this._isServer = true;

this.setSocket(socket, head);
Expand Down
5 changes: 2 additions & 3 deletions lib/WebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class WebSocketServer extends EventEmitter {
this._ultron.on('error', (err) => this.emit('error', err));
this._ultron.on('upgrade', (req, socket, head) => {
this.handleUpgrade(req, socket, head, (client) => {
this.emit(`connection${req.url}`, client);
this.emit('connection', client);
this.emit('connection', client, req);
});
});
}
Expand Down Expand Up @@ -256,7 +255,7 @@ class WebSocketServer extends EventEmitter {

socket.write(headers.concat('', '').join('\r\n'));

const client = new WebSocket([req, socket, head], null, {
const client = new WebSocket([socket, head], null, {
maxPayload: this.options.maxPayload,
protocolVersion: version,
extensions,
Expand Down
8 changes: 4 additions & 4 deletions test/WebSocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('WebSocket', function () {
});
});

wss.on('connection', (ws) => {
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '127.0.0.2');
wss.on('connection', (ws, req) => {
assert.strictEqual(req.connection.remoteAddress, '127.0.0.2');
wss.close(done);
});
});
Expand All @@ -95,8 +95,8 @@ describe('WebSocket', function () {
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
});

wss.on('connection', (ws) => {
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '::1');
wss.on('connection', (ws, req) => {
assert.strictEqual(req.connection.remoteAddress, '::1');
wss.close(done);
});
});
Expand Down
31 changes: 3 additions & 28 deletions test/WebSocketServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ describe('WebSocketServer', function () {
server.listen(sockPath, () => {
const wss = new WebSocketServer({ server });

wss.on('connection', (ws) => {
wss.on('connection', (ws, req) => {
if (wss.clients.size === 1) {
assert.strictEqual(ws.upgradeReq.url, '/foo?bar=bar');
assert.strictEqual(req.url, '/foo?bar=bar');
} else {
assert.strictEqual(ws.upgradeReq.url, '/');
assert.strictEqual(req.url, '/');
wss.close();
server.close(done);
}
Expand All @@ -105,20 +105,6 @@ describe('WebSocketServer', function () {
});
});

it('emits path specific connection event', function (done) {
const server = http.createServer();

server.listen(++port, () => {
const wss = new WebSocketServer({ server });
const ws = new WebSocket(`ws://localhost:${port}/endpointName`);

wss.on('connection/endpointName', (ws) => {
wss.close();
server.close(done);
});
});
});

it('will not crash when it receives an unhandled opcode', function (done) {
const wss = new WebSocketServer({ port: ++port });

Expand Down Expand Up @@ -940,17 +926,6 @@ describe('WebSocketServer', function () {
wss.close(done);
});
});

it('upgradeReq is the original request object', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = new WebSocket(`ws://localhost:${port}`, { protocolVersion: 8 });
});

wss.on('connection', (client) => {
assert.strictEqual(client.upgradeReq.httpVersion, '1.1');
wss.close(done);
});
});
});

describe('permessage-deflate', function () {
Expand Down