diff --git a/Global.cpp b/Global.cpp index d7715e2..d8ff733 100644 --- a/Global.cpp +++ b/Global.cpp @@ -1,7 +1,6 @@ #include "Global.h" #include "Exception.h" #include "__mysql.h" -#include "Modifiers.h" #include #include @@ -39,7 +38,6 @@ namespace jet { } coreutils::MString& Global::processModifier(coreutils::MString &value, coreutils::MString &modifier) { - Modifiers modifiers; if(modifier.getLength() == 0) return value; if(modifier == "tobinary") @@ -142,8 +140,7 @@ namespace jet { } } - void Global::setupFormData(coreutils::ZString &formdata) { - + void Global::setupFormData(coreutils::ZString &formdata) { coreutils::ZString boundary = formdata.goeol(); while(!formdata.eod()) { if(formdata.ifNext("Content-Disposition: form-data;")) { @@ -163,7 +160,6 @@ namespace jet { namex << name << ":" << index++; } while(cgiVariables.count(namex) != 0); cgiVariables[namex] = data; -// std::cout << namex << ":[" << data << "]" << std::endl; if(formdata.ifNext("--")) break; formdata.goeol(); @@ -176,5 +172,24 @@ namespace jet { throw coreutils::Exception("expecting Content-Disposition header in received CGI data."); } } - + + 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."); + } + } + } diff --git a/Global.h b/Global.h index c56f4a2..2dc66d6 100644 --- a/Global.h +++ b/Global.h @@ -2,6 +2,7 @@ #define __Global_h__ #include "MString.h" +#include "Modifiers.h" #include namespace jet { @@ -25,7 +26,8 @@ namespace jet { coreutils::ZString getSessionVariable(coreutils::MString &splitName); void outputHeaders(); void setupFormData(coreutils::ZString &formdata); - + void setupFormURLEncoded(coreutils::ZString &formdata); + std::map variables; std::map cgiVariables; std::map sessions; @@ -33,6 +35,7 @@ namespace jet { std::map tags; coreutils::MString lastConverted; char **envp; + Modifiers modifiers; }; diff --git a/Modifiers.cpp b/Modifiers.cpp index 2c2cd57..5796f75 100644 --- a/Modifiers.cpp +++ b/Modifiers.cpp @@ -93,6 +93,32 @@ namespace jet { } 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) { diff --git a/__jet.cpp b/__jet.cpp index f6c0a78..5a7ffd0 100644 --- a/__jet.cpp +++ b/__jet.cpp @@ -23,8 +23,8 @@ namespace jet { if(contentType == "multipart/form-data") global.setupFormData(postdata); -// else if(contentType == "application/x-www-form-urlencoded") -// std::cout << "output urlencoded variables to global" << std::endl; + else if(contentType == "application/x-www-form-urlencoded") + global.setupFormURLEncoded(postdata); } } processContainer(container); diff --git a/tests/post.example b/tests/post.example.formdata similarity index 100% rename from tests/post.example rename to tests/post.example.formdata diff --git a/tests/post.example.urlencoded b/tests/post.example.urlencoded new file mode 100644 index 0000000..9925551 --- /dev/null +++ b/tests/post.example.urlencoded @@ -0,0 +1 @@ +name=1+test&name=2+%26test&name=3+%2B+test&name=4+*test&name=5&name2=6 \ No newline at end of file diff --git a/tests/posttest.sh b/tests/posttest.sh index 80e35cf..83fc1ef 100755 --- a/tests/posttest.sh +++ b/tests/posttest.sh @@ -1,5 +1,5 @@ #!/bin/bash export REQUEST_METHOD=POST export CONTENT_LENGTH=200 -export CONTENT_TYPE=multipart/form-data -cat post.example | ./testpost.jet +export CONTENT_TYPE=application/x-www-form-urlencoded +cat post.example.urlencoded | ./testpost.jet diff --git a/tests/testpost.jet b/tests/testpost.jet index b4d2b3d..cc436fc 100755 --- a/tests/testpost.jet +++ b/tests/testpost.jet @@ -1,6 +1,11 @@ #!../jet-2.0 $[:name] +$[:name:1] +$[:name:2] +$[:name:3] +$[:name2] +$[:name:2;tohex] $[:name:3;tohex] $[:name2;tohex]