From fea556ca1b8aeec975f5276d5d829ee6275841d9 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 3 Jan 2016 15:59:55 +0100 Subject: Renamed HTTPServer folder to HTTP. It contains client code as well. --- src/HTTP/HTTPMessage.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/HTTP/HTTPMessage.h (limited to 'src/HTTP/HTTPMessage.h') diff --git a/src/HTTP/HTTPMessage.h b/src/HTTP/HTTPMessage.h new file mode 100644 index 000000000..50b1f9e19 --- /dev/null +++ b/src/HTTP/HTTPMessage.h @@ -0,0 +1,84 @@ + +// HTTPMessage.h + +// Declares the cHTTPMessage class representing the common ancestor for HTTP request and response classes + + + + + +#pragma once + +#include "EnvelopeParser.h" + + + + + +class cHTTPMessage +{ +public: + enum eStatus + { + HTTP_OK = 200, + HTTP_BAD_REQUEST = 400, + } ; + + enum eKind + { + mkRequest, + mkResponse, + } ; + + cHTTPMessage(eKind a_Kind); + + // Force a virtual destructor in all descendants + virtual ~cHTTPMessage() {} + + /** Adds a header into the internal map of headers. Recognizes special headers: Content-Type and Content-Length */ + void AddHeader(const AString & a_Key, const AString & a_Value); + + void SetContentType (const AString & a_ContentType) { m_ContentType = a_ContentType; } + void SetContentLength(size_t a_ContentLength) { m_ContentLength = a_ContentLength; } + + const AString & GetContentType (void) const { return m_ContentType; } + size_t GetContentLength(void) const { return m_ContentLength; } + +protected: + typedef std::map cNameValueMap; + + eKind m_Kind; + + /** Map of headers, with their keys lowercased. */ + AStringMap m_Headers; + + /** Type of the content; parsed by AddHeader(), set directly by SetContentLength() */ + AString m_ContentType; + + /** Length of the content that is to be received. + AString::npos when the object is created. + Parsed by AddHeader() or set directly by SetContentLength() */ + size_t m_ContentLength; +} ; + + + + + +class cHTTPResponse : + public cHTTPMessage +{ + typedef cHTTPMessage super; + +public: + cHTTPResponse(void); + + /** Appends the response to the specified datastream - response line and headers. + The body will be sent later directly through cConnection::Send() + */ + void AppendToData(AString & a_DataStream) const; +} ; + + + + -- cgit v1.2.3 From 52d18b4559cbaca949f722aa6901a6eb5f505f02 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 20 Feb 2016 11:50:52 +0100 Subject: WebAdmin uses the new HTTP parser framework. --- src/HTTP/HTTPMessage.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'src/HTTP/HTTPMessage.h') diff --git a/src/HTTP/HTTPMessage.h b/src/HTTP/HTTPMessage.h index 50b1f9e19..683734b67 100644 --- a/src/HTTP/HTTPMessage.h +++ b/src/HTTP/HTTPMessage.h @@ -36,7 +36,7 @@ public: virtual ~cHTTPMessage() {} /** Adds a header into the internal map of headers. Recognizes special headers: Content-Type and Content-Length */ - void AddHeader(const AString & a_Key, const AString & a_Value); + virtual void AddHeader(const AString & a_Key, const AString & a_Value); void SetContentType (const AString & a_ContentType) { m_ContentType = a_ContentType; } void SetContentLength(size_t a_ContentLength) { m_ContentLength = a_ContentLength; } @@ -82,3 +82,77 @@ public: + +/** Provides storage for an incoming HTTP request. */ +class cHTTPIncomingRequest: + public cHTTPMessage +{ + typedef cHTTPMessage Super; +public: + /** Base class for anything that can be used as the UserData for the request. */ + class cUserData + { + public: + // Force a virtual destructor in descendants: + virtual ~cUserData() {} + }; + typedef SharedPtr cUserDataPtr; + + + /** Creates a new instance of the class, containing the method and URL provided by the client. */ + cHTTPIncomingRequest(const AString & a_Method, const AString & a_URL); + + /** Returns the method used in the request */ + const AString & GetMethod(void) const { return m_Method; } + + /** Returns the URL used in the request */ + const AString & GetURL(void) const { return m_URL; } + + /** Returns the path part of the URL. */ + AString GetURLPath(void) const; + + /** Returns true if the request has had the Auth header present. */ + bool HasAuth(void) const { return m_HasAuth; } + + /** Returns the username that the request presented. Only valid if HasAuth() is true */ + const AString & GetAuthUsername(void) const { return m_AuthUsername; } + + /** Returns the password that the request presented. Only valid if HasAuth() is true */ + const AString & GetAuthPassword(void) const { return m_AuthPassword; } + + bool DoesAllowKeepAlive(void) const { return m_AllowKeepAlive; } + + /** Attaches any kind of data to this request, to be later retrieved by GetUserData(). */ + void SetUserData(cUserDataPtr a_UserData) { m_UserData = a_UserData; } + + /** Returns the data attached to this request by the class client. */ + cUserDataPtr GetUserData(void) { return m_UserData; } + + /** Adds the specified header into the internal list of headers. + Overrides the parent to add recognizing additional headers: auth and keepalive. */ + virtual void AddHeader(const AString & a_Key, const AString & a_Value) override; + +protected: + + /** Method of the request (GET / PUT / POST / ...) */ + AString m_Method; + + /** Full URL of the request */ + AString m_URL; + + /** Set to true if the request contains auth data that was understood by the parser */ + bool m_HasAuth; + + /** The username used for auth */ + AString m_AuthUsername; + + /** The password used for auth */ + AString m_AuthPassword; + + /** Set to true if the request indicated that it supports keepalives. + If false, the server will close the connection once the request is finished */ + bool m_AllowKeepAlive; + + /** Any data attached to the request by the class client. */ + cUserDataPtr m_UserData; +}; -- cgit v1.2.3 From 71a1fa81f00cf897d91e8f76cc7e646dd61cc2ff Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 20 Feb 2016 12:33:27 +0100 Subject: Renamed HTTPResponse to HTTPOutgoingResponse. --- src/HTTP/HTTPMessage.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/HTTP/HTTPMessage.h') diff --git a/src/HTTP/HTTPMessage.h b/src/HTTP/HTTPMessage.h index 683734b67..9afcd5b97 100644 --- a/src/HTTP/HTTPMessage.h +++ b/src/HTTP/HTTPMessage.h @@ -65,17 +65,17 @@ protected: -class cHTTPResponse : +/** Stores outgoing response headers and serializes them to an HTTP data stream. */ +class cHTTPOutgoingResponse : public cHTTPMessage { typedef cHTTPMessage super; public: - cHTTPResponse(void); + cHTTPOutgoingResponse(void); /** Appends the response to the specified datastream - response line and headers. - The body will be sent later directly through cConnection::Send() - */ + The body will be sent later directly through cConnection::Send() */ void AppendToData(AString & a_DataStream) const; } ; -- cgit v1.2.3