Skip to content

Commit 686314b

Browse files
committed
fixup
1 parent ec8d7c1 commit 686314b

File tree

1 file changed

+56
-66
lines changed

1 file changed

+56
-66
lines changed

lib/client.js

Lines changed: 56 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const {
7373
kMaxResponseSize,
7474
kHTTPConnVersion,
7575
// HTTP2
76-
kHost,
7776
kHTTP2SessionState,
7877
kHTTP2BuildRequest,
7978
kHTTP2CopyHeaders,
@@ -285,7 +284,6 @@ class Client extends DispatcherBase {
285284
openStreams: 0, // Keep track of them to decide whether or not unref the session
286285
maxConcurrentStreams: maxConcurrentStreams != null ? maxConcurrentStreams : 100 // Max peerConcurrentStreams for a Node h2 server
287286
}
288-
this[kHost] = `${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}`
289287

290288
// kQueue is built up of 3 sections separated by
291289
// the kRunningIdx and kPendingIdx indices.
@@ -395,8 +393,6 @@ class Client extends DispatcherBase {
395393
resolve()
396394
}
397395

398-
this[kHTTP2SessionState] = null
399-
400396
if (this[kSocket]) {
401397
util.destroy(this[kSocket].on('close', callback), err)
402398
} else {
@@ -408,63 +404,6 @@ class Client extends DispatcherBase {
408404
}
409405
}
410406

411-
function onHttp2SessionError (err) {
412-
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')
413-
414-
this[kSocket][kError] = err
415-
416-
onError(this[kClient], err)
417-
}
418-
419-
function onHttp2FrameError (type, code, id) {
420-
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`)
421-
422-
if (id === 0) {
423-
this[kSocket][kError] = err
424-
onError(this[kClient], err)
425-
}
426-
}
427-
428-
function onHttp2SessionEnd () {
429-
util.destroy(this, new SocketError('other side closed'))
430-
util.destroy(this[kSocket], new SocketError('other side closed'))
431-
}
432-
433-
function onHTTP2GoAway (code) {
434-
const client = this[kClient]
435-
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
436-
client[kSocket] = null
437-
438-
if (client.destroyed) {
439-
assert(this[kPending] === 0)
440-
441-
// Fail entire queue.
442-
const requests = client[kQueue].splice(client[kRunningIdx])
443-
for (let i = 0; i < requests.length; i++) {
444-
const request = requests[i]
445-
errorRequest(this, request, err)
446-
}
447-
} else if (client[kRunning] > 0) {
448-
// Fail head of pipeline.
449-
const request = client[kQueue][client[kRunningIdx]]
450-
client[kQueue][client[kRunningIdx]++] = null
451-
452-
errorRequest(client, request, err)
453-
}
454-
455-
client[kPendingIdx] = client[kRunningIdx]
456-
457-
assert(client[kRunning] === 0)
458-
459-
client.emit('disconnect',
460-
client[kUrl],
461-
[client],
462-
err
463-
)
464-
465-
resume(client)
466-
}
467-
468407
const constants = require('./llhttp/constants')
469408
const createRedirectInterceptor = require('./interceptor/redirectInterceptor')
470409
const EMPTY_BUF = Buffer.alloc(0)
@@ -1222,10 +1161,59 @@ async function connect (client) {
12221161
})
12231162

12241163
client[kHTTPConnVersion] = 'h2'
1225-
session.on('error', onHttp2SessionError)
1226-
session.on('frameError', onHttp2FrameError)
1227-
session.on('end', onHttp2SessionEnd)
1228-
session.on('goaway', onHTTP2GoAway)
1164+
session.on('error', err => {
1165+
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')
1166+
1167+
this[kSocket][kError] = err
1168+
1169+
onError(this[kClient], err)
1170+
})
1171+
session.on('frameError', (type, code, id) => {
1172+
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`)
1173+
1174+
if (id === 0) {
1175+
this[kSocket][kError] = err
1176+
onError(this[kClient], err)
1177+
}
1178+
})
1179+
session.on('end', () => {
1180+
util.destroy(session, new SocketError('other side closed'))
1181+
util.destroy(socket, new SocketError('other side closed'))
1182+
})
1183+
session.on('goaway', (code) => {
1184+
const client = this[kClient]
1185+
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
1186+
client[kSocket] = null
1187+
1188+
if (client.destroyed) {
1189+
assert(this[kPending] === 0)
1190+
1191+
// Fail entire queue.
1192+
const requests = client[kQueue].splice(client[kRunningIdx])
1193+
for (let i = 0; i < requests.length; i++) {
1194+
const request = requests[i]
1195+
errorRequest(this, request, err)
1196+
}
1197+
} else if (client[kRunning] > 0) {
1198+
// Fail head of pipeline.
1199+
const request = client[kQueue][client[kRunningIdx]]
1200+
client[kQueue][client[kRunningIdx]++] = null
1201+
1202+
errorRequest(client, request, err)
1203+
}
1204+
1205+
client[kPendingIdx] = client[kRunningIdx]
1206+
1207+
assert(client[kRunning] === 0)
1208+
1209+
client.emit('disconnect',
1210+
client[kUrl],
1211+
[client],
1212+
err
1213+
)
1214+
1215+
resume(client)
1216+
})
12291217
session.on('close', onSocketClose)
12301218
session.unref()
12311219

@@ -1665,7 +1653,9 @@ function writeH2 (client, session, request) {
16651653
let stream
16661654
const h2State = client[kHTTP2SessionState]
16671655

1668-
headers[HTTP2_HEADER_AUTHORITY] = host || client[kHost]
1656+
const { hostname, port } = client[kUrl]
1657+
1658+
headers[HTTP2_HEADER_AUTHORITY] = host || `${hostname}${port ? `:${port}` : ''}`
16691659
headers[HTTP2_HEADER_METHOD] = method
16701660

16711661
try {

0 commit comments

Comments
 (0)