#include "Web/DHttpContext.h" #pragma warning(disable: 4996) #include #include #include #include "Web/DConfigReader.h" #include "Providers/DDataProvider.h" #include "Providers/DMySQLDataProvider.h" #include "Model/DDirectory.h" #include "DLog.h" using namespace std; using namespace ccdb; // Global static pointer used to ensure a single instance of the class. DHttpContext* ccdb::DHttpContext::mInstance = NULL; /** This function is called to create an instance of the class. Calling the constructor publicly is not allowed. The constructor is private and is only called by this Instance function. */ DHttpContext* ccdb::DHttpContext::Instance() { if (!mInstance) // Only allow one instance of class to be generated. mInstance = new DHttpContext(); return mInstance; } ccdb::DHttpContext::DHttpContext() { InitializeGet(mGet); InitializePost(mPost); mConnectionString = "None"; DConfigReader reader("ccdb.inp"); reader.readInto(mConnectionString, "ConnectionString"); DLog::SetUseColors(false); } void ccdb::DHttpContext::ProcessRequest() { //TODO: Implement method cout<<"Content-Type: text/html"<::iterator iter=mGet.begin(); while(iter!=mGet.end()) { cout<<"{ \"text\": \""<< iter->first <<" -> "<< iter->second <<"\"}"; ++iter; if(iter!=mGet.end()) cout<<","<Connect(mConnectionString)) { cout<<"error connect"< dirs;// = prov->SearchDirectories("*", "/"); string rootDir="/"; if(mGet.find("root")!=mGet.end() && mGet["root"]!="source") { rootDir = mGet["root"]; } prov->SearchDirectories(dirs, "*", rootDir); if(dirs.size()>0) { cout<<"["; for(int i=0; iGetSubdirectories().size()>0)?"true":"false"; cout<<"{ \"text\": \""<< dirs[i]->GetName() <<"\", \"id\": \""<GetFullPath()<<"\",\"hasChildren\": "< &Post ) { //TODO: Implement method std::string tmpkey, tmpvalue; std::string *tmpstr = &tmpkey; int content_length; register char *ibuffer; char *buffer = NULL; char *strlength = getenv("CONTENT_LENGTH"); if (strlength == NULL) { Post.clear(); return; } content_length = atoi(strlength); if (content_length == 0) { Post.clear(); return; } try { buffer = new char[content_length*sizeof(char)]; } catch (std::bad_alloc xa) { Post.clear(); return; } if(fread(buffer, sizeof(char), content_length, stdin) != (unsigned int)content_length) { Post.clear(); return; } *(buffer+content_length) = '\0'; ibuffer = buffer; while (*ibuffer != '\0') { if (*ibuffer=='&') { if (tmpkey!="") { Post[UrlDecode(tmpkey)] = UrlDecode(tmpvalue); } tmpkey.clear(); tmpvalue.clear(); tmpstr = &tmpkey; } else if (*ibuffer=='=') { tmpstr = &tmpvalue; } else { (*tmpstr) += (*ibuffer); } ibuffer++; } //enter the last pair to the map if (tmpkey!="") { Post[UrlDecode(tmpkey)] = UrlDecode(tmpvalue); tmpkey.clear(); tmpvalue.clear(); } } void ccdb::DHttpContext::InitializeGet( std::map &values ) { //TODO: Implement method std::string tmpkey, tmpvalue; std::string *tmpstr = &tmpkey; register char* raw_get = getenv("QUERY_STRING"); values.clear(); if (raw_get==NULL) { return; } //lets iterate through values while (*raw_get != '\0') { //have we reach the next value? if (*raw_get=='&') { if (tmpkey!="") { values[UrlDecode(tmpkey)] = UrlDecode(tmpvalue); } tmpkey.clear(); tmpvalue.clear(); tmpstr = &tmpkey; } else if (*raw_get=='=') //Have ve reached a key value change? { tmpstr = &tmpvalue; } else { (*tmpstr) += (*raw_get); } raw_get++; } //enter the last pair to the map if (tmpkey!="") { values[UrlDecode(tmpkey)] = UrlDecode(tmpvalue); tmpkey.clear(); tmpvalue.clear(); } } std::string ccdb::DHttpContext::UrlDecode( const string& str ) { //TODO: Implement method register int i; string temp; char tmp[5], tmpchar; strcpy(tmp,"0x"); int size = str.size(); for (i=0; i::iterator iter=mGet.begin(); iter!=mGet.end(); ++iter) { cout<< iter->first <<" -> "<< iter->second <<"
"<