Skip to content

Commit 9c4cfbf

Browse files
author
Guilherme Souza
committed
Also check for connection header; added test for this
1 parent 4635aca commit 9c4cfbf

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Connection.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,13 @@ Connection.prototype.answerHandshake = function (lines) {
360360
this.readHeaders(lines)
361361

362362
// Validate necessary headers
363-
if (!('host' in this.headers) || !('sec-websocket-key' in this.headers)) {
363+
if (!('host' in this.headers) ||
364+
!('sec-websocket-key' in this.headers) ||
365+
!('upgrade' in this.headers) ||
366+
!('connection' in this.headers)) {
364367
return false
365368
}
366-
if (!this.headers.upgrade
367-
this.headers.upgrade.toLowerCase() !== 'websocket' ||
369+
if (this.headers.upgrade.toLowerCase() !== 'websocket' ||
368370
this.headers.connection.toLowerCase().split(', ').indexOf('upgrade') === -1) {
369371
return false
370372
}
@@ -554,4 +556,4 @@ Connection.prototype.processCloseFrame = function (payload) {
554556
this.socket.write(frame.createCloseFrame(code, reason, !this.server))
555557
this.readyState = this.CLOSED
556558
this.emit('close', code, reason)
557-
}
559+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-websocket",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"author": "Sitegui <[email protected]>",
55
"description": "Basic server&client approach to websocket (text and binary frames)",
66
"main": "./index.js",

test/test.js

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
require('should')
55
var ws = require('../index')
6+
var net = require('net')
67

78
var TEST_PORT = 8017
89
var testServer, testClient, testConn
@@ -149,6 +150,19 @@ describe('handshake', function () {
149150
})
150151
})
151152
})
153+
154+
it('should work when there is some missing headers', function (done) {
155+
var conn = net.connect(TEST_PORT)
156+
conn.write('GET / HTTP/1.1\r\n' +
157+
'Host: localhost\r\n' +
158+
'Sec-websocket-key: key\r\n' +
159+
'C: 3\r\n' +
160+
'D: 4\r\n' +
161+
'E: 5\r\n\r\n')
162+
conn.once('close', function () {
163+
done()
164+
})
165+
})
152166
})
153167

154168
function getClient() {

0 commit comments

Comments
 (0)