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 275ce77
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 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,34 @@ function _resume (client, sync) {
return
}

if (!request.aborted && write(client, request)) {
write(client, request)

if (!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 +1475,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 +1499,7 @@ function write (client, request) {

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

if (method === 'HEAD') {
Expand Down Expand Up @@ -1577,24 +1587,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 +2278,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 +2339,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 275ce77

Please sign in to comment.