Skip to content

Commit

Permalink
fix: accept close as message complete (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Oct 23, 2022
1 parent f57fe82 commit 7c6040b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
22 changes: 7 additions & 15 deletions lib/client.js
Expand Up @@ -549,19 +549,6 @@ class Parser {
}
}

finish () {
try {
try {
currentParser = this
} finally {
currentParser = null
}
} catch (err) {
/* istanbul ignore next: difficult to make a test case for */
util.destroy(this.socket, err)
}
}

destroy () {
assert(this.ptr != null)
assert(currentParser == null)
Expand Down Expand Up @@ -924,7 +911,7 @@ function onSocketError (err) {
// to the user.
if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so for as a valid response.
parser.finish()
parser.onMessageComplete()
return
}

Expand Down Expand Up @@ -958,7 +945,7 @@ function onSocketEnd () {

if (parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
parser.finish()
parser.onMessageComplete()
return
}

Expand All @@ -968,6 +955,11 @@ function onSocketEnd () {
function onSocketClose () {
const { [kClient]: client } = this

if (!this[kError] && this[kParser].statusCode && !this[kParser].shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
this[kParser].onMessageComplete()
}

this[kParser].destroy()
this[kParser] = null

Expand Down
4 changes: 2 additions & 2 deletions test/content-length.js
Expand Up @@ -281,7 +281,7 @@ test('response invalid content length with close', (t) => {
t.teardown(client.destroy.bind(client))

client.on('disconnect', (origin, client, err) => {
t.equal(err.code, 'UND_ERR_SOCKET')
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
})

client.request({
Expand All @@ -294,7 +294,7 @@ test('response invalid content length with close', (t) => {
t.fail()
})
.on('error', (err) => {
t.equal(err.code, 'UND_ERR_SOCKET')
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
})
.resume()
})
Expand Down
12 changes: 12 additions & 0 deletions test/issue-1670.js
@@ -0,0 +1,12 @@
'use strict'

const { test } = require('tap')
const { request } = require('..')

test('https://github.com/mcollina/undici/issues/810', async (t) => {
const { body } = await request('https://api.github.com/user/emails')

await body.text()

t.end()
})

0 comments on commit 7c6040b

Please sign in to comment.