Skip to content

Commit 8426ab6

Browse files
authored
fix: CONNECT calls onUpgrade multiple times (#348)
1 parent 79ce037 commit 8426ab6

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

Diff for: lib/core/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ class Parser extends HTTPParser {
454454
client[kQueue][client[kRunningIdx]++] = null
455455
client.emit('disconnect', new InformationalError('upgrade'))
456456

457-
this.unconsume()
458457
setImmediate(() => this.close())
459458

460459
resume(client)
@@ -499,6 +498,7 @@ class Parser extends HTTPParser {
499498
this.shouldKeepAlive = shouldKeepAlive
500499

501500
if (upgrade || request.method === 'CONNECT') {
501+
this.unconsume()
502502
this.upgrade = true
503503
return 2
504504
}

Diff for: test/client-dispatch.js

+59
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,62 @@ test('dispatch onConnect error', (t) => {
269269
})
270270
})
271271
})
272+
273+
test('connect call onUpgrade once', (t) => {
274+
t.plan(2)
275+
276+
const server = http.createServer((c) => {
277+
t.fail()
278+
})
279+
server.on('connect', (req, socket, firstBodyChunk) => {
280+
socket.write('HTTP/1.1 200 Connection established\r\n\r\n')
281+
282+
let data = firstBodyChunk.toString()
283+
socket.on('data', (buf) => {
284+
data += buf.toString()
285+
})
286+
287+
socket.on('end', () => {
288+
socket.end(data)
289+
})
290+
})
291+
t.tearDown(server.close.bind(server))
292+
293+
server.listen(0, async () => {
294+
const client = new Client(`http://localhost:${server.address().port}`)
295+
t.tearDown(client.close.bind(client))
296+
297+
let recvData = ''
298+
let count = 0
299+
client.dispatch({
300+
method: 'CONNECT',
301+
path: '/'
302+
}, {
303+
onConnect () {
304+
},
305+
onUpgrade (statusCode, headers, socket) {
306+
t.strictEqual(count++, 0)
307+
308+
socket.on('data', (d) => {
309+
recvData += d
310+
})
311+
312+
socket.on('end', () => {
313+
t.strictEqual(recvData.toString(), 'Body')
314+
})
315+
316+
socket.write('Body')
317+
socket.end()
318+
},
319+
onData (buf) {
320+
t.fail()
321+
},
322+
onComplete (trailers) {
323+
t.fail()
324+
},
325+
onError () {
326+
t.fail()
327+
}
328+
})
329+
})
330+
})

Diff for: test/client-upgrade.js

+3
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ test('basic upgrade error', (t) => {
346346
c.write('\r\n')
347347
c.write('Body')
348348
})
349+
c.on('error', () => {
350+
351+
})
349352
})
350353
t.tearDown(server.close.bind(server))
351354

0 commit comments

Comments
 (0)