Files
CoreUtils/CGIFormData.h
T
Brad ArantandClaude Opus 5.5 f5aee23f5e 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>
2026-11-16 16:10:53 +00:00

87 lines
3.1 KiB
C++

#ifndef __CGIFormData_h__
# define __CGIFormData_h__
#include "Parser.h"
#include "IMFHeader.h"
#include "IMFBoundary.h"
#include <map>
namespace coreutils {
///
/// CGIFormData parses a CGI request body (application/x-www-form-urlencoded
/// or multipart/form-data) from the shared StreamReader and exposes the
/// resulting name/value pairs via operator[] - the object a caller (e.g.
/// JetCore's Tag::getVariable, for its $[:name] CGI variable syntax) holds
/// onto and indexes by variable name once doParse() reports COMPLETE.
///
/// Resumable like every other Parser here: for multipart/form-data, each
/// part's boundary line and header block are parsed via IMFBoundary/
/// IMFHeader (chained through checkParse(), sharing this same
/// StreamReader), so a part - or even its boundary marker - can straddle
/// any number of feed() calls without losing state. File-upload parts
/// (Content-Disposition with a filename= parameter) are recognized and
/// skipped - matching this codebase's existing convention that uploads
/// get a separate mechanism, not buffered through here.
///
class CGIFormData : public Parser {
public:
CGIFormData(StreamReader &reader, ZString contentType, int contentLength, ZString boundary);
virtual ~CGIFormData();
MString operator[](ZString name);
// Every parsed field, keyed as "name:N" (see storeValue's own doc
// comment below) - lets a caller (ApiSession::handleComplete, to
// merge POST body fields into jet::Global::variables the same way
// path params/query string already are - see its own doc comment)
// iterate every field without knowing its name in advance, which
// operator[] alone doesn't support.
const std::map<MString, MString> &fields() const { return data; }
protected:
Status parse() override;
private:
enum Mode { URLENCODED, MULTIPART, UNKNOWN } mode;
enum State { INIT, OPENING_TAIL, PART_HEADERS, PART_BODY, PART_BOUNDARY, URLENCODED_BODY, DONE } state = INIT;
ZString contentType;
int contentLength;
ZString boundary;
std::map<MString, MString> data;
IMFHeader *headerParser = NULL;
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);
///
/// Stores value under "name:N", where N is how many values have
/// already been stored under this same base name (0 for the first,
/// 1 for the second, etc.) - matches the indexed key format
/// JetCore's Tag::getVariable's $[:name] syntax looks up by
/// (defaulting to :0 when the caller doesn't specify an index),
/// supporting repeated field names (checkboxes, multi-select) the
/// same way.
///
void storeValue(MString name, MString value);
};
}
#endif