#include #include #include "CGI.hpp" using namespace std; /****************************************************** * * * * * urlDeCode() * * * * ******************************************************/ void CGI::urlDeCode(const string &tok, size_t &i, string &val){ if (!mapHex.size()){ mapHex['0'] = 0; mapHex['1'] = 1; mapHex['2'] = 2; mapHex['3'] = 3; mapHex['4'] = 4; mapHex['5'] = 5; mapHex['6'] = 6; mapHex['7'] = 7; mapHex['8'] = 8; mapHex['9'] = 9; mapHex['a'] = 10; mapHex['b'] = 11; mapHex['c'] = 12; mapHex['d'] = 13; mapHex['e'] = 14; mapHex['f'] = 15; } size_t size = tok.size(); //also do urldecoding here... if (tok[i] == '%'){//hex encoded character if (i+3 > size){ //likely spoofing val += tok[i++]; return; } char a = tolower(tok[i+1]); char b = tolower(tok[i+2]); if (mapHex.find(a) == mapHex.end()){ //likely spoofing val += tok[i++]; return; } unsigned char chrval = 16 * mapHex[a]; if (mapHex.find(b) == mapHex.end()){ //likely spoofing val += tok[i++]; return; } chrval += mapHex[b]; val += char(chrval); i+=3; }else{ if (tok[i] == '+') val += " "; else val += tok[i]; i++; } } /****************************************************** * * * * * GetToks() * * * * ******************************************************/ void CGI::GetToks(const string &buf, map < string, string > &m){ stringstream iostr(buf); string tok, key, value; while (true){ tok = ""; getline(iostr, tok, '&'); if (!tok.size()){ if (iostr.eof()) break; continue; } size_t i=0, size = tok.size(); key=value=""; while (i &m, string *getstr, string *poststr){ m.clear(); char *get = getenv("QUERY_STRING"); if (get){ GetToks(get, m); if (getstr) *getstr = get; } string buf; if (poststr) *poststr = ""; while (true){ buf = ""; getline(cin, buf); if (!buf.size()){ if (cin.eof()) break; continue; } GetToks(buf, m); if (poststr) *poststr += buf; } }