Panda3D
|
00001 #pragma warning(disable : 4789) 00002 #pragma warning(disable : 4786) 00003 00004 #include "parsedhttprequest.h" 00005 00006 //////////////////////////////////////////////////////////////////// 00007 inline std::string & trimleft_inplace(std::string & s) 00008 { 00009 s.erase(0, s.find_first_not_of(" \t\n\r")); 00010 return s; 00011 } 00012 00013 00014 //////////////////////////////////////////////////////////////////// 00015 inline std::string & trimright_inplace(std::string & s) 00016 { 00017 size_t idx = s.find_last_not_of(" \t\n\r"); 00018 00019 if (std::string::npos == idx) 00020 { 00021 s.erase(); 00022 } 00023 else 00024 { 00025 char c = s.at(idx); 00026 s.erase(idx, std::string::npos); 00027 s.append(1, c); 00028 } 00029 00030 return s; 00031 } 00032 //////////////////////////////////////////////////////////////////// 00033 inline std::string & trim_inplace(std::string & s) 00034 { 00035 trimleft_inplace(s); 00036 trimright_inplace(s); 00037 return s; 00038 } 00039 00040 inline std::string trim_tonew(const std::string &in) 00041 { 00042 std::string ss = in; 00043 return trim_inplace(ss); 00044 } 00045 00046 00047 00048 00049 std::string ParsedHttpRequest::deCanonicalize(std::string &inval) 00050 { 00051 std::string work(""); 00052 unsigned int x=0; 00053 while (x < inval.size()) 00054 { 00055 switch(inval[x]) 00056 { 00057 case('+'): 00058 work+=' '; 00059 x++; 00060 break; 00061 00062 case('%'): 00063 if(x+2 < inval.size()) 00064 { 00065 x++; 00066 char aa[5]; 00067 char * end; 00068 aa[0] = inval[x++]; 00069 aa[1] = inval[x++]; 00070 aa[2] = '\0'; 00071 char c = ( char ) strtoul(aa,&end,16); 00072 work+=c; 00073 } 00074 else 00075 x+=3; 00076 break; 00077 00078 default: 00079 work+=inval[x++]; 00080 break; 00081 } 00082 } 00083 return work; 00084 } 00085 00086 size_t ParsedHttpRequest::PullCR(std::string &src, std::string &dst) 00087 { 00088 size_t offset = src.find(' '); 00089 if(offset >= 0 ) 00090 { 00091 dst = src.substr(0,offset); 00092 src = src.substr(offset+1); 00093 } 00094 return offset; 00095 } 00096 00097 00098 void ParsedHttpRequest::clear(void) 00099 { 00100 _RequestType = ""; 00101 _parameters.clear(); 00102 } 00103 00104 const std::string * ParsedHttpRequest::GetOption(const std::string & query) 00105 { 00106 std::map<std::string,std::string>::iterator ii; 00107 ii = _parameters.find(query); 00108 if(ii == _parameters.end()) 00109 return NULL; 00110 00111 return &ii->second; 00112 } 00113 00114 00115 bool ParsedHttpRequest::GetOption(const std::string & query, std::string & out_value) 00116 { 00117 std::map<std::string,std::string>::iterator ii; 00118 ii = _parameters.find(query); 00119 if(ii == _parameters.end()) 00120 { 00121 out_value = ""; 00122 return false; 00123 } 00124 out_value = ii->second; 00125 return true; 00126 } 00127 00128 bool ParsedHttpRequest::ParseThis(char * request) 00129 { 00130 _Raw_Text = request; 00131 // printf("%s\n\n",request); 00132 00133 00134 std::string work1(_Raw_Text); 00135 for(size_t pos = work1.find_first_of("\n\r\0") ; pos != std::string::npos ; pos = work1.find_first_of("\n\r\0") ) 00136 { 00137 std::string line1 = work1.substr(0,pos); 00138 work1 = work1.substr(pos+1); 00139 if(line1.size() > 2) 00140 { 00141 // printf(" Line[%s]\n",line1.c_str()); 00142 size_t i_pos = line1.find(':'); 00143 if(i_pos != std::string::npos && i_pos > 1) 00144 { 00145 std::string noune = trim_tonew(line1.substr(0,i_pos)); 00146 std::string verb = trim_tonew(line1.substr(i_pos+1)); 00147 00148 //printf(" Noune [%s][%s]\n",noune.c_str(),verb.c_str()); 00149 _header_Lines[noune] = verb; 00150 00151 } 00152 } 00153 } 00154 00155 // 00156 // Get the url for the request .. 00157 // 00158 std::string work(request); 00159 std::string line1 = work.substr(0,work.find_first_of("\n\r\0")); 00160 if(line1.size() < 4) 00161 return false; 00162 00163 if(PullCR(line1,_RequestType) < 3) 00164 return false; 00165 00166 if(PullCR(line1,_RequestText) < 1) 00167 return false; 00168 00169 size_t loc = (int)_RequestText.find('?'); 00170 if(loc != std::string::npos) 00171 { 00172 _Requestoptions = _RequestText.substr(loc+1); 00173 _RequestText = _RequestText.substr(0,loc); 00174 } 00175 00176 return ProcessOptionString(_Requestoptions); 00177 } 00178 00179 std::string & ParsedHttpRequest::GetRequestURL(void) 00180 { 00181 return _RequestText; 00182 }; 00183 00184 bool ParsedHttpRequest::ProcessOptionString(std::string str) 00185 { 00186 size_t loc; 00187 for(loc = str.find('&'); loc != std::string::npos; loc = str.find('&')) 00188 { 00189 std::string valset = str.substr(0,loc); 00190 str = str.substr(loc+1); 00191 if(ProcessParamSet(valset) != true) 00192 return false; 00193 } 00194 return ProcessParamSet(str); 00195 }; 00196 00197 bool ParsedHttpRequest::ProcessParamSet(std::string &str) 00198 { 00199 std::string val(""); 00200 size_t loc = str.find('='); 00201 00202 if(loc != std::string::npos) 00203 { 00204 val = str.substr(loc+1); 00205 str = str.substr(0,loc); 00206 00207 std::string ind1 = deCanonicalize(str); 00208 _parameters[ind1] = deCanonicalize(val); 00209 } 00210 return true; 00211 } 00212 00213 00214