EHS Embedded HTTP Server  1.5.0.132
httprequest.h
00001 /* $Id: httprequest.h 131 2012-04-13 10:33:27Z 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 
00060         HttpRequest (int inRequestId, EHSConnection *ipoSourceEHSConnection);
00061 
00062     public:
00063 
00065         virtual ~HttpRequest ( );
00066 
00071         std::string RemoteAddress();
00072 
00077         int RemotePort();
00078 
00083         std::string LocalAddress();
00084 
00089         int LocalPort();
00090 
00091         DEPRECATED("Use RemoteAddress()")
00097             std::string Address() { return RemoteAddress(); }
00098 
00099         DEPRECATED("Use RemotePort()")
00105             int Port() { return RemotePort(); }
00106 
00111         int Id() const { return m_nRequestId; }
00112 
00117         EHSConnection *Connection() const { return m_poSourceEHSConnection; }
00118 
00123         RequestMethod Method() const { return m_nRequestMethod; }
00124 
00129         bool Secure() const { return m_bSecure; }
00130 
00135         bool ClientDisconnected();
00136 
00141         const std::string &Uri() const { return m_sUri; }
00142 
00147         const std::string &HttpVersion() const { return m_sHttpVersionNumber; }
00148 
00153         const std::string &Body() const { return m_sBody; }
00154 
00159         StringCaseMap &Headers() { return m_oRequestHeaders; }
00160 
00165         FormValueMap &FormValues() { return m_oFormValueMap; }
00166 
00171         CookieMap &Cookies() { return m_oCookieMap; }
00172 
00178         FormValue &FormValues(const std::string & name)
00179         {
00180             return m_oFormValueMap[name];
00181         }
00182 
00188         std::string Headers(const std::string & name)
00189         {
00190             if (m_oRequestHeaders.find(name) != m_oRequestHeaders.end()) {
00191                 return m_oRequestHeaders[name];
00192             }
00193             return std::string();
00194         }
00195 
00203         void SetHeader(const std::string & name, const std::string & value)
00204         {
00205             m_oRequestHeaders[name] = value;
00206         }
00207 
00213         std::string Cookies(const std::string & name)
00214         {
00215             if (m_oCookieMap.find(name) != m_oCookieMap.end()) {
00216                 return m_oCookieMap[name];
00217             }
00218             return std::string();
00219         }
00220 
00221     private:
00222 
00224         HttpRequest(const HttpRequest &);
00225 
00227         HttpRequest & operator=(const HttpRequest &);
00228 
00230         void GetFormDataFromString(const std::string &irsString);
00231 
00233         enum HttpParseStates {
00234             HTTPPARSESTATE_INVALID = 0,
00235             HTTPPARSESTATE_REQUEST,
00236             HTTPPARSESTATE_HEADERS,
00237             HTTPPARSESTATE_BODY,
00238             HTTPPARSESTATE_BODYCHUNK,
00239             HTTPPARSESTATE_BODYTRAILER,
00240             HTTPPARSESTATE_BODYTRAILER2,
00241             HTTPPARSESTATE_COMPLETEREQUEST,
00242             HTTPPARSESTATE_INVALIDREQUEST
00243         };
00244 
00246         enum ParseMultipartFormDataResult { 
00247             PARSEMULTIPARTFORMDATA_INVALID = 0,
00248             PARSEMULTIPARTFORMDATA_SUCCESS,
00249             PARSEMULTIPARTFORMDATA_FAILED 
00250         };
00251 
00253         enum ParseSubbodyResult {
00254             PARSESUBBODY_INVALID = 0,
00255             PARSESUBBODY_SUCCESS,
00256             PARSESUBBODY_INVALIDSUBBODY, // no blank line?
00257             PARSESUBBODY_FAILED // other reason
00258         };
00259 
00261         ParseMultipartFormDataResult ParseMultipartFormData();
00262 
00268         ParseSubbodyResult ParseSubbody(std::string sSubBody);
00269 
00271         HttpParseStates ParseData(std::string & irsData);
00272 
00274         int ParseCookieData (std::string & irsData);
00275 
00277         HttpParseStates m_nCurrentHttpParseState;
00278 
00280         RequestMethod m_nRequestMethod;
00281 
00283         std::string m_sUri;
00284 
00286         std::string m_sOriginalUri;
00287 
00289         std::string m_sHttpVersionNumber;
00290 
00292         std::string m_sBody; 
00293 
00295         std::string m_sLastHeaderName;
00296 
00298         bool m_bSecure;
00299 
00301         StringCaseMap m_oRequestHeaders;
00302 
00304         FormValueMap m_oFormValueMap;
00305 
00307         CookieMap m_oCookieMap;
00308 
00310         int m_nRequestId;
00311 
00313         EHSConnection * m_poSourceEHSConnection;
00314 
00315         bool m_bChunked;
00316 
00317         size_t m_nChunkLen;
00318 
00319         friend class EHSConnection;
00320         friend class EHS;
00321 };
00322 
00323 
00324 // GLOBAL HELPER FUNCTIONS
00325 
00333 std::string GetNextLine(std::string & buffer);
00334 
00340 RequestMethod GetRequestMethodFromString(const std::string & method);
00341 
00348 bool IsMultivalHeader(const std::string &header);
00349 
00356 bool MultivalHeaderContains(const std::string &header, const std::string &value);
00357 
00358 #endif // HTTPREQUEST_H