@@ -33,11 +33,13 @@ function Connection(socket, parentOrUrl, callback) {
33
33
this . server = parentOrUrl
34
34
this . path = null
35
35
this . host = null
36
+ this . extraHeaders = null
36
37
} else {
37
38
// Client-side
38
39
this . server = null
39
40
this . path = parentOrUrl . path
40
41
this . host = parentOrUrl . host
42
+ this . extraHeaders = parentOrUrl . extraHeaders
41
43
}
42
44
43
45
this . socket = socket
@@ -235,18 +237,26 @@ Connection.prototype.doRead = function () {
235
237
* @private
236
238
*/
237
239
Connection . prototype . startHandshake = function ( ) {
238
- var str , i , key
240
+ var str , i , key , headers
239
241
key = new Buffer ( 16 )
240
242
for ( i = 0 ; i < 16 ; i ++ ) {
241
243
key [ i ] = Math . floor ( Math . random ( ) * 256 )
242
244
}
243
245
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 )
250
260
this . socket . write ( str )
251
261
}
252
262
@@ -357,7 +367,7 @@ Connection.prototype.checkHandshake = function (lines) {
357
367
* @private
358
368
*/
359
369
Connection . prototype . answerHandshake = function ( lines ) {
360
- var path , key , sha1
370
+ var path , key , sha1 , str , headers
361
371
362
372
// First line
363
373
if ( lines . length < 6 ) {
@@ -393,10 +403,14 @@ Connection.prototype.answerHandshake = function (lines) {
393
403
sha1 = crypto . createHash ( 'sha1' )
394
404
sha1 . end ( this . key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' )
395
405
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 )
400
414
return true
401
415
}
402
416
@@ -570,4 +584,24 @@ Connection.prototype.processCloseFrame = function (payload) {
570
584
this . socket . write ( frame . createCloseFrame ( code , reason , ! this . server ) )
571
585
this . readyState = this . CLOSED
572
586
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
573
607
}
0 commit comments