EHS Embedded HTTP Server  1.5.1.0
ehs.h
1 /* $Id: ehs.h 121 2012-04-05 22:39:22Z 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 EHS_H
27 #define EHS_H
28 
29 // Pragma'ing away nasty MS 255-char-name problem. Otherwise
30 // you will get warnings on many template names that
31 // "identifier was truncated to '255' characters in the debug information".
32 #ifdef _MSC_VER
33 # pragma warning(disable : 4786)
34 #endif
35 
36 // EHS headers
37 
38 #include <formvalue.h>
39 #include <ehstypes.h>
40 #include <datum.h>
41 #include <httpresponse.h>
42 #include <httprequest.h>
43 
44 #include <memory>
45 #include <string>
46 
47 extern "C" {
48  const char * getEHSconfig();
49 }
50 
62  public:
70  virtual bool OnData(EHSConnection *conn, std::string data) = 0;
71 
77  virtual void OnConnect(EHSConnection *conn) = 0;
78 
84  virtual void OnDisconnect(EHSConnection *conn) = 0;
85 
86  virtual ~RawSocketHandler ( ) { }
87 };
88 
100 
101  public:
102 
110  virtual bool BindPrivilegedPort(int socket, const char *addr, const unsigned short port) = 0;
111 
112  virtual ~PrivilegedBindHelper ( ) { }
113 
114 };
115 
124 
125  public:
126 
133  virtual const std::string GetPassphrase(bool twice) = 0;
134 
136  virtual ~PassphraseHandler ( ) { }
137 };
138 
140 #define MAX_REQUEST_SIZE_DEFAULT (256 * 1024)
141 
147 class EHS : public PassphraseHandler {
148 
149  private:
153  EHS(const EHS &);
154 
158  EHS & operator=(const EHS &);
159 
160  protected:
161 
163  EHSMap m_oEHSMap;
164 
167 
169  std::string m_sRegisteredAs;
170 
173 
176 
179 
182 
185 
186  public:
187 
193  EHS(EHS *parent = NULL, std::string registerpath = "");
194 
198  virtual ~EHS ();
199 
207  virtual const std::string GetPassphrase(bool twice);
208 
216  void RegisterEHS(EHS *child, const char *uripath);
217 
223  void UnregisterEHS (const char *uripath);
224 
230  ehs_autoptr<HttpResponse> RouteRequest(HttpRequest *request);
231 
241  virtual ResponseCode HandleRequest(HttpRequest *request, HttpResponse *response);
242 
249  void SetSourceEHS (EHS & source);
250 
252  EHSServerParameters m_oParams;
253 
260  void StartServer(EHSServerParameters &params);
261 
267  void StopServer();
268 
272  void HandleData(int timeout = 0);
273 
282  virtual bool ThreadInitHandler();
283 
291  virtual void ThreadExitHandler();
292 
307  virtual HttpResponse *HandleThreadException(ehs_threadid_t tid, HttpRequest *request, std::exception & ex);
308 
313  bool ShouldTerminate() const;
314 
323  {
324  m_poBindHelper = helper;
325  }
326 
332  {
333  return m_poBindHelper;
334  }
335 
341  {
342  m_poRawSocketHandler = helper;
343  }
344 
350  {
351  return m_poRawSocketHandler;
352  }
353 
358  void AddResponse(ehs_autoptr<GenericResponse> ehs_rvref response);
359 };
360 
361 #endif // EHS_H
EHSServer contains all the network related services for EHS.
Definition: ehsserver.h:34
EHS provides HTTP server functionality to a child class.
Definition: ehs.h:147
EHSConnection abstracts the concept of a connection to an EHS application.
Definition: ehsconnection.h:40
EHSServer * m_poEHSServer
EHSServer object associated with this EHS object.
Definition: ehs.h:172
This interface describes a handler for retrieving passphrases.
Definition: ehs.h:123
void SetRawSocketHandler(RawSocketHandler *helper)
Sets a RawSocketHandler for use by the network abstraction layer.
Definition: ehs.h:340
PrivilegedBindHelper * GetBindHelper() const
Retieves our PrivilegedBindHelper.
Definition: ehs.h:331
RawSocketHandler * GetRawSocketHandler() const
Retrieves our RawSocketHandler.
Definition: ehs.h:349
EHS * m_poParent
Points to the EHS object this object was registered with, NULL if top level.
Definition: ehs.h:166
Helper class for performing raw socket IO.
Definition: ehs.h:61
EHSServerParameters m_oParams
Stores a map with server parameters.
Definition: ehs.h:252
void SetBindHelper(PrivilegedBindHelper *helper)
Sets a PrivilegedBindHelper for use by the network abstraction layer.
Definition: ehs.h:322
RawSocketHandler * m_poRawSocketHandler
Our RawSocketHandler.
Definition: ehs.h:181
PrivilegedBindHelper * m_poBindHelper
Our bind helper.
Definition: ehs.h:178
EHS * m_poSourceEHS
Source EHS object to route requests to for data instead of processing it ourselves.
Definition: ehs.h:175
virtual bool OnData(EHSConnection *conn, std::string data)=0
Handle raw data.
bool m_bNoRouting
Flag: We don&#39;t do request routing.
Definition: ehs.h:184
This class represents a clients HTTP request.
Definition: httprequest.h:51
virtual ~PassphraseHandler()
Destructor.
Definition: ehs.h:136
virtual void OnConnect(EHSConnection *conn)=0
Handle connect event.
EHSMap m_oEHSMap
Stores path => EHSConnection pairs for path/tree traversal.
Definition: ehs.h:163
virtual void OnDisconnect(EHSConnection *conn)=0
Handle disconnect event.
Helper class for binding of sockets to privileged ports.
Definition: ehs.h:99
std::string m_sRegisteredAs
The string that this EHS object is regestered as.
Definition: ehs.h:169
This class represents what is sent back to the client.
Definition: httpresponse.h:54