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(
Printf(
"Invalid content length header value: \"%s\"", a_Value.c_str()));
186 if (Key ==
"transfer-encoding")
virtual void OnError(const AString &a_ErrorDescription) override
Called when an error has occured while parsing.
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.
cCallbacks & m_Callbacks
The callbacks used for reporting.
AString m_TransferEncoding
The transfer encoding to be used by the parser.
virtual void OnBodyData(const void *a_Data, size_t a_Size)=0
Called for each chunk of the incoming body data.
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
bool m_IsFinished
True if the response has been fully parsed.
AString m_Buffer
Buffer for the incoming data until the status line is parsed.
virtual void OnBodyFinished(void) override
Called when the entire body has been reported by OnBodyData().
void Reset(void)
Resets the parser to the initial state, so that a new request can be parsed.
size_t Parse(const char *a_Data, size_t a_Size)
Parses the incoming data.
bool m_HasHadError
Set to true if an error has been encountered by the parser.
cHTTPMessageParser(cCallbacks &a_Callbacks)
Creates a new parser instance that will use the specified callbacks for reporting.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value) override
Called when a full header line is parsed.
size_t ParseBody(const char *a_Data, size_t a_Size)
Parses the message body.
size_t ParseFirstLine(void)
Parses the first line out of m_Buffer.
bool IsInHeaders(void) const
Returns true if more input is expected for the envelope header.
cEnvelopeParser m_EnvelopeParser
Parser for the envelope data (headers)
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
static cTransferEncodingParserPtr Create(cCallbacks &a_Callbacks, const AString &a_TransferEncoding, size_t a_ContentLength)
Creates a new parser for the specified encoding.
virtual void OnBodyData(const void *a_Data, size_t a_Size) override
Called for each chunk of the incoming body data.
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().
cTransferEncodingParserPtr m_TransferEncodingParser
The specific parser for the transfer encoding used by this response.
AString m_FirstLine
The complete first line of the response.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value)=0
Called when a single header line is parsed.
void Reset(void)
Makes the parser forget everything parsed so far, so that it can be reused for parsing another datast...
virtual void OnError(const AString &a_ErrorDescription)=0
Called when an error has occured while parsing.
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
virtual void OnHeadersFinished(void)=0
Called when all the headers have been parsed.
size_t Parse(const char *a_Data, size_t a_Size)
Parses the incoming data and calls the appropriate callbacks.