EHS Embedded HTTP Server  1.5.0.132
ehs.h
00001 /* $Id: ehs.h 121 2012-04-05 22:39:22Z 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 EHS_H
00027 #define EHS_H
00028 
00029 // Pragma'ing away nasty MS 255-char-name problem.  Otherwise
00030 // you will get warnings on many template names that
00031 //  "identifier was truncated to '255' characters in the debug information".
00032 #ifdef _MSC_VER
00033 # pragma warning(disable : 4786)
00034 #endif
00035 
00036 // EHS headers
00037 
00038 #include <formvalue.h>
00039 #include <ehstypes.h>
00040 #include <datum.h>
00041 #include <httpresponse.h>
00042 #include <httprequest.h>
00043 
00044 #include <memory>
00045 #include <string>
00046 
00047 extern "C" {
00048     const char * getEHSconfig();
00049 }
00050 
00061 class RawSocketHandler {
00062     public:
00070         virtual bool OnData(EHSConnection *conn, std::string data) = 0;
00071 
00077         virtual void OnConnect(EHSConnection *conn) = 0;
00078 
00084         virtual void OnDisconnect(EHSConnection *conn) = 0;
00085 
00086         virtual ~RawSocketHandler ( ) { }
00087 };
00088 
00099 class PrivilegedBindHelper {
00100 
00101     public:
00102 
00110         virtual bool BindPrivilegedPort(int socket, const char *addr, const unsigned short port) = 0;
00111 
00112         virtual ~PrivilegedBindHelper ( ) { }
00113 
00114 };
00115 
00123 class PassphraseHandler {
00124 
00125     public:
00126 
00133         virtual const std::string GetPassphrase(bool twice) = 0;
00134 
00136         virtual ~PassphraseHandler ( ) { }
00137 };
00138 
00140 #define MAX_REQUEST_SIZE_DEFAULT (256 * 1024)
00141 
00147 class EHS : public PassphraseHandler {
00148 
00149     private:
00153         EHS(const EHS &);
00154 
00158         EHS & operator=(const EHS &);
00159 
00160     protected:
00161 
00163         EHSMap m_oEHSMap;
00164 
00166         EHS *m_poParent; 
00167 
00169         std::string m_sRegisteredAs;
00170 
00172         EHSServer *m_poEHSServer;
00173 
00175         EHS *m_poSourceEHS;
00176 
00178         PrivilegedBindHelper *m_poBindHelper;
00179 
00181         RawSocketHandler *m_poRawSocketHandler;
00182 
00184         bool m_bNoRouting;
00185 
00186     public:
00187 
00193         EHS(EHS *parent = NULL, std::string registerpath = "");
00194 
00198         virtual ~EHS ();
00199 
00207         virtual const std::string GetPassphrase(bool twice);
00208 
00216         void RegisterEHS(EHS *child, const char *uripath);
00217 
00223         void UnregisterEHS (const char *uripath);
00224 
00230         ehs_autoptr<HttpResponse> RouteRequest(HttpRequest *request);
00231 
00241         virtual ResponseCode HandleRequest(HttpRequest *request, HttpResponse *response);
00242 
00249         void SetSourceEHS (EHS & source);
00250 
00252         EHSServerParameters m_oParams;
00253 
00260         void StartServer(EHSServerParameters &params);
00261 
00267         void StopServer();
00268 
00272         void HandleData(int timeout = 0);
00273 
00282         virtual bool ThreadInitHandler();
00283 
00291         virtual void ThreadExitHandler();
00292 
00307         virtual HttpResponse *HandleThreadException(ehs_threadid_t tid, HttpRequest *request, std::exception & ex);
00308 
00313         bool ShouldTerminate() const;
00314 
00322         void SetBindHelper(PrivilegedBindHelper *helper)
00323         {
00324             m_poBindHelper = helper;
00325         }
00326 
00331         PrivilegedBindHelper * GetBindHelper() const
00332         {
00333             return m_poBindHelper;
00334         }
00335 
00340         void SetRawSocketHandler(RawSocketHandler *helper)
00341         {
00342             m_poRawSocketHandler = helper;
00343         }
00344 
00349         RawSocketHandler * GetRawSocketHandler() const
00350         {
00351             return m_poRawSocketHandler;
00352         }
00353 
00358         void AddResponse(ehs_autoptr<GenericResponse> ehs_rvref response);
00359 };
00360 
00361 #endif // EHS_H