From 7188131ec5f5b9527fd74cfa56060718be5e9c69 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 28 Feb 2024 12:25:09 +0100 Subject: [PATCH] refactor: split out last h1 specific code from core --- lib/dispatcher/client-h1.js | 34 ++++++++++++++++++++++++++++++++++ lib/dispatcher/client-h2.js | 3 +++ lib/dispatcher/client.js | 35 +++++------------------------------ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/lib/dispatcher/client-h1.js b/lib/dispatcher/client-h1.js index df644e0360c..60d1b511172 100644 --- a/lib/dispatcher/client-h1.js +++ b/lib/dispatcher/client-h1.js @@ -742,6 +742,40 @@ async function connectH1 (client, socket) { resumeH1(client) }, destroy () { + }, + busy (request) { + if (socket[kWriting] || socket[kReset] || socket[kBlocking]) { + return true + } + + if (client[kRunning] > 0 && !request.idempotent) { + // Non-idempotent request cannot be retried. + // Ensure that no other requests are inflight and + // could cause failure. + return true + } + + if (client[kRunning] > 0 && (request.upgrade || request.method === 'CONNECT')) { + // Don't dispatch an upgrade until all preceding requests have completed. + // A misbehaving server might upgrade the connection before all pipelined + // request has completed. + return true + } + + if (client[kRunning] > 0 && util.bodyLength(request.body) !== 0 && + (util.isStream(request.body) || util.isAsyncIterable(request.body) || util.isFormDataLike(request.body))) { + // Request with stream or iterator body can error while other requests + // are inflight and indirectly error those as well. + // Ensure this doesn't happen by waiting for inflight + // to complete before dispatching. + + // Request with stream or iterator body cannot be retried. + // Ensure that no other requests are inflight and + // could cause failure. + return true + } + + return false } } } diff --git a/lib/dispatcher/client-h2.js b/lib/dispatcher/client-h2.js index 5a6cb2ed91b..c0cfd5c88f6 100644 --- a/lib/dispatcher/client-h2.js +++ b/lib/dispatcher/client-h2.js @@ -133,6 +133,9 @@ async function connectH2 (client, socket) { }, destroy (err) { session.destroy(err) + }, + busy () { + return false } } } diff --git a/lib/dispatcher/client.js b/lib/dispatcher/client.js index 29dde5cdab3..72a6e945ae5 100644 --- a/lib/dispatcher/client.js +++ b/lib/dispatcher/client.js @@ -598,37 +598,12 @@ function _resume (client, sync) { return } - if (client[kHTTPContext].version === 'h1') { - if (socket[kWriting] || socket[kReset] || socket[kBlocking]) { - return - } - - if (client[kRunning] > 0 && !request.idempotent) { - // Non-idempotent request cannot be retried. - // Ensure that no other requests are inflight and - // could cause failure. - return - } - - if (client[kRunning] > 0 && (request.upgrade || request.method === 'CONNECT')) { - // Don't dispatch an upgrade until all preceding requests have completed. - // A misbehaving server might upgrade the connection before all pipelined - // request has completed. - return - } - - if (client[kRunning] > 0 && util.bodyLength(request.body) !== 0 && - (util.isStream(request.body) || util.isAsyncIterable(request.body) || util.isFormDataLike(request.body))) { - // Request with stream or iterator body can error while other requests - // are inflight and indirectly error those as well. - // Ensure this doesn't happen by waiting for inflight - // to complete before dispatching. + if (!client[kHTTPContext]) { + return + } - // Request with stream or iterator body cannot be retried. - // Ensure that no other requests are inflight and - // could cause failure. - return - } + if (client[kHTTPContext].busy(request)) { + return } if (!request.aborted && client[kHTTPContext].write(request)) {