173 lines
4.6 KiB
C++
173 lines
4.6 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/HttpValidator.h
|
|
*
|
|
* @brief Header file for the HTTP validator classes.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_HTTP_VALIDATOR_H)
|
|
#define ACTIV_BASE_HTTP_VALIDATOR_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/Http.h"
|
|
#include "ActivMiddleware/ActivBase/MessageValidator.h"
|
|
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
|
|
#include "ActivMiddleware/Misc/StlHelper.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief HTTO validator base.
|
|
*
|
|
* @param T an HttpRequest or HttpResponse derived class.
|
|
*/
|
|
template<typename T>
|
|
class HttpValidatorBase
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Reset.
|
|
*/
|
|
ACTIV_BASE_API void Reset();
|
|
|
|
/**
|
|
* @brief Consume some bytes.
|
|
*
|
|
* The OnBody() callback in the HttpMessage passed to the constructor will be invoked with any data which is in the
|
|
* body of the HTTP message stream.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS bytes are valid HTTP.
|
|
* @retval STATUS_CODE_INVALID_FORMAT invalid HTTP; connection should be closed.
|
|
*/
|
|
ACTIV_BASE_API const StatusCode ConsumeBytes(const void * const pData, const size_t dataLength);
|
|
|
|
protected:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param httpMessage an HttpRequest or HttpResponse to validate against.
|
|
*/
|
|
ACTIV_BASE_API HttpValidatorBase(T &httpMessage);
|
|
|
|
private:
|
|
static const char CR = 0x0d;
|
|
static const char LF = 0x0a;
|
|
|
|
/**
|
|
* @brief Parsing states.
|
|
*/
|
|
enum State
|
|
{
|
|
STATE_INITIAL_LINE,
|
|
STATE_HEADERS,
|
|
STATE_BODY
|
|
};
|
|
|
|
/**
|
|
* @brief Line parsing states.
|
|
*/
|
|
enum LineState
|
|
{
|
|
LINE_STATE_W4_CR,
|
|
LINE_STATE_W4_LF
|
|
};
|
|
|
|
/**
|
|
* @brief Set a new state.
|
|
*
|
|
* @param state
|
|
*/
|
|
void SetState(const State state);
|
|
|
|
/**
|
|
* @brief Validate a line.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS complete line in m_line.
|
|
* @retval STATUS_CODE_END_OF_DATA more data required to parse complete line.
|
|
* @retval STATUS_CODE_INVALID_FORMAT
|
|
* @retval STATUS_CODE_FAILURE internal error.
|
|
*/
|
|
StatusCode ValidateLine();
|
|
|
|
/**
|
|
* @brief Validate the initial line of an HTTP message.
|
|
*
|
|
* Specializations for HttpRequest and HttpResponse are provided.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_COMMAND for HttpRequest; unknown verb.
|
|
* @retval STATUS_CODE_INVALID_FORMAT
|
|
*/
|
|
StatusCode ValidateInitialLine();
|
|
|
|
/**
|
|
* @brief Validate header lines of an HTTP message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_FORMAT not for the form "key: value"
|
|
*/
|
|
StatusCode ValidateMessageHeader();
|
|
|
|
/**
|
|
* @brief Validate body of message.
|
|
*
|
|
* Specializations for HttpRequest and HttpResponse are provided.
|
|
*
|
|
* @param pContent to receive a pointer to the body content.
|
|
* @param contentLength to receive the (partial) content length.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS complete content-length has been received.
|
|
* @retval STATUS_CODE_END_OF_DATA more data required to complete content-length, or no content-length received.
|
|
*/
|
|
StatusCode ValidateBody();
|
|
|
|
/**
|
|
* @brief Validate body of message, common to requests and responses.
|
|
*
|
|
* @param pContent to receive a pointer to the body content.
|
|
* @param contentLength to receive the (partial) content length.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS complete content-length has been received.
|
|
* @retval STATUS_CODE_END_OF_DATA more data required to complete content-length, or no content-length received.
|
|
*/
|
|
StatusCode InternalValidateBody();
|
|
|
|
T & m_httpMessage;
|
|
|
|
MessageValidator m_messageValidator;
|
|
|
|
State m_state; ///< Current receive state.
|
|
LineState m_lineState; ///< Current line-parsing state.
|
|
String m_line; ///< Current line.
|
|
|
|
const byte_t * m_pData; ///< Start of current buffer passed to ConsumeBytes().
|
|
const byte_t * m_pNextByte; ///< Next byte to parse.
|
|
size_t m_bytesLeft; ///< Number of bytes left of m_pData to parse.
|
|
|
|
UInt m_headerContentLength; ///< Content-length from header.
|
|
uint64_t m_totalNumberOfBytesBodyReceived; ///< Number of bytes content received in total.
|
|
const void * m_pBody; ///< Body to pass back to user.
|
|
size_t m_bodyLength; ///< Body length to pass back to user.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
class HttpValidator : public HttpValidatorBase<typename T::Self>
|
|
{
|
|
public:
|
|
HttpValidator(T &httpMessage) :
|
|
HttpValidatorBase<typename T::Self>(httpMessage)
|
|
{
|
|
}
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_HTTP_VALIDATOR_H)
|