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