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);