Panda3D
parsedhttprequest.cxx
1 #pragma warning(disable : 4789)
2 #pragma warning(disable : 4786)
3 
4 #include "parsedhttprequest.h"
5 
6 ////////////////////////////////////////////////////////////////////
7 inline std::string & trimleft_inplace(std::string & s)
8 {
9  s.erase(0, s.find_first_not_of(" \t\n\r"));
10  return s;
11 }
12 
13 
14 ////////////////////////////////////////////////////////////////////
15 inline std::string & trimright_inplace(std::string & s)
16 {
17  size_t idx = s.find_last_not_of(" \t\n\r");
18 
19  if (std::string::npos == idx)
20  {
21  s.erase();
22  }
23  else
24  {
25  char c = s.at(idx);
26  s.erase(idx, std::string::npos);
27  s.append(1, c);
28  }
29 
30  return s;
31 }
32 ////////////////////////////////////////////////////////////////////
33 inline std::string & trim_inplace(std::string & s)
34 {
35  trimleft_inplace(s);
36  trimright_inplace(s);
37  return s;
38 }
39 
40 inline std::string trim_tonew(const std::string &in)
41 {
42  std::string ss = in;
43  return trim_inplace(ss);
44 }
45 
46 
47 
48 
49 std::string ParsedHttpRequest::deCanonicalize(std::string &inval)
50 {
51  std::string work("");
52  unsigned int x=0;
53  while (x < inval.size())
54  {
55  switch(inval[x])
56  {
57  case('+'):
58  work+=' ';
59  x++;
60  break;
61 
62  case('%'):
63  if(x+2 < inval.size())
64  {
65  x++;
66  char aa[5];
67  char * end;
68  aa[0] = inval[x++];
69  aa[1] = inval[x++];
70  aa[2] = '\0';
71  char c = ( char ) strtoul(aa,&end,16);
72  work+=c;
73  }
74  else
75  x+=3;
76  break;
77 
78  default:
79  work+=inval[x++];
80  break;
81  }
82  }
83  return work;
84 }
85 
86 size_t ParsedHttpRequest::PullCR(std::string &src, std::string &dst)
87 {
88  size_t offset = src.find(' ');
89  if(offset >= 0 )
90  {
91  dst = src.substr(0,offset);
92  src = src.substr(offset+1);
93  }
94  return offset;
95 }
96 
97 
98 void ParsedHttpRequest::clear(void)
99 {
100  _RequestType = "";
101  _parameters.clear();
102 }
103 
104 const std::string * ParsedHttpRequest::GetOption(const std::string & query)
105 {
106  std::map<std::string,std::string>::iterator ii;
107  ii = _parameters.find(query);
108  if(ii == _parameters.end())
109  return NULL;
110 
111  return &ii->second;
112 }
113 
114 
115 bool ParsedHttpRequest::GetOption(const std::string & query, std::string & out_value)
116 {
117  std::map<std::string,std::string>::iterator ii;
118  ii = _parameters.find(query);
119  if(ii == _parameters.end())
120  {
121  out_value = "";
122  return false;
123  }
124  out_value = ii->second;
125  return true;
126 }
127 
128 bool ParsedHttpRequest::ParseThis(char * request)
129 {
130  _Raw_Text = request;
131 // printf("%s\n\n",request);
132 
133 
134  std::string work1(_Raw_Text);
135  for(size_t pos = work1.find_first_of("\n\r\0") ; pos != std::string::npos ; pos = work1.find_first_of("\n\r\0") )
136  {
137  std::string line1 = work1.substr(0,pos);
138  work1 = work1.substr(pos+1);
139  if(line1.size() > 2)
140  {
141 // printf(" Line[%s]\n",line1.c_str());
142  size_t i_pos = line1.find(':');
143  if(i_pos != std::string::npos && i_pos > 1)
144  {
145  std::string noune = trim_tonew(line1.substr(0,i_pos));
146  std::string verb = trim_tonew(line1.substr(i_pos+1));
147 
148  //printf(" Noune [%s][%s]\n",noune.c_str(),verb.c_str());
149  _header_Lines[noune] = verb;
150 
151  }
152  }
153  }
154 
155  //
156  // Get the url for the request ..
157  //
158  std::string work(request);
159  std::string line1 = work.substr(0,work.find_first_of("\n\r\0"));
160  if(line1.size() < 4)
161  return false;
162 
163  if(PullCR(line1,_RequestType) < 3)
164  return false;
165 
166  if(PullCR(line1,_RequestText) < 1)
167  return false;
168 
169  size_t loc = (int)_RequestText.find('?');
170  if(loc != std::string::npos)
171  {
172  _Requestoptions = _RequestText.substr(loc+1);
173  _RequestText = _RequestText.substr(0,loc);
174  }
175 
176  return ProcessOptionString(_Requestoptions);
177 }
178 
179 std::string & ParsedHttpRequest::GetRequestURL(void)
180 {
181  return _RequestText;
182 };
183 
184 bool ParsedHttpRequest::ProcessOptionString(std::string str)
185 {
186  size_t loc;
187  for(loc = str.find('&'); loc != std::string::npos; loc = str.find('&'))
188  {
189  std::string valset = str.substr(0,loc);
190  str = str.substr(loc+1);
191  if(ProcessParamSet(valset) != true)
192  return false;
193  }
194  return ProcessParamSet(str);
195 };
196 
197 bool ParsedHttpRequest::ProcessParamSet(std::string &str)
198 {
199  std::string val("");
200  size_t loc = str.find('=');
201 
202  if(loc != std::string::npos)
203  {
204  val = str.substr(loc+1);
205  str = str.substr(0,loc);
206 
207  std::string ind1 = deCanonicalize(str);
208  _parameters[ind1] = deCanonicalize(val);
209  }
210  return true;
211 }
212 
213 
214