EHS Embedded HTTP Server  1.5.1.0
httpresponse.h
1 /* $Id: httpresponse.h 150 2012-06-07 14:57:34Z felfert $
2  *
3  * EHS is a library for embedding HTTP(S) support into a C++ application
4  *
5  * Copyright (C) 2004 Zachary J. Hansen
6  *
7  * Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1 as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * This can be found in the 'COPYING' file.
23  *
24  */
25 
26 #ifndef HTTPRESPONSE_H
27 #define HTTPRESPONSE_H
28 
29 #include <ehstypes.h>
30 
32 enum ResponseCode {
33  HTTPRESPONSECODE_INVALID = 0,
34  HTTPRESPONSECODE_101_SWITCHING_PROTOCOLS = 101,
35  HTTPRESPONSECODE_200_OK = 200,
36  HTTPRESPONSECODE_301_MOVEDPERMANENTLY = 301,
37  HTTPRESPONSECODE_302_FOUND = 302,
38  HTTPRESPONSECODE_304_NOT_MODIFIED = 304,
39  HTTPRESPONSECODE_400_BADREQUEST = 400,
40  HTTPRESPONSECODE_401_UNAUTHORIZED = 401,
41  HTTPRESPONSECODE_403_FORBIDDEN = 403,
42  HTTPRESPONSECODE_404_NOTFOUND = 404,
43  HTTPRESPONSECODE_413_TOOLARGE = 413,
44  HTTPRESPONSECODE_426_UPGRADE_REQUIRED = 426,
45  HTTPRESPONSECODE_500_INTERNALSERVERERROR = 500,
46  HTTPRESPONSECODE_503_SERVICEUNAVAILABLE = 503
47 };
48 
54 class HttpResponse : public GenericResponse {
55 
56  private:
57  HttpResponse( const HttpResponse & );
58  HttpResponse &operator = (const HttpResponse &);
59 
60  public:
66  HttpResponse(int inResponseId, EHSConnection * ipoEHSConnection);
67 
75  static HttpResponse *Error(ResponseCode code, int inResponseId, EHSConnection * ipoEHSConnection);
76 
84  static HttpResponse *Error(ResponseCode code, HttpRequest *request);
85 
92  static const char *GetPhrase(ResponseCode code);
93 
95  virtual ~HttpResponse ( ) { }
96 
103  void SetBody(const char *ipsBody, size_t inBodyLength);
104 
109  void SetCookie(CookieParameters & iroCookieParameters);
110 
115  void SetResponseCode(ResponseCode code) { m_nResponseCode = code; }
116 
120  ResponseCode GetResponseCode() { return m_nResponseCode; }
121 
125  StringCaseMap& GetHeaders() { return m_oResponseHeaders; }
126 
130  StringList& GetCookies() { return m_oCookieList; }
131 
137  void SetHeader(const std::string & name, const std::string & value)
138  {
139  m_oResponseHeaders[name] = value;
140  }
141 
146  void RemoveHeader(const std::string & name)
147  {
148  m_oResponseHeaders.erase(name);
149  }
150 
155  std::string GetStatusString();
156 
161  void SetDate(time_t stamp);
162 
167  void SetLastModified(time_t stamp);
168 
175  std::string HttpTime(time_t stamp);
176 
182  std::string Header(const std::string & name)
183  {
184  if (m_oResponseHeaders.find(name) != m_oResponseHeaders.end()) {
185  return m_oResponseHeaders[name];
186  }
187  return std::string();
188  }
189 
190  private:
191 
193  ResponseCode m_nResponseCode;
194 
197  StringCaseMap m_oResponseHeaders;
198 
200  StringList m_oCookieList;
201 };
202 
203 #endif // HTTPRESPONSE_H
EHSConnection
EHSConnection abstracts the concept of a connection to an EHS application.
Definition: ehsconnection.h:40
HttpResponse::SetResponseCode
void SetResponseCode(ResponseCode code)
Sets the response code for this response.
Definition: httpresponse.h:115
HttpResponse::GetResponseCode
ResponseCode GetResponseCode()
Retrieves the status code of this this response.
Definition: httpresponse.h:120
HttpResponse::HttpResponse
HttpResponse(int inResponseId, EHSConnection *ipoEHSConnection)
Constructs a new instance.
HttpResponse::Header
std::string Header(const std::string &name)
Retrieves a specific HTTP header.
Definition: httpresponse.h:182
HttpResponse::Error
static HttpResponse * Error(ResponseCode code, int inResponseId, EHSConnection *ipoEHSConnection)
Constructs a new standardized error response.
HttpResponse
This class represents what is sent back to the client.
Definition: httpresponse.h:54
HttpResponse::HttpTime
std::string HttpTime(time_t stamp)
Utility function for converting a UNIX timestamp into an RFC-conformant HTTP time string.
HttpResponse::GetHeaders
StringCaseMap & GetHeaders()
Retrieves the headers of this this response.
Definition: httpresponse.h:125
HttpResponse::SetCookie
void SetCookie(CookieParameters &iroCookieParameters)
Sets cookies for this response.
HttpResponse::SetLastModified
void SetLastModified(time_t stamp)
Sets the HTTP Last-Modified header.
HttpResponse::~HttpResponse
virtual ~HttpResponse()
Destructor.
Definition: httpresponse.h:95
HttpResponse::SetHeader
void SetHeader(const std::string &name, const std::string &value)
Sets an HTTP header.
Definition: httpresponse.h:137
HttpResponse::Error
static HttpResponse * Error(ResponseCode code, HttpRequest *request)
Constructs a new standardized error response.
HttpResponse::GetCookies
StringList & GetCookies()
Retrieves the cookies of this this response.
Definition: httpresponse.h:130
GenericResponse
This class represents what is sent back to the client.
Definition: ehstypes.h:86
HttpResponse::SetDate
void SetDate(time_t stamp)
Sets the HTTP Date header.
HttpRequest
This class represents a clients HTTP request.
Definition: httprequest.h:51
HttpResponse::RemoveHeader
void RemoveHeader(const std::string &name)
Removes an HTTP header.
Definition: httpresponse.h:146
HttpResponse::GetStatusString
std::string GetStatusString()
Retrieves the status string of this this response.
HttpResponse::SetBody
void SetBody(const char *ipsBody, size_t inBodyLength)
Sets the body of this instance.
HttpResponse::GetPhrase
static const char * GetPhrase(ResponseCode code)
Helper function for translating response codes into the corresponding text message.