Skip to content

Commit af9356e

Browse files
committed
Merge pull request #20 from AppSaloon/master
adding extra header to options of connection
2 parents 526d87c + 9f7e6e5 commit af9356e

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

Connection.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ function Connection(socket, parentOrUrl, callback) {
3333
this.server = parentOrUrl
3434
this.path = null
3535
this.host = null
36+
this.extraHeaders = null
3637
} else {
3738
// Client-side
3839
this.server = null
3940
this.path = parentOrUrl.path
4041
this.host = parentOrUrl.host
42+
this.extraHeaders = parentOrUrl.extraHeaders
4143
}
4244

4345
this.socket = socket
@@ -235,18 +237,26 @@ Connection.prototype.doRead = function () {
235237
* @private
236238
*/
237239
Connection.prototype.startHandshake = function () {
238-
var str, i, key
240+
var str, i, key, headers
239241
key = new Buffer(16)
240242
for (i = 0; i < 16; i++) {
241243
key[i] = Math.floor(Math.random() * 256)
242244
}
243245
this.key = key.toString('base64')
244-
str = 'GET ' + this.path + ' HTTP/1.1\r\n' +
245-
'Host: ' + this.host + '\r\n' +
246-
'Upgrade: websocket\r\n' +
247-
'Connection: Upgrade\r\n' +
248-
'Sec-WebSocket-Key: ' + this.key + '\r\n' +
249-
'Sec-WebSocket-Version: 13\r\n\r\n'
246+
headers = {
247+
'' : 'GET ' + this.path + ' HTTP/1.1',
248+
'Host': this.host,
249+
'Upgrade' : 'websocket',
250+
'Connection': 'Upgrade',
251+
'Sec-WebSocket-Key' : this.key,
252+
'Sec-WebSocket-Version' : '13'
253+
}
254+
255+
for (var attrname in this.extraHeaders) {
256+
headers[attrname] = this.extraHeaders[attrname]
257+
}
258+
259+
str = this.buildHeaders(headers)
250260
this.socket.write(str)
251261
}
252262

@@ -357,7 +367,7 @@ Connection.prototype.checkHandshake = function (lines) {
357367
* @private
358368
*/
359369
Connection.prototype.answerHandshake = function (lines) {
360-
var path, key, sha1
370+
var path, key, sha1, str, headers
361371

362372
// First line
363373
if (lines.length < 6) {
@@ -393,10 +403,14 @@ Connection.prototype.answerHandshake = function (lines) {
393403
sha1 = crypto.createHash('sha1')
394404
sha1.end(this.key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
395405
key = sha1.read().toString('base64')
396-
this.socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
397-
'Upgrade: websocket\r\n' +
398-
'Connection: Upgrade\r\n' +
399-
'Sec-WebSocket-Accept: ' + key + '\r\n\r\n')
406+
headers = {
407+
'': 'HTTP/1.1 101 Switching Protocols',
408+
Upgrade: 'websocket',
409+
Connection: 'Upgrade',
410+
'Sec-WebSocket-Accept': key
411+
}
412+
str = this.buildHeaders(headers)
413+
this.socket.write(str)
400414
return true
401415
}
402416

@@ -570,4 +584,24 @@ Connection.prototype.processCloseFrame = function (payload) {
570584
this.socket.write(frame.createCloseFrame(code, reason, !this.server))
571585
this.readyState = this.CLOSED
572586
this.emit('close', code, reason)
587+
}
588+
589+
/**
590+
* Build the header String
591+
* @param {object} headers
592+
* @fires header string
593+
* @private
594+
*/
595+
Connection.prototype.buildHeaders = function (headers) {
596+
var headerString = ''
597+
598+
for (var prop in headers) {
599+
var separator = (prop === '')? '':': '
600+
var str = prop + separator + headers[prop] + '\r\n'
601+
headerString = headerString + str
602+
}
603+
604+
headerString = headerString + '\r\n'
605+
606+
return headerString
573607
}

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ exports.connect = function (URL, options, callback) {
3838
URL = parseWSURL(URL)
3939
options.port = URL.port
4040
options.host = URL.host
41+
42+
if (options.hasOwnProperty('extraHeaders')) {
43+
URL.extraHeaders = options.extraHeaders
44+
}
45+
4146
if (URL.secure) {
4247
socket = tls.connect(options)
4348
} else {
@@ -76,7 +81,7 @@ function parseWSURL(URL) {
7681

7782
parts.protocol = parts.protocol || 'ws:'
7883
if (parts.protocol === 'ws:') {
79-
secure = false
84+
secure = false
8085
} else if (parts.protocol === 'wss:') {
8186
secure = true
8287
} else {

test/test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,52 @@ var net = require('net')
77

88
var TEST_PORT = 8017
99
var testServer, testClient, testConn
10+
var name = 'test'
11+
var password = 'test123'
12+
var options = {
13+
extraHeaders: {
14+
Authorization: 'Basic ' + new Buffer(name+ ':' + password).toString('base64')
15+
}
16+
}
17+
18+
describe('extraHeaders', function () {
19+
before(function (done) {
20+
// Create a test server and one client
21+
testServer = ws.createServer(function (conn) {
22+
testConn = conn
23+
}).listen(TEST_PORT, function () {
24+
testClient = ws.connect('ws://localhost:' + TEST_PORT,options, done)
25+
})
26+
})
27+
28+
after(function (done) {
29+
testClient.close()
30+
testServer.socket.close(done)
31+
})
32+
33+
it('should create a headerString with extra header options', function (done) {
34+
var client = getClient(),
35+
string = 'GET '+ client.path +' HTTP/1.1\r\nHost: '+client.host+'\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: '+client.key+'\r\nSec-WebSocket-Version: 13\r\nAuthorization: Basic ' + new Buffer(name+ ':' + password).toString('base64') +'\r\n\r\n'
36+
37+
var headers = {
38+
'' : 'GET ' + client.path + ' HTTP/1.1',
39+
'Host': client.host,
40+
'Upgrade' : 'websocket',
41+
'Connection': 'Upgrade',
42+
'Sec-WebSocket-Key' : client.key,
43+
'Sec-WebSocket-Version' : '13'
44+
}
45+
46+
headers.Authorization = 'Basic ' + new Buffer(name+ ':' + password).toString('base64')
47+
48+
var buildstring = client.buildHeaders(headers)
49+
50+
if (string === buildstring) {
51+
done()
52+
}
53+
54+
})
55+
})
1056

1157
describe('text frames', function () {
1258
before(function (done) {
@@ -118,7 +164,6 @@ describe('text frames', function () {
118164

119165
it('should expose the headers', function () {
120166
var client = getClient()
121-
122167
client.headers.should.have.property('upgrade', 'websocket')
123168
client.headers.should.have.property('connection', 'Upgrade')
124169
client.headers.should.have.property('sec-websocket-accept')

0 commit comments

Comments
 (0)