From 4e83f6e92cdea84580d563af7b1e507c8573f601 Mon Sep 17 00:00:00 2001 From: Songkeys Date: Mon, 10 Apr 2023 16:34:16 +0800 Subject: [PATCH 1/3] fix: http2 error message --- lib/client.js | 5 ++++- test/http2.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/http2.js diff --git a/lib/client.js b/lib/client.js index b230c368dab..5a727934d16 100644 --- a/lib/client.js +++ b/lib/client.js @@ -567,7 +567,10 @@ class Parser { /* istanbul ignore else: difficult to make a test case for */ if (ptr) { const len = new Uint8Array(llhttp.memory.buffer, ptr).indexOf(0) - message = Buffer.from(llhttp.memory.buffer, ptr, len).toString() + message = + 'Response does not match the HTTP/1.1 protocol (' + + Buffer.from(llhttp.memory.buffer, ptr, len).toString() + + ')' } throw new HTTPParserError(message, constants.ERROR[ret], data.slice(offset)) } diff --git a/test/http2.js b/test/http2.js new file mode 100644 index 00000000000..ab8752a7816 --- /dev/null +++ b/test/http2.js @@ -0,0 +1,32 @@ +'use strict' + +const { test } = require('tap') +const { Client, errors } = require('..') +const { createSecureServer } = require('http2') +const pem = require('https-pem') + +test('throw http2 not supported error', (t) => { + t.plan(1) + + const server = createSecureServer({ key: pem.key, cert: pem.cert }, (req, res) => { + res.stream.respond({ 'content-type': 'text/plain' }) + res.stream.end('hello') + }).on('unknownProtocol', (socket) => { + // continue sending data in http2 to our http1.1 client to trigger error + socket.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n') + }) + t.teardown(server.close.bind(server)) + + server.listen(0, () => { + const client = new Client(`https://localhost:${server.address().port}`, { + tls: { + rejectUnauthorized: false + } + }) + t.teardown(client.close.bind(client)) + + client.request({ path: '/', method: 'GET' }, (err, data) => { + t.type(err, errors.HTTPParserError) + }) + }) +}) From 5cc584e7af432aa488da8c094323912684fb0482 Mon Sep 17 00:00:00 2001 From: Songkeys Date: Tue, 11 Apr 2023 08:13:00 +0800 Subject: [PATCH 2/3] test: fix --- test/parser-issues.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/parser-issues.js b/test/parser-issues.js index 7f5bfbb5f99..36c3825eaba 100644 --- a/test/parser-issues.js +++ b/test/parser-issues.js @@ -1,6 +1,6 @@ const net = require('net') const { test } = require('tap') -const { Client } = require('..') +const { Client, errors } = require('..') test('https://github.com/mcollina/undici/issues/268', (t) => { t.plan(2) @@ -56,8 +56,7 @@ test('parser fail', (t) => { path: '/' }, (err, data) => { t.ok(err) - t.equal(err.code, 'HPE_INVALID_CONSTANT') - t.equal(err.message, 'Expected HTTP/') + t.type(err, errors.HTTPParserError) }) }) }) From 5b116cf64f0892692686eb883d81213af63250e4 Mon Sep 17 00:00:00 2001 From: Songkeys Date: Tue, 11 Apr 2023 08:15:42 +0800 Subject: [PATCH 3/3] test: fix --- test/parser-issues.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parser-issues.js b/test/parser-issues.js index 36c3825eaba..b98edf159ef 100644 --- a/test/parser-issues.js +++ b/test/parser-issues.js @@ -40,7 +40,7 @@ test('https://github.com/mcollina/undici/issues/268', (t) => { }) test('parser fail', (t) => { - t.plan(3) + t.plan(2) const server = net.createServer(socket => { socket.write('HTT/1.1 200 OK\r\n')