Skip to content

Commit c482110

Browse files
author
Guilherme Souza
committed
Change buildHeaders to buildRequest
This makes the semantics of this function more clear, since it also writes the first (request) line and the final CRLF
1 parent a40b9d5 commit c482110

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

Connection.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -237,26 +237,25 @@ Connection.prototype.doRead = function () {
237237
* @private
238238
*/
239239
Connection.prototype.startHandshake = function () {
240-
var str, i, key, headers
240+
var str, i, key, headers, header
241241
key = new Buffer(16)
242242
for (i = 0; i < 16; i++) {
243243
key[i] = Math.floor(Math.random() * 256)
244244
}
245245
this.key = key.toString('base64')
246246
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',
251250
'Sec-WebSocket-Key': this.key,
252251
'Sec-WebSocket-Version': '13'
253252
}
254253

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]
257256
}
258257

259-
str = this.buildHeaders(headers)
258+
str = this.buildRequest('GET ' + this.path + ' HTTP/1.1', headers)
260259
this.socket.write(str)
261260
}
262261

@@ -367,7 +366,7 @@ Connection.prototype.checkHandshake = function (lines) {
367366
* @private
368367
*/
369368
Connection.prototype.answerHandshake = function (lines) {
370-
var path, key, sha1, str, headers
369+
var path, key, sha1
371370

372371
// First line
373372
if (lines.length < 6) {
@@ -403,14 +402,11 @@ Connection.prototype.answerHandshake = function (lines) {
403402
sha1 = crypto.createHash('sha1')
404403
sha1.end(this.key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
405404
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', {
408406
Upgrade: 'websocket',
409407
Connection: 'Upgrade',
410408
'Sec-WebSocket-Accept': key
411-
}
412-
str = this.buildHeaders(headers)
413-
this.socket.write(str)
409+
}))
414410
return true
415411
}
416412

@@ -587,21 +583,19 @@ Connection.prototype.processCloseFrame = function (payload) {
587583
}
588584

589585
/**
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}
593590
* @private
594591
*/
595-
Connection.prototype.buildHeaders = function (headers) {
596-
var headerString = ''
592+
Connection.prototype.buildRequest = function (requestLine, headers) {
593+
var headerString = requestLine + '\r\n',
594+
headerName
597595

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'
602598
}
603599

604-
headerString = headerString + '\r\n'
605-
606-
return headerString
600+
return headerString + '\r\n'
607601
}

test/test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('extraHeaders', function () {
3535
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'
3636

3737
var headers = {
38-
'': 'GET ' + client.path + ' HTTP/1.1',
3938
'Host': client.host,
4039
'Upgrade': 'websocket',
4140
'Connection': 'Upgrade',
@@ -45,7 +44,7 @@ describe('extraHeaders', function () {
4544

4645
headers.Authorization = 'Basic ' + new Buffer(name + ':' + password).toString('base64')
4746

48-
var buildstring = client.buildHeaders(headers)
47+
var buildstring = client.buildRequest('GET ' + client.path + ' HTTP/1.1', headers)
4948

5049
if (string === buildstring) {
5150
done()

0 commit comments

Comments
 (0)