libnavajo
web server for static and dynamic pages developement in C++
HttpResponse.hh
Go to the documentation of this file.
1 //****************************************************************************
12 //****************************************************************************
13 
14 #ifndef HTTPRESPONSE_HH_
15 #define HTTPRESPONSE_HH_
16 
17 
19 {
20  unsigned char *responseContent;
21  size_t responseContentLength;
22  std::vector<std::string> responseCookies;
23  bool zippedFile;
24  std::string mimeType;
25  std::string forwardToUrl;
26  bool cors, corsCred;
27  std::string corsDomain;
28 
29  public:
30  HttpResponse(std::string mime="") : responseContent (NULL), responseContentLength (0), zippedFile (false), mimeType(mime), forwardToUrl(""), cors(false), corsCred(false), corsDomain("")
31  {
32  }
33 
34  /************************************************************************/
40  inline void setContent(unsigned char *content, size_t length)
41  {
42  responseContent = content;
43  responseContentLength = length;
44  }
45 
46  /************************************************************************/
54  inline void getContent(unsigned char **content, size_t *length, bool *zip)
55  {
56  *content = responseContent;
57  *length = responseContentLength;
58  *zip = zippedFile;
59  }
60 
61  /************************************************************************/
66  inline void setIsZipped(bool b=true) { zippedFile=b; };
67 
68  /************************************************************************/
72  inline bool isZipped() const { return zippedFile; };
73 
74  /************************************************************************/
89  inline void addCookie(const std::string& name, const std::string& value, const time_t maxage=0, const time_t expiresTime=0, const std::string& path="/", const std::string& domain="", const bool secure=false, bool httpOnly=false)
90  {
91  std::string cookieEntry=name+'='+value;
92 
93  if (maxage)
94  {
95  std::stringstream maxageSs; maxageSs << maxage;
96  cookieEntry+="; Max-Age="+maxageSs.str();
97  }
98 
99  if (expiresTime)
100  {
101  char expBuf[100];
102  struct tm timeinfo;
103  gmtime_r ( &expiresTime, &timeinfo );
104  strftime (expBuf,100,"%a, %d %b %Y %H:%M:%S GMT", &timeinfo);
105  cookieEntry+="; expires="+std::string(expBuf);
106  }
107 
108  if (domain.length()) cookieEntry+="; domain="+domain;
109 
110  if (path != "/" && path.length()) cookieEntry+="; path="+path;
111 
112  if (secure) cookieEntry+="; secure";
113 
114  if (httpOnly) cookieEntry+="; HttpOnly";
115 
116  responseCookies.push_back(cookieEntry);
117  }
118 
119  /************************************************************************/
124  inline void addSessionCookie(const std::string& sid)
125  {
126  addCookie("SID",sid, HttpSession::getSessionLifeTime(), 0, "", "", false, true);
127  }
128 
129  /************************************************************************/
134  inline std::vector<std::string>& getCookies()
135  {
136  return responseCookies;
137  };
138 
139  /************************************************************************/
144  inline void setMimeType(const std::string& mime)
145  {
146  mimeType=mime;
147  }
148 
149  /************************************************************************/
154  inline const std::string& getMimeType() const
155  {
156  return mimeType;
157  }
158 
159  /************************************************************************/
164  void forwardTo(const std::string& url)
165  {
166  forwardToUrl=url;
167  }
168 
169  /************************************************************************/
174  std::string getForwardedUrl()
175  {
176  return forwardToUrl;
177  }
178 
179  /************************************************************************/
185  void setCORS(bool cors=true, bool cred=false, std::string domain="*")
186  {
187  this->cors=cors;
188  corsCred=cred;
189  corsDomain=domain;
190  }
191 
196  bool isCORS()
197  {
198  return cors;
199  }
200 
201  bool isCORSwithCredentials()
202  {
203  return corsCred;
204  }
205 
206  std::string& getCORSdomain()
207  {
208  return corsDomain;
209  };
210 };
211 
212 
213 
214 //****************************************************************************
215 
216 #endif
void addCookie(const std::string &name, const std::string &value, const time_t maxage=0, const time_t expiresTime=0, const std::string &path="/", const std::string &domain="", const bool secure=false, bool httpOnly=false)
Definition: HttpResponse.hh:89
const std::string & getMimeType() const
Definition: HttpResponse.hh:154
void setMimeType(const std::string &mime)
Definition: HttpResponse.hh:144
void setIsZipped(bool b=true)
Definition: HttpResponse.hh:66
void setCORS(bool cors=true, bool cred=false, std::string domain="*")
Definition: HttpResponse.hh:185
void getContent(unsigned char **content, size_t *length, bool *zip)
Definition: HttpResponse.hh:54
void forwardTo(const std::string &url)
Definition: HttpResponse.hh:164
std::vector< std::string > & getCookies()
Definition: HttpResponse.hh:134
void setContent(unsigned char *content, size_t length)
Definition: HttpResponse.hh:40
std::string getForwardedUrl()
Definition: HttpResponse.hh:174
bool isZipped() const
Definition: HttpResponse.hh:72
Definition: HttpResponse.hh:18
void addSessionCookie(const std::string &sid)
Definition: HttpResponse.hh:124
bool isCORS()
Definition: HttpResponse.hh:196