otsdaq  v2_01_00
CgiDataUtilities.cc
1 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 
4 using namespace ots;
5 
6 
7 //========================================================================================================================
8 //getOrPostData
9 // return std::string value of needle from get or post std::string
10 // post format is expected to be: needle1=value1&needle2=value2...
11 // if not found, return ""
12 std::string CgiDataUtilities::getOrPostData(cgicc::Cgicc& cgi, const std::string& needle)
13 {
14  std::string postData = "";
15  if((postData = CgiDataUtilities::postData(cgi,needle)) == "")
16  postData = CgiDataUtilities::getData(cgi,needle); //get command from form, if PreviewEntry
17  return postData;
18 }
19 
20 //========================================================================================================================
21 //getPostData
22 // return std::string value of needle from post std::string
23 // post format is expected to be: needle1=value1&needle2=value2...
24 // if not found, return ""
25 std::string CgiDataUtilities::postData(cgicc::Cgicc& cgi, const std::string& needle)
26 {
27  std::string postData = "&"+cgi.getEnvironment().getPostData();
28  //__COUT__ << "PostData: " + postData << std::endl;
29  size_t start_pos = postData.find("&"+needle+"="); //add & and = to make sure found field and not part of a value
30  if(start_pos == std::string::npos) return ""; //needle not found
31 
32  size_t end_pos = postData.find('=',start_pos); //verify = sign
33  if(end_pos == std::string::npos) return ""; //= not found
34 
35  start_pos += needle.length() + 2; //get past & and field
36  end_pos = postData.find('&',start_pos); //skip needle and = sign
37  if(end_pos == std::string::npos) postData.length(); //not found, so take data to end
38 
39  //__COUT__ << "start_pos=" << start_pos
40  // << "end_pos=" << end_pos << std::endl;
41  return postData.substr(start_pos,end_pos-start_pos); //return value
42 }
43 
44 //========================================================================================================================
45 //getData
46 // returns "" if not found
47 // get query data format is expected to be: needle1=value1&needle2=value2...
48 std::string CgiDataUtilities::getData(cgicc::Cgicc &cgi, const std::string &needle)
49 {
50  std::string getData = "&"+cgi.getEnvironment().getQueryString();
51  //__COUT__ << "getData: " + getData << std::endl;
52 
53  size_t start_pos = getData.find("&"+needle+"="); //add & and = to make sure found field and not part of a value
54  if(start_pos == std::string::npos) return ""; //needle not found
55 
56  size_t end_pos = getData.find('=',start_pos); //verify = sign
57  if(end_pos == std::string::npos) return ""; //= not found
58 
59  start_pos += needle.length() + 2; //get past & and field
60  end_pos = getData.find('&',start_pos); //skip needle and = sign
61  if(end_pos != std::string::npos) end_pos -= start_pos; //found, so determine sz of field
62 
63  //__COUT__ << "start_pos=" << start_pos << " '" << getData[start_pos] <<
64  // "' end_pos=" << end_pos << " := " << getData.substr(start_pos,end_pos) << std::endl;
65 
66  return getData.substr(start_pos,end_pos); //return value
67 }
68 
69 //========================================================================================================================
70 int CgiDataUtilities::getOrPostDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
71 { return atoi(getOrPostData(cgi,needle).c_str()); }
72 int CgiDataUtilities::postDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
73 { return atoi(postData(cgi,needle).c_str()); }
74 int CgiDataUtilities::getDataAsInt (cgicc::Cgicc& cgi, const std::string& needle)
75 { return atoi(getData(cgi,needle).c_str()); }
76 
77 
78 
79 //==============================================================================
80 //decodeURIComponent
81 // converts all %## to the ascii character
82 std::string CgiDataUtilities::decodeURIComponent(const std::string &data)
83 {
84  std::string decodeURIString(data.size(),0); //init to same size
85  unsigned int j=0;
86  for(unsigned int i=0;i<data.size();++i,++j)
87  {
88  if(data[i] == '%')
89  {
90  //high order hex nibble digit
91  if(data[i+1] > '9') //then ABCDEF
92  decodeURIString[j] += (data[i+1]-55)*16;
93  else
94  decodeURIString[j] += (data[i+1]-48)*16;
95 
96  //low order hex nibble digit
97  if(data[i+2] > '9') //then ABCDEF
98  decodeURIString[j] += (data[i+2]-55);
99  else
100  decodeURIString[j] += (data[i+2]-48);
101 
102  i+=2; //skip to next char
103  }
104  else
105  decodeURIString[j] = data[i];
106  }
107  decodeURIString.resize(j);
108  return decodeURIString;
109 }