From e1714856f743dc3c01ffe23e5c4e7657f3cd0b03 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 23 Oct 2022 17:52:25 +0200 Subject: [PATCH] fix: accept close as message complete (#1726) --- lib/client.js | 22 +++++++--------------- test/content-length.js | 4 ++-- test/issue-1670.js | 12 ++++++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 test/issue-1670.js diff --git a/lib/client.js b/lib/client.js index 6c063cf34e0..436142247b4 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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) @@ -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 } @@ -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 } @@ -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 diff --git a/test/content-length.js b/test/content-length.js index f529a1214a3..ecc1da34cc6 100644 --- a/test/content-length.js +++ b/test/content-length.js @@ -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({ @@ -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() }) diff --git a/test/issue-1670.js b/test/issue-1670.js new file mode 100644 index 00000000000..c27bdb272dc --- /dev/null +++ b/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() +})