@@ -237,26 +237,25 @@ Connection.prototype.doRead = function () {
237
237
* @private
238
238
*/
239
239
Connection . prototype . startHandshake = function ( ) {
240
- var str , i , key , headers
240
+ var str , i , key , headers , header
241
241
key = new Buffer ( 16 )
242
242
for ( i = 0 ; i < 16 ; i ++ ) {
243
243
key [ i ] = Math . floor ( Math . random ( ) * 256 )
244
244
}
245
245
this . key = key . toString ( 'base64' )
246
246
headers = {
247
- '' : 'GET ' + this . path + ' HTTP/1.1' ,
248
- 'Host' : this . host ,
249
- 'Upgrade' : 'websocket' ,
250
- 'Connection' : 'Upgrade' ,
247
+ Host : this . host ,
248
+ Upgrade : 'websocket' ,
249
+ Connection : 'Upgrade' ,
251
250
'Sec-WebSocket-Key' : this . key ,
252
251
'Sec-WebSocket-Version' : '13'
253
252
}
254
253
255
- for ( var attrname in this . extraHeaders ) {
256
- headers [ attrname ] = this . extraHeaders [ attrname ]
254
+ for ( header in this . extraHeaders ) {
255
+ headers [ header ] = this . extraHeaders [ header ]
257
256
}
258
257
259
- str = this . buildHeaders ( headers )
258
+ str = this . buildRequest ( 'GET ' + this . path + ' HTTP/1.1' , headers )
260
259
this . socket . write ( str )
261
260
}
262
261
@@ -367,7 +366,7 @@ Connection.prototype.checkHandshake = function (lines) {
367
366
* @private
368
367
*/
369
368
Connection . prototype . answerHandshake = function ( lines ) {
370
- var path , key , sha1 , str , headers
369
+ var path , key , sha1
371
370
372
371
// First line
373
372
if ( lines . length < 6 ) {
@@ -403,14 +402,11 @@ Connection.prototype.answerHandshake = function (lines) {
403
402
sha1 = crypto . createHash ( 'sha1' )
404
403
sha1 . end ( this . key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' )
405
404
key = sha1 . read ( ) . toString ( 'base64' )
406
- headers = {
407
- '' : 'HTTP/1.1 101 Switching Protocols' ,
405
+ this . socket . write ( this . buildRequest ( 'HTTP/1.1 101 Switching Protocols' , {
408
406
Upgrade : 'websocket' ,
409
407
Connection : 'Upgrade' ,
410
408
'Sec-WebSocket-Accept' : key
411
- }
412
- str = this . buildHeaders ( headers )
413
- this . socket . write ( str )
409
+ } ) )
414
410
return true
415
411
}
416
412
@@ -587,21 +583,19 @@ Connection.prototype.processCloseFrame = function (payload) {
587
583
}
588
584
589
585
/**
590
- * Build the header String
591
- * @param {object } headers
592
- * @fires header string
586
+ * Build the header string
587
+ * @param {string } requestLine
588
+ * @param {Object<string> } headers
589
+ * @returns {string }
593
590
* @private
594
591
*/
595
- Connection . prototype . buildHeaders = function ( headers ) {
596
- var headerString = ''
592
+ Connection . prototype . buildRequest = function ( requestLine , headers ) {
593
+ var headerString = requestLine + '\r\n' ,
594
+ headerName
597
595
598
- for ( var prop in headers ) {
599
- var separator = ( prop === '' ) ? '' : ': '
600
- var str = prop + separator + headers [ prop ] + '\r\n'
601
- headerString = headerString + str
596
+ for ( headerName in headers ) {
597
+ headerString += headerName + ': ' + headers [ headerName ] + '\r\n'
602
598
}
603
599
604
- headerString = headerString + '\r\n'
605
-
606
- return headerString
600
+ return headerString + '\r\n'
607
601
}
0 commit comments