2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
// HTTPConnection.h
|
|
|
|
|
|
|
|
// Declares the cHTTPConnection class representing a single persistent connection in the HTTP server.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
#include "../OSSupport/Network.h"
|
2016-02-20 05:50:52 -05:00
|
|
|
#include "HTTPMessageParser.h"
|
2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
class cHTTPServer;
|
2016-02-20 06:33:27 -05:00
|
|
|
class cHTTPOutgoingResponse;
|
2016-02-20 05:50:52 -05:00
|
|
|
class cHTTPIncomingRequest;
|
2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-25 13:42:50 -05:00
|
|
|
class cHTTPServerConnection :
|
2016-02-20 05:50:52 -05:00
|
|
|
public cTCPLink::cCallbacks,
|
|
|
|
public cHTTPMessageParser::cCallbacks
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
|
|
|
public:
|
2016-02-20 05:50:52 -05:00
|
|
|
/** Creates a new instance, connected to the specified HTTP server instance */
|
2015-12-25 13:42:50 -05:00
|
|
|
cHTTPServerConnection(cHTTPServer & a_HTTPServer);
|
2016-02-20 05:50:52 -05:00
|
|
|
|
|
|
|
// Force a virtual destructor in all descendants
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cHTTPServerConnection() override;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-04-19 11:32:44 -04:00
|
|
|
/** Sends HTTP status code together with a_Reason (used for HTTP errors).
|
2016-02-20 05:50:52 -05:00
|
|
|
Sends the a_Reason as the body as well, so that browsers display it.
|
|
|
|
Clears the current request (since it's finished by this call). */
|
2013-09-27 13:34:46 -04:00
|
|
|
void SendStatusAndReason(int a_StatusCode, const AString & a_Reason);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
/** Sends the "401 unauthorized" reply together with instructions on authorizing, using the specified realm.
|
|
|
|
Clears the current request (since it's finished by this call). */
|
2013-10-04 14:28:30 -04:00
|
|
|
void SendNeedAuth(const AString & a_Realm);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends the headers contained in a_Response */
|
2016-02-20 06:33:27 -05:00
|
|
|
void Send(const cHTTPOutgoingResponse & a_Response);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends the data as the response (may be called multiple times) */
|
2014-04-01 10:36:00 -04:00
|
|
|
void Send(const void * a_Data, size_t a_Size);
|
2013-09-27 13:34:46 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends the data as the response (may be called multiple times) */
|
2013-09-27 13:34:46 -04:00
|
|
|
void Send(const AString & a_Data) { Send(a_Data.data(), a_Data.size()); }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
/** Indicates that the current response is finished, gets ready for receiving another request (HTTP 1.1 keepalive).
|
|
|
|
Clears the current request (since it's finished by this call). */
|
2013-09-27 13:34:46 -04:00
|
|
|
void FinishResponse(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Terminates the connection; finishes any request being currently processed */
|
2013-10-06 10:40:28 -04:00
|
|
|
void Terminate(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 13:34:46 -04:00
|
|
|
protected:
|
|
|
|
typedef std::map<AString, AString> cNameValueMap;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** The parent webserver that is to be notified of events on this connection */
|
2013-09-27 13:34:46 -04:00
|
|
|
cHTTPServer & m_HTTPServer;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
/** The parser responsible for reading the requests. */
|
|
|
|
cHTTPMessageParser m_Parser;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** The request being currently received
|
|
|
|
Valid only between having parsed the headers and finishing receiving the body. */
|
2016-02-20 05:50:52 -05:00
|
|
|
std::unique_ptr<cHTTPIncomingRequest> m_CurrentRequest;
|
2015-01-24 04:11:34 -05:00
|
|
|
|
|
|
|
/** The network link attached to this connection. */
|
|
|
|
cTCPLinkPtr m_Link;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
// cTCPLink::cCallbacks overrides:
|
|
|
|
/** The link instance has been created, remember it. */
|
|
|
|
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override;
|
|
|
|
|
|
|
|
/** Data is received from the client. */
|
|
|
|
virtual void OnReceivedData(const char * a_Data, size_t a_Size) override;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
/** The socket has been closed for any reason. */
|
|
|
|
virtual void OnRemoteClosed(void) override;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
/** An error has occurred on the socket. */
|
|
|
|
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override;
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
// cHTTPMessageParser::cCallbacks overrides:
|
|
|
|
virtual void OnError(const AString & a_ErrorDescription) override;
|
|
|
|
virtual void OnFirstLine(const AString & a_FirstLine) override;
|
|
|
|
virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
|
|
|
|
virtual void OnHeadersFinished(void) override;
|
|
|
|
virtual void OnBodyData(const void * a_Data, size_t a_Size) override;
|
|
|
|
virtual void OnBodyFinished(void) override;
|
|
|
|
|
2015-01-24 04:11:34 -05:00
|
|
|
// Overridable:
|
|
|
|
/** Called to send raw data over the link. Descendants may provide data transformations (SSL etc.) */
|
|
|
|
virtual void SendData(const void * a_Data, size_t a_Size);
|
|
|
|
|
|
|
|
/** Sends the raw data over the link.
|
|
|
|
Descendants may provide data transformations (SSL etc.) via the overridable SendData() function. */
|
|
|
|
void SendData(const AString & a_Data)
|
|
|
|
{
|
|
|
|
SendData(a_Data.data(), a_Data.size());
|
|
|
|
}
|
2013-09-27 13:34:46 -04:00
|
|
|
} ;
|
|
|
|
|
2015-12-25 13:42:50 -05:00
|
|
|
typedef std::vector<cHTTPServerConnection *> cHTTPServerConnections;
|
2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|