From f5aee23f5e3aa87b09e15463cff3d258e604307c Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Mon, 16 Nov 2026 16:10:53 +0000 Subject: [PATCH] Make multipart upload parsing linear instead of quadratic CGIFormData::parse() runs again for every 4KB TLS read, and PART_BODY searched the whole part from its start each time with ZString::find() (one character at a time), so uploads were quadratic: 1MB took about a minute and larger camera JPEGs never finished. Track how much of the current part has already been scanned (partScanned) and memmem() only the new bytes (backing up by the marker length in case it straddles two reads). Adds a member to CGIFormData, so everything that allocates it (HTTPRequest.cpp) must be rebuilt along with the library. Co-Authored-By: Claude Opus 5.5 --- CGIFormData.cpp | 20 +++++++++++++++++--- CGIFormData.h | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CGIFormData.cpp b/CGIFormData.cpp index 87b295b..b50266d 100644 --- a/CGIFormData.cpp +++ b/CGIFormData.cpp @@ -1,6 +1,7 @@ #include "CGIFormData.h" #include "CGIFormattedData.h" #include "Exception.h" +#include namespace coreutils { @@ -111,10 +112,23 @@ namespace coreutils { marker << "\r\n--"; marker << boundary; char *bodyStart = buffer.getCursor(); - int pos = buffer.find(marker); - if (pos < 0) + // parse() runs again for every 4KB TLS read, and searching the + // whole part from bodyStart each time (buffer.find(), one + // character at a time) made uploads quadratic: 1MB took a + // minute and a camera JPEG never finished. memmem() from where + // the last call stopped (less the marker length, in case the + // marker straddles two reads) keeps it linear. + size_t avail = buffer.getLength(); + size_t markerLength = marker.getLength(); + size_t from = partScanned >= markerLength ? partScanned - markerLength + 1 : 0; + void *hit = from < avail ? memmem(bodyStart + from, avail - from, marker.getData(), markerLength) : NULL; + if (!hit) { + partScanned = avail; return Status::INCOMPLETE; - char *bodyEnd = buffer.getCursor(); + } + partScanned = 0; + char *bodyEnd = (char *)hit; + buffer.setCursor(bodyEnd); // A part whose Content-Disposition includes filename="..." // (a real ) used to be silently discarded // here (currentSkip) rather than stored like any other part - diff --git a/CGIFormData.h b/CGIFormData.h index 2c8378b..e60289f 100644 --- a/CGIFormData.h +++ b/CGIFormData.h @@ -60,6 +60,10 @@ namespace coreutils { IMFBoundary *boundaryParser = NULL; MString currentName; std::map nameCounts; + /// How many bytes of the current part's body (from the cursor) have + /// already been searched for the closing boundary, so each new 4KB + /// TLS read only searches the new bytes (see PART_BODY). + size_t partScanned = 0; void extractPartName(IMFHeader &header);