@@ -1178,9 +1178,9 @@ async function connect (client) {
1178
1178
1179
1179
const isH2 = socket . alpnProtocol === 'h2'
1180
1180
if ( isH2 ) {
1181
- await onHTTP2Connect ( client , socket )
1181
+ await connectH2 ( client , socket )
1182
1182
} else {
1183
- await onHTTP1Connect ( client , socket )
1183
+ await connectH1 ( client , socket )
1184
1184
}
1185
1185
1186
1186
addListener ( 'close' , onSocketClose )
@@ -1284,6 +1284,7 @@ function _resume (client, sync) {
1284
1284
1285
1285
const socket = client [ kSocket ]
1286
1286
1287
+ // TODO (refactor): Avoid http specific code here...
1287
1288
if ( socket && ! socket . destroyed && socket . alpnProtocol !== 'h2' ) {
1288
1289
if ( client [ kSize ] === 0 ) {
1289
1290
if ( ! socket [ kNoRef ] && socket . unref ) {
@@ -1385,25 +1386,34 @@ function _resume (client, sync) {
1385
1386
return
1386
1387
}
1387
1388
1388
- if ( ! request . aborted && write ( client , request ) ) {
1389
+ write ( client , request )
1390
+
1391
+ if ( ! request . aborted ) {
1389
1392
client [ kPendingIdx ] ++
1390
1393
} else {
1391
1394
client [ kQueue ] . splice ( client [ kPendingIdx ] , 1 )
1392
1395
}
1393
1396
}
1394
1397
}
1395
1398
1399
+ function write ( client , request ) {
1400
+ if ( request . aborted ) {
1401
+ return
1402
+ }
1403
+
1404
+ if ( client [ kHTTPConnVersion ] === 'h2' ) {
1405
+ writeH2 ( client , request )
1406
+ } else {
1407
+ writeH1 ( client , request )
1408
+ }
1409
+ }
1410
+
1396
1411
// https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
1397
1412
function shouldSendContentLength ( method ) {
1398
1413
return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT'
1399
1414
}
1400
1415
1401
- function write ( client , request ) {
1402
- if ( client [ kHTTPConnVersion ] === 'h2' ) {
1403
- writeH2 ( client , client [ kHTTP2Session ] , request )
1404
- return
1405
- }
1406
-
1416
+ function writeH1 ( client , request ) {
1407
1417
const { method, path, host, upgrade, blocking, reset } = request
1408
1418
1409
1419
let { body, headers, contentLength } = request
@@ -1465,7 +1475,7 @@ function write (client, request) {
1465
1475
if ( shouldSendContentLength ( method ) && contentLength > 0 && request . contentLength !== null && request . contentLength !== contentLength ) {
1466
1476
if ( client [ kStrictContentLength ] ) {
1467
1477
errorRequest ( client , request , new RequestContentLengthMismatchError ( ) )
1468
- return false
1478
+ return
1469
1479
}
1470
1480
1471
1481
process . emitWarning ( new RequestContentLengthMismatchError ( ) )
@@ -1489,7 +1499,7 @@ function write (client, request) {
1489
1499
1490
1500
if ( request . aborted ) {
1491
1501
util . destroy ( body )
1492
- return false
1502
+ return
1493
1503
}
1494
1504
1495
1505
if ( method === 'HEAD' ) {
@@ -1577,24 +1587,26 @@ function write (client, request) {
1577
1587
} else {
1578
1588
assert ( false )
1579
1589
}
1580
-
1581
- return true
1582
1590
}
1583
1591
1584
- function writeH2 ( client , session , request ) {
1592
+ function writeH2 ( client , request ) {
1593
+ const session = client [ kHTTP2Session ]
1585
1594
const { body, method, path, host, upgrade, expectContinue, signal, headers : reqHeaders } = request
1586
1595
1587
1596
let headers
1588
- if ( typeof reqHeaders === 'string' ) headers = Request [ kHTTP2CopyHeaders ] ( reqHeaders . trim ( ) )
1589
- else headers = reqHeaders
1597
+ if ( typeof reqHeaders === 'string' ) {
1598
+ headers = Request [ kHTTP2CopyHeaders ] ( reqHeaders . trim ( ) )
1599
+ } else {
1600
+ headers = reqHeaders
1601
+ }
1590
1602
1591
1603
if ( upgrade ) {
1592
1604
errorRequest ( client , request , new Error ( 'Upgrade not supported for H2' ) )
1593
- return false
1605
+ return
1594
1606
}
1595
1607
1596
1608
if ( request . aborted ) {
1597
- return false
1609
+ return
1598
1610
}
1599
1611
1600
1612
/** @type {import('node:http2').ClientHttp2Stream } */
@@ -2266,7 +2278,7 @@ function errorRequest (client, request, err) {
2266
2278
}
2267
2279
}
2268
2280
2269
- async function onHTTP1Connect ( client , socket ) {
2281
+ async function connectH1 ( client , socket ) {
2270
2282
if ( ! llhttpInstance ) {
2271
2283
llhttpInstance = await llhttpPromise
2272
2284
llhttpPromise = null
@@ -2327,7 +2339,7 @@ async function onHTTP1Connect (client, socket) {
2327
2339
} )
2328
2340
}
2329
2341
2330
- async function onHTTP2Connect ( client , socket ) {
2342
+ async function connectH2 ( client , socket ) {
2331
2343
if ( ! h2ExperimentalWarned ) {
2332
2344
h2ExperimentalWarned = true
2333
2345
process . emitWarning ( 'H2 support is experimental, expect them to change at any time.' , {
0 commit comments