EHS Embedded HTTP Server  1.5.1.173
ehsconnection.h
1 /* $Id: ehsconnection.h 166 2018-09-11 13:29:27Z 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 _EHSCONNECTION_H_
27 #define _EHSCONNECTION_H_
28 
29 #include <ehstypes.h>
30 #include <ctime>
31 
32 class EHSServer;
33 class NetworkAbstraction;
34 
41 
42  private:
43 
44  EHSConnection ( const EHSConnection & );
45 
46  EHSConnection & operator = ( const EHSConnection & );
47 
48  private:
49 
50  bool m_bRawMode;
51 
52  bool m_bDoneReading;
53 
54  bool m_bDisconnected;
55 
56  bool m_bIdleHandling;
57 
58  HttpRequest * m_poCurrentHttpRequest;
59 
60  EHSServer * m_poEHSServer;
61 
62  time_t m_nLastActivity;
63 
64  int m_nRequests;
65 
66  int m_nResponses;
67 
68  int m_nActiveRequests;
69 
71  NetworkAbstraction * m_poNetworkAbstraction;
72 
74  std::string m_sBuffer;
75 
77  ResponseQueue m_oResponseQueue;
78 
80  HttpRequestList m_oHttpRequestList;
81 
83  std::string m_sRemoteAddress;
84 
86  std::string m_sLocalAddress;
87 
89  int m_nRemotePort;
90 
92  int m_nLocalPort;
93 
94  size_t m_nMaxRequestSize;
95 
97  std::string m_sParseContentType;
98 
99  pthread_mutex_t m_oMutex;
100 
101  public:
102 
104  std::string GetRemoteAddress() const { return m_sRemoteAddress; }
105 
107  int GetRemotePort() const { return m_nRemotePort; }
108 
110  std::string GetLocalAddress() const { return m_sLocalAddress; }
111 
113  int GetLocalPort() const { return m_nLocalPort; }
114 
115  DEPRECATED("Use GetRemoteAddress()")
120  std::string GetAddress() const { return GetRemoteAddress(); }
121 
122  DEPRECATED("Use GetRemotePort()")
127  int GetPort() const { return GetRemotePort(); }
128 
130  bool Disconnected() const { return m_bDisconnected; }
131 
133  bool IsRaw() const { return m_bRawMode; }
134 
136  void AddResponse(ehs_autoptr<GenericResponse> ehs_rvref response);
137 
144  void EnableIdleTimeout(bool enable = true) { m_bIdleHandling = enable; }
145 
153  void EnableKeepAlive(bool enable = true);
154 
155  private:
156 
158  EHSConnection(NetworkAbstraction * ipoNetworkAbstraction,
159  EHSServer * ipoEHSServer);
160 
162  ~EHSConnection();
163 
165  void UpdateLastActivity() { m_nLastActivity = time(NULL); }
166 
168  time_t LastActivity() { return m_bIdleHandling ? m_nLastActivity : time(NULL); }
169 
171  bool StillReading() { return !m_bDoneReading; }
172 
175  void DoneReading ( bool ibDisconnected );
176 
178  HttpRequest *GetNextRequest();
179 
181  int CheckDone();
182 
184  enum AddBufferResult {
185  ADDBUFFER_INVALID = 0,
186  ADDBUFFER_OK,
187  ADDBUFFER_INVALIDREQUEST,
188  ADDBUFFER_TOOBIG,
189  ADDBUFFER_NORESOURCE
190  };
191 
193  AddBufferResult AddBuffer(char * ipsData, int inSize);
194 
201  void SendResponse(GenericResponse *response);
202 
204  int RequestsPending() { return (0 != m_nActiveRequests) || !m_oHttpRequestList.empty(); }
205 
207  NetworkAbstraction * GetNetworkAbstraction();
208 
210  void SetMaxRequestSize(size_t n) { m_nMaxRequestSize = n; }
211 
213  void SetParseContentType(const std::string & s) { m_sParseContentType = s; }
214 
215  friend class EHSServer;
216 };
217 
218 #endif // _EHSCONNECTION_H_
EHSServer contains all the network related services for EHS.
Definition: ehsserver.h:34
void EnableKeepAlive(bool enable=true)
Enable/Disable TCP keepalive on the underlying socket.
EHSConnection abstracts the concept of a connection to an EHS application.
Definition: ehsconnection.h:40
std::string GetLocalAddress() const
returns the local address of the connection.
int GetLocalPort() const
returns the local port of the connection.
void AddResponse(ehs_autoptr< GenericResponse > ehs_rvref response)
adds a response to the response list and sends as many responses as are ready
std::string GetRemoteAddress() const
returns the remote address of the connection.
This class represents what is sent back to the client.
Definition: ehstypes.h:86
This class represents a clients HTTP request.
Definition: httprequest.h:51
bool Disconnected() const
returns whether the client has disconnected from us.
bool IsRaw() const
returns whether the this connection is in raw mode.
int GetRemotePort() const
returns the remote port of the connection.
std::string GetAddress() const
returns address of the connection.
int GetPort() const
returns client port of the connection.
void EnableIdleTimeout(bool enable=true)
Enable/Disable idle-timeout handling for this connection.
Abstracts different socket types.