Skip to content

Commit

Permalink
refactor: write h1/h2
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 25, 2024
1 parent 747507b commit 542ed6f
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,9 @@ async function connect (client) {

const isH2 = socket.alpnProtocol === 'h2'
if (isH2) {
await onHTTP2Connect(client, socket)
await connectH2(client, socket)
} else {
await onHTTP1Connect(client, socket)
await connectH1(client, socket)
}

addListener('close', onSocketClose)
Expand Down Expand Up @@ -1284,6 +1284,7 @@ function _resume (client, sync) {

const socket = client[kSocket]

// TODO (refactor): Avoid http specific code here...
if (socket && !socket.destroyed && socket.alpnProtocol !== 'h2') {
if (client[kSize] === 0) {
if (!socket[kNoRef] && socket.unref) {
Expand Down Expand Up @@ -1385,25 +1386,32 @@ function _resume (client, sync) {
return
}

if (!request.aborted && write(client, request)) {
if (write(client, request) && !request.aborted) {
client[kPendingIdx]++
} else {
client[kQueue].splice(client[kPendingIdx], 1)
}
}
}

function write (client, request) {
if (request.aborted) {
return
}

if (client[kHTTPConnVersion] === 'h2') {
writeH2(client, request)
} else {
writeH1(client, request)
}
}

// https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
function shouldSendContentLength (method) {
return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT'
}

function write (client, request) {
if (client[kHTTPConnVersion] === 'h2') {
writeH2(client, client[kHTTP2Session], request)
return
}

function writeH1 (client, request) {
const { method, path, host, upgrade, blocking, reset } = request

let { body, headers, contentLength } = request
Expand Down Expand Up @@ -1465,7 +1473,7 @@ function write (client, request) {
if (shouldSendContentLength(method) && contentLength > 0 && request.contentLength !== null && request.contentLength !== contentLength) {
if (client[kStrictContentLength]) {
errorRequest(client, request, new RequestContentLengthMismatchError())
return false
return
}

process.emitWarning(new RequestContentLengthMismatchError())
Expand All @@ -1489,7 +1497,7 @@ function write (client, request) {

if (request.aborted) {
util.destroy(body)
return false
return
}

if (method === 'HEAD') {
Expand Down Expand Up @@ -1577,24 +1585,26 @@ function write (client, request) {
} else {
assert(false)
}

return true
}

function writeH2 (client, session, request) {
function writeH2 (client, request) {
const session = client[kHTTP2Session]
const { body, method, path, host, upgrade, expectContinue, signal, headers: reqHeaders } = request

let headers
if (typeof reqHeaders === 'string') headers = Request[kHTTP2CopyHeaders](reqHeaders.trim())
else headers = reqHeaders
if (typeof reqHeaders === 'string') {
headers = Request[kHTTP2CopyHeaders](reqHeaders.trim())
} else {
headers = reqHeaders
}

if (upgrade) {
errorRequest(client, request, new Error('Upgrade not supported for H2'))
return false
return
}

if (request.aborted) {
return false
return
}

/** @type {import('node:http2').ClientHttp2Stream} */
Expand Down Expand Up @@ -2266,7 +2276,7 @@ function errorRequest (client, request, err) {
}
}

async function onHTTP1Connect (client, socket) {
async function connectH1 (client, socket) {
if (!llhttpInstance) {
llhttpInstance = await llhttpPromise
llhttpPromise = null
Expand Down Expand Up @@ -2327,7 +2337,7 @@ async function onHTTP1Connect (client, socket) {
})
}

async function onHTTP2Connect (client, socket) {
async function connectH2 (client, socket) {
if (!h2ExperimentalWarned) {
h2ExperimentalWarned = true
process.emitWarning('H2 support is experimental, expect them to change at any time.', {
Expand Down

0 comments on commit 542ed6f

Please sign in to comment.