Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class Parser {

this.keepAlive = ''
this.contentLength = ''
this.connection = ''
this.connectionKeepAlive = false
this.maxResponseSize = client[kMaxResponseSize]
}

Expand Down Expand Up @@ -447,7 +447,9 @@ class Parser {
if (headerName === 'keep-alive') {
this.keepAlive += buf.toString()
} else if (headerName === 'connection') {
this.connection += buf.toString()
this.connectionKeepAlive =
this.headers[len - 1].length === 10 &&
util.bufferToLowerCasedHeaderName(this.headers[len - 1]) === 'keep-alive'
}
} else if (key.length === 14 && util.bufferToLowerCasedHeaderName(key) === 'content-length') {
this.contentLength += buf.toString()
Expand Down Expand Up @@ -554,7 +556,7 @@ class Parser {
this.shouldKeepAlive = (
shouldKeepAlive ||
// Override llhttp value which does not allow keepAlive for HEAD.
(request.method === 'HEAD' && !socket[kReset] && this.connection.toLowerCase() === 'keep-alive')
(request.method === 'HEAD' && !socket[kReset] && this.connectionKeepAlive)
)

if (this.statusCode >= 200) {
Expand Down Expand Up @@ -689,7 +691,7 @@ class Parser {
this.bytesRead = 0
this.contentLength = ''
this.keepAlive = ''
this.connection = ''
this.connectionKeepAlive = false

this.headers = []
this.headersSize = 0
Expand Down
61 changes: 61 additions & 0 deletions test/client-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,67 @@ test('keep-alive header 1', async (t) => {
await t.completed
})

test('HEAD keep-alive header reuses socket when connection header is fragmented', async (t) => {
t = tspl(t, { plan: 4 })

let connections = 0
let requests = 0

const server = createServer((socket) => {
connections++

let request = ''
socket.on('data', (chunk) => {
request += chunk.toString()

while (request.includes('\r\n\r\n')) {
const endOfHeaders = request.indexOf('\r\n\r\n') + 4
request = request.slice(endOfHeaders)
requests++

socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
socket.write('Connection: keep-')
socket.write('alive\r\n')
socket.write('\r\n')

if (requests === 2) {
socket.end()
}
}
})
})
after(() => server.close())
await once(server.listen(0), 'listening')

const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.destroy())

const disconnect = once(client, 'disconnect')

const first = await client.request({
path: '/',
method: 'HEAD',
reset: false
})
t.strictEqual(first.statusCode, 200)
await first.body.text()

const second = await client.request({
path: '/',
method: 'HEAD',
reset: false
})
t.strictEqual(second.statusCode, 200)
await second.body.text()

await disconnect
t.strictEqual(connections, 1)
t.strictEqual(requests, 2)

await t.completed
})

test('keep-alive header no postfix', async (t) => {
t = tspl(t, { plan: 2 })

Expand Down
Loading