$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h" 00002 #include "otsdaq-core/Macros/CoutMacros.h" 00003 00004 using namespace ots; 00005 00006 //======================================================================================================================== 00007 // getOrPostData 00008 // return std::string value of needle from get or post std::string 00009 // post format is expected to be: needle1=value1&needle2=value2... 00010 // if not found, return "" 00011 std::string CgiDataUtilities::getOrPostData(cgicc::Cgicc& cgi, const std::string& needle) 00012 { 00013 std::string postData = ""; 00014 if((postData = CgiDataUtilities::postData(cgi, needle)) == "") 00015 postData = CgiDataUtilities::getData( 00016 cgi, needle); // get command from form, if PreviewEntry 00017 return postData; 00018 } 00019 00020 //======================================================================================================================== 00021 // getPostData 00022 // return std::string value of needle from post std::string 00023 // post format is expected to be: needle1=value1&needle2=value2... 00024 // if not found, return "" 00025 std::string CgiDataUtilities::postData(cgicc::Cgicc& cgi, const std::string& needle) 00026 { 00027 std::string postData = "&" + cgi.getEnvironment().getPostData(); 00028 //__COUT__ << "PostData: " + postData << std::endl; 00029 size_t start_pos = postData.find( 00030 "&" + needle + 00031 "="); // add & and = to make sure found field and not part of a value 00032 if(start_pos == std::string::npos) 00033 return ""; // needle not found 00034 00035 size_t end_pos = postData.find('=', start_pos); // verify = sign 00036 if(end_pos == std::string::npos) 00037 return ""; //= not found 00038 00039 start_pos += needle.length() + 2; // get past & and field 00040 end_pos = postData.find('&', start_pos); // skip needle and = sign 00041 if(end_pos == std::string::npos) 00042 postData.length(); // not found, so take data to end 00043 00044 //__COUT__ << "start_pos=" << start_pos 00045 // << "end_pos=" << end_pos << std::endl; 00046 return postData.substr(start_pos, end_pos - start_pos); // return value 00047 } 00048 00049 //======================================================================================================================== 00050 // getData 00051 // returns "" if not found 00052 // get query data format is expected to be: needle1=value1&needle2=value2... 00053 std::string CgiDataUtilities::getData(cgicc::Cgicc& cgi, const std::string& needle) 00054 { 00055 std::string getData = "&" + cgi.getEnvironment().getQueryString(); 00056 //__COUT__ << "getData: " + getData << std::endl; 00057 00058 size_t start_pos = getData.find( 00059 "&" + needle + 00060 "="); // add & and = to make sure found field and not part of a value 00061 if(start_pos == std::string::npos) 00062 return ""; // needle not found 00063 00064 size_t end_pos = getData.find('=', start_pos); // verify = sign 00065 if(end_pos == std::string::npos) 00066 return ""; //= not found 00067 00068 start_pos += needle.length() + 2; // get past & and field 00069 end_pos = getData.find('&', start_pos); // skip needle and = sign 00070 if(end_pos != std::string::npos) 00071 end_pos -= start_pos; // found, so determine sz of field 00072 00073 //__COUT__ << "start_pos=" << start_pos << " '" << getData[start_pos] << 00074 // "' end_pos=" << end_pos << " := " << getData.substr(start_pos,end_pos) << 00075 // std::endl; 00076 00077 return getData.substr(start_pos, end_pos); // return value 00078 } 00079 00080 //======================================================================================================================== 00081 int CgiDataUtilities::getOrPostDataAsInt(cgicc::Cgicc& cgi, const std::string& needle) 00082 { 00083 return atoi(getOrPostData(cgi, needle).c_str()); 00084 } 00085 int CgiDataUtilities::postDataAsInt(cgicc::Cgicc& cgi, const std::string& needle) 00086 { 00087 return atoi(postData(cgi, needle).c_str()); 00088 } 00089 int CgiDataUtilities::getDataAsInt(cgicc::Cgicc& cgi, const std::string& needle) 00090 { 00091 return atoi(getData(cgi, needle).c_str()); 00092 } 00093 00094 //============================================================================== 00095 // decodeURIComponent 00096 // converts all %## to the ascii character 00097 std::string CgiDataUtilities::decodeURIComponent(const std::string& data) 00098 { 00099 std::string decodeURIString(data.size(), 0); // init to same size 00100 unsigned int j = 0; 00101 for(unsigned int i = 0; i < data.size(); ++i, ++j) 00102 { 00103 if(data[i] == '%') 00104 { 00105 // high order hex nibble digit 00106 if(data[i + 1] > '9') // then ABCDEF 00107 decodeURIString[j] += (data[i + 1] - 55) * 16; 00108 else 00109 decodeURIString[j] += (data[i + 1] - 48) * 16; 00110 00111 // low order hex nibble digit 00112 if(data[i + 2] > '9') // then ABCDEF 00113 decodeURIString[j] += (data[i + 2] - 55); 00114 else 00115 decodeURIString[j] += (data[i + 2] - 48); 00116 00117 i += 2; // skip to next char 00118 } 00119 else 00120 decodeURIString[j] = data[i]; 00121 } 00122 decodeURIString.resize(j); 00123 return decodeURIString; 00124 }