0

Post env var comparaison works now but not the assignement of variables here where i am.

For now when i press submit it just do not stop loading...;

i dont have compile problem exept if i replace

if("a"==v2[j]){

by

if('a'==v2[j]){

I need to put a b & c from POST string : a=something&b=something&c=something

in c++ a,b & c strings

it worked with GET method but not now.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <typeinfo>
using namespace std;
std::string get_env_var( std::string const & key ) {                                 
    char * val;                                                                        
    val = getenv( key.c_str() );                                                       
    std::string retval = "";                                                           
    if (val != NULL) {                                                                 
        retval = val;                                                                    
    }                                                                                  
    return retval;                                                                        
}
string replaceinstring(string myString, string search, string replace){
    string::size_type pos = 0;
    while((pos=myString.find(search, pos)) != string::npos){
        myString.replace(pos, search.length(), replace);
        pos += replace.length();
    }
    return myString;
}
string httpgetdecode(string myString){
    myString = replaceinstring(myString, "%3A", ":");
    myString = replaceinstring(myString, "%3B", ";");
    return myString;
}
vector<string> split(string s, char delimiter){
    vector<string> tokens;
    string token;
    stringstream ss(s);
    while (getline(ss,token,delimiter)){
        tokens.push_back(token);
    }
    return tokens;
}
int main(){
    string val = get_env_var("REQUEST_METHOD");
    if(val == "POST"){
        string a, b ,c, buffer;
        cout << "Content-type: text/html" << endl << endl;
        cout << "<html><head><title>Hello World</title></head><body>" << endl;
        
        // get the post data
        getline(cin,buffer, '\0');
        vector<string> v;
        v=split(buffer, '&');
        int i,j;
        for(int i=0; i<v.size(); i++){
            vector<string> v2;
            v2=split(v[i], '=');
            for(j=0; j<v2.size(); j+2){
                if("a"==v2[j]){
                    a = v2[j+1];
                }else if("b"==v2[j]){
                     b = v2[j+1];
                }else if("c"==v2[j]){
                    c = v2[j+1];
                }
            }
        }
        cout << "a" << a << "b:" << b << "c:"<< c << endl;
        cout <<"</body></html>" << endl;
    }else{
        cout << "Content-type: text/html" << endl << endl;
        cout << "<html><head><title>Hello World</title></head><body>" << endl;
        cout << "<h1>Hello World</h1>" << endl;
        cout << "<form method=\"POST\" action=\"/cgi-bin/hello.cgi\">" << endl;
        cout << "<input type=\"text\" name=\"a\" value=\"a\"/>" << endl;
        cout << "<input type=\"text\" name=\"b\" value=\"b\"/>" << endl;
        cout << "<input type=\"text\" name=\"c\" value=\"c\"/>" << endl;
        cout << "<input type=\"submit\" value=\"Submit\"/></form></body></html>" << endl;
    }
    return 0;
}

0 Answers0