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:
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
HTTP_OK = 200,
|
|
|
|
HTTP_BAD_REQUEST = 400,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
enum eKind
|
|
|
|
{
|
|
|
|
mkRequest,
|
|
|
|
mkResponse,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
cHTTPMessage(eKind a_Kind);
|
|
|
|
|
2013-12-20 10:20:54 -05:00
|
|
|
// Force a virtual destructor in all descendants
|
|
|
|
virtual ~cHTTPMessage() {};
|
|
|
|
|
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 */
|
2013-09-27 12:14:26 -04:00
|
|
|
void AddHeader(const AString & a_Key, const AString & a_Value);
|
|
|
|
|
|
|
|
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; }
|
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;
|
|
|
|
|
|
|
|
eKind m_Kind;
|
|
|
|
|
|
|
|
cNameValueMap m_Headers;
|
|
|
|
|
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
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cHTTPRequest :
|
2013-10-04 07:07:57 -04:00
|
|
|
public cHTTPMessage,
|
|
|
|
protected cEnvelopeParser::cCallbacks
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
typedef cHTTPMessage super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
cHTTPRequest(void);
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
/** Parses the request line and then headers from the received data.
|
2014-04-01 10:36:00 -04:00
|
|
|
Returns the number of bytes consumed or AString::npos number for error
|
2013-10-04 07:07:57 -04:00
|
|
|
*/
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t ParseHeaders(const char * a_Data, size_t a_Size);
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns true if the request did contain a Content-Length header */
|
2014-04-02 08:06:38 -04:00
|
|
|
bool HasReceivedContentLength(void) const { return (m_ContentLength != AString::npos); }
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns the method used in the request */
|
2013-09-28 13:30:25 -04:00
|
|
|
const AString & GetMethod(void) const { return m_Method; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns the URL used in the request */
|
2013-09-28 13:30:25 -04:00
|
|
|
const AString & GetURL(void) const { return m_URL; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns the URL used in the request, without any parameters */
|
2013-10-05 17:08:16 -04:00
|
|
|
AString GetBareURL(void) const;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Sets the UserData pointer that is stored within this request.
|
|
|
|
The request doesn't touch this data (doesn't delete it)! */
|
2013-09-27 15:38:54 -04:00
|
|
|
void SetUserData(void * a_UserData) { m_UserData = a_UserData; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Retrieves the UserData pointer that has been stored within this request. */
|
2013-09-27 15:38:54 -04:00
|
|
|
void * GetUserData(void) const { return m_UserData; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns true if more data is expected for the request headers */
|
2013-10-04 07:07:57 -04:00
|
|
|
bool IsInHeaders(void) const { return m_EnvelopeParser.IsInHeaders(); }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns true if the request did present auth data that was understood by the parser */
|
2013-10-04 14:28:30 -04:00
|
|
|
bool HasAuth(void) const { return m_HasAuth; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns the username that the request presented. Only valid if HasAuth() is true */
|
2013-10-04 14:28:30 -04:00
|
|
|
const AString & GetAuthUsername(void) const { return m_AuthUsername; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Returns the password that the request presented. Only valid if HasAuth() is true */
|
2013-10-04 14:28:30 -04:00
|
|
|
const AString & GetAuthPassword(void) const { return m_AuthPassword; }
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
bool DoesAllowKeepAlive(void) const { return m_AllowKeepAlive; }
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
protected:
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Parser for the envelope data */
|
2013-10-04 07:07:57 -04:00
|
|
|
cEnvelopeParser m_EnvelopeParser;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** True if the data received so far is parsed successfully. When false, all further parsing is skipped */
|
2013-10-04 07:07:57 -04:00
|
|
|
bool m_IsValid;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Bufferred incoming data, while parsing for the request line */
|
2013-10-04 07:07:57 -04:00
|
|
|
AString m_IncomingHeaderData;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Method of the request (GET / PUT / POST / ...) */
|
2013-09-27 12:14:26 -04:00
|
|
|
AString m_Method;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Full URL of the request */
|
2013-09-27 12:14:26 -04:00
|
|
|
AString m_URL;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Data that the HTTPServer callbacks are allowed to store. */
|
2013-09-27 15:38:54 -04:00
|
|
|
void * m_UserData;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** Set to true if the request contains auth data that was understood by the parser */
|
2013-10-04 14:28:30 -04:00
|
|
|
bool m_HasAuth;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** The username used for auth */
|
2013-10-04 14:28:30 -04:00
|
|
|
AString m_AuthUsername;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** The password used for auth */
|
2013-10-04 14:28:30 -04:00
|
|
|
AString m_AuthPassword;
|
|
|
|
|
2014-01-18 14:20:56 -05:00
|
|
|
/** 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;
|
|
|
|
|
2013-09-27 15:38:54 -04:00
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
/** Parses the incoming data for the first line (RequestLine)
|
|
|
|
Returns the number of bytes consumed, or -1 for an error
|
2013-09-27 12:14:26 -04:00
|
|
|
*/
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t ParseRequestLine(const char * a_Data, size_t a_Size);
|
2013-09-27 12:14:26 -04:00
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
// cEnvelopeParser::cCallbacks overrides:
|
|
|
|
virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
|
2013-09-27 12:14:26 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|