Skip to content

Commit ab2e0ce

Browse files
authored
fix: more informative error message to tell that the server doesn't match http/1.1 protocol (#2055)
* fix: http2 error message * test: fix * test: fix
1 parent c1c8d83 commit ab2e0ce

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

lib/client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,10 @@ class Parser {
569569
/* istanbul ignore else: difficult to make a test case for */
570570
if (ptr) {
571571
const len = new Uint8Array(llhttp.memory.buffer, ptr).indexOf(0)
572-
message = Buffer.from(llhttp.memory.buffer, ptr, len).toString()
572+
message =
573+
'Response does not match the HTTP/1.1 protocol (' +
574+
Buffer.from(llhttp.memory.buffer, ptr, len).toString() +
575+
')'
573576
}
574577
throw new HTTPParserError(message, constants.ERROR[ret], data.slice(offset))
575578
}

test/http2.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { Client, errors } = require('..')
5+
const { createSecureServer } = require('http2')
6+
const pem = require('https-pem')
7+
8+
test('throw http2 not supported error', (t) => {
9+
t.plan(1)
10+
11+
const server = createSecureServer({ key: pem.key, cert: pem.cert }, (req, res) => {
12+
res.stream.respond({ 'content-type': 'text/plain' })
13+
res.stream.end('hello')
14+
}).on('unknownProtocol', (socket) => {
15+
// continue sending data in http2 to our http1.1 client to trigger error
16+
socket.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n')
17+
})
18+
t.teardown(server.close.bind(server))
19+
20+
server.listen(0, () => {
21+
const client = new Client(`https://localhost:${server.address().port}`, {
22+
tls: {
23+
rejectUnauthorized: false
24+
}
25+
})
26+
t.teardown(client.close.bind(client))
27+
28+
client.request({ path: '/', method: 'GET' }, (err, data) => {
29+
t.type(err, errors.HTTPParserError)
30+
})
31+
})
32+
})

test/parser-issues.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const net = require('net')
22
const { test } = require('tap')
3-
const { Client } = require('..')
3+
const { Client, errors } = require('..')
44

55
test('https://github.com/mcollina/undici/issues/268', (t) => {
66
t.plan(2)
@@ -40,7 +40,7 @@ test('https://github.com/mcollina/undici/issues/268', (t) => {
4040
})
4141

4242
test('parser fail', (t) => {
43-
t.plan(3)
43+
t.plan(2)
4444

4545
const server = net.createServer(socket => {
4646
socket.write('HTT/1.1 200 OK\r\n')
@@ -56,8 +56,7 @@ test('parser fail', (t) => {
5656
path: '/'
5757
}, (err, data) => {
5858
t.ok(err)
59-
t.equal(err.code, 'HPE_INVALID_CONSTANT')
60-
t.equal(err.message, 'Expected HTTP/')
59+
t.type(err, errors.HTTPParserError)
6160
})
6261
})
6362
})

0 commit comments

Comments
 (0)