EHS Embedded HTTP Server  1.5.1.0
httprequest.h
1 /* $Id: httprequest.h 161 2013-02-27 00:02:04Z 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 HTTPREQUEST_H
27 #define HTTPREQUEST_H
28 
29 #ifdef _MSC_VER
30 # pragma warning(disable : 4786)
31 #endif
32 
34 enum RequestMethod {
35  REQUESTMETHOD_OPTIONS, /* not implemented */
36  REQUESTMETHOD_GET,
37  REQUESTMETHOD_HEAD,
38  REQUESTMETHOD_POST,
39  REQUESTMETHOD_PUT,
40  REQUESTMETHOD_DELETE,
41  REQUESTMETHOD_TRACE,
42  REQUESTMETHOD_CONNECT,
43  REQUESTMETHOD_UNKNOWN
44 };
45 
51 class HttpRequest {
52 
53  private:
54 
61  HttpRequest (int inRequestId, EHSConnection *ipoSourceEHSConnection,
62  const std::string & irsParseContentType);
63 
64  public:
65 
67  virtual ~HttpRequest ( );
68 
73  std::string RemoteAddress();
74 
79  int RemotePort();
80 
85  std::string LocalAddress();
86 
91  int LocalPort();
92 
93  DEPRECATED("Use RemoteAddress()")
99  std::string Address() { return RemoteAddress(); }
100 
101  DEPRECATED("Use RemotePort()")
107  int Port() { return RemotePort(); }
108 
113  int Id() const { return m_nRequestId; }
114 
119  EHSConnection *Connection() const { return m_poSourceEHSConnection; }
120 
125  RequestMethod Method() const { return m_nRequestMethod; }
126 
131  bool Secure() const { return m_bSecure; }
132 
137  bool ClientDisconnected();
138 
143  const std::string &Uri() const { return m_sUri; }
144 
149  const std::string &HttpVersion() const { return m_sHttpVersionNumber; }
150 
155  const std::string &Body() const { return m_sBody; }
156 
161  StringCaseMap &Headers() { return m_oRequestHeaders; }
162 
167  FormValueMap &FormValues() { return m_oFormValueMap; }
168 
173  CookieMap &Cookies() { return m_oCookieMap; }
174 
180  FormValue &FormValues(const std::string & name)
181  {
182  return m_oFormValueMap[name];
183  }
184 
190  std::string Headers(const std::string & name)
191  {
192  if (m_oRequestHeaders.find(name) != m_oRequestHeaders.end()) {
193  return m_oRequestHeaders[name];
194  }
195  return std::string();
196  }
197 
205  void SetHeader(const std::string & name, const std::string & value)
206  {
207  m_oRequestHeaders[name] = value;
208  }
209 
215  std::string Cookies(const std::string & name)
216  {
217  if (m_oCookieMap.find(name) != m_oCookieMap.end()) {
218  return m_oCookieMap[name];
219  }
220  return std::string();
221  }
222 
223  private:
224 
226  HttpRequest(const HttpRequest &);
227 
229  HttpRequest & operator=(const HttpRequest &);
230 
232  void GetFormDataFromString(const std::string &irsString);
233 
235  enum HttpParseStates {
236  HTTPPARSESTATE_INVALID = 0,
237  HTTPPARSESTATE_REQUEST,
238  HTTPPARSESTATE_HEADERS,
239  HTTPPARSESTATE_BODY,
240  HTTPPARSESTATE_BODYCHUNK,
241  HTTPPARSESTATE_BODYTRAILER,
242  HTTPPARSESTATE_BODYTRAILER2,
243  HTTPPARSESTATE_COMPLETEREQUEST,
244  HTTPPARSESTATE_INVALIDREQUEST
245  };
246 
248  enum ParseMultipartFormDataResult {
249  PARSEMULTIPARTFORMDATA_INVALID = 0,
250  PARSEMULTIPARTFORMDATA_SUCCESS,
251  PARSEMULTIPARTFORMDATA_FAILED
252  };
253 
255  enum ParseSubbodyResult {
256  PARSESUBBODY_INVALID = 0,
257  PARSESUBBODY_SUCCESS,
258  PARSESUBBODY_INVALIDSUBBODY, // no blank line?
259  PARSESUBBODY_FAILED // other reason
260  };
261 
263  ParseMultipartFormDataResult ParseMultipartFormData();
264 
270  ParseSubbodyResult ParseSubbody(std::string sSubBody);
271 
273  HttpParseStates ParseData(std::string & irsData);
274 
276  int ParseCookieData (std::string & irsData);
277 
279  HttpParseStates m_nCurrentHttpParseState;
280 
282  RequestMethod m_nRequestMethod;
283 
285  std::string m_sUri;
286 
288  std::string m_sOriginalUri;
289 
291  std::string m_sHttpVersionNumber;
292 
294  std::string m_sBody;
295 
297  std::string m_sLastHeaderName;
298 
300  bool m_bSecure;
301 
303  StringCaseMap m_oRequestHeaders;
304 
306  FormValueMap m_oFormValueMap;
307 
309  CookieMap m_oCookieMap;
310 
312  int m_nRequestId;
313 
315  EHSConnection * m_poSourceEHSConnection;
316 
317  bool m_bChunked;
318 
319  size_t m_nChunkLen;
320 
322  std::string m_sParseContentType;
323 
324  friend class EHSConnection;
325  friend class EHS;
326 };
327 
328 
329 // GLOBAL HELPER FUNCTIONS
330 
338 std::string GetNextLine(std::string & buffer);
339 
345 RequestMethod GetRequestMethodFromString(const std::string & method);
346 
353 bool IsMultivalHeader(const std::string &header);
354 
361 bool MultivalHeaderContains(const std::string &header, const std::string &value);
362 
363 #endif // HTTPREQUEST_H
EHSConnection
EHSConnection abstracts the concept of a connection to an EHS application.
Definition: ehsconnection.h:40
HttpRequest::SetHeader
void SetHeader(const std::string &name, const std::string &value)
Sets a single request header.
Definition: httprequest.h:205
HttpRequest::Connection
EHSConnection * Connection() const
Retrieves the receiving connection.
Definition: httprequest.h:119
HttpRequest::Address
std::string Address()
Retrieves the peer's IP address.
Definition: httprequest.h:99
HttpRequest::FormValues
FormValueMap & FormValues()
Retrieves form values.
Definition: httprequest.h:167
HttpRequest::Headers
std::string Headers(const std::string &name)
Retrieves a specific HTTP header.
Definition: httprequest.h:190
HttpRequest::Uri
const std::string & Uri() const
Retrieves this request's URI.
Definition: httprequest.h:143
HttpRequest::RemoteAddress
std::string RemoteAddress()
Retrieves the peer's IP address.
HttpRequest::Secure
bool Secure() const
Retrieves the security status.
Definition: httprequest.h:131
HttpRequest::Cookies
std::string Cookies(const std::string &name)
Retrieves a specific cookie value.
Definition: httprequest.h:215
HttpRequest::FormValues
FormValue & FormValues(const std::string &name)
Retrieves a specific form value.
Definition: httprequest.h:180
HttpRequest::Port
int Port()
Retrieves the peer's port.
Definition: httprequest.h:107
HttpRequest::Body
const std::string & Body() const
Retrieves this request's body.
Definition: httprequest.h:155
HttpRequest::ClientDisconnected
bool ClientDisconnected()
Retrieves the client connection status.
HttpRequest::LocalAddress
std::string LocalAddress()
Retrieves the local IP address.
HttpRequest::RemotePort
int RemotePort()
Retrieves the peer's port.
HttpRequest::~HttpRequest
virtual ~HttpRequest()
Destructor.
FormValue
This class stores form data sent from the client in GET and POST requests.
Definition: formvalue.h:39
HttpRequest::Headers
StringCaseMap & Headers()
Retrieves HTTP headers.
Definition: httprequest.h:161
HttpRequest
This class represents a clients HTTP request.
Definition: httprequest.h:51
HttpRequest::Cookies
CookieMap & Cookies()
Retrieves cookies.
Definition: httprequest.h:173
HttpRequest::HttpVersion
const std::string & HttpVersion() const
Retrieves the HTTP version.
Definition: httprequest.h:149
HttpRequest::LocalPort
int LocalPort()
Retrieves the local port.
HttpRequest::Method
RequestMethod Method() const
Retrieves the request method.
Definition: httprequest.h:125
HttpRequest::Id
int Id() const
Retrieves this request's Id.
Definition: httprequest.h:113
EHS
EHS provides HTTP server functionality to a child class.
Definition: ehs.h:147