15 m_Callbacks(a_Callbacks),
16 m_EnvelopeParser(*this)
34 auto inBufferSoFar =
m_Buffer.size();
39 ASSERT(bytesConsumedFirstLine <= inBufferSoFar + a_Size);
40 ASSERT(bytesConsumedFirstLine > inBufferSoFar);
52 if (bytesConsumedEnvelope == AString::npos)
58 ASSERT(bytesConsumedEnvelope <= bytesConsumedFirstLine + a_Size);
59 m_Buffer.erase(0, bytesConsumedEnvelope);
65 if (bytesConsumedBody == AString::npos)
70 return bytesConsumedBody + bytesConsumedEnvelope + bytesConsumedFirstLine - inBufferSoFar;
79 if (bytesConsumed == AString::npos)
89 auto bytesConsumedBody =
ParseBody(a_Data + bytesConsumed, a_Size - bytesConsumed);
90 if (bytesConsumedBody == AString::npos)
125 auto idxLineEnd =
m_Buffer.find(
"\r\n");
126 if (idxLineEnd == AString::npos)
134 return idxLineEnd + 2;
146 OnError(
"No transfer encoding parser");
147 return AString::npos;
178 if (Key ==
"content-length")
182 OnError(fmt::format(FMT_STRING(
"Invalid content length header value: \"{}\""), a_Value));
186 if (Key ==
"transfer-encoding")
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
void Reset(void)
Makes the parser forget everything parsed so far, so that it can be reused for parsing another datast...
size_t Parse(const char *a_Data, size_t a_Size)
Parses the incoming data.
bool IsInHeaders(void) const
Returns true if more input is expected for the envelope header.
void Reset(void)
Resets the parser to the initial state, so that a new request can be parsed.
virtual void OnBodyData(const void *a_Data, size_t a_Size) override
Called for each chunk of the incoming body data.
size_t Parse(const char *a_Data, size_t a_Size)
Parses the incoming data and calls the appropriate callbacks.
cCallbacks & m_Callbacks
The callbacks used for reporting.
virtual void OnBodyFinished(void) override
Called when the entire body has been reported by OnBodyData().
virtual void OnError(const AString &a_ErrorDescription) override
Called when an error has occured while parsing.
bool m_IsFinished
True if the response has been fully parsed.
cEnvelopeParser m_EnvelopeParser
Parser for the envelope data (headers)
cHTTPMessageParser(cCallbacks &a_Callbacks)
Creates a new parser instance that will use the specified callbacks for reporting.
void HeadersFinished(void)
Called internally when the headers-parsing has just finished.
size_t m_ContentLength
The content length, parsed from the headers, if available.
AString m_FirstLine
The complete first line of the response.
size_t ParseBody(const char *a_Data, size_t a_Size)
Parses the message body.
AString m_TransferEncoding
The transfer encoding to be used by the parser.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value) override
Called when a full header line is parsed.
size_t ParseFirstLine(void)
Parses the first line out of m_Buffer.
cTransferEncodingParserPtr m_TransferEncodingParser
The specific parser for the transfer encoding used by this response.
bool m_HasHadError
Set to true if an error has been encountered by the parser.
AString m_Buffer
Buffer for the incoming data until the status line is parsed.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value)=0
Called when a single header line is parsed.
virtual void OnFirstLine(const AString &a_FirstLine)=0
Called when the first line of the request or response is fully parsed.
virtual void OnBodyFinished(void)=0
Called when the entire body has been reported by OnBodyData().
virtual void OnBodyData(const void *a_Data, size_t a_Size)=0
Called for each chunk of the incoming body data.
virtual void OnError(const AString &a_ErrorDescription)=0
Called when an error has occured while parsing.
virtual void OnHeadersFinished(void)=0
Called when all the headers have been parsed.
static cTransferEncodingParserPtr Create(cCallbacks &a_Callbacks, const AString &a_TransferEncoding, size_t a_ContentLength)
Creates a new parser for the specified encoding.