ffead.server.doc
HttpRequest.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * HttpRequest.cpp
18  *
19  * Created on: Aug 10, 2009
20  * Author: sumeet
21  */
22 
23 #include "HttpRequest.h"
24 using namespace std;
25 
26 void HttpRequest::unbase64(string str)
27 {
28  //logger << "before " << str << endl;
29  authinfo["Method"] = (str.substr(0,str.find(" ")));
30  str = str.substr(str.find(" ")+1);
31  //logger << "after " << str << endl;
32 
33  /*unsigned char *input = (unsigned char *)str.c_str();
34  int length = str.length();
35 
36  BIO *b64, *bmem;
37 
38  char *buffer = (char *)malloc(length);
39  memset(buffer, 0, length);
40 
41  b64 = BIO_new(BIO_f_base64());
42  bmem = BIO_new_mem_buf(input, length);
43  bmem = BIO_push(b64, bmem);
44 
45  BIO_read(bmem, buffer, length);
46 
47  BIO_free_all(bmem);
48 
49  string temp(buffer);*/
50  unsigned char *input = (unsigned char *)str.c_str();
51  int length = str.length();
52  string temp = CryptoHandler::base64decode(input,length);
53  //logger << "after " << temp << endl;
54 
55  authinfo["Username"] = (temp.substr(0,temp.find(":")));
56  temp = temp.substr(temp.find(":")+1);
57  authinfo["Password"] = (temp);
58 }
59 
60 void HttpRequest::getOauthParams(string str)
61 {
62  authinfo["Method"] = str.substr(0,str.find(" "));
63  str = str.substr(str.find(" ")+1);
64  //logger << str << endl;
65 
66  strVec tempv;
67  StringUtil::split(tempv, str, (","));
68  for(unsigned int i=0;i<tempv.size();i++)
69  {
70  strVec tempvv;
71  StringUtil::split(tempvv, tempv.at(i), ("="));
72  StringUtil::replaceFirst(tempvv.at(0),"\r","");
73  StringUtil::replaceFirst(tempvv.at(0),"\n","");
74  string temr = tempvv.at(1);
75  temr = temr.substr(temr.find("\"")+1);
76  temr = temr.substr(0,temr.find("\""));
77  authinfo[tempvv.at(0)] = temr;
78  authorderinf[authorderinf.size()+1] = CryptoHandler::urlDecode(tempvv.at(0));
79  //logger << tempvv.at(0) << " = " << temr << endl;
80  }
81 }
82 
83 HttpRequest::HttpRequest()
84 {
85  logger = Logger::getLogger("HttpRequest");
86 }
87 
88 HttpRequest::HttpRequest(strVec vec,string path)
89 {
90  if(vec.size()!=0){
91  this->setContent("");
92  string conten;
93  bool contStarts = false;
94  this->cookie = false;
95  for(unsigned int i=0;i<vec.size();i++)
96  {
97  strVec temp,vemp,memp;
98  if((vec.at(i)=="\r" || vec.at(i)==""|| vec.at(i)=="\r\n") && !contStarts)
99  {
100  contStarts = true;
101  continue;
102  }
103  //if(!contStarts && vec.at(i)=="\r")
104  // contStarts = true;
105  StringUtil::split(temp, vec.at(i), (": "));
106  if(!contStarts && temp.size()>1)
107  {
108  StringUtil::replaceFirst(temp.at(1),"\r","");
109  if(temp.at(0)=="Host")
110  this->setHost(temp.at(1));
111  else if(temp.at(0)=="User-Agent" || temp.at(0)=="User-agent")
112  this->setUser_agent(temp.at(1));
113  else if(temp.at(0)=="Accept")
114  this->setAccept(temp.at(1));
115  else if(temp.at(0)=="Authorization")
116  {
117  //logger << "found auth" <<endl;
118  if(temp.at(1).find("oauth_")!=string::npos)
119  {
120  this->getOauthParams(temp.at(1));
121  }
122  else
123  this->unbase64(temp.at(1));
124  }
125  else if(temp.at(0)=="Accept-Language" || temp.at(0)=="Accept-language")
126  {
127  strVec lemp;
128  this->setAccept_lang(temp.at(1));
129  StringUtil::split(lemp, temp.at(1), (","));
130  for(unsigned int li=0;li<lemp.size();li++)
131  {
132  if(lemp.at(li).find(";")==string::npos && lemp.at(li)!="")
133  {
134  string t = lemp.at(li);
135  size_t s = t.find_first_not_of(" ");
136  size_t e = t.find_last_not_of(" ")+1;
137  t = t.substr(s,e-s);
138  this->localeInfo.push_back(t);
139  }
140  else if(lemp.at(li)!="")
141  {
142  string t = lemp.at(li);
143  size_t s = t.find_first_not_of(" ");
144  size_t e = t.find(";");
145  t = t.substr(s,e-s);
146  e = t.find_last_not_of(" ")+1;
147  t = t.substr(0,e);
148  this->localeInfo.push_back(t);
149  }
150  }
151  //logger << temp.at(1) << flush;
152  }
153  else if(temp.at(0)=="Accept-Encoding" || temp.at(0)=="Accept-encoding")
154  this->setAccept_encod(temp.at(1));
155  else if(temp.at(0)=="Accept-Charset" || temp.at(0)=="Accept-charset")
156  this->setAccept_lang(temp.at(1));
157  else if(temp.at(0)=="Keep-Alive" || temp.at(0)=="Keep-alive")
158  this->setKeep_alive(temp.at(1));
159  else if(temp.at(0)=="Connection")
160  this->setConnection(temp.at(1));
161  else if(temp.at(0)=="Cache-Control" || temp.at(0)=="Cache-control")
162  this->setCache_ctrl(temp.at(1));
163  else if(temp.at(0)=="Content-Type" || temp.at(0)=="Content-type")
164  {
165  this->setContent_type(temp.at(1));
166  string tempi(temp.at(1));
167  size_t s = tempi.find("boundary");
168  if(s!=string::npos)
169  {
170  this->setContent_type(tempi.substr(0,s));
171  tempi = tempi.substr(s);
172  strVec results;
173  StringUtil::split(results, tempi, ("="));
174  if(results.size()==2)
175  {
176  string bound = "--" + results.at(1).substr(0,results.at(1).length());
177  this->setContent_boundary(bound);
178  }
179  }
180  }
181  else if(temp.at(0)=="Cookie")
182  {
183  this->cookie = true;
184  strVec results;
185  StringUtil::split(results, temp.at(1), ("; "));
186  for(unsigned j=0;j<(int)results.size();j++)
187  {
188  strVec results1;
189  StringUtil::split(results1, results.at(j), ("="));
190  if(results1.size()==2)
191  cookieattrs[results1.at(0)] = results1.at(1);
192  else
193  cookieattrs[results1.at(0)] = "true";
194  }
195  }
196  else if(temp.at(0)=="Content-Length" || temp.at(0)=="Content-length")
197  this->setContent_len(temp.at(1));
198  else if(temp.at(0)=="Referer")
199  this->setReferer(temp.at(1));
200  else if(temp.at(0)=="Pragma")
201  this->setPragma(temp.at(1));
202  else
203  xtraHeaders[temp.at(0)] = temp.at(1);
204  //logger << temp.at(0) << "---" << temp.at(1) << flush;
205  }
206  else
207  {
208  string tem = temp.at(0);
209  if(!contStarts && tem.find("GET")!=string::npos)
210  {
211  StringUtil::replaceFirst(tem,"GET ","");
212  this->setMethod("GET");
213  StringUtil::split(vemp, tem, (" "));
214  if(vemp.size()==2)
215  {
216  StringUtil::replaceFirst(vemp.at(1),"\r","");
217  this->setHttpVersion(vemp.at(1));
218  StringUtil::replaceFirst(vemp.at(0)," ","");
219  if(vemp.at(0).find("?")!=string ::npos)
220  {
221  strVec params;
222  string valu(vemp.at(0));
223  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
224  valu = valu.substr(valu.find("?")+1);
225  //valu = CryptoHandler::urlDecode(valu);
226  StringUtil::split(params,valu , ("&"));
227  map<string ,int> indices;
228  map<string,string>::iterator it;
229  for(unsigned j=0;j<params.size();j++)
230  {
231  strVec param;
232  StringUtil::split(param, params.at(j), ("="));
233  if(param.size()==2)
234  {
235  string att = param.at(0);
236  StringUtil::replaceFirst(att,"\r","");
237  StringUtil::replaceFirst(att,"\t","");
238  StringUtil::replaceFirst(att," ","");
239  //this->setRequestParam(att,CryptoHandler::urlDecode(param.at(1)));
240  string attN = CryptoHandler::urlDecode(att);
241  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
242  {
243  if(indices.find(attN)==indices.end())
244  {
245  indices[attN] = 0;
246  }
247  else
248  {
249  indices[attN] = indices[attN] + 1;
250  }
251  this->queryParams[attN.substr(0, attN.find("[")+1)
252  + CastUtil::lexical_cast<string>(indices[attN])
253  + "]"] = CryptoHandler::urlDecode(param.at(1));
254  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
255  + CastUtil::lexical_cast<string>(indices[attN])
256  + "]"
257  + CryptoHandler::urlDecode(param.at(1))) << endl;
258  }
259  else
260  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
261  //logger << att << " = " << param.at(1) << endl;
262  reqorderinf[reqorderinf.size()+1] = att;
263  }
264  }
265  }
266  this->setActUrl(vemp.at(0));
267  memp = StringUtil::split(vemp.at(0), ("/"));
268  int fs = vemp.at(0).find_first_of("/");
269  int es = vemp.at(0).find_last_of("/");
270  if(fs==es)
271  {
272  this->setCntxt_root(path+"default");
273  this->setCntxt_name("default");
274  this->setFile(vemp.at(0).substr(es+1));
275  this->setUrl(path+"default/"+vemp.at(0));
276  }
277  else
278  {
279  int ss = vemp.at(0).substr(fs+1).find("/");
280  if(ss>fs)
281  {
282  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
283  this->setCntxt_root(path+this->getCntxt_name());
284  this->setFile(vemp.at(0).substr(es+1));
285  this->setUrl(path+vemp.at(0));
286  }
287  }
288  }
289  }
290  else if(!contStarts && tem.find("HEAD")!=string::npos)
291  {
292  StringUtil::replaceFirst(tem,"HEAD ","");
293  this->setMethod("HEAD");
294  StringUtil::split(vemp, tem, (" "));
295  if(vemp.size()==2)
296  {
297  StringUtil::replaceFirst(vemp.at(1),"\r","");
298  this->setHttpVersion(vemp.at(1));
299  StringUtil::replaceFirst(vemp.at(0)," ","");
300  if(vemp.at(0).find("?")!=string ::npos)
301  {
302  strVec params;
303  string valu(vemp.at(0));
304  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
305  valu = valu.substr(valu.find("?")+1);
306  //valu = CryptoHandler::urlDecode(valu);
307  StringUtil::split(params,valu , ("&"));
308  map<string ,int> indices;
309  map<string,string>::iterator it;
310  for(unsigned j=0;j<params.size();j++)
311  {
312  strVec param;
313  StringUtil::split(param, params.at(j), ("="));
314  if(param.size()==2)
315  {
316  string att = param.at(0);
317  StringUtil::replaceFirst(att,"\r","");
318  StringUtil::replaceFirst(att,"\t","");
319  StringUtil::replaceFirst(att," ","");
320  //this->setRequestParam(att,CryptoHandler::urlDecode(param.at(1)));
321  string attN = CryptoHandler::urlDecode(att);
322  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
323  {
324  if(indices.find(attN)==indices.end())
325  {
326  indices[attN] = 0;
327  }
328  else
329  {
330  indices[attN] = indices[attN] + 1;
331  }
332  this->queryParams[attN.substr(0, attN.find("[")+1)
333  + CastUtil::lexical_cast<string>(indices[attN])
334  + "]"] = CryptoHandler::urlDecode(param.at(1));
335  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
336  + CastUtil::lexical_cast<string>(indices[attN])
337  + "]"
338  + CryptoHandler::urlDecode(param.at(1))) << endl;
339  }
340  else
341  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
342  //logger << att << " = " << param.at(1) << endl;
343  reqorderinf[reqorderinf.size()+1] = att;
344  }
345  }
346  }
347  this->setActUrl(vemp.at(0));
348  memp = StringUtil::split(vemp.at(0), ("/"));
349  int fs = vemp.at(0).find_first_of("/");
350  int es = vemp.at(0).find_last_of("/");
351  if(fs==es)
352  {
353  this->setCntxt_root(path+"default");
354  this->setCntxt_name("default");
355  this->setFile(vemp.at(0).substr(es+1));
356  this->setUrl(path+"default/"+vemp.at(0));
357  }
358  else
359  {
360  int ss = vemp.at(0).substr(fs+1).find("/");
361  if(ss>fs)
362  {
363  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
364  this->setCntxt_root(path+this->getCntxt_name());
365  this->setFile(vemp.at(0).substr(es+1));
366  this->setUrl(path+vemp.at(0));
367  }
368  }
369  }
370  }
371  else if(!contStarts && tem.find("TRACE")!=string::npos)
372  {
373  StringUtil::replaceFirst(tem,"TRACE ","");
374  this->setMethod("TRACE");
375  StringUtil::split(vemp, tem, (" "));
376  if(vemp.size()==2)
377  {
378  StringUtil::replaceFirst(vemp.at(1),"\r","");
379  this->setHttpVersion(vemp.at(1));
380  StringUtil::replaceFirst(vemp.at(0)," ","");
381  if(vemp.at(0).find("?")!=string ::npos)
382  {
383  strVec params;
384  string valu(vemp.at(0));
385  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
386  valu = valu.substr(valu.find("?")+1);
387  //valu = CryptoHandler::urlDecode(valu);
388  StringUtil::split(params,valu , ("&"));
389  map<string ,int> indices;
390  map<string,string>::iterator it;
391  for(unsigned j=0;j<params.size();j++)
392  {
393  strVec param;
394  StringUtil::split(param, params.at(j), ("="));
395  if(param.size()==2)
396  {
397  string att = param.at(0);
398  StringUtil::replaceFirst(att,"\r","");
399  StringUtil::replaceFirst(att,"\t","");
400  StringUtil::replaceFirst(att," ","");
401  //this->setRequestParam(att,CryptoHandler::urlDecode(param.at(1)));
402  string attN = CryptoHandler::urlDecode(att);
403  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
404  {
405  if(indices.find(attN)==indices.end())
406  {
407  indices[attN] = 0;
408  }
409  else
410  {
411  indices[attN] = indices[attN] + 1;
412  }
413  this->queryParams[attN.substr(0, attN.find("[")+1)
414  + CastUtil::lexical_cast<string>(indices[attN])
415  + "]"] = CryptoHandler::urlDecode(param.at(1));
416  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
417  + CastUtil::lexical_cast<string>(indices[attN])
418  + "]"
419  + CryptoHandler::urlDecode(param.at(1))) << endl;
420  }
421  else
422  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
423  //logger << att << " = " << param.at(1) << endl;
424  reqorderinf[reqorderinf.size()+1] = att;
425  }
426  }
427  }
428  this->setActUrl(vemp.at(0));
429  memp = StringUtil::split(vemp.at(0), ("/"));
430  int fs = vemp.at(0).find_first_of("/");
431  int es = vemp.at(0).find_last_of("/");
432  if(fs==es)
433  {
434  this->setCntxt_root(path+"default");
435  this->setCntxt_name("default");
436  this->setFile(vemp.at(0).substr(es+1));
437  this->setUrl(path+"default/"+vemp.at(0));
438  }
439  else
440  {
441  int ss = vemp.at(0).substr(fs+1).find("/");
442  if(ss>fs)
443  {
444  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
445  this->setCntxt_root(path+this->getCntxt_name());
446  this->setFile(vemp.at(0).substr(es+1));
447  this->setUrl(path+vemp.at(0));
448  }
449  }
450  }
451  }
452  else if(!contStarts && tem.find("OPTIONS")!=string::npos)
453  {
454  StringUtil::replaceFirst(tem,"OPTIONS ","");
455  this->setMethod("OPTIONS");
456  StringUtil::split(vemp, tem, (" "));
457  if(vemp.size()==2)
458  {
459  StringUtil::replaceFirst(vemp.at(1),"\r","");
460  this->setHttpVersion(vemp.at(1));
461  StringUtil::replaceFirst(vemp.at(0)," ","");
462  this->setActUrl(vemp.at(0));
463  memp = StringUtil::split(vemp.at(0), ("/"));
464  int fs = vemp.at(0).find_first_of("/");
465  int es = vemp.at(0).find_last_of("/");
466  if(fs==es)
467  {
468  this->setCntxt_root(path+"default");
469  this->setCntxt_name("default");
470  this->setFile(vemp.at(0).substr(es+1));
471  this->setUrl(path+"default/"+vemp.at(0));
472  }
473  else
474  {
475  int ss = vemp.at(0).substr(fs+1).find("/");
476  if(ss>fs)
477  {
478  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
479  this->setCntxt_root(path+this->getCntxt_name());
480  this->setFile(vemp.at(0).substr(es+1));
481  this->setUrl(path+vemp.at(0));
482  }
483  }
484  }
485  }
486  else if(!contStarts && tem.find("DELETE")!=string::npos)
487  {
488  StringUtil::replaceFirst(tem,"DELETE ","");
489  this->setMethod("DELETE");
490  StringUtil::split(vemp, tem, (" "));
491  if(vemp.size()==2)
492  {
493  StringUtil::replaceFirst(vemp.at(1),"\r","");
494  this->setHttpVersion(vemp.at(1));
495  StringUtil::replaceFirst(vemp.at(0)," ","");
496  if(vemp.at(0).find("?")!=string ::npos)
497  {
498  strVec params;
499  string valu(vemp.at(0));
500  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
501  valu = valu.substr(valu.find("?")+1);
502  valu = CryptoHandler::urlDecode(valu);
503  StringUtil::split(params,valu, ("&"));
504  map<string ,int> indices;
505  map<string,string>::iterator it;
506  for(unsigned j=0;j<params.size();j++)
507  {
508  strVec param;
509  StringUtil::split(param, params.at(j), ("="));
510  if(param.size()==2)
511  {
512  string att = param.at(0);
513  StringUtil::replaceFirst(att,"\r","");
514  StringUtil::replaceFirst(att,"\t","");
515  StringUtil::replaceFirst(att," ","");
516  string attN = CryptoHandler::urlDecode(att);
517  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
518  {
519  if(indices.find(attN)==indices.end())
520  {
521  indices[attN] = 0;
522  }
523  else
524  {
525  indices[attN] = indices[attN] + 1;
526  }
527  this->queryParams[attN.substr(0, attN.find("[")+1)
528  + CastUtil::lexical_cast<string>(indices[attN])
529  + "]"] = CryptoHandler::urlDecode(param.at(1));
530  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
531  + CastUtil::lexical_cast<string>(indices[attN])
532  + "]"
533  + CryptoHandler::urlDecode(param.at(1))) << endl;
534  }
535  else
536  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
537  reqorderinf[reqorderinf.size()+1] = att;
538  }
539  }
540  }
541  this->setActUrl(vemp.at(0));
542  memp = StringUtil::split(vemp.at(0), ("/"));
543  int fs = vemp.at(0).find_first_of("/");
544  int es = vemp.at(0).find_last_of("/");
545  if(fs==es)
546  {
547  this->setCntxt_root(path+"default");
548  this->setCntxt_name("default");
549  this->setFile(vemp.at(0).substr(es+1));
550  this->setUrl(path+"default/"+vemp.at(0));
551  }
552  else
553  {
554  int ss = vemp.at(0).substr(fs+1).find("/");
555  if(ss>fs)
556  {
557  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
558  this->setCntxt_root(path+this->getCntxt_name());
559  this->setFile(vemp.at(0).substr(es+1));
560  this->setUrl(path+vemp.at(0));
561  }
562  }
563  }
564  }
565  else if(!contStarts && tem.find("PUT")!=string::npos)
566  {
567  StringUtil::replaceFirst(tem,"PUT ","");
568  this->setMethod("PUT");
569  StringUtil::split(vemp, tem, (" "));
570  if(vemp.size()==2)
571  {
572  StringUtil::replaceFirst(vemp.at(1),"\r","");
573  this->setHttpVersion(vemp.at(1));
574  StringUtil::replaceFirst(vemp.at(0)," ","");
575  if(vemp.at(0).find("?")!=string ::npos)
576  {
577  strVec params;
578  string valu(vemp.at(0));
579  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
580  valu = valu.substr(valu.find("?")+1);
581  valu = CryptoHandler::urlDecode(valu);
582  StringUtil::split(params,valu, ("&"));
583  map<string ,int> indices;
584  map<string,string>::iterator it;
585  for(unsigned j=0;j<params.size();j++)
586  {
587  strVec param;
588  StringUtil::split(param, params.at(j), ("="));
589  if(param.size()==2)
590  {
591  string att = param.at(0);
592  StringUtil::replaceFirst(att,"\r","");
593  StringUtil::replaceFirst(att,"\t","");
594  StringUtil::replaceFirst(att," ","");
595  string attN = CryptoHandler::urlDecode(att);
596  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
597  {
598  if(indices.find(attN)==indices.end())
599  {
600  indices[attN] = 0;
601  }
602  else
603  {
604  indices[attN] = indices[attN] + 1;
605  }
606  this->queryParams[attN.substr(0, attN.find("[")+1)
607  + CastUtil::lexical_cast<string>(indices[attN])
608  + "]"] = CryptoHandler::urlDecode(param.at(1));
609  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
610  + CastUtil::lexical_cast<string>(indices[attN])
611  + "]"
612  + CryptoHandler::urlDecode(param.at(1))) << endl;
613  }
614  else
615  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
616  reqorderinf[reqorderinf.size()+1] = att;
617  }
618  }
619  }
620  this->setActUrl(vemp.at(0));
621  memp = StringUtil::split(vemp.at(0), ("/"));
622  int fs = vemp.at(0).find_first_of("/");
623  int es = vemp.at(0).find_last_of("/");
624  if(fs==es)
625  {
626  this->setCntxt_root(path+"default");
627  this->setCntxt_name("default");
628  this->setFile(vemp.at(0).substr(es+1));
629  this->setUrl(path+"default/"+vemp.at(0));
630  }
631  else
632  {
633  int ss = vemp.at(0).substr(fs+1).find("/");
634  if(ss>fs)
635  {
636  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
637  this->setCntxt_root(path+this->getCntxt_name());
638  this->setFile(vemp.at(0).substr(es+1));
639  this->setUrl(path+vemp.at(0));
640  }
641  }
642  }
643  }
644  else if(!contStarts && tem.find("POST")!=string::npos)
645  {
646  StringUtil::replaceFirst(tem,"POST ","");
647  this->setMethod("POST");
648  StringUtil::split(vemp, tem, (" "));
649  if(vemp.size()==2)
650  {
651  //this->setUrl(vemp.at(0));
652  //string pat(vemp.at(0));
653  StringUtil::replaceFirst(vemp.at(1),"\r","");
654  this->setHttpVersion(vemp.at(1));
655  StringUtil::replaceFirst(vemp.at(0)," ","");
656  if(vemp.at(0).find("?")!=string ::npos)
657  {
658  strVec params;
659  string valu(vemp.at(0));
660  vemp[0] = valu.substr(0,vemp.at(0).find("?"));
661  valu = valu.substr(valu.find("?")+1);
662  valu = CryptoHandler::urlDecode(valu);
663  StringUtil::split(params,valu, ("&"));
664  map<string ,int> indices;
665  map<string,string>::iterator it;
666  for(unsigned j=0;j<params.size();j++)
667  {
668  strVec param;
669  StringUtil::split(param, params.at(j), ("="));
670  if(param.size()==2)
671  {
672  string att = param.at(0);
673  StringUtil::replaceFirst(att,"\r","");
674  StringUtil::replaceFirst(att,"\t","");
675  StringUtil::replaceFirst(att," ","");
676  string attN = CryptoHandler::urlDecode(att);
677  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
678  {
679  if(indices.find(attN)==indices.end())
680  {
681  indices[attN] = 0;
682  }
683  else
684  {
685  indices[attN] = indices[attN] + 1;
686  }
687  this->queryParams[attN.substr(0, attN.find("[")+1)
688  + CastUtil::lexical_cast<string>(indices[attN])
689  + "]"] = CryptoHandler::urlDecode(param.at(1));
690  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
691  + CastUtil::lexical_cast<string>(indices[attN])
692  + "]"
693  + CryptoHandler::urlDecode(param.at(1))) << endl;
694  }
695  else
696  this->setQueryParam(attN,CryptoHandler::urlDecode(param.at(1)));
697  reqorderinf[reqorderinf.size()+1] = att;
698  }
699  }
700  }
701  this->setActUrl(vemp.at(0));
702  memp = StringUtil::split(vemp.at(0), ("/"));
703  int fs = vemp.at(0).find_first_of("/");
704  int es = vemp.at(0).find_last_of("/");
705  if(fs==es)
706  {
707  this->setCntxt_root(path+"default");
708  this->setCntxt_name("default");
709  this->setFile(vemp.at(0).substr(es+1));
710  this->setUrl(path+"default/"+vemp.at(0));
711  }
712  else
713  {
714  int ss = vemp.at(0).substr(fs+1).find("/");
715  if(ss>fs)
716  {
717  this->setCntxt_name(vemp.at(0).substr(fs+1,ss-fs));
718  this->setCntxt_root(path+this->getCntxt_name());
719  this->setFile(vemp.at(0).substr(es+1));
720  this->setUrl(path+vemp.at(0));
721  }
722  }
723  }
724  }
725  else if(contStarts)
726  {
727  /*string temp;
728  if(vec.at(i).find("<?")!=string::npos && vec.at(i).find("?>")!=string::npos)
729  {
730  temp = vec.at(i).substr(vec.at(i).find("?>")+2);
731  conten.append(temp);
732  }
733  else*/
734  conten.append(vec.at(i));
735  if(i!=vec.size()-1)
736  conten.append("\n");
737  //this->content=con;
738  }
739  }
740  }
741  this->setContent(conten);
742  /*if(this->getContent()!="")
743  {
744  strVec params;
745  string content = this->getContent();
746  //logger << content << flush;
747  StringUtil::split(params, content, ("&"));
748  //logger << "\n\n\nsize: " << params.size() << flush;
749  for(unsigned j=0;j<params.size();j++)
750  {
751  strVec param;
752  //logger << params.at(j) << flush;
753  StringUtil::split(param, params.at(j), ("="));
754  //logger << param.size() << flush;
755  if(param.size()==2)
756  {
757  string att = param.at(0);
758  StringUtil::replaceFirst(att,"\r","");
759  StringUtil::replaceFirst(att,"\t","");
760  StringUtil::replaceFirst(att," ","");
761  //logger << "attribute: " << param.at(0) << "\n"<< flush;
762  //logger << "value: " << param.at(1) << "\n" << flush;
763  this->setRequestParam(att,param.at(1));
764  }
765  }
766  }*/
767  if(this->getContent()!="")
768  {
769  //logger << this->getContent() << flush;
770  if(this->getContent_type().find("application/x-www-form-urlencoded")!=string::npos)
771  {
772  strVec params;
773  string valu(this->getContent());
774  StringUtil::split(params,valu , ("&"));
775  map<string ,int> indices;
776  map<string,string>::iterator it;
777  for(unsigned j=0;j<params.size();j++)
778  {
779  strVec param;
780  StringUtil::split(param, params.at(j), ("="));
781  if(param.size()==2)
782  {
783  string att = param.at(0);
784  StringUtil::replaceFirst(att,"\r","");
785  StringUtil::replaceFirst(att,"\t","");
786  StringUtil::replaceFirst(att," ","");
787  string attN = CryptoHandler::urlDecode(att);
788  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
789  {
790  if(indices.find(attN)==indices.end())
791  {
792  indices[attN] = 0;
793  }
794  else
795  {
796  indices[attN] = indices[attN] + 1;
797  }
798  this->requestParams[attN.substr(0, attN.find("[")+1)
799  + CastUtil::lexical_cast<string>(indices[attN])
800  + "]"] = CryptoHandler::urlDecode(param.at(1));
801  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
802  + CastUtil::lexical_cast<string>(indices[attN])
803  + "]"
804  + CryptoHandler::urlDecode(param.at(1))) << endl;
805  }
806  else
807  this->setRequestParam(attN,CryptoHandler::urlDecode(param.at(1)));
808  reqorderinf[reqorderinf.size()+1] = att;
809  }
810  }
811  }
812  else if(this->getContent()!="" && this->getContent_boundary()!="")
813  {
814  string delb = "\r"+this->getContent_boundary();
815  string delend = "\r"+this->getContent_boundary()+"--";
816  size_t stb = this->getContent().find_first_of(delb)+delb.length()+1;
817  //size_t enb = this->getContent().find_last_not_of(delend);
818  string param_conts = this->getContent().substr(stb);
819  StringUtil::replaceFirst(param_conts,delend,"");
820  param_conts = param_conts.substr(0,param_conts.length()-1);
821  strVec parameters;
822  StringUtil::split(parameters, param_conts, (delb));
823  //logger << "Boundary: " << this->getContent_boundary() << flush;
824  //logger << "\nLength: " << this->getContent().length() << flush;
825  //logger << "\nStart End: " << stb << " " << enb << "\n" << flush;
826  //logger << "\nContent: " << param_conts << "\n" << flush;
827  map<string ,int> indices;
828  map<string,string>::iterator it;
829  for(unsigned j=0;j<parameters.size();j++)
830  {
831  if(parameters.at(j)=="" || parameters.at(j).find_first_not_of(" ")==string::npos
832  || parameters.at(j).find_first_not_of("\r")==string::npos)
833  continue;
834  FormData datf;
835  string parm = parameters.at(j);
836  //logger << parm << "\nparm" << flush;
837  size_t dis = parm.find("Content-Disposition: ");
838  if(dis==string::npos)
839  dis = parm.find("Content-disposition: ");
840  string cont_disp,cont_type;
841  if(dis!=string::npos)
842  {
843  size_t dist = parm.find("Content-Type: ");
844  if(dist==string::npos)
845  dist = parm.find("Content-yype: ");
846  size_t dise;
847  if(dist==string::npos)
848  {
849  dist = parm.find("\r\r");
850  dise = dist + 2;
851  //logger << "\ndist = npos" << flush;
852  }
853  else
854  {
855  //parm = parm.substr(dist+14);
856  cont_type = parm.substr(dist+14,parm.find("\r\r")-(dist+14));
857  dise = parm.find("\r\r") + 2;
858  //logger << "\nctype = " << cont_type << flush;
859  //dist = dist-12;
860  }
861  cont_disp = parm.substr(dis+21,dist-(dis+21));
862  StringUtil::replaceFirst(cont_disp,"\r","");
863  //logger << "\ncdisp = " << cont_disp << flush;
864  //logger << "\ndise = " << dise << flush;
865  parm = parm.substr(dise);
866  }
867  strVec parmdef;
868  StringUtil::split(parmdef, cont_disp, (";"));
869  string key;
870  for(unsigned k=0;k<parmdef.size();k++)
871  {
872  if(parmdef.at(k)!="" && parmdef.at(k).find("=")!=string::npos)
873  {
874  size_t stpd = parmdef.at(k).find_first_not_of(" ");
875  size_t enpd = parmdef.at(k).find_last_not_of(" ");
876  //logger << "\nparmdef = " << parmdef.at(k) << flush;
877  //logger << "\nst en = " << stpd << " " << enpd << flush;
878  string propert = parmdef.at(k).substr(stpd,enpd-stpd+1);
879  strVec proplr;
880  StringUtil::split(proplr, propert, ("="));
881  if(proplr.size()==2)
882  {
883  if(proplr.at(0)=="name" && proplr.at(1)!="\"\"")
884  {
885  key = proplr.at(1);
886  key = key.substr(key.find_first_not_of("\""),key.find_last_not_of("\"")-key.find_first_not_of("\"")+1);
887  key = CryptoHandler::urlDecode(key);
888  StringUtil::replaceFirst(cont_type,"\r","");
889  datf.type = cont_type;
890  datf.value = parm;
891  }
892  else if(proplr.at(0)=="filename" && proplr.at(1)!="\"\"")
893  {
894  string fna = proplr.at(1);
895  fna = fna.substr(fna.find_first_not_of("\""),fna.find_last_not_of("\"")-fna.find_first_not_of("\"")+1);
896  fna = CryptoHandler::urlDecode(fna);
897  datf.fileName = fna;
898  }
899  }
900  }
901  }
902  if(key!="")
903  {
904  string attN = CryptoHandler::urlDecode(key);
905  if(attN.find("[")!=string::npos && attN.find("]")!=string::npos)
906  {
907  if(indices.find(attN)==indices.end())
908  {
909  indices[attN] = 0;
910  }
911  else
912  {
913  indices[attN] = indices[attN] + 1;
914  }
915  this->requestParamsF[attN.substr(0, attN.find("[")+1)
916  + CastUtil::lexical_cast<string>(indices[attN])
917  + "]"] = datf;
918  logger << ("creating array from similar params" + attN.substr(0, attN.find("[")+1)
919  + CastUtil::lexical_cast<string>(indices[attN])
920  + "]"
921  + datf.fileName) << endl;
922  }
923  this->setRequestParamF(attN,datf);
924  if(datf.fileName!="")
925  {
926  string tmpfile = this->getCntxt_root() + "/temp/"+ this->getContent_boundary() +datf.value;
927  ofstream os;
928  os.open(tmpfile.c_str());
929  os.write(datf.value.c_str(), datf.value.length());
930  os.close();
931  datf.tmpFileName = tmpfile;
932  datf.length = datf.value.length();
933  }
934  string hr = (key + " " + datf.type + " "+ datf.fileName+" "+ datf.value);
935  //logger << hr << flush;
936  }
937  }
938  }
939 
940  }
941  }
942  //logger << this->toString() << flush;
943 }
944 
945 string HttpRequest::buildRequest(const char *keyc,const char *valuec)
946 {
947  string retval;
948  stringstream ss;
949  string key,value;
950  key = keyc;
951  value = valuec;
952  if(key=="Host")
953  this->setHost(value);
954  else if(key=="User-Agent" || key=="User-agent")
955  this->setUser_agent(value);
956  else if(key=="Accept")
957  this->setAccept(value);
958  else if(key=="Accept-Language" || key=="Accept-language")
959  {
960  strVec lemp;
961  this->setAccept_lang(value);
962  StringUtil::split(lemp, value, (","));
963  for(unsigned int li=0;li<lemp.size();li++)
964  {
965  if(lemp.at(li).find(";")==string::npos && lemp.at(li)!="")
966  {
967  string t = lemp.at(li);
968  size_t s = t.find_first_not_of(" ");
969  size_t e = t.find_last_not_of(" ")+1;
970  t = t.substr(s,e-s);
971  this->localeInfo.push_back(t);
972  }
973  else if(lemp.at(li)!="")
974  {
975  string t = lemp.at(li);
976  size_t s = t.find_first_not_of(" ");
977  size_t e = t.find(";");
978  t = t.substr(s,e-s);
979  e = t.find_last_not_of(" ")+1;
980  t = t.substr(0,e);
981  this->localeInfo.push_back(t);
982  }
983  }
984  //logger << temp.at(1) << flush;
985  }
986  else if(key=="Accept-Encoding" || key=="Accept-encoding")
987  this->setAccept_encod(value);
988  else if(key=="Accept-Charset" || key=="Accept-charset")
989  this->setAccept_lang(value);
990  else if(key=="Keep-Alive" || key=="Keep-alive")
991  this->setKeep_alive(value);
992  else if(key=="Connection")
993  this->setConnection(value);
994  else if(key=="Cache-Control" || key=="Cache-control")
995  this->setCache_ctrl(value);
996  else if(key=="Content-Type" || key=="Content-type")
997  {
998  this->setContent_type(value);
999  string tempi(value);
1000  size_t s = tempi.find("boundary");
1001  if(s!=string::npos)
1002  {
1003  this->setContent_type(tempi.substr(0,s));
1004  tempi = tempi.substr(s);
1005  strVec results;
1006  StringUtil::split(results, tempi, ("="));
1007  if(results.size()==2)
1008  {
1009  string bound = "--" + results.at(1).substr(0,results.at(1).length());
1010  this->setContent_boundary(bound);
1011  }
1012  }
1013  }
1014  else if(key=="Content" && value!="")
1015  {
1016  if((this->getContent_type().find("application/soap+xml")!=string::npos || this->getContent_type().find("text/xml")!=string::npos))
1017  {
1018  value = value.substr(0,value.find_last_of(">")+1);
1019  if(value.find("<?")!=string::npos && value.find("?>")!=string::npos)
1020  {
1021  value = value.substr(value.find("?>")+2);
1022  }
1023  this->setContent(value);
1024  }
1025  else if(this->getContent_type().find("application/x-www-form-urlencoded")!=string::npos)
1026  {
1027  strVec params;
1028  StringUtil::split(params,value , ("&"));
1029  for(unsigned j=0;j<params.size();j++)
1030  {
1031  strVec param;
1032  StringUtil::split(param, params.at(j), ("="));
1033  if(param.size()==2)
1034  {
1035  string att = param.at(0);
1036  StringUtil::replaceFirst(att,"\r","");
1037  StringUtil::replaceFirst(att,"\t","");
1038  StringUtil::replaceFirst(att," ","");
1039  this->setRequestParam(att,param.at(1));
1040  }
1041  }
1042  }
1043  else
1044  {
1045  size_t rn = value.find_first_of("\r\n");
1046  string h = CastUtil::lexical_cast<string>((int)rn);
1047 
1048  string boundary = this->getContent_boundary();
1049  fprintf(stderr,boundary.c_str());
1050  fprintf(stderr,value.c_str());
1051  fflush(stderr);
1052 
1053  string retval;
1054  string delb = boundary+"\r\n";
1055  string delend = boundary+"--\r\n";
1056  size_t stb = value.find_first_of(delb)+delb.length();
1057  size_t enb = value.find_last_not_of(delend);
1058  h = CastUtil::lexical_cast<string>((int)stb)+" "+CastUtil::lexical_cast<string>((int)enb);
1059 
1060  string param_conts = value.substr(stb,enb-stb-2);
1061  strVec parameters;
1062  StringUtil::replaceFirst(value,delb,"");
1063  StringUtil::replaceFirst(value,delend,"");
1064  delb = "\r\n"+delb;
1065  StringUtil::split(parameters, value, (delb));
1066  retval = "Boundary: "+ boundary + "\nLength: " ;
1067  retval += CastUtil::lexical_cast<string>((int)value.length()) +"\nStart End: "
1068  + CastUtil::lexical_cast<string>((int)stb) + " " + CastUtil::lexical_cast<string>((int)enb) +"\n";
1069 
1070  for(unsigned j=0;j<parameters.size();j++)
1071  {
1072  if(parameters.at(j)=="" || parameters.at(j).find_first_not_of(" ")==string::npos
1073  || parameters.at(j).find_first_not_of("\r\n")==string::npos)
1074  continue;
1075  fprintf(stderr,parameters.at(j).c_str());
1076  fflush(stderr);
1077  FormData datf;
1078  string parm = parameters.at(j);
1079  retval+= parm + "\nparm";
1080  size_t dis = parm.find("Content-Disposition: ");
1081  if(dis==string::npos)
1082  dis = parm.find("Content-disposition: ");
1083  string cont_disp,cont_type;
1084  if(dis!=string::npos)
1085  {
1086  size_t dist = parm.find("Content-Type: ");
1087  if(dist==string::npos)
1088  dist = parm.find("Content-type: ");
1089  size_t dise;
1090  if(dist==string::npos)
1091  {
1092  dist = parm.find("\r\n\r\n");
1093  dise = dist + 4;
1094  }
1095  else
1096  {
1097  cont_type = parm.substr(dist+14,parm.find("\r\n\r\n")-(dist+14));
1098  dise = parm.find("\r\n\r\n") + 4;
1099  }
1100  cont_disp = parm.substr(dis+21,dist-(dis+21));
1101  StringUtil::replaceFirst(cont_disp,"\r\n","");
1102  retval+= "\ncdisp = " + cont_disp;
1103  retval+= "\ndise = " + CastUtil::lexical_cast<string>((int)dise);
1104  parm = parm.substr(dise);
1105  parm = parm.substr(0,parm.length()-1);
1106  }
1107  strVec parmdef;
1108  StringUtil::split(parmdef, cont_disp, (";"));
1109  string key;
1110  for(unsigned k=0;k<parmdef.size();k++)
1111  {
1112  if(parmdef.at(k)!="" && parmdef.at(k).find("=")!=string::npos)
1113  {
1114  size_t stpd = parmdef.at(k).find_first_not_of(" ");
1115  size_t enpd = parmdef.at(k).find_last_not_of(" ");
1116  retval+= "\nparmdef = " + parmdef.at(k) ;
1117  retval+= "\nst en = " + CastUtil::lexical_cast<string>((int)stpd) + " " + CastUtil::lexical_cast<string>((int)enpd);
1118  string propert = parmdef.at(k).substr(stpd,enpd-stpd+1);
1119  strVec proplr;
1120  StringUtil::split(proplr, propert, ("="));
1121  if(proplr.size()==2)
1122  {
1123  if(proplr.at(0)=="name" && proplr.at(1)!="\"\"")
1124  {
1125  key = proplr.at(1);
1126  key = key.substr(key.find_first_not_of("\""),key.find_last_not_of("\"")-key.find_first_not_of("\"")+1);
1127  datf.type = cont_type;
1128  datf.value = parm;
1129  }
1130  else if(proplr.at(0)=="filename" && proplr.at(1)!="\"\"")
1131  {
1132  string fna = proplr.at(1);
1133  fna = fna.substr(fna.find_first_not_of("\""),fna.find_last_not_of("\"")-fna.find_first_not_of("\"")+1);
1134  datf.fileName = fna;
1135  }
1136  }
1137  }
1138  }
1139  if(key!="")
1140  {
1141  this->setRequestParamF(key,datf);
1142  retval+= (key + " " + datf.type + " "+ datf.fileName+" "+ datf.value);
1143  }
1144  }
1145  }
1146 
1147  }
1148  else if(key=="Content-Length" || key=="Content-length")
1149  this->setContent_len(value);
1150  else if(key=="Referer")
1151  this->setReferer(value);
1152  else if(key=="Pragma")
1153  this->setPragma(value);
1154  else if(key=="Method")
1155  this->setMethod(value);
1156  else if(key=="HttpVersion")
1157  this->setHttpVersion(value);
1158  else if(key=="GetArguments")
1159  {
1160  strVec params;
1161  StringUtil::split(params, value, ("&"));
1162  for(unsigned j=0;j<params.size();j++)
1163  {
1164  strVec param;
1165  StringUtil::split(param, params.at(j), ("="));
1166  if(param.size()==2)
1167  {
1168  string att = param.at(0);
1169  StringUtil::replaceFirst(att,"\r","");
1170  StringUtil::replaceFirst(att,"\t","");
1171  StringUtil::replaceFirst(att," ","");
1172  this->setQueryParam(att,param.at(1));
1173  }
1174  }
1175  }
1176  /*else if(key=="PostArguments")
1177  {
1178  strVec params;
1179  StringUtil::split(params, value, ("&"));
1180  for(unsigned j=0;j<params.size();j++)
1181  {
1182  strVec param;
1183  StringUtil::split(param, params.at(j), ("="));
1184  if(param.size()==2)
1185  {
1186  string att = param.at(0);
1187  StringUtil::replaceFirst(att,"\r","");
1188  StringUtil::replaceFirst(att,"\t","");
1189  StringUtil::replaceFirst(att," ","");
1190  this->setRequestParam(att,param.at(1));
1191  }
1192  }
1193  }*/
1194  else if(key.find("URL")!=string::npos)
1195  {
1196  StringUtil::replaceFirst(key,"URL","");
1197  strVec memp;
1198  this->setActUrl(value);
1199  StringUtil::split(memp, value, ("/"));
1200  int fs = value.find_first_of("/");
1201  int es = value.find_last_of("/");
1202  if(fs==es)
1203  {
1204  this->setCntxt_root(key+"default");
1205  this->setCntxt_name("default");
1206  this->setFile(value.substr(es+1));
1207  this->setUrl(key+"default/"+value);
1208  }
1209  else
1210  {
1211  int ss = value.substr(fs+1).find("/");
1212  if(ss>fs)
1213  {
1214  this->setCntxt_name(value.substr(fs+1,ss-fs));
1215  this->setCntxt_root(key+this->getCntxt_name());
1216  this->setFile(value.substr(es+1));
1217  this->setUrl(key+value);
1218  }
1219  }
1220  }
1221  else
1222  xtraHeaders[key] = value;
1223  return retval;
1224 }
1225 
1226 string HttpRequest::toString()
1227 {
1228  string ret;
1229  ret = "\nHost: "+this->getHost();
1230  ret += "\nAccept: "+this->getAccept();
1231  ret += "\nAccept Chars: "+this->getAccept_chars();
1232  ret += "\nAccept Encoding: "+this->getAccept_encod();
1233  ret += "\nAccept Language: "+this->getAccept_lang();
1234  ret += "\nCache Ctrl: "+this->getCache_ctrl();
1235  ret += "\nConnection: "+this->getConnection();
1236  ret += "\nContent: "+this->getContent();
1237  ret += "\nContent Length: "+this->getContent_len();
1238  ret += "\nContent Type: "+this->getContent_type();
1239  ret += "\nHttp Version: "+this->getHttpVersion();
1240  ret += "\nMethod: "+this->getMethod();
1241  ret += "\nUser agent: "+this->getUser_agent();
1242  ret += "\nUrl: "+this->getUrl();
1243  ret += "\nFile: "+this->getFile();
1244  ret += "\nContext Name: "+this->getCntxt_name();
1245  ret += "\nContext Root: "+this->getCntxt_root();
1246  ret += "\nDefault Locale: "+this->getDefaultLocale();
1247  ret += "\nContent Boundary: "+this->getContent_boundary();
1248  string vals;
1249  if(this->requestParams.size()>0)
1250  {
1251  RMap::iterator iter;
1252  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
1253  {
1254  vals+= ("\nKey: "+iter->first + " Value: "+iter->second);
1255  }
1256  }
1257  if(this->requestParamsF.size()>0)
1258  {
1259  FMap::iterator iter;
1260  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
1261  {
1262  FormData dat = iter->second;
1263  vals+= ("\nKey: "+iter->first + " Type: "+dat.type + " FileName: "+dat.fileName);
1264  vals+= ("\nValue: "+dat.value);
1265  }
1266  }
1267  ret += "\nRequest Parameters "+vals;//CastUtil::lexical_cast<string>(this->getRequestParams().size());
1268  return ret;
1269 }
1270 
1271 
1272 HttpRequest::~HttpRequest()
1273 {
1274 // /delete this;
1275 }
1276 string HttpRequest::getMethod() const
1277 {
1278  return method;
1279 }
1280 
1281 void HttpRequest::setMethod(string method)
1282 {
1283  this->method = method;
1284 }
1285 
1286 string HttpRequest::getUser_agent() const
1287 {
1288  return user_agent;
1289 }
1290 
1291 void HttpRequest::setUser_agent(string user_agent)
1292 {
1293  this->user_agent = user_agent;
1294 }
1295 
1296 string HttpRequest::getAccept() const
1297 {
1298  return accept;
1299 }
1300 
1301 void HttpRequest::setAccept(string accept)
1302 {
1303  this->accept = accept;
1304 }
1305 
1306 string HttpRequest::getAccept_lang() const
1307 {
1308  return accept_lang;
1309 }
1310 
1311 void HttpRequest::setAccept_lang(string accept_lang)
1312 {
1313  this->accept_lang = accept_lang;
1314 }
1315 
1316 string HttpRequest::getAccept_encod() const
1317 {
1318  return accept_encod;
1319 }
1320 
1321 void HttpRequest::setAccept_encod(string accept_encod)
1322 {
1323  this->accept_encod = accept_encod;
1324 }
1325 
1326 string HttpRequest::getAccept_chars() const
1327 {
1328  return accept_chars;
1329 }
1330 
1331 void HttpRequest::setAccept_chars(string accept_chars)
1332 {
1333  this->accept_chars = accept_chars;
1334 }
1335 
1336 string HttpRequest::getKeep_alive() const
1337 {
1338  return keep_alive;
1339 }
1340 
1341 void HttpRequest::setKeep_alive(string keep_alive)
1342 {
1343  this->keep_alive = keep_alive;
1344 }
1345 
1346 string HttpRequest::getConnection() const
1347 {
1348  return connection;
1349 }
1350 
1351 void HttpRequest::setConnection(string connection)
1352 {
1353  this->connection = connection;
1354 }
1355 
1356 string HttpRequest::getCache_ctrl() const
1357 {
1358  return cache_ctrl;
1359 }
1360 
1361 void HttpRequest::setCache_ctrl(string cache_ctrl)
1362 {
1363  this->cache_ctrl = cache_ctrl;
1364 }
1365 
1366 Map HttpRequest::getAttributes() const
1367 {
1368  return attributes;
1369 }
1370 
1371 void HttpRequest::setAttributes(Map attributes)
1372 {
1373  this->attributes = attributes;
1374 }
1375 
1376 string HttpRequest::getAttribute(string key)
1377 {
1378  return attributes[key];
1379 }
1380 
1381 void HttpRequest::setAttribute(string key,string value)
1382 {
1383  this->attributes[key] = value;
1384 }
1385 
1386 HttpSession* HttpRequest::getSession()
1387 {
1388  return &(this->session);
1389 }
1390 void HttpRequest::setSession(HttpSession session)
1391 {
1392  this->session = session;
1393 }
1394 
1395 string HttpRequest::getHost() const
1396 {
1397  return this->host;
1398 }
1399 void HttpRequest::setHost(string host)
1400 {
1401  this->host = host;
1402 }
1403 
1404 void HttpRequest::setUrl(string url)
1405 {
1406  StringUtil::replaceFirst(url,"//","/");
1407  this->url = url;
1408 }
1409 
1410 string HttpRequest::getUrl()
1411 {
1412  return this->url;
1413 }
1414 
1415 void HttpRequest::setHttpVersion(string httpVersion)
1416 {
1417  this->httpVersion = httpVersion;
1418 }
1419 string HttpRequest::getHttpVersion()
1420 {
1421  return this->httpVersion;
1422 }
1423 
1424 string HttpRequest::getContent_type() const
1425 {
1426  return content_type;
1427 }
1428 
1429 void HttpRequest::setContent_type(string content_type)
1430 {
1431  this->content_type = content_type;
1432 }
1433 
1434 string HttpRequest::getContent_boundary() const
1435 {
1436  return content_boundary;
1437 }
1438 
1439 void HttpRequest::setContent_boundary(string content_boundary)
1440 {
1441  this->content_boundary = content_boundary;
1442 }
1443 
1444 string HttpRequest::getContent_len() const
1445 {
1446  return content_len;
1447 }
1448 
1449 void HttpRequest::setContent_len(string content_len)
1450 {
1451  this->content_len = content_len;
1452 }
1453 
1454 string HttpRequest::getContent() const
1455 {
1456  return content;
1457 }
1458 
1459 void HttpRequest::setContent(string content)
1460 {
1461  this->content = content;
1462 }
1463 
1464 string HttpRequest::getReferer() const
1465 {
1466  return referer;
1467 }
1468 
1469 void HttpRequest::setReferer(string referer)
1470 {
1471  this->referer = referer;
1472 }
1473 
1474 string HttpRequest::getPragma() const
1475 {
1476  return pragma;
1477 }
1478 
1479 void HttpRequest::setPragma(string pragma)
1480 {
1481  this->pragma = pragma;
1482 }
1483 
1484 RMap HttpRequest::getRequestParams() const
1485 {
1486  return requestParams;
1487 }
1488 
1489 void HttpRequest::setRequestParams(RMap requestParams)
1490 {
1491  this->requestParams = requestParams;
1492 }
1493 
1494 string HttpRequest::getRequestParam(string key)
1495 {
1496  if(this->requestParams.find(key)!=this->requestParams.end())
1497  return this->requestParams[key];
1498  else if(this->requestParamsF.find(key)!=this->requestParamsF.end())
1499  return this->requestParamsF[key].value;
1500  else
1501  return "";
1502 }
1503 
1504 string HttpRequest::getRequestParamType(string key)
1505 {
1506  if(this->requestParamsF.find(key)!=this->requestParamsF.end())
1507  return this->requestParamsF[key].type;
1508  else
1509  return "";
1510 }
1511 
1512 void HttpRequest::setRequestParam(string key,string value)
1513 {
1514  this->requestParams[key] = value;
1515 }
1516 
1517 void HttpRequest::setRequestParamF(string key,FormData value)
1518 {
1519  this->requestParamsF[key] = value;
1520 }
1521 
1522 string HttpRequest::getCntxt_root() const
1523 {
1524  return cntxt_root;
1525 }
1526 
1527 void HttpRequest::setCntxt_root(string cntxt_root)
1528 {
1529  this->cntxt_root = cntxt_root;
1530 }
1531 string HttpRequest::getDefaultLocale()
1532 {
1533  if(this->localeInfo.size()>0)
1534  return this->localeInfo.at(0);
1535  else
1536  return "en";
1537 }
1538 string HttpRequest::getCntxt_name() const
1539 {
1540  return cntxt_name;
1541 }
1542 
1543 void HttpRequest::setCntxt_name(string cntxt_name)
1544 {
1545  this->cntxt_name = cntxt_name;
1546 }
1547 
1548 string HttpRequest::getFile() const
1549 {
1550  return file;
1551 }
1552 
1553 void HttpRequest::setFile(string file)
1554 {
1555  //logger << "file is " << this->file << endl;
1556  if(this->file!="" && this->url.find(this->file)!=string::npos
1557  && this->url.find("/")!=string::npos)
1558  {
1559  //int fst = this->url.find_last_of(this->file) - this->file.length() + 1;
1560  this->url = this->url.substr(0, this->url.find_last_of("/")+1) + file;
1561  }
1562  else
1563  {
1564  this->url += "/" + file;
1565  }
1566  this->file = file;
1567 }
1568 
1569 string HttpRequest::getActUrl() const
1570 {
1571  return actUrl;
1572 }
1573 
1574 void HttpRequest::setActUrl(string actUrl)
1575 {
1576  this->actUrl = actUrl;
1577 }
1578 
1579 map<string,string> HttpRequest::getAuthinfo() const
1580 {
1581  return authinfo;
1582 }
1583 
1584 void HttpRequest::setAuthinfo(map<string,string> authinfo)
1585 {
1586  this->authinfo = authinfo;
1587 }
1588 
1589 string HttpRequest::toPHPVariablesString(string def)
1590 {
1591  string ret;
1592  ret = "<?php\n$_SERVER['HTTP_HOST'] = '"+this->getHost();
1593  ret += "';\n$_SERVER['HTTP_ACCEPT'] = '"+this->getAccept();
1594  ret += "';\n$_SERVER['HTTP_ACCEPT_CHARSET'] = '"+this->getAccept_chars();
1595  ret += "';\n$_SERVER['HTTP_ACCEPT_ENCODING'] = '"+this->getAccept_encod();
1596  ret += "';\n$_SERVER['HTTP_ACCEPT_LANGUAGE'] = '"+this->getAccept_lang();
1597  ret += "';\n$_SERVER_EX['CACHE_CNTRL'] = '"+this->getCache_ctrl();
1598  ret += "';\n$_SERVER['HTTP_CONNECTION'] = '"+this->getConnection();
1599  ret += "';\n$_SERVER_EX['HTTP_CONTENT'] = '"+this->getContent();
1600  ret += "';\n$_SERVER_EX['HTTP_CNTENT_LENGTH'] = '"+this->getContent_len();
1601  ret += "';\n$_SERVER_EX['HTTP_CNTENT_TYPE'] = '"+this->getContent_type();
1602  ret += "';\n$_SERVER_EX['HTTP_VERSION'] = '"+this->getHttpVersion();
1603  ret += "';\n$_SERVER['REQUEST_METHOD'] = '"+this->getMethod();
1604  ret += "';\n$_SERVER['HTTP_USER_AGENT'] = '"+this->getUser_agent();
1605  string requri = this->getActUrl();
1606  StringUtil::replaceFirst(requri, ("/"+this->getCntxt_name()), "");
1607  if(requri=="")
1608  requri = "/";
1609  ret += "';\n$_SERVER['REQUEST_URI'] = '"+requri;
1610  ret += "';\n$_SERVER_EX['HTTP_REQ_FILE'] = '"+this->getFile();
1611  ret += "';\n$_SERVER_EX['CONTEXT_NAME'] = '"+this->getCntxt_name();
1612  if(def=="")
1613  ret += "';\n$_SERVER['DOCUMENT_ROOT'] = '"+this->getCntxt_root();
1614  else
1615  ret += "';\n$_SERVER['DOCUMENT_ROOT'] = '/"+this->getCntxt_name();
1616  ret += "';\n$_SERVER_EX['DEFAULT_LOCALE'] = '"+this->getDefaultLocale();
1617  ret += "';\n$_SERVER['HTTP_HOST'] = '"+this->getContent_boundary() + "';\n";
1618  if(this->queryParams.size()>0)
1619  {
1620  RMap::iterator iter;
1621  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
1622  {
1623  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1624  {
1625  ret += "\nif(!isset($_GET['"+iter->first.substr(0, iter->first.find("["))+"']))\n{\n$_GET['"+iter->first.substr(0, iter->first.find("["))+"']=array();\n}\n";
1626  ret += ("\n$_GET['"+iter->first.substr(0, iter->first.find("["))+"']" +
1627  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "';");
1628  }
1629  else
1630  ret += "\n$_GET['"+iter->first+"'] = '"+ iter->second + "';";
1631  }
1632  }
1633  if(this->requestParams.size()>0)
1634  {
1635  RMap::iterator iter;
1636  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
1637  {
1638  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1639  {
1640  ret += "\nif(!isset($_POST['"+iter->first.substr(0, iter->first.find("["))+"']))\n{\n$_POST['"+iter->first.substr(0, iter->first.find("["))+"']=array();\n}\n";
1641  ret += ("\n$_POST['"+iter->first.substr(0, iter->first.find("["))+"']" +
1642  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "';");
1643  }
1644  else
1645  ret += "\n$_POST['"+iter->first+"'] = '"+iter->second + "';";
1646  }
1647  }
1648  if(this->requestParamsF.size()>0)
1649  {
1650  FMap::iterator iter;
1651  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
1652  {
1653  FormData dat = iter->second;
1654  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1655  {
1656  ret += "\nif(!isset($_FILES['"+iter->first.substr(0, iter->first.find("["))+"']))\n{\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']=array()\n}\n";
1657  ret += "\nif(!isset($_FILES['"+iter->first.substr(0, iter->first.find("["))+"']"+iter->first.substr(iter->first.find("["))+"))\n"
1658  + "{\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']"+iter->first.substr(iter->first.find("["))+"=array();\n}\n";
1659  ret += "\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1660  iter->first.substr(iter->first.find("[")) + "['name'] = '"+ dat.fileName + "';";
1661  ret += "\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1662  iter->first.substr(iter->first.find("[")) + "['type'] = '"+ dat.type + "';";
1663  ret += "\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1664  iter->first.substr(iter->first.find("[")) + "['size'] = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
1665  ret += "\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1666  iter->first.substr(iter->first.find("[")) + "['tmp_name'] = '"+ dat.tmpFileName + "';";
1667  ret += "\n$_FILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1668  iter->first.substr(iter->first.find("[")) + "['error'] = 0;";
1669  }
1670  else
1671  {
1672  ret += "\nif(!isset($_FILES['"+iter->first+"']))\n{\n$_FILES['"+iter->first+"']=array();\n}\n";
1673  ret += "\n$_FILES['"+iter->first+"']['name'] = '"+ dat.fileName + "';";
1674  ret += "\n$_FILES['"+iter->first+"']['type'] = '"+ dat.type + "';";
1675  ret += "\n$_FILES['"+iter->first+"']['size'] = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
1676  ret += "\n$_FILES['"+iter->first+"']['tmp_name'] = '"+ dat.tmpFileName + "';";
1677  ret += "\n$_FILES['"+iter->first+"']['error'] = 0;";
1678  }
1679  }
1680  }
1681  if(def=="")
1682  ret += "\ninclude_once('"+this->getUrl()+"');";
1683  else
1684  ret += "\ninclude_once('"+def+"');";
1685  ret += "?>";
1686  return ret;
1687 }
1688 
1689 string HttpRequest::toPerlVariablesString()
1690 {
1691  string ret;
1692  ret += "$_SERVER = {};";
1693  ret += "\n$_SERVER_EX = {};";
1694  ret += "\n$_GET = {};";
1695  ret += "\n$_POST = {};";
1696  ret += "\n$_FILES = {};";
1697  ret += "\n$_SERVER{'HTTP_HOST'} = '"+this->getHost();
1698  ret += "';\n$_SERVER{'HTTP_ACCEPT'} = '"+this->getAccept();
1699  ret += "';\n$_SERVER{'HTTP_ACCEPT_CHARSET'} = '"+this->getAccept_chars();
1700  ret += "';\n$_SERVER{'HTTP_ACCEPT_ENCODING'} = '"+this->getAccept_encod();
1701  ret += "';\n$_SERVER{'HTTP_ACCEPT_LANGUAGE'} = '"+this->getAccept_lang();
1702  ret += "';\n$_SERVER_EX{'CACHE_CNTRL'} = '"+this->getCache_ctrl();
1703  ret += "';\n$_SERVER{'HTTP_CONNECTION'} = '"+this->getConnection();
1704  ret += "';\n$_SERVER_EX{'HTTP_CONTENT'} = '"+this->getContent();
1705  ret += "';\n$_SERVER_EX{'HTTP_CNTENT_LENGTH'} = '"+this->getContent_len();
1706  ret += "';\n$_SERVER_EX{'HTTP_CNTENT_TYPE'} = '"+this->getContent_type();
1707  ret += "';\n$_SERVER_EX{'HTTP_VERSION'} = '"+this->getHttpVersion();
1708  ret += "';\n$_SERVER{'REQUEST_METHOD'} = '"+this->getMethod();
1709  ret += "';\n$_SERVER{'HTTP_USER_AGENT'} = '"+this->getUser_agent();
1710  ret += "';\n$_SERVER{'REQUEST_URI'} = '"+this->getUrl();
1711  ret += "';\n$_SERVER_EX{'HTTP_REQ_FILE'} = '"+this->getFile();
1712  ret += "';\n$_SERVER_EX{'CONTEXT_NAME'} = '"+this->getCntxt_name();
1713  ret += "';\n$_SERVER{'DOCUMENT_ROOT'} = '"+this->getCntxt_root();
1714  ret += "';\n$_SERVER_EX{'DEFAULT_LOCALE'} = '"+this->getDefaultLocale();
1715  ret += "';\n$_SERVER{'HTTP_HOST'} = '"+this->getContent_boundary() + "';\n";
1716  if(this->queryParams.size()>0)
1717  {
1718  RMap::iterator iter;
1719  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
1720  {
1721  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1722  {
1723  string key = iter->first;
1724  StringUtil::replaceFirst(key,"[","{");
1725  StringUtil::replaceFirst(key,"]","}");
1726  ret += "\nif(!exists $_GET{'"+key.substr(0, key.find("{"))+"'})\n{\n$_GET{'"+key.substr(0, key.find("{"))+"'}={}\n}\n";
1727  ret += ("\n$_GET{'"+key.substr(0, key.find("{"))+"'}" +
1728  key.substr(key.find("{")) + " = '"+iter->second + "';");
1729  }
1730  else
1731  ret += "\n$_GET{'"+iter->first+"'} = '"+ iter->second + "';";
1732  }
1733  }
1734  if(this->requestParams.size()>0)
1735  {
1736  RMap::iterator iter;
1737  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
1738  {
1739  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1740  {
1741  string key = iter->first;
1742  StringUtil::replaceFirst(key,"[","{");
1743  StringUtil::replaceFirst(key,"]","}");
1744  ret += "\nif(!exists $_POST{'"+key.substr(0, key.find("{"))+"'})\n{\n$_POST{'"+key.substr(0, key.find("{"))+"'}={}\n}\n";
1745  ret += ("\n$_POST{'"+key.substr(0, key.find("{"))+"'}" +
1746  key.substr(key.find("{")) + " = '"+iter->second + "';");
1747  }
1748  else
1749  ret += "\n$_POST{'"+iter->first+"'} = '"+iter->second + "';";
1750  }
1751  }
1752  if(this->requestParamsF.size()>0)
1753  {
1754  FMap::iterator iter;
1755  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
1756  {
1757  FormData dat = iter->second;
1758  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1759  {
1760  string key = iter->first;
1761  StringUtil::replaceFirst(key,"[","{");
1762  StringUtil::replaceFirst(key,"]","}");
1763  ret += "\nif(!exists $_FILES{'"+key.substr(0, key.find("{"))+"'})\n{\n$_FILES{'"+key.substr(0, key.find("{"))+"'}={}\n}\n";
1764  ret += "\nif(!exists $_FILES{'"+key.substr(0, key.find("{"))+"'}{'"+key.substr(key.find("{"))+"'})\n"
1765  + "{\n$_FILES{'"+key.substr(0, key.find("{"))+"'}{'"+key.substr(key.find("{"))+"'}={}\n}\n";
1766  ret += "\n$_FILES{'"+key.substr(0, key.find("{"))+"'}" +
1767  key.substr(key.find("{")) + "{'name'} = '"+ dat.fileName + "';";
1768  ret += "\n$_FILES{'"+key.substr(0, key.find("{"))+"'}" +
1769  key.substr(key.find("{")) + "{'type'} = '"+ dat.type + "';";
1770  ret += "\n$_FILES{'"+key.substr(0, key.find("{"))+"'}" +
1771  key.substr(key.find("{")) + "{'size'} = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
1772  ret += "\n$_FILES{'"+key.substr(0, key.find("{"))+"'}" +
1773  key.substr(key.find("{")) + "{'tmp_name'} = '"+ dat.tmpFileName + "';";
1774  ret += "\n$_FILES{'"+key.substr(0, key.find("{"))+"'}" +
1775  key.substr(key.find("{")) + "{'error'} = 0;";
1776  }
1777  else
1778  {
1779  ret += "\nif(!exists $_FILES{'"+iter->first+"'})\n{\n$_FILES{'"+iter->first+"'}={}\n}\n";
1780  ret += "\n$_FILES{'"+iter->first+"'}{'name'} = '"+ dat.fileName + "';";
1781  ret += "\n$_FILES{'"+iter->first+"'}{'type'} = '"+ dat.type + "';";
1782  ret += "\n$_FILES{'"+iter->first+"'}{'size'} = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
1783  ret += "\n$_FILES{'"+iter->first+"'}{'tmp_name'} = '"+ dat.tmpFileName + "';";
1784  ret += "\n$_FILES{'"+iter->first+"'}{'error'} = 0;";
1785  }
1786  }
1787  }
1788  ret += "\n\n";
1789  return ret;
1790 }
1791 
1792 string HttpRequest::toRubyVariablesString()
1793 {
1794  string ret;
1795  ret += "SERVER = {}";
1796  ret += "\nSERVER_EX = {}";
1797  ret += "\nGET = {}";
1798  ret += "\nPOST = {}";
1799  ret += "\nFILES = {}";
1800  ret += "\nSERVER['HTTP_HOST'] = '"+this->getHost();
1801  ret += "'\nSERVER['HTTP_ACCEPT'] = '"+this->getAccept();
1802  ret += "'\nSERVER['HTTP_ACCEPT_CHARSET'] = '"+this->getAccept_chars();
1803  ret += "'\nSERVER['HTTP_ACCEPT_ENCODING'] = '"+this->getAccept_encod();
1804  ret += "'\nSERVER['HTTP_ACCEPT_LANGUAGE'] = '"+this->getAccept_lang();
1805  ret += "'\nSERVER_EX['CACHE_CNTRL'] = '"+this->getCache_ctrl();
1806  ret += "'\nSERVER['HTTP_CONNECTION'] = '"+this->getConnection();
1807  ret += "'\nSERVER_EX['HTTP_CONTENT'] = '"+this->getContent();
1808  ret += "'\nSERVER_EX['HTTP_CNTENT_LENGTH'] = '"+this->getContent_len();
1809  ret += "'\nSERVER_EX['HTTP_CNTENT_TYPE'] = '"+this->getContent_type();
1810  ret += "'\nSERVER_EX['HTTP_VERSION'] = '"+this->getHttpVersion();
1811  ret += "'\nSERVER['REQUEST_METHOD'] = '"+this->getMethod();
1812  ret += "'\nSERVER['HTTP_USER_AGENT'] = '"+this->getUser_agent();
1813  ret += "'\nSERVER['REQUEST_URI'] = '"+this->getUrl();
1814  ret += "'\nSERVER_EX['HTTP_REQ_FILE'] = '"+this->getFile();
1815  ret += "'\nSERVER_EX['CONTEXT_NAME'] = '"+this->getCntxt_name();
1816  ret += "'\nSERVER['DOCUMENT_ROOT'] = '"+this->getCntxt_root();
1817  ret += "'\nSERVER_EX['DEFAULT_LOCALE'] = '"+this->getDefaultLocale();
1818  ret += "'\nSERVER['HTTP_HOST'] = '"+this->getContent_boundary() + "'\n";
1819  if(this->queryParams.size()>0)
1820  {
1821  RMap::iterator iter;
1822  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
1823  {
1824  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1825  {
1826  string key = iter->first.substr(0, iter->first.find("["));
1827  ret += "\nif(!GET.has_key?('"+key+"'))\nGET['"+key+"']={}\nend";
1828  ret += ("\nGET['"+iter->first.substr(0, iter->first.find("["))+"']" +
1829  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
1830  }
1831  else
1832  ret += "\nGET['"+iter->first+"'] = '"+ iter->second + "'";
1833  }
1834  }
1835  if(this->requestParams.size()>0)
1836  {
1837  RMap::iterator iter;
1838  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
1839  {
1840  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1841  {
1842  string key = iter->first.substr(0, iter->first.find("["));
1843  ret += "\nif(!POST.has_key?('"+key+"'))\nPOST['"+key+"']={}\nend";
1844  ret += ("\nPOST['"+iter->first.substr(0, iter->first.find("["))+"']" +
1845  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
1846  }
1847  else
1848  ret += "\nPOST['"+iter->first+"'] = '"+iter->second + "'";
1849  }
1850  }
1851  if(this->requestParamsF.size()>0)
1852  {
1853  FMap::iterator iter;
1854  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
1855  {
1856  FormData dat = iter->second;
1857  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1858  {
1859  string key = iter->first.substr(iter->first.find("["));
1860  StringUtil::replaceFirst(key,"[","");
1861  StringUtil::replaceFirst(key,"]","");
1862  ret += "\nif(!FILES.has_key?('"+iter->first.substr(0, iter->first.find("["))
1863  +"'))\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']={}\nend";
1864  ret += "\nif(!FILES['"+iter->first.substr(0, iter->first.find("["))+"'].has_key?('"+key+"'))"
1865  + "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']['"+key+"']={}\nend";
1866  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1867  iter->first.substr(iter->first.find("[")) + "['name'] = '"+ dat.fileName + "'";
1868  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1869  iter->first.substr(iter->first.find("[")) + "['type'] = '"+ dat.type + "'";
1870  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1871  iter->first.substr(iter->first.find("[")) + "['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
1872  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1873  iter->first.substr(iter->first.find("[")) + "['tmp_name'] = '"+ dat.tmpFileName + "'";
1874  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1875  iter->first.substr(iter->first.find("[")) + "['error'] = 0;";
1876  }
1877  else
1878  {
1879  ret += "\nFILES['"+iter->first+"'] = {}";
1880  ret += "\nFILES['"+iter->first+"']['name'] = '"+ dat.fileName + "'";
1881  ret += "\nFILES['"+iter->first+"']['type'] = '"+ dat.type + "'";
1882  ret += "\nFILES['"+iter->first+"']['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
1883  ret += "\nFILES['"+iter->first+"']['tmp_name'] = '"+ dat.tmpFileName + "'";
1884  ret += "\nFILES['"+iter->first+"']['error'] = 0";
1885  }
1886  }
1887  }
1888  ret += "\nrequire ('"+this->getUrl()+"')\n";
1889  return ret;
1890 }
1891 
1892 string HttpRequest::toPythonVariablesString()
1893 {
1894  string ret;
1895  ret += "SERVER = {}";
1896  ret += "\nSERVER_EX = {}";
1897  ret += "\nGET = {}";
1898  ret += "\nPOST = {}";
1899  ret += "\nFILES = {}";
1900  ret += "\nSERVER['HTTP_HOST'] = '"+this->getHost();
1901  ret += "'\nSERVER['HTTP_ACCEPT'] = '"+this->getAccept();
1902  ret += "'\nSERVER['HTTP_ACCEPT_CHARSET'] = '"+this->getAccept_chars();
1903  ret += "'\nSERVER['HTTP_ACCEPT_ENCODING'] = '"+this->getAccept_encod();
1904  ret += "'\nSERVER['HTTP_ACCEPT_LANGUAGE'] = '"+this->getAccept_lang();
1905  ret += "'\nSERVER_EX['CACHE_CNTRL'] = '"+this->getCache_ctrl();
1906  ret += "'\nSERVER['HTTP_CONNECTION'] = '"+this->getConnection();
1907  ret += "'\nSERVER_EX['HTTP_CONTENT'] = '"+this->getContent();
1908  ret += "'\nSERVER_EX['HTTP_CNTENT_LENGTH'] = '"+this->getContent_len();
1909  ret += "'\nSERVER_EX['HTTP_CNTENT_TYPE'] = '"+this->getContent_type();
1910  ret += "'\nSERVER_EX['HTTP_VERSION'] = '"+this->getHttpVersion();
1911  ret += "'\nSERVER['REQUEST_METHOD'] = '"+this->getMethod();
1912  ret += "'\nSERVER['HTTP_USER_AGENT'] = '"+this->getUser_agent();
1913  ret += "'\nSERVER['REQUEST_URI'] = '"+this->getUrl();
1914  ret += "'\nSERVER_EX['HTTP_REQ_FILE'] = '"+this->getFile();
1915  ret += "'\nSERVER_EX['CONTEXT_NAME'] = '"+this->getCntxt_name();
1916  ret += "'\nSERVER['DOCUMENT_ROOT'] = '"+this->getCntxt_root();
1917  ret += "'\nSERVER_EX['DEFAULT_LOCALE'] = '"+this->getDefaultLocale();
1918  ret += "'\nSERVER['HTTP_HOST'] = '"+this->getContent_boundary() + "'\n";
1919  if(this->queryParams.size()>0)
1920  {
1921  RMap::iterator iter;
1922  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
1923  {
1924  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1925  {
1926  string key = iter->first.substr(0, iter->first.find("["));
1927  ret += "\nif '"+key+"' not in GET:\n\tGET['"+key+"']={}\n";
1928  ret += ("\nGET['"+iter->first.substr(0, iter->first.find("["))+"']" +
1929  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
1930  }
1931  else
1932  ret += "\nGET['"+iter->first+"'] = '"+ iter->second + "'";
1933  }
1934  }
1935  if(this->requestParams.size()>0)
1936  {
1937  RMap::iterator iter;
1938  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
1939  {
1940  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1941  {
1942  string key = iter->first.substr(0, iter->first.find("["));
1943  ret += "\nif '"+key+"' not in POST:\n\tPOST['"+key+"']={}\n";
1944  ret += ("\nPOST['"+iter->first.substr(0, iter->first.find("["))+"']" +
1945  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
1946  }
1947  else
1948  ret += "\nPOST['"+iter->first+"'] = '"+iter->second + "'";
1949  }
1950  }
1951  if(this->requestParamsF.size()>0)
1952  {
1953  FMap::iterator iter;
1954  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
1955  {
1956  FormData dat = iter->second;
1957  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
1958  {
1959  string key = iter->first.substr(iter->first.find("["));
1960  StringUtil::replaceFirst(key,"[","");
1961  StringUtil::replaceFirst(key,"]","");
1962  ret += "\nif '"+iter->first.substr(0, iter->first.find("["))
1963  +"' not in FILES:\n\tFILES['"+iter->first.substr(0, iter->first.find("["))+"']={}\n";
1964  ret += "\nif '"+key+"' not in FILES['"+iter->first.substr(0, iter->first.find("["))+"']:\n"
1965  + "\n\tFILES['"+iter->first.substr(0, iter->first.find("["))+"']['"+key+"']={}\n";
1966  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1967  iter->first.substr(iter->first.find("[")) + "['name'] = '"+ dat.fileName + "'";
1968  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1969  iter->first.substr(iter->first.find("[")) + "['type'] = '"+ dat.type + "'";
1970  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1971  iter->first.substr(iter->first.find("[")) + "['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
1972  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1973  iter->first.substr(iter->first.find("[")) + "['tmp_name'] = '"+ dat.tmpFileName + "'";
1974  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
1975  iter->first.substr(iter->first.find("[")) + "['error'] = 0;";
1976  }
1977  else
1978  {
1979  ret += "\nFILES['"+iter->first+"'] = {}";
1980  ret += "\nFILES['"+iter->first+"']['name'] = '"+ dat.fileName + "'";
1981  ret += "\nFILES['"+iter->first+"']['type'] = '"+ dat.type + "'";
1982  ret += "\nFILES['"+iter->first+"']['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
1983  ret += "\nFILES['"+iter->first+"']['tmp_name'] = '"+ dat.tmpFileName + "'";
1984  ret += "\nFILES['"+iter->first+"']['error'] = 0";
1985  }
1986  }
1987  }
1988  ret += "\n\n";
1989  return ret;
1990 }
1991 
1992 string HttpRequest::toLuaVariablesString()
1993 {
1994  string ret;
1995  ret += "SERVER = {}";
1996  ret += "\nSERVER_EX = {}";
1997  ret += "\nGET = {}";
1998  ret += "\nPOST = {}";
1999  ret += "\nFILES = {}";
2000  ret += "\nSERVER['HTTP_HOST'] = '"+this->getHost();
2001  ret += "'\nSERVER['HTTP_ACCEPT'] = '"+this->getAccept();
2002  ret += "'\nSERVER['HTTP_ACCEPT_CHARSET'] = '"+this->getAccept_chars();
2003  ret += "'\nSERVER['HTTP_ACCEPT_ENCODING'] = '"+this->getAccept_encod();
2004  ret += "'\nSERVER['HTTP_ACCEPT_LANGUAGE'] = '"+this->getAccept_lang();
2005  ret += "'\nSERVER_EX['CACHE_CNTRL'] = '"+this->getCache_ctrl();
2006  ret += "'\nSERVER['HTTP_CONNECTION'] = '"+this->getConnection();
2007  ret += "'\nSERVER_EX['HTTP_CONTENT'] = '"+this->getContent();
2008  ret += "'\nSERVER_EX['HTTP_CNTENT_LENGTH'] = '"+this->getContent_len();
2009  ret += "'\nSERVER_EX['HTTP_CNTENT_TYPE'] = '"+this->getContent_type();
2010  ret += "'\nSERVER_EX['HTTP_VERSION'] = '"+this->getHttpVersion();
2011  ret += "'\nSERVER['REQUEST_METHOD'] = '"+this->getMethod();
2012  ret += "'\nSERVER['HTTP_USER_AGENT'] = '"+this->getUser_agent();
2013  ret += "'\nSERVER['REQUEST_URI'] = '"+this->getUrl();
2014  ret += "'\nSERVER_EX['HTTP_REQ_FILE'] = '"+this->getFile();
2015  ret += "'\nSERVER_EX['CONTEXT_NAME'] = '"+this->getCntxt_name();
2016  ret += "'\nSERVER['DOCUMENT_ROOT'] = '"+this->getCntxt_root();
2017  ret += "'\nSERVER_EX['DEFAULT_LOCALE'] = '"+this->getDefaultLocale();
2018  ret += "'\nSERVER['HTTP_HOST'] = '"+this->getContent_boundary() + "'\n";
2019  if(this->queryParams.size()>0)
2020  {
2021  RMap::iterator iter;
2022  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
2023  {
2024  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2025  {
2026  string key = iter->first.substr(0, iter->first.find("["));
2027  ret += "\nif GET['"+key+"'] == nil then\nGET['"+key+"']={}\nend\n";
2028  ret += ("\nGET['"+iter->first.substr(0, iter->first.find("["))+"']" +
2029  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
2030  }
2031  else
2032  ret += "\nGET['"+iter->first+"'] = '"+ iter->second + "'";
2033  }
2034  }
2035  if(this->requestParams.size()>0)
2036  {
2037  RMap::iterator iter;
2038  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
2039  {
2040  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2041  {
2042  string key = iter->first.substr(0, iter->first.find("["));
2043  ret += "\nif POST['"+key+"'] == nil then\nPOST['"+key+"']={}\nend\n";
2044  ret += ("\nPOST['"+iter->first.substr(0, iter->first.find("["))+"']" +
2045  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "'");
2046  }
2047  else
2048  ret += "\nPOST['"+iter->first+"'] = '"+iter->second + "'";
2049  }
2050  }
2051  if(this->requestParamsF.size()>0)
2052  {
2053  FMap::iterator iter;
2054  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
2055  {
2056  FormData dat = iter->second;
2057  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2058  {
2059  string key = iter->first.substr(iter->first.find("["));
2060  StringUtil::replaceFirst(key,"[","");
2061  StringUtil::replaceFirst(key,"]","");
2062  ret += "\nif FILES['"+iter->first.substr(0, iter->first.find("["))+"'] == nil then"
2063  + "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']={}\nend\n";
2064  ret += "\nif FILES['"+iter->first.substr(0, iter->first.find("["))+"']['"+key+"'] == nil then"
2065  + "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']['"+key+"']={}\nend\n";
2066  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2067  iter->first.substr(iter->first.find("[")) + "['name'] = '"+ dat.fileName + "'";
2068  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2069  iter->first.substr(iter->first.find("[")) + "['type'] = '"+ dat.type + "'";
2070  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2071  iter->first.substr(iter->first.find("[")) + "['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
2072  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2073  iter->first.substr(iter->first.find("[")) + "['tmp_name'] = '"+ dat.tmpFileName + "'";
2074  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2075  iter->first.substr(iter->first.find("[")) + "['error'] = 0;";
2076  }
2077  else
2078  {
2079  ret += "\nFILES['"+iter->first+"'] = {}";
2080  ret += "\nFILES['"+iter->first+"']['name'] = '"+ dat.fileName + "'";
2081  ret += "\nFILES['"+iter->first+"']['type'] = '"+ dat.type + "'";
2082  ret += "\nFILES['"+iter->first+"']['size'] = "+ CastUtil::lexical_cast<string>(dat.length);
2083  ret += "\nFILES['"+iter->first+"']['tmp_name'] = '"+ dat.tmpFileName + "'";
2084  ret += "\nFILES['"+iter->first+"']['error'] = 0";
2085  }
2086  }
2087  }
2088  ret += "\ndofile(\""+this->getUrl()+"\")\n";
2089  return ret;
2090 }
2091 
2092 string HttpRequest::toNodejsVariablesString()
2093 {
2094  string ret;
2095  ret += "SERVER = {};";
2096  ret += "\nSERVER_EX = {};";
2097  ret += "\nGET = {};";
2098  ret += "\nPOST = {};";
2099  ret += "\nFILES = {};";
2100  ret += "\nSERVER['HTTP_HOST'] = '"+this->getHost();
2101  ret += "';\nSERVER['HTTP_ACCEPT'] = '"+this->getAccept();
2102  ret += "';\nSERVER['HTTP_ACCEPT_CHARSET'] = '"+this->getAccept_chars();
2103  ret += "';\nSERVER['HTTP_ACCEPT_ENCODING'] = '"+this->getAccept_encod();
2104  ret += "';\nSERVER['HTTP_ACCEPT_LANGUAGE'] = '"+this->getAccept_lang();
2105  ret += "';\nSERVER_EX['CACHE_CNTRL'] = '"+this->getCache_ctrl();
2106  ret += "';\nSERVER['HTTP_CONNECTION'] = '"+this->getConnection();
2107  ret += "';\nSERVER_EX['HTTP_CONTENT'] = '"+this->getContent();
2108  ret += "';\nSERVER_EX['HTTP_CNTENT_LENGTH'] = '"+this->getContent_len();
2109  ret += "';\nSERVER_EX['HTTP_CNTENT_TYPE'] = '"+this->getContent_type();
2110  ret += "';\nSERVER_EX['HTTP_VERSION'] = '"+this->getHttpVersion();
2111  ret += "';\nSERVER['REQUEST_METHOD'] = '"+this->getMethod();
2112  ret += "';\nSERVER['HTTP_USER_AGENT'] = '"+this->getUser_agent();
2113  ret += "';\nSERVER['REQUEST_URI'] = '"+this->getUrl();
2114  ret += "';\nSERVER_EX['HTTP_REQ_FILE'] = '"+this->getFile();
2115  ret += "';\nSERVER_EX['CONTEXT_NAME'] = '"+this->getCntxt_name();
2116  ret += "';\nSERVER['DOCUMENT_ROOT'] = '"+this->getCntxt_root();
2117  ret += "';\nSERVER_EX['DEFAULT_LOCALE'] = '"+this->getDefaultLocale();
2118  ret += "';\nSERVER['HTTP_HOST'] = '"+this->getContent_boundary() + "';\n";
2119  if(this->queryParams.size()>0)
2120  {
2121  RMap::iterator iter;
2122  for (iter=this->queryParams.begin();iter!=this->queryParams.end();iter++)
2123  {
2124  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2125  {
2126  ret += "\nif((GET['"+iter->first.substr(0, iter->first.find("["))+"'])==undefined)\n{\nGET['"+iter->first.substr(0, iter->first.find("["))+"']={};\n}\n";
2127  ret += ("\nGET['"+iter->first.substr(0, iter->first.find("["))+"']" +
2128  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "';");
2129  }
2130  else
2131  ret += "\nGET['"+iter->first+"'] = '"+ iter->second + "';";
2132  }
2133  }
2134  if(this->requestParams.size()>0)
2135  {
2136  RMap::iterator iter;
2137  for (iter=this->requestParams.begin();iter!=this->requestParams.end();iter++)
2138  {
2139  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2140  {
2141  ret += "\nif((POST['"+iter->first.substr(0, iter->first.find("["))+"'])==undefined)\n{\nPOST['"+iter->first.substr(0, iter->first.find("["))+"']={};\n}\n";
2142  ret += ("\nPOST['"+iter->first.substr(0, iter->first.find("["))+"']" +
2143  iter->first.substr(iter->first.find("[")) + " = '"+iter->second + "';");
2144  }
2145  else
2146  ret += "\nPOST['"+iter->first+"'] = '"+iter->second + "';";
2147  }
2148  }
2149  if(this->requestParamsF.size()>0)
2150  {
2151  FMap::iterator iter;
2152  for (iter=this->requestParamsF.begin();iter!=this->requestParamsF.end();iter++)
2153  {
2154  FormData dat = iter->second;
2155  if(iter->first.find("[")!=string::npos && iter->first.find("]")!=string::npos)
2156  {
2157  ret += "\nif((FILES['"+iter->first.substr(0, iter->first.find("["))+"'])==undefined)\n{\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']={};\n}\n";
2158  ret += "\nif((FILES['"+iter->first.substr(0, iter->first.find("["))+"']"+iter->first.substr(iter->first.find("["))+")==undefined)\n"
2159  + "{\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']"+iter->first.substr(iter->first.find("["))+"={};\n}\n";
2160  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2161  iter->first.substr(iter->first.find("[")) + "['name'] = '"+ dat.fileName + "';";
2162  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2163  iter->first.substr(iter->first.find("[")) + "['type'] = '"+ dat.type + "';";
2164  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2165  iter->first.substr(iter->first.find("[")) + "['size'] = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
2166  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2167  iter->first.substr(iter->first.find("[")) + "['tmp_name'] = '"+ dat.tmpFileName + "';";
2168  ret += "\nFILES['"+iter->first.substr(0, iter->first.find("["))+"']" +
2169  iter->first.substr(iter->first.find("[")) + "['error'] = 0;";
2170  }
2171  else
2172  {
2173  ret += "\nif(!isset(FILES['"+iter->first+"']))\n{\nFILES['"+iter->first+"']={};\n}\n";
2174  ret += "\nFILES['"+iter->first+"']['name'] = '"+ dat.fileName + "';";
2175  ret += "\nFILES['"+iter->first+"']['type'] = '"+ dat.type + "';";
2176  ret += "\nFILES['"+iter->first+"']['size'] = "+ CastUtil::lexical_cast<string>(dat.length) + ";";
2177  ret += "\nFILES['"+iter->first+"']['tmp_name'] = '"+ dat.tmpFileName + "';";
2178  ret += "\nFILES['"+iter->first+"']['error'] = 0;";
2179  }
2180  }
2181  }
2182  ret += "\nrequire('"+this->getUrl()+"');";
2183  return ret;
2184 }
2185 
2186 RMap HttpRequest::getAllParams()
2187 {
2188  map<string,string>::iterator it;
2189  map<string,string> reqparams = this->getRequestParams();
2190  map<string,string> qryparams = this->getQueryParams();
2191  for(it=qryparams.begin();it!=qryparams.end();it++)
2192  {
2193  reqparams[it->first] = it->second;
2194  }
2195  return reqparams;
2196 }