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 <noreply@anthropic.com>
This commit is contained in:
1 parent
2cea81c996
commit
f5aee23f5e
2 files changed
+21
-3
No files matched your search
+17
-3
@@ -1,6 +1,7 @@
|
||||
#include "CGIFormData.h"
|
||||
#include "CGIFormattedData.h"
|
||||
#include "Exception.h"
|
||||
#include <string.h>
|
||||
|
||||
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 <input type="file">) used to be silently discarded
|
||||
// here (currentSkip) rather than stored like any other part -
|
||||
|
||||
@@ -60,6 +60,10 @@ namespace coreutils {
|
||||
IMFBoundary *boundaryParser = NULL;
|
||||
MString currentName;
|
||||
std::map<MString, int> 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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user