2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
// HTTPServer.h
|
|
|
|
|
|
|
|
// Declares the cHTTPServer class representing a HTTP webserver that uses cListenThread and cSocketThreads for processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../OSSupport/ListenThread.h"
|
|
|
|
#include "../OSSupport/SocketThreads.h"
|
2014-10-23 09:15:10 -04:00
|
|
|
#include "../IniFile.h"
|
2014-05-01 05:48:03 -04:00
|
|
|
#include "PolarSSL++/RsaPrivateKey.h"
|
2014-05-01 09:21:41 -04:00
|
|
|
#include "PolarSSL++/CryptoKey.h"
|
2014-05-01 05:48:03 -04:00
|
|
|
#include "PolarSSL++/X509Cert.h"
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
class cHTTPMessage;
|
|
|
|
class cHTTPRequest;
|
|
|
|
class cHTTPResponse;
|
2013-09-27 13:34:46 -04:00
|
|
|
class cHTTPConnection;
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2013-09-27 13:34:46 -04:00
|
|
|
typedef std::vector<cHTTPConnection *> cHTTPConnections;
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cHTTPServer :
|
|
|
|
public cListenThread::cCallback
|
|
|
|
{
|
|
|
|
public:
|
2013-09-27 15:28:41 -04:00
|
|
|
class cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
2014-03-28 16:35:45 -04:00
|
|
|
virtual ~cCallbacks() {}
|
|
|
|
|
2013-09-27 15:28:41 -04:00
|
|
|
/** Called when a new request arrives over a connection and its headers have been parsed.
|
|
|
|
The request body needn't have arrived yet.
|
|
|
|
*/
|
|
|
|
virtual void OnRequestBegun(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) = 0;
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
/** Called when another part of request body has arrived.
|
|
|
|
May be called multiple times for a single request. */
|
|
|
|
virtual void OnRequestBody(cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) = 0;
|
2013-09-27 15:28:41 -04:00
|
|
|
|
|
|
|
/// Called when the request body has been fully received in previous calls to OnRequestBody()
|
|
|
|
virtual void OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & a_Request) = 0;
|
|
|
|
} ;
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
cHTTPServer(void);
|
2014-03-28 16:35:45 -04:00
|
|
|
virtual ~cHTTPServer();
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
/// Initializes the server on the specified ports
|
|
|
|
bool Initialize(const AString & a_PortsIPv4, const AString & a_PortsIPv6);
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2013-09-27 15:28:41 -04:00
|
|
|
/// Starts the server and assigns the callbacks to use for incoming requests
|
|
|
|
bool Start(cCallbacks & a_Callbacks);
|
|
|
|
|
|
|
|
/// Stops the server, drops all current connections
|
|
|
|
void Stop(void);
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
protected:
|
|
|
|
friend class cHTTPConnection;
|
2014-05-01 05:48:03 -04:00
|
|
|
friend class cSslHTTPConnection;
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
cListenThread m_ListenThreadIPv4;
|
|
|
|
cListenThread m_ListenThreadIPv6;
|
|
|
|
|
|
|
|
cSocketThreads m_SocketThreads;
|
|
|
|
|
|
|
|
cCriticalSection m_CSConnections;
|
|
|
|
cHTTPConnections m_Connections; ///< All the connections that are currently being serviced
|
2013-09-27 15:28:41 -04:00
|
|
|
|
|
|
|
/// The callbacks to call for various events
|
|
|
|
cCallbacks * m_Callbacks;
|
2014-05-01 05:48:03 -04:00
|
|
|
|
|
|
|
/** The server certificate to use for the SSL connections */
|
|
|
|
cX509CertPtr m_Cert;
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
/** The private key for m_Cert. */
|
|
|
|
cCryptoKeyPtr m_CertPrivKey;
|
2013-09-27 15:28:41 -04:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
// cListenThread::cCallback overrides:
|
|
|
|
virtual void OnConnectionAccepted(cSocket & a_Socket) override;
|
|
|
|
|
|
|
|
/// Called by cHTTPConnection to close the connection (presumably due to an error)
|
|
|
|
void CloseConnection(cHTTPConnection & a_Connection);
|
|
|
|
|
2013-09-27 14:33:18 -04:00
|
|
|
/// Called by cHTTPConnection to notify SocketThreads that there's data to be sent for the connection
|
|
|
|
void NotifyConnectionWrite(cHTTPConnection & a_Connection);
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
/// Called by cHTTPConnection when it finishes parsing the request header
|
|
|
|
void NewRequest(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
/** Called by cHTTPConenction when it receives more data for the request body.
|
|
|
|
May be called multiple times for a single request. */
|
|
|
|
void RequestBody(cHTTPConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size);
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
/// Called by cHTTPConnection when it detects that the request has finished (all of its body has been received)
|
|
|
|
void RequestFinished(cHTTPConnection & a_Connection, cHTTPRequest & a_Request);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|