libnavajo
web server for static and dynamic pages developement in C++
HttpRequest.hh
Go to the documentation of this file.
1 //****************************************************************************
12 //****************************************************************************
13 
14 #ifndef HTTPREQUEST_HH_
15 #define HTTPREQUEST_HH_
16 
17 #include <map>
18 #include <vector>
19 #include <string>
20 #include <sstream>
21 #include <openssl/ssl.h>
22 
23 #include "libnavajo/IpAddress.hh"
24 #include "HttpSession.hh"
25 
26 #include "MPFDParser/Parser.h"
27 
28 //****************************************************************************
29 
30 typedef enum { UNKNOWN_METHOD = 0, GET_METHOD = 1, POST_METHOD = 2, PUT_METHOD = 3, DELETE_METHOD = 4 } HttpRequestMethod;
31 typedef enum { GZIP, ZLIB, NONE } CompressionMode;
32 typedef struct
33 {
34  int socketId;
35  IpAddress ip;
36  CompressionMode compression;
37  SSL *ssl;
38  BIO *bio;
39  std::string *peerDN;
41 
43 {
44  typedef std::map <std::string, std::string> HttpRequestParametersMap;
45  typedef std::map <std::string, std::string> HttpRequestCookiesMap;
46 
47  const char *url;
48  const char *origin;
49  ClientSockData *clientSockData;
50  std::string httpAuthUsername;
51  HttpRequestMethod httpMethod;
52  HttpRequestCookiesMap cookies;
53  HttpRequestParametersMap parameters;
54  std::string sessionId;
55  MPFD::Parser *mutipartContentParser;
56 
57  /**********************************************************************/
62  inline void decodParams( const std::string& p )
63  {
64  size_t start = 0, end = 0;
65  std::string paramstr=p;
66 
67  while ((end = paramstr.find_first_of("%+", start)) != std::string::npos)
68  {
69  switch (paramstr[end])
70  {
71  case '%':
72  if (paramstr[end+1]=='%')
73  paramstr=paramstr.erase(end+1,1);
74  else
75  {
76  unsigned int specar;
77  std::string hexChar=paramstr.substr(end+1,2);
78  std::stringstream ss; ss << std::hex << hexChar.c_str();
79  ss >> specar;
80  paramstr[end] = (char)specar;
81  paramstr=paramstr.erase(end+1,2);
82  }
83  break;
84 
85  case '+':
86  paramstr[end]=' ';
87  break;
88  }
89 
90  start=end+1;
91  }
92 
93  start = 0; end = 0;
94  bool islastParam=false;
95  while (!islastParam)
96  {
97  islastParam= (end = paramstr.find('&', start)) == std::string::npos;
98  if (islastParam) end=paramstr.size();
99 
100  std::string theParam=paramstr.substr(start, end - start);
101 
102  size_t posEq=0;
103  if ((posEq = theParam.find('=')) == std::string::npos)
104  parameters[theParam]="";
105  else
106  parameters[theParam.substr(0,posEq)]=theParam.substr(posEq+1);
107 
108  start = end + 1;
109  }
110  };
111 
112  /**********************************************************************/
117  inline void decodCookies( const std::string& c )
118  {
119  std::stringstream ss(c);
120  std::string theCookie;
121  while (std::getline(ss, theCookie, ';'))
122  {
123  size_t posEq=0;
124  if ((posEq = theCookie.find('=')) != std::string::npos)
125  {
126  size_t firstC=0; while (!iswgraph(theCookie[firstC]) && firstC < posEq) { firstC++; };
127 
128  if (posEq-firstC > 0 && theCookie.length()-posEq > 0) cookies[theCookie.substr(firstC,posEq-firstC)]=theCookie.substr(posEq+1, theCookie.length()-posEq);
129  }
130  }
131  };
132 
133  /**********************************************************************/
138  inline void getSession()
139  {
140  sessionId = getCookie("SID");
141 
142  if (sessionId.length() && HttpSession::find(sessionId))
143  return;
144 
145  initSessionId();
146  };
147 
148 
149 
150  public:
151 
152  /**********************************************************************/
157  inline std::string getCookie( const std::string& name ) const
158  {
159  std::string res="";
160  getCookie(name, res);
161  return res;
162  }
163 
164  /**********************************************************************/
171  inline bool getCookie( const std::string& name, std::string &value ) const
172  {
173  if(!cookies.empty())
174  {
175  HttpRequestCookiesMap::const_iterator it;
176  if((it = cookies.find(name)) != cookies.end())
177  {
178  value=it->second;
179  return true;
180  }
181  }
182  return false;
183  }
184 
185  /**********************************************************************/
190  inline std::vector<std::string> getCookiesNames() const
191  {
192  std::vector<std::string> res;
193  for(HttpRequestCookiesMap::const_iterator iter=cookies.begin(); iter!=cookies.end(); ++iter)
194  res.push_back(iter->first);
195  return res;
196  }
197 
198  /**********************************************************************/
205  inline bool getParameter( const std::string& name, std::string &value ) const
206  {
207  if(!parameters.empty())
208  {
209  HttpRequestParametersMap::const_iterator it;
210  if((it = parameters.find(name)) != parameters.end())
211  {
212  value=it->second;
213  return true;
214  }
215  }
216  return false;
217  }
218 
219  /**********************************************************************/
225  inline std::string getParameter( const std::string& name ) const
226  {
227  std::string res="";
228  getParameter(name, res);
229  return res;
230  }
231 
232  /**********************************************************************/
238  inline bool hasParameter( const std::string& name ) const
239  {
240  std::string tmp;
241  return getParameter(name, tmp);
242  }
243 
244  /**********************************************************************/
249  inline std::vector<std::string> getParameterNames() const
250  {
251  std::vector<std::string> res;
252  for(HttpRequestParametersMap::const_iterator iter=parameters.begin(); iter!=parameters.end(); ++iter)
253  res.push_back(iter->first);
254  return res;
255  }
256 
257  /**********************************************************************/
261  inline bool isSessionValid()
262  {
263  return sessionId != "";
264  }
265 
266  /**********************************************************************/
270  inline void createSession()
271  {
272  HttpSession::create(sessionId);
273  }
274 
278  inline void removeSession()
279  {
280  if (sessionId == "") return;
281  HttpSession::remove(sessionId);
282  }
283 
289  void setSessionAttribute ( const std::string &name, void* value )
290  {
291  if (sessionId == "") createSession();
292  HttpSession::setAttribute(sessionId, name, value);
293  }
294 
300  void setSessionObjectAttribute ( const std::string &name, SessionAttributeObject* value )
301  {
302  if (sessionId == "") createSession();
303  HttpSession::setObjectAttribute(sessionId, name, value);
304  }
305 
311  void *getSessionAttribute( const std::string &name )
312  {
313  if (sessionId == "") return NULL;
314  return HttpSession::getAttribute(sessionId, name);
315  }
316 
323  {
324  if (sessionId == "") return NULL;
325  return HttpSession::getObjectAttribute(sessionId, name);
326  }
327 
332  inline std::vector<std::string> getSessionAttributeNames()
333  {
334  if (sessionId == "") return std::vector<std::string>();;
335  return HttpSession::getAttributeNames(sessionId);
336  }
337 
342  inline void getSessionRemoveAttribute( const std::string &name )
343  {
344  if (sessionId != "")
345  HttpSession::removeAttribute( sessionId, name );
346  }
347 
351  inline void initSessionId() { sessionId = ""; };
352 
357  std::string getSessionId() const { return sessionId; };
358 
359  /**********************************************************************/
367  HttpRequest(const HttpRequestMethod type, const char *url, const char *params, const char *cookies, const char *origin, const std::string &username, ClientSockData *client, MPFD::Parser *parser=NULL)
368  {
369  this->httpMethod = type;
370  this->url = url;
371  this->origin = origin;
372  this->httpAuthUsername=username;
373  this->clientSockData=client;
374  this->mutipartContentParser=parser;
375 
376  if (params != NULL && strlen(params))
377  decodParams(params);
378 
379  if (cookies != NULL && strlen(cookies))
380  decodCookies(cookies);
381  getSession();
382  };
383 
384  /**********************************************************************/
389  inline bool isMultipartContent() const { return mutipartContentParser != NULL; };
390 
391  /**********************************************************************/
396  inline MPFD::Parser *getMPFDparser() { return mutipartContentParser; };
397 
398  /**********************************************************************/
403  inline const char *getUrl() const { return url; };
404 
405  /**********************************************************************/
410  inline HttpRequestMethod getRequestType() const { return httpMethod; };
411 
412  /**********************************************************************/
417  inline const char* getRequestOrigin() const { return origin; };
418 
419  /**********************************************************************/
425  {
426  return clientSockData->ip;
427  };
428 
429  /**********************************************************************/
434  inline std::string& getHttpAuthUsername()
435  {
436  return httpAuthUsername;
437  };
438 
439  /**********************************************************************/
444  inline std::string& getX509PeerDN()
445  {
446  return *(clientSockData->peerDN);
447  };
448 
449  /**********************************************************************/
454  inline CompressionMode getCompressionMode()
455  {
456  return clientSockData->compression;
457  };
458 
459  /**********************************************************************/
465  {
466  return clientSockData;
467  }
468 };
469 
470 //****************************************************************************
471 
472 #endif
bool hasParameter(const std::string &name) const
Definition: HttpRequest.hh:238
void createSession()
Definition: HttpRequest.hh:270
std::vector< std::string > getSessionAttributeNames()
Definition: HttpRequest.hh:332
Definition: Parser.h:22
std::string getParameter(const std::string &name) const
Definition: HttpRequest.hh:225
HttpRequest(const HttpRequestMethod type, const char *url, const char *params, const char *cookies, const char *origin, const std::string &username, ClientSockData *client, MPFD::Parser *parser=NULL)
Definition: HttpRequest.hh:367
Definition: IpAddress.hh:64
void * getSessionAttribute(const std::string &name)
Definition: HttpRequest.hh:311
IpAddress & getPeerIpAddress()
Definition: HttpRequest.hh:424
std::vector< std::string > getParameterNames() const
Definition: HttpRequest.hh:249
ClientSockData * getClientSockData()
Definition: HttpRequest.hh:464
bool getParameter(const std::string &name, std::string &value) const
Definition: HttpRequest.hh:205
std::string & getHttpAuthUsername()
Definition: HttpRequest.hh:434
void getSessionRemoveAttribute(const std::string &name)
Definition: HttpRequest.hh:342
std::string getSessionId() const
Definition: HttpRequest.hh:357
std::vector< std::string > getCookiesNames() const
Definition: HttpRequest.hh:190
void setSessionObjectAttribute(const std::string &name, SessionAttributeObject *value)
Definition: HttpRequest.hh:300
bool isSessionValid()
Definition: HttpRequest.hh:261
The Http Sessions Manager class.
SessionAttributeObject * getSessionObjectAttribute(const std::string &name)
Definition: HttpRequest.hh:322
void initSessionId()
Definition: HttpRequest.hh:351
CompressionMode getCompressionMode()
Definition: HttpRequest.hh:454
Definition: HttpSession.hh:24
std::string & getX509PeerDN()
Definition: HttpRequest.hh:444
const char * getRequestOrigin() const
Definition: HttpRequest.hh:417
Definition: HttpRequest.hh:42
MPFD::Parser * getMPFDparser()
Definition: HttpRequest.hh:396
bool getCookie(const std::string &name, std::string &value) const
Definition: HttpRequest.hh:171
std::string getCookie(const std::string &name) const
Definition: HttpRequest.hh:157
void setSessionAttribute(const std::string &name, void *value)
Definition: HttpRequest.hh:289
Ip Address (V4 and V6) class definition.
const char * getUrl() const
Definition: HttpRequest.hh:403
Definition: HttpRequest.hh:32
void removeSession()
Definition: HttpRequest.hh:278
bool isMultipartContent() const
Definition: HttpRequest.hh:389
HttpRequestMethod getRequestType() const
Definition: HttpRequest.hh:410