000
17.01.2008, 11:03 Uhr
Blubber2063
|
Hi Leute,
ich bin langsam ratlos, ich hab ein kleines CGI in C++ geschrieben, weil unser uni webserver meines wissens nur cgis zulaesst. Das ganze soll einfach nur den Inhalt von 2 Formfeldern in einer Datei ablegen. Das ablegen hatte auch mal funktioniert, jetzt kriege ich jedoch nur noch die Datei mit 0 Byte erzeugt, lasse ich das cgi als normales Programm laufen, dann schreibt er die Datei trotzdem nicht. Hier mal der Code vielleicht habt ihr ja noch Ideen:
C++: |
#include <iostream> #include <string> #include <cstdlib> #include <fstream> #include <sstream> #include <map>
std::map<std::string, std::string> * parseQueryString(std::string& input, int & err){ std::string tmpdom = ""; std::string tmpcodom = ""; bool parsingDom = true; err = 0; //std::fstream evalFile("eval.erg", std::fstream::in | std::fstream::out | std::fstream::trunc); /*if (!evalFile) err = 1;*/ if (input == "") return NULL; std::map<std::string, std::string> * query = new std::map<std::string, std::string>(); for(std::string::const_iterator it = input.begin() ; it < input.end(); it++ ){ if ((*it) == '&'){ (*query)[tmpdom] = tmpcodom; parsingDom = true; tmpdom = ""; tmpcodom = ""; //evalFile << "& detected" << std::endl; continue; } if ((*it) == '='){ parsingDom = false; //evalFile << "= detected" << std::endl; continue; } if (parsingDom) tmpdom += *it; else tmpcodom += *it; //evalFile << "durchlauf" << *it << std::endl; } if (tmpdom != "") (*query)[tmpdom] = tmpcodom; //evalFile.close(); return query; }
int main(int argc, char** argv){ std::fstream evalFile("eval.erg", std::fstream::in | std::fstream::out | std::fstream::app); if (!evalFile.is_open()) evalFile.open("eval.erg", std::fstream::in | std::fstream::out | std::fstream::trunc); int err = 0; evalFile << "evalFILE"; std::string requestMethod = getenv("REQUEST_METHOD"); std::string queryString = ""; std::string postQueryString = ""; char* tmp = NULL; std::stringstream postdata(""); char buf; tmp = getenv("QUERY_STRING"); if (tmp != NULL) queryString = tmp; std::map<std::string, std::string> * query = parseQueryString(queryString, err); std::map<std::string, std::string> * postquery = NULL; if (requestMethod == "POST"){ //Sstd::cout << "err"; char * cont_len = getenv("CONTENT_LENGTH"); if (cont_len != NULL){ unsigned int length = 0; std::stringstream conv; conv << cont_len; conv >> length; for(unsigned int i = 0; i < length; i++){ std::cin.get(buf); postdata.write(&buf, 1); } postQueryString = postdata.str(); postquery = parseQueryString(postQueryString, err); if (postquery != NULL){ evalFile << (*postquery)["Kommentar"] << std::endl; evalFile << (*postquery)["Bewertung"] << std::endl; } } } evalFile.flush(); evalFile.close(); std::string page = ""; if (query != NULL) page = (*query)["page"]; std::string openSite = "<html>"; std::string endSite = "</html>"; std::string header = "<head><title>Evaluation Tutorium Björn Breitmeyer</title></head>"; std::string openBody = "<body>"; std::string endBody = "</body>"; std::stringstream form(""); form << "<form action=eval.cgi?page=danke method=POST>"; form << "<center><b>Bitte geben Sie Ihren Namen ein :</b><br>"; form << "<input name=\"Bewertung\" size=\"40\" maxlength=\"20\"> </input><br>"; form << "<textarea name=\"Kommentar\" size=\"40\" cols=\"46\" rows=\"30\"> </textarea><br>"; form << "<input type=submit value=\"abschicken\"></center><br>"; form << "</form>"; std::cout << "Content-type: text/html\r\n"; std::cout << "\r\n"; std::cout << openSite; std::cout << header; std::cout << openBody; //std::cout << requestMethod; if (page!="danke") std::cout << form.str(); else{ std::cout << "<center>Danke für deinen Meinung<br>"; std::cout << "<a href=\"http://cs.tu-berlin.de/~bjbreitm/eval.cgi\">Zurück News</a></center>"; } if (postquery){ for (std::map<std::string, std::string>::const_iterator it = postquery->begin(); it != postquery->end(); it++) std::cout << "<p>" << it->first << "=" << it->second << "</p>"; } std::cout << "<p>queryData</p>"; if (err) std::cout << "<p>file not created</p>"; if (query){ for (std::map<std::string, std::string>::const_iterator it = query->begin(); it != query->end(); it++) std::cout << "<p>" << it->first << "=" << it->second << "</p>"; } //std::cout << queryString; std::cout << err; std::cout << endBody; std::cout << endSite; delete query; delete postquery;
}
|
|