Global now owns a long-lived StreamReader (cgiReader) that CGIFormData
(a Parser) holds a reference to - declared before cgiData so it's
constructed first and destroyed last. __jet.cpp's POST handling
constructs CGIFormData against it and feeds the request body off stdin
in 4KB chunks, driving doParse() after each feed rather than blocking to
read the whole body up front - the same feed()/doParse() pattern a
socket-fed StreamReader would use, keeping memory bounded for large
multipart uploads.
Also fixes two real, pre-existing bugs surfaced while getting an actual
end-to-end CGI request working through jet-2.0 (not just compiling):
- Tag::Tag's opening-tag parser was gated on "parent && in.ifNext(\"<\")",
but jet-2.0.cpp's main() constructs the top-level __jet with
parent=NULL - meaning the outer <jet ...> tag's own attributes
(cgi="yes" and everything else) were never actually parsed at all, just
echoed as literal passthrough content. parent=NULL is used at exactly
one call site in the whole codebase, so dropping the "parent &&"
requirement only changes behavior for that one top-level case - every
nested tag construction already passes a real parent.
- __jet.cpp's boundary-extraction code calls contentType.find("boundary=")
to locate the multipart boundary parameter, but find() repositions
contentType's own cursor to the match as a side effect. That same
cursor-shifted contentType then got passed into CGIFormData's
constructor, whose startsWith("multipart/form-data")/
startsWith("application/x-www-form-urlencoded") checks are
cursor-relative and so silently matched neither, falling through to an
unrecognized content type that parsed nothing (reported COMPLETE
immediately, no error, just empty results). Fixed with
contentType.reset() before the CGIFormData construction.
Verified with a real end-to-end run: jet-2.0 invoked with simulated CGI
environment variables (REQUEST_METHOD=POST, CONTENT_TYPE with a
boundary=, CONTENT_LENGTH) and a genuine multipart/form-data body
(including a file-upload part) piped over stdin, using $[:name] in a
.jet script - correctly returns each field's value and correctly leaves
the skipped upload field empty.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
82 lines
3.3 KiB
C++
82 lines
3.3 KiB
C++
#include "__jet.h"
|
|
#include "Exception.h"
|
|
#include "Global.h"
|
|
#include "SessionId.h"
|
|
#include <stdlib.h>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <unistd.h>
|
|
|
|
namespace jet {
|
|
|
|
__jet::__jet(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, this) {
|
|
char *cookies;
|
|
if(keywordDefined("cgi") && (resolveKeyword("cgi") == "yes")) {
|
|
global.cgi = true;
|
|
|
|
if(keywordDefined("sessiondir")) {
|
|
global.session = true;
|
|
global.cookies = getenv("HTTP_COOKIE");
|
|
if(global.cookies.data["session"] != NULL) {
|
|
// pull sessionfile from sessiondir.
|
|
// if last activity time is expired then ignore.
|
|
// follow sessiontimeoutredirecturl.
|
|
} else {
|
|
SessionId sessionId;
|
|
global.headers["Set-Cookie"] << "session=" << sessionId;
|
|
global.cookies.data["session"] = sessionId;
|
|
if(keywordDefined("sessiontimeout")) {
|
|
time_t timeout = time(0) + keywords["sessiontimeout"].asInteger();
|
|
}
|
|
// also save last activity time in session file.
|
|
}
|
|
}
|
|
|
|
coreutils::ZString requestMethod(getenv("REQUEST_METHOD"));
|
|
if(requestMethod == "POST") {
|
|
coreutils::ZString contentLength(getenv("CONTENT_LENGTH"));
|
|
coreutils::ZString contentType(getenv("CONTENT_TYPE"));
|
|
coreutils::ZString boundary;
|
|
int boundaryPos = contentType.find("boundary=");
|
|
if(boundaryPos != -1) {
|
|
boundary = contentType.substring(boundaryPos + 9);
|
|
}
|
|
// find() leaves contentType's own cursor at the match position -
|
|
// reset it before handing contentType to CGIFormData, whose
|
|
// startsWith("multipart/form-data")/startsWith("application/...")
|
|
// checks are cursor-relative and would otherwise silently miss,
|
|
// falling through to an unrecognized content type that never
|
|
// parses anything.
|
|
contentType.reset();
|
|
|
|
int bodyLength = contentLength.asInteger();
|
|
global.cgiData = new coreutils::CGIFormData(global.cgiReader, contentType, bodyLength, boundary);
|
|
|
|
// Feed the request body off stdin in chunks and drive the parser
|
|
// resumably (the same feed()/doParse() pattern as a socket-fed
|
|
// StreamReader) rather than blocking to read it all before parsing
|
|
// starts - keeps memory bounded for large multipart uploads and
|
|
// exercises the real Parser::Status::INCOMPLETE resumption path.
|
|
char cgiReadBuf[4096];
|
|
int cgiBytesRead = 0;
|
|
coreutils::Parser::Status cgiStatus = coreutils::Parser::Status::INCOMPLETE;
|
|
while(cgiBytesRead < bodyLength && cgiStatus != coreutils::Parser::Status::COMPLETE) {
|
|
int want = bodyLength - cgiBytesRead;
|
|
if(want > (int)sizeof(cgiReadBuf))
|
|
want = sizeof(cgiReadBuf);
|
|
int got = ::read(0, cgiReadBuf, want);
|
|
if(got <= 0)
|
|
throw coreutils::Exception("CGI request body ended before Content-Length was reached.");
|
|
cgiBytesRead += got;
|
|
coreutils::ZString chunk(cgiReadBuf, got);
|
|
global.cgiReader.feed(chunk);
|
|
cgiStatus = global.cgiData->doParse();
|
|
}
|
|
if(cgiStatus != coreutils::Parser::Status::COMPLETE)
|
|
throw coreutils::Exception("CGI request body did not parse to completion.");
|
|
}
|
|
}
|
|
processContainer(container, NULL, true);
|
|
}
|
|
}
|