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