2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
// HTTPMessage.h
|
|
|
|
|
|
|
|
// Declares the cHTTPMessage class representing the common ancestor for HTTP request and response classes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
#include "EnvelopeParser.h"
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cHTTPMessage
|
|
|
|
{
|
|
|
|
public:
|
2014-08-03 01:56:08 -04:00
|
|
|
enum eStatus
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
HTTP_OK = 200,
|
|
|
|
HTTP_BAD_REQUEST = 400,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
enum eKind
|
|
|
|
{
|
|
|
|
mkRequest,
|
|
|
|
mkResponse,
|
|
|
|
} ;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
cHTTPMessage(eKind a_Kind);
|
|
|
|
|
2013-12-20 10:20:54 -05:00
|
|
|
// Force a virtual destructor in all descendants
|
2014-07-22 18:36:13 -04:00
|
|
|
virtual ~cHTTPMessage() {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Adds a header into the internal map of headers. Recognizes special headers: Content-Type and Content-Length */
|
2016-02-20 05:50:52 -05:00
|
|
|
virtual void AddHeader(const AString & a_Key, const AString & a_Value);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
void SetContentType (const AString & a_ContentType) { m_ContentType = a_ContentType; }
|
2014-04-02 09:36:25 -04:00
|
|
|
void SetContentLength(size_t a_ContentLength) { m_ContentLength = a_ContentLength; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
const AString & GetContentType (void) const { return m_ContentType; }
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t GetContentLength(void) const { return m_ContentLength; }
|
2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef std::map<AString, AString> cNameValueMap;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
eKind m_Kind;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-01-01 10:42:22 -05:00
|
|
|
/** Map of headers, with their keys lowercased. */
|
2015-12-25 13:54:04 -05:00
|
|
|
AStringMap m_Headers;
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Type of the content; parsed by AddHeader(), set directly by SetContentLength() */
|
2013-09-27 12:14:26 -04:00
|
|
|
AString m_ContentType;
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
/** 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;
|
2013-09-27 12:14:26 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 06:33:27 -05:00
|
|
|
/** Stores outgoing response headers and serializes them to an HTTP data stream. */
|
|
|
|
class cHTTPOutgoingResponse :
|
2013-09-27 12:14:26 -04:00
|
|
|
public cHTTPMessage
|
|
|
|
{
|
|
|
|
typedef cHTTPMessage super;
|
|
|
|
|
|
|
|
public:
|
2016-02-20 06:33:27 -05:00
|
|
|
cHTTPOutgoingResponse(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
/** Appends the response to the specified datastream - response line and headers.
|
2016-02-20 06:33:27 -05:00
|
|
|
The body will be sent later directly through cConnection::Send() */
|
2013-09-27 12:14:26 -04:00
|
|
|
void AppendToData(AString & a_DataStream) const;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-20 05:50:52 -05:00
|
|
|
|
|
|
|
/** 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() {}
|
|
|
|
};
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cUserData> cUserDataPtr;
|
2016-02-20 05:50:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
};
|