EHS Embedded HTTP Server  1.5.1.173
httprequest.h
00001 /* $Id: httprequest.h 161 2013-02-27 00:02:04Z felfert $
00002  *
00003  * EHS is a library for embedding HTTP(S) support into a C++ application
00004  *
00005  * Copyright (C) 2004 Zachary J. Hansen
00006  *
00007  * Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
00008  *
00009  *    This library is free software; you can redistribute it and/or
00010  *    modify it under the terms of the GNU Lesser General Public
00011  *    License version 2.1 as published by the Free Software Foundation;
00012  *
00013  *    This library is distributed in the hope that it will be useful,
00014  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *    Lesser General Public License for more details.
00017  *
00018  *    You should have received a copy of the GNU Lesser General Public
00019  *    License along with this library; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  *    This can be found in the 'COPYING' file.
00023  *
00024  */
00025 
00026 #ifndef HTTPREQUEST_H
00027 #define HTTPREQUEST_H
00028 
00029 #ifdef _MSC_VER
00030 # pragma warning(disable : 4786)
00031 #endif
00032 
00034 enum RequestMethod {
00035     REQUESTMETHOD_OPTIONS, /* not implemented */
00036     REQUESTMETHOD_GET,
00037     REQUESTMETHOD_HEAD,
00038     REQUESTMETHOD_POST,
00039     REQUESTMETHOD_PUT, 
00040     REQUESTMETHOD_DELETE, 
00041     REQUESTMETHOD_TRACE, 
00042     REQUESTMETHOD_CONNECT, 
00043     REQUESTMETHOD_UNKNOWN 
00044 };
00045 
00051 class HttpRequest {
00052 
00053     private:
00054 
00061         HttpRequest (int inRequestId, EHSConnection *ipoSourceEHSConnection,
00062             const std::string & irsParseContentType);
00063 
00064     public:
00065 
00067         virtual ~HttpRequest ( );
00068 
00073         std::string RemoteAddress();
00074 
00079         int RemotePort();
00080 
00085         std::string LocalAddress();
00086 
00091         int LocalPort();
00092 
00093         DEPRECATED("Use RemoteAddress()")
00099             std::string Address() { return RemoteAddress(); }
00100 
00101         DEPRECATED("Use RemotePort()")
00107             int Port() { return RemotePort(); }
00108 
00113         int Id() const { return m_nRequestId; }
00114 
00119         EHSConnection *Connection() const { return m_poSourceEHSConnection; }
00120 
00125         RequestMethod Method() const { return m_nRequestMethod; }
00126 
00131         bool Secure() const { return m_bSecure; }
00132 
00137         bool ClientDisconnected();
00138 
00143         const std::string &Uri() const { return m_sUri; }
00144 
00149         const std::string &HttpVersion() const { return m_sHttpVersionNumber; }
00150 
00155         const std::string &Body() const { return m_sBody; }
00156 
00161         StringCaseMap &Headers() { return m_oRequestHeaders; }
00162 
00167         FormValueMap &FormValues() { return m_oFormValueMap; }
00168 
00173         CookieMap &Cookies() { return m_oCookieMap; }
00174 
00180         FormValue &FormValues(const std::string & name)
00181         {
00182             return m_oFormValueMap[name];
00183         }
00184 
00190         std::string Headers(const std::string & name)
00191         {
00192             if (m_oRequestHeaders.find(name) != m_oRequestHeaders.end()) {
00193                 return m_oRequestHeaders[name];
00194             }
00195             return std::string();
00196         }
00197 
00205         void SetHeader(const std::string & name, const std::string & value)
00206         {
00207             m_oRequestHeaders[name] = value;
00208         }
00209 
00215         std::string Cookies(const std::string & name)
00216         {
00217             if (m_oCookieMap.find(name) != m_oCookieMap.end()) {
00218                 return m_oCookieMap[name];
00219             }
00220             return std::string();
00221         }
00222 
00223     private:
00224 
00226         HttpRequest(const HttpRequest &);
00227 
00229         HttpRequest & operator=(const HttpRequest &);
00230 
00232         void GetFormDataFromString(const std::string &irsString);
00233 
00235         enum HttpParseStates {
00236             HTTPPARSESTATE_INVALID = 0,
00237             HTTPPARSESTATE_REQUEST,
00238             HTTPPARSESTATE_HEADERS,
00239             HTTPPARSESTATE_BODY,
00240             HTTPPARSESTATE_BODYCHUNK,
00241             HTTPPARSESTATE_BODYTRAILER,
00242             HTTPPARSESTATE_BODYTRAILER2,
00243             HTTPPARSESTATE_COMPLETEREQUEST,
00244             HTTPPARSESTATE_INVALIDREQUEST
00245         };
00246 
00248         enum ParseMultipartFormDataResult { 
00249             PARSEMULTIPARTFORMDATA_INVALID = 0,
00250             PARSEMULTIPARTFORMDATA_SUCCESS,
00251             PARSEMULTIPARTFORMDATA_FAILED 
00252         };
00253 
00255         enum ParseSubbodyResult {
00256             PARSESUBBODY_INVALID = 0,
00257             PARSESUBBODY_SUCCESS,
00258             PARSESUBBODY_INVALIDSUBBODY, // no blank line?
00259             PARSESUBBODY_FAILED // other reason
00260         };
00261 
00263         ParseMultipartFormDataResult ParseMultipartFormData();
00264 
00270         ParseSubbodyResult ParseSubbody(std::string sSubBody);
00271 
00273         HttpParseStates ParseData(std::string & irsData);
00274 
00276         int ParseCookieData (std::string & irsData);
00277 
00279         HttpParseStates m_nCurrentHttpParseState;
00280 
00282         RequestMethod m_nRequestMethod;
00283 
00285         std::string m_sUri;
00286 
00288         std::string m_sOriginalUri;
00289 
00291         std::string m_sHttpVersionNumber;
00292 
00294         std::string m_sBody; 
00295 
00297         std::string m_sLastHeaderName;
00298 
00300         bool m_bSecure;
00301 
00303         StringCaseMap m_oRequestHeaders;
00304 
00306         FormValueMap m_oFormValueMap;
00307 
00309         CookieMap m_oCookieMap;
00310 
00312         int m_nRequestId;
00313 
00315         EHSConnection * m_poSourceEHSConnection;
00316 
00317         bool m_bChunked;
00318 
00319         size_t m_nChunkLen;
00320 
00322         std::string m_sParseContentType;
00323 
00324         friend class EHSConnection;
00325         friend class EHS;
00326 };
00327 
00328 
00329 // GLOBAL HELPER FUNCTIONS
00330 
00338 std::string GetNextLine(std::string & buffer);
00339 
00345 RequestMethod GetRequestMethodFromString(const std::string & method);
00346 
00353 bool IsMultivalHeader(const std::string &header);
00354 
00361 bool MultivalHeaderContains(const std::string &header, const std::string &value);
00362 
00363 #endif // HTTPREQUEST_H
 All Classes Functions Variables Enumerations