00001 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
00002 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00003
00004 using namespace ots;
00005
00006
00007
00008
00009
00010
00011
00012 std::string CgiDataUtilities::getOrPostData(cgicc::Cgicc& cgi, const std::string& needle)
00013 {
00014 std::string postData = "";
00015 if((postData = CgiDataUtilities::postData(cgi,needle)) == "")
00016 postData = CgiDataUtilities::getData(cgi,needle);
00017 return postData;
00018 }
00019
00020
00021
00022
00023
00024
00025 std::string CgiDataUtilities::postData(cgicc::Cgicc& cgi, const std::string& needle)
00026 {
00027 std::string postData = "&"+cgi.getEnvironment().getPostData();
00028
00029 size_t start_pos = postData.find("&"+needle+"=");
00030 if(start_pos == std::string::npos) return "";
00031
00032 size_t end_pos = postData.find('=',start_pos);
00033 if(end_pos == std::string::npos) return "";
00034
00035 start_pos += needle.length() + 2;
00036 end_pos = postData.find('&',start_pos);
00037 if(end_pos == std::string::npos) postData.length();
00038
00039
00040
00041 return postData.substr(start_pos,end_pos-start_pos);
00042 }
00043
00044
00045
00046
00047
00048 std::string CgiDataUtilities::getData(cgicc::Cgicc &cgi, const std::string &needle)
00049 {
00050 std::string getData = "&"+cgi.getEnvironment().getQueryString();
00051
00052
00053 size_t start_pos = getData.find("&"+needle+"=");
00054 if(start_pos == std::string::npos) return "";
00055
00056 size_t end_pos = getData.find('=',start_pos);
00057 if(end_pos == std::string::npos) return "";
00058
00059 start_pos += needle.length() + 2;
00060 end_pos = getData.find('&',start_pos);
00061 if(end_pos != std::string::npos) end_pos -= start_pos;
00062
00063
00064
00065
00066 return getData.substr(start_pos,end_pos);
00067 }
00068
00069
00070 int CgiDataUtilities::getOrPostDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
00071 { return atoi(getOrPostData(cgi,needle).c_str()); }
00072 int CgiDataUtilities::postDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
00073 { return atoi(postData(cgi,needle).c_str()); }
00074 int CgiDataUtilities::getDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
00075 { return atoi(getData(cgi,needle).c_str()); }
00076
00077
00078
00079
00080
00081
00082 std::string CgiDataUtilities::decodeURIComponent(const std::string &data)
00083 {
00084 std::string decodeURIString(data.size(),0);
00085 unsigned int j=0;
00086 for(unsigned int i=0;i<data.size();++i,++j)
00087 {
00088 if(data[i] == '%')
00089 {
00090
00091 if(data[i+1] > '9')
00092 decodeURIString[j] += (data[i+1]-55)*16;
00093 else
00094 decodeURIString[j] += (data[i+1]-48)*16;
00095
00096
00097 if(data[i+2] > '9')
00098 decodeURIString[j] += (data[i+2]-55);
00099 else
00100 decodeURIString[j] += (data[i+2]-48);
00101
00102 i+=2;
00103 }
00104 else
00105 decodeURIString[j] = data[i];
00106 }
00107 decodeURIString.resize(j);
00108 return decodeURIString;
00109 }