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
|
|
|
|
|
|
|
|
#include "../OSSupport/SocketThreads.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
class cHTTPServer;
|
|
|
|
class cHTTPResponse;
|
|
|
|
class cHTTPRequest;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cHTTPConnection :
|
|
|
|
public cSocketThreads::cCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum eState
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
wcsRecvHeaders, ///< Receiving request headers (m_CurrentRequest is created if NULL)
|
2013-09-27 13:34:46 -04:00
|
|
|
wcsRecvBody, ///< Receiving request body (m_CurrentRequest is valid)
|
|
|
|
wcsRecvIdle, ///< Has received the entire body, waiting to send the response (m_CurrentRequest == NULL)
|
|
|
|
wcsSendingResp, ///< Sending response body (m_CurrentRequest == NULL)
|
|
|
|
wcsInvalid, ///< The request was malformed, the connection is closing
|
|
|
|
} ;
|
|
|
|
|
|
|
|
cHTTPConnection(cHTTPServer & a_HTTPServer);
|
2013-12-20 10:20:54 -05:00
|
|
|
virtual ~cHTTPConnection();
|
2013-09-27 13:34:46 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends HTTP status code together with a_Reason (used for HTTP errors) */
|
2013-09-27 13:34:46 -04:00
|
|
|
void SendStatusAndReason(int a_StatusCode, const AString & a_Reason);
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends the "401 unauthorized" reply together with instructions on authorizing, using the specified realm */
|
2013-10-04 14:28:30 -04:00
|
|
|
void SendNeedAuth(const AString & a_Realm);
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sends the headers contained in a_Response */
|
2013-09-27 13:34:46 -04:00
|
|
|
void Send(const cHTTPResponse & a_Response);
|
|
|
|
|
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()); }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Indicates that the current response is finished, gets ready for receiving another request (HTTP 1.1 keepalive) */
|
2013-09-27 13:34:46 -04:00
|
|
|
void FinishResponse(void);
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Resets the internal connection state for a new request.
|
|
|
|
Depending on the state, this will send an "InternalServerError" status or a "ResponseEnd" */
|
2013-09-27 15:28:41 -04:00
|
|
|
void AwaitNextRequest(void);
|
|
|
|
|
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);
|
|
|
|
|
2013-09-27 13:34:46 -04:00
|
|
|
protected:
|
|
|
|
typedef std::map<AString, AString> cNameValueMap;
|
|
|
|
|
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;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** All the incoming data until the entire request header is parsed */
|
2013-09-27 13:34:46 -04:00
|
|
|
AString m_IncomingHeaderData;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Status in which the request currently is */
|
2013-09-27 13:34:46 -04:00
|
|
|
eState m_State;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Data that is queued for sending, once the socket becomes writable */
|
2013-09-27 13:34:46 -04:00
|
|
|
AString m_OutgoingData;
|
|
|
|
|
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. */
|
2013-09-27 13:34:46 -04:00
|
|
|
cHTTPRequest * m_CurrentRequest;
|
2013-09-27 14:33:18 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Number of bytes that remain to read for the complete body of the message to be received.
|
|
|
|
Valid only in wcsRecvBody */
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t m_CurrentRequestBodyRemaining;
|
2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
// cSocketThreads::cCallback overrides:
|
2014-04-02 09:36:25 -04:00
|
|
|
virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client
|
2013-09-27 13:34:46 -04:00
|
|
|
virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client
|
|
|
|
virtual void SocketClosed (void) override; // The socket has been closed for any reason
|
|
|
|
} ;
|
|
|
|
|
|
|
|
typedef std::vector<cHTTPConnection *> cHTTPConnections;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|