00001 #include <string>
00002 #include <vector>
00003 #include <iostream>
00004
00005 using namespace std;
00006
00010 class CGIpair : public pair<string, string> {
00011 public:
00012 CGIpair() {}
00013 CGIpair(string name, string value) {
00014 first = decodeURLString(name);
00015 second = decodeURLString(value);
00016 }
00020 operator bool() const {
00021 return (first.length() != 0);
00022 }
00023 private:
00032 static string decodeURLString(string URLstr) {
00033 const int len = URLstr.length();
00034 string result;
00035 for(int i = 0; i < len; i++) {
00036 if(URLstr[i] == '+') {
00037 result += ' ';
00038 } else if(URLstr[i] == '%') {
00039 result +=
00040 translateHex(URLstr[i + 1]) * 16 +
00041 translateHex(URLstr[i + 2]);
00042 i += 2;
00043 } else {
00044 result += URLstr[i];
00045 }
00046 }
00047 return result;
00048 }
00049
00050
00051 static char translateHex(char hex) {
00052 if(hex >= 'A') {
00053 return (hex & 0xdf) - 'A' + 10;
00054 } else {
00055 return hex - '0';
00056 }
00057 }
00058 };
00065 class CGImap : public vector<CGIpair> {
00066
00067 int index;
00068 string gq;
00070 void operator=(CGImap&);
00072 CGImap(CGImap&);
00073 public:
00075 CGImap(string query): index(0), gq(query){
00076
00077 CGIpair p;
00078 while((p = nextPair()) != 0) {
00079 push_back(p);
00080 }
00081 }
00086 string operator[](const string& key) {
00087 iterator i = begin();
00088 while(i != end()) {
00089 if((*i).first == key) {
00090 return (*i).second;
00091 }
00092 i++;
00093 }
00094 return string();
00095 }
00101 void dump(ostream& o, string nl = "<br>") {
00102 for(iterator i = begin(); i != end(); i++) {
00103 o << (*i).first << " = " << (*i).second << nl;
00104 }
00105 }
00106 private:
00112 CGIpair nextPair() {
00113 if(gq.length() == 0) {
00114 return CGIpair();
00115 }
00116 if(gq.find('=') == string::npos) {
00117 return CGIpair();
00118 }
00119 string name = gq.substr(0, gq.find('='));
00120 gq = gq.substr(gq.find('=') + 1);
00121 string value = gq.substr(0, gq.find('&'));
00122 gq = gq.substr(gq.find('&') + 1);
00123 return CGIpair(name, value);
00124 }
00125 };
00126
00130 class Post : public string {
00131 public:
00132 Post() {
00133
00134
00135
00136 char* clen = getenv("CONTENT_LENGTH");
00137 if(clen == NULL ) {
00138 throw(string("CONTENT_LENGTH=Zero. Tenha certeza que isso é um POST e não um GET")) ;
00139 }
00140 int len = atoi(clen);
00141 char* s = new char[len];
00142
00143
00144
00145 cin.read(s, len);
00146 append(s, len);
00147 delete []s;
00148 }
00149 };