urlencoded forms data is now workng as well.

This commit is contained in:
brad Arant 2024-11-24 13:21:44 -08:00
parent c90c7353c7
commit a9ce087939
8 changed files with 61 additions and 11 deletions

View File

@ -1,7 +1,6 @@
#include "Global.h" #include "Global.h"
#include "Exception.h" #include "Exception.h"
#include "__mysql.h" #include "__mysql.h"
#include "Modifiers.h"
#include <iostream> #include <iostream>
#include <stdlib.h> #include <stdlib.h>
@ -39,7 +38,6 @@ namespace jet {
} }
coreutils::MString& Global::processModifier(coreutils::MString &value, coreutils::MString &modifier) { coreutils::MString& Global::processModifier(coreutils::MString &value, coreutils::MString &modifier) {
Modifiers modifiers;
if(modifier.getLength() == 0) if(modifier.getLength() == 0)
return value; return value;
if(modifier == "tobinary") if(modifier == "tobinary")
@ -143,7 +141,6 @@ namespace jet {
} }
void Global::setupFormData(coreutils::ZString &formdata) { void Global::setupFormData(coreutils::ZString &formdata) {
coreutils::ZString boundary = formdata.goeol(); coreutils::ZString boundary = formdata.goeol();
while(!formdata.eod()) { while(!formdata.eod()) {
if(formdata.ifNext("Content-Disposition: form-data;")) { if(formdata.ifNext("Content-Disposition: form-data;")) {
@ -163,7 +160,6 @@ namespace jet {
namex << name << ":" << index++; namex << name << ":" << index++;
} while(cgiVariables.count(namex) != 0); } while(cgiVariables.count(namex) != 0);
cgiVariables[namex] = data; cgiVariables[namex] = data;
// std::cout << namex << ":[" << data << "]" << std::endl;
if(formdata.ifNext("--")) if(formdata.ifNext("--"))
break; break;
formdata.goeol(); formdata.goeol();
@ -177,4 +173,23 @@ namespace jet {
} }
} }
void Global::setupFormURLEncoded(coreutils::ZString &formdata) {
while(!formdata.eod()) {
coreutils::ZString name = formdata.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(formdata.ifNext("=")) {
coreutils::MString data = formdata.getTokenExclude("&");
formdata.ifNext("&");
int index = 0;
coreutils::MString namex;
do {
namex = "";
namex << name << ":" << index++;
} while(cgiVariables.count(namex) != 0);
modifiers.processFromCGIModifier(data, lastConverted);
cgiVariables[namex] = lastConverted;
} else
throw coreutils::Exception("expecting = after name in received CGI data.");
}
}
} }

View File

@ -2,6 +2,7 @@
#define __Global_h__ #define __Global_h__
#include "MString.h" #include "MString.h"
#include "Modifiers.h"
#include <map> #include <map>
namespace jet { namespace jet {
@ -25,6 +26,7 @@ namespace jet {
coreutils::ZString getSessionVariable(coreutils::MString &splitName); coreutils::ZString getSessionVariable(coreutils::MString &splitName);
void outputHeaders(); void outputHeaders();
void setupFormData(coreutils::ZString &formdata); void setupFormData(coreutils::ZString &formdata);
void setupFormURLEncoded(coreutils::ZString &formdata);
std::map<coreutils::MString, coreutils::MString> variables; std::map<coreutils::MString, coreutils::MString> variables;
std::map<coreutils::MString, coreutils::MString> cgiVariables; std::map<coreutils::MString, coreutils::MString> cgiVariables;
@ -33,6 +35,7 @@ namespace jet {
std::map<coreutils::MString, coreutils::MString> tags; std::map<coreutils::MString, coreutils::MString> tags;
coreutils::MString lastConverted; coreutils::MString lastConverted;
char **envp; char **envp;
Modifiers modifiers;
}; };

View File

@ -93,6 +93,32 @@ namespace jet {
} }
void Modifiers::processFromCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted) { void Modifiers::processFromCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted) {
value.reset();
lastConverted = "";
while(!value.eod()) {
char c = value.nextChar();
if(c == '+')
lastConverted.write(' ');
else if(c == '%') {
char ch1 = value.nextChar();
ch1 -= 48;
if(ch1 > 9)
ch1 -= 7;
ch1 <<= 4;
ch1 &= 240;
if(value.eod())
coreutils::Exception("conversion from hex requires even number of characters.");
char ch2 = value.nextChar();
ch2 -= 48;
if(ch2 > 9)
ch2 -= 7;
ch2 &= 15;
ch1 |= ch2;
lastConverted.write(ch1);
} else
lastConverted.write(c);
}
value.reset();
} }
char Modifiers::hexChar(char c) { char Modifiers::hexChar(char c) {

View File

@ -23,8 +23,8 @@ namespace jet {
if(contentType == "multipart/form-data") if(contentType == "multipart/form-data")
global.setupFormData(postdata); global.setupFormData(postdata);
// else if(contentType == "application/x-www-form-urlencoded") else if(contentType == "application/x-www-form-urlencoded")
// std::cout << "output urlencoded variables to global" << std::endl; global.setupFormURLEncoded(postdata);
} }
} }
processContainer(container); processContainer(container);

View File

@ -0,0 +1 @@
name=1+test&name=2+%26test&name=3+%2B+test&name=4+*test&name=5&name2=6

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
export REQUEST_METHOD=POST export REQUEST_METHOD=POST
export CONTENT_LENGTH=200 export CONTENT_LENGTH=200
export CONTENT_TYPE=multipart/form-data export CONTENT_TYPE=application/x-www-form-urlencoded
cat post.example | ./testpost.jet cat post.example.urlencoded | ./testpost.jet

View File

@ -1,6 +1,11 @@
#!../jet-2.0 #!../jet-2.0
<jet cgi="true"> <jet cgi="true">
$[:name] $[:name]
$[:name:1]
$[:name:2]
$[:name:3]
$[:name2]
$[:name:2;tohex]
$[:name:3;tohex] $[:name:3;tohex]
$[:name2;tohex] $[:name2;tohex]
</jet> </jet>