From c21dea14112d6cf45d3980dd6f554f61dc3db19a Mon Sep 17 00:00:00 2001 From: brad Arant Date: Sat, 23 Nov 2024 19:25:14 -0800 Subject: [PATCH] some cgi variable work. --- Global.cpp | 31 ++++++++++++++++++++++++++++++- Global.h | 3 ++- __jet.cpp | 15 ++++++++------- tests/post.example | 45 +++++++++++++++++++++++++-------------------- tests/posttest.sh | 2 +- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/Global.cpp b/Global.cpp index 9917c94..c4397dc 100644 --- a/Global.cpp +++ b/Global.cpp @@ -136,5 +136,34 @@ namespace jet { std::cout << std::endl; } } - + + void Global::setupFormData(coreutils::ZString &formdata) { + + coreutils::ZString boundary = formdata.goeol(); + while(!formdata.eod()) { + if(formdata.ifNext("Content-Disposition: form-data;")) { + formdata.skipWhitespace(); + if(formdata.ifNext("name=\"")) { + coreutils::ZString name = formdata.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); + if(formdata.ifNext("\"")) { + formdata.goeol(); + formdata.goeol(); + coreutils::ZString data = formdata.getTokenExclude("-"); // TODO: Fix this parsing. Need a string exclusion method to check for 'boundary'. + data.trimCRLF(); + formdata.ifNext(boundary); + cgiVariables[name] = data; + std::cout << name << ":[" << data << "]" << std::endl; + if(formdata.ifNext("--")) + break; + formdata.goeol(); + } + else + throw coreutils::Exception("expecting closing double quote on vairable name in received CGI data."); + } else + throw coreutils::Exception("expecting name subfield in received CGI data."); + } else + throw coreutils::Exception("expecting Content-Disposition header in received CGI data."); + } + } + } diff --git a/Global.h b/Global.h index 58a4ffe..713c3ff 100644 --- a/Global.h +++ b/Global.h @@ -24,9 +24,10 @@ namespace jet { __mysql * getSession(coreutils::MString sessionId); coreutils::ZString getSessionVariable(coreutils::MString &splitName); void outputHeaders(); + void setupFormData(coreutils::ZString &formdata); std::map variables; - std::map cgiVariables; + std::map cgiVariables; std::map sessions; std::map headers; std::map tags; diff --git a/__jet.cpp b/__jet.cpp index 5ad4385..f6c0a78 100644 --- a/__jet.cpp +++ b/__jet.cpp @@ -1,6 +1,7 @@ #include "__jet.h" #include "Exception.h" #include +#include namespace jet { @@ -13,17 +14,17 @@ namespace jet { coreutils::ZString contentLength(getenv("CONTENT_LENGTH")); coreutils::ZString contentType(getenv("CONTENT_TYPE")); + std::ofstream outFile("/tmp/output.txt"); + coreutils::MString postdata; postdata.read(0); - std::cout << postdata << std::endl; - - coreutils::IMFRequest request(postdata); - coreutils::IMFMessage message(postdata); + outFile << postdata << std::endl; + outFile.close(); if(contentType == "multipart/form-data") - std::cout << "output multipart variables to global" << std::endl; - else if(contentType == "application/x-www-form-urlencoded") - std::cout << "output urlencoded variables to global" << std::endl; + global.setupFormData(postdata); +// else if(contentType == "application/x-www-form-urlencoded") +// std::cout << "output urlencoded variables to global" << std::endl; } } processContainer(container); diff --git a/tests/post.example b/tests/post.example index b40ba69..3de5998 100644 --- a/tests/post.example +++ b/tests/post.example @@ -1,21 +1,26 @@ -POST /cgi-bin/qtest HTTP/1.1 -Content-Type: multipart/form-data; - boundary=2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f -Content-Length: 514 +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +1 test +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +2&test +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +3+test +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +4 test +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +5 +------WebKitFormBoundaryA1LWIjMAIsdFW9XA +Content-Disposition: form-data; name="name" + +6 +------WebKitFormBoundaryA1LWIjMAIsdFW9XA-- ---2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f -Content-Disposition: form-data; name="datafile1"; filename="r.gif" -Content-Type: image/gif - -GIF87a.............,...........D..; ---2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f -Content-Disposition: form-data; name="datafile2"; filename="g.gif" -Content-Type: image/gif - -GIF87a.............,...........D..; ---2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f -Content-Disposition: form-data; name="datafile3"; filename="b.gif" -Content-Type: image/gif - -GIF87a.............,...........D..; ---2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f-- \ No newline at end of file diff --git a/tests/posttest.sh b/tests/posttest.sh index bf22782..80e35cf 100755 --- a/tests/posttest.sh +++ b/tests/posttest.sh @@ -1,5 +1,5 @@ #!/bin/bash export REQUEST_METHOD=POST -export CONTENT_LENGTH=554 +export CONTENT_LENGTH=200 export CONTENT_TYPE=multipart/form-data cat post.example | ./testpost.jet