Skip to content

Commit

Permalink
perf: reduce string ops (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Sep 5, 2020
1 parent a304511 commit 4b92118
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const {
kTLSSession
} = require('./symbols')

const kHostHeader = Symbol('host header')

const nodeMajorVersion = parseInt(process.version.split('.')[0].slice(1))
const insecureHTTPParser = process.execArgv.includes('--insecure-http-parser')

Expand Down Expand Up @@ -161,6 +163,7 @@ class Client extends EventEmitter {
}
}
this[kTLSSession] = null
this[kHostHeader] = `host: ${this[kUrl].hostname}\r\n`

// kQueue is built up of 3 sections separated by
// the kRunningIdx and kPendingIdx indices.
Expand Down Expand Up @@ -993,18 +996,18 @@ function write (client, request) {
// or follow a request with an extra CRLF.
// https://tools.ietf.org/html/rfc7230#section-3.5

let header = `${method} ${path} HTTP/1.1\r\n`
let header

if (upgrade) {
header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n`
header = `${method} ${path} HTTP/1.1\r\nconnection: upgrade\r\nupgrade: ${upgrade}\r\n`
} else if (client[kKeepAlive]) {
header += 'connection: keep-alive\r\n'
header = `${method} ${path} HTTP/1.1\r\nconnection: keep-alive\r\n`
} else {
header += 'connection: close\r\n'
header = `${method} ${path} HTTP/1.1\r\nconnection: close\r\n`
}

if (!host) {
header += `host: ${client[kUrl].hostname}\r\n`
header += client[kHostHeader]
}

if (headers) {
Expand Down

0 comments on commit 4b92118

Please # to comment.