From d0b9e817956a57389f17a3d8e00df51cbe8cc309 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 27 Sep 2013 19:34:46 +0200 Subject: Split cHTTPConnection implementation into a separate file. --- source/HTTPServer/HTTPConnection.h | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 source/HTTPServer/HTTPConnection.h (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h new file mode 100644 index 000000000..e2df5de46 --- /dev/null +++ b/source/HTTPServer/HTTPConnection.h @@ -0,0 +1,88 @@ + +// 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 + { + wcsRecvHeaders, ///< Receiving request headers (m_CurrentRequest == NULL) + 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); + + /// Sends HTTP status code together with a_Reason (used for HTTP errors) + void SendStatusAndReason(int a_StatusCode, const AString & a_Reason); + + /// Sends the headers contained in a_Response + void Send(const cHTTPResponse & a_Response); + + /// Sends the data as the response (may be called multiple times) + void Send(const void * a_Data, int a_Size); + + /// Sends the data as the response (may be called multiple times) + void Send(const AString & a_Data) { Send(a_Data.data(), a_Data.size()); } + + /// Finishes sending current response, gets ready for receiving another request (HTTP 1.1 keepalive) + void FinishResponse(void); + +protected: + typedef std::map cNameValueMap; + + /// The parent webserver that is to be notified of events on this connection + cHTTPServer & m_HTTPServer; + + /// All the incoming data until the entire request header is parsed + AString m_IncomingHeaderData; + + /// Status in which the request currently is + eState m_State; + + /// Data that is queued for sending, once the socket becomes writable + AString m_OutgoingData; + + /// The request being currently received (valid only between having parsed the headers and finishing receiving the body) + cHTTPRequest * m_CurrentRequest; + + + // cSocketThreads::cCallback overrides: + virtual void DataReceived (const char * a_Data, int a_Size) override; // Data is received from the client + 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 cHTTPConnections; + + + + + -- cgit v1.2.3 From 0c3fd5e77d681c25757efaab6acb305d0b5630c1 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 27 Sep 2013 20:33:18 +0200 Subject: Fixed parsing and implemented write nofitication. The web connection finally works with a browser. --- source/HTTPServer/HTTPConnection.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index e2df5de46..d8ecdf1d9 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -72,6 +72,9 @@ protected: /// The request being currently received (valid only between having parsed the headers and finishing receiving the body) cHTTPRequest * m_CurrentRequest; + + /// Number of bytes that remain to read for the complete body of the message to be received. Valid only in wcsRecvBody + int m_CurrentRequestBodyRemaining; // cSocketThreads::cCallback overrides: -- cgit v1.2.3 From 5cf8fc12ae000cd2d2b54a2bf158f82bdb8a0e67 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 27 Sep 2013 21:28:41 +0200 Subject: Added cHTTPServer callbacks; fixed keep-alives. The HTTP server now calls callbacks specified in its start function (debugified atm.) and it processes multiple requests on a single connection. --- source/HTTPServer/HTTPConnection.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index d8ecdf1d9..46c36a8a2 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -52,9 +52,12 @@ public: /// Sends the data as the response (may be called multiple times) void Send(const AString & a_Data) { Send(a_Data.data(), a_Data.size()); } - /// Finishes sending current response, gets ready for receiving another request (HTTP 1.1 keepalive) + /// Indicates that the current response is finished, gets ready for receiving another request (HTTP 1.1 keepalive) void FinishResponse(void); + /// Resets the connection for a new request. Depending on the state, this will send an "InternalServerError" status or a "ResponseEnd" + void AwaitNextRequest(void); + protected: typedef std::map cNameValueMap; -- cgit v1.2.3 From 1012fd82fda9e9bc75d2308a3c68cb3b3738bf1b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 4 Oct 2013 13:07:57 +0200 Subject: HTTP Server can now parse multipart/form-data forms; better architecture. --- source/HTTPServer/HTTPConnection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index 46c36a8a2..9e05d342b 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -31,7 +31,7 @@ public: enum eState { - wcsRecvHeaders, ///< Receiving request headers (m_CurrentRequest == NULL) + wcsRecvHeaders, ///< Receiving request headers (m_CurrentRequest is created if NULL) 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) -- cgit v1.2.3 From db3d83b38dd61b90466a0721fa9104e742f3fb8b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 4 Oct 2013 20:28:30 +0200 Subject: Added Basic auth support to cHTTPRequest. --- source/HTTPServer/HTTPConnection.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index 9e05d342b..941a29000 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -43,6 +43,9 @@ public: /// Sends HTTP status code together with a_Reason (used for HTTP errors) void SendStatusAndReason(int a_StatusCode, const AString & a_Reason); + /// Sends the "401 unauthorized" reply together with instructions on authorizing, using the specified realm + void SendNeedAuth(const AString & a_Realm); + /// Sends the headers contained in a_Response void Send(const cHTTPResponse & a_Response); -- cgit v1.2.3 From f55b77a98a41ba784109842cde39ba0e1d2bc262 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 6 Oct 2013 16:40:28 +0200 Subject: Fixed memory leaks in the HTTP framework --- source/HTTPServer/HTTPConnection.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source/HTTPServer/HTTPConnection.h') diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index 941a29000..14603bb70 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -39,6 +39,7 @@ public: } ; cHTTPConnection(cHTTPServer & a_HTTPServer); + ~cHTTPConnection(); /// Sends HTTP status code together with a_Reason (used for HTTP errors) void SendStatusAndReason(int a_StatusCode, const AString & a_Reason); @@ -61,6 +62,9 @@ public: /// Resets the connection for a new request. Depending on the state, this will send an "InternalServerError" status or a "ResponseEnd" void AwaitNextRequest(void); + /// Terminates the connection; finishes any request being currently processed + void Terminate(void); + protected: typedef std::map cNameValueMap; -- cgit v1.2.3