17 #pragma warning(disable:4355)
29 class cMultipartParserTest :
33 cMultipartParserTest(
void)
35 cMultipartParser Parser(
"multipart/mixed; boundary=\"MyBoundaryString\"; foo=bar", *
this);
37 "ThisIsIgnoredPrologue\r\n\
38 --MyBoundaryString\r\n\
40 Body with confusing strings\r\n\
42 --MyBoundaryStringWithPostfix\r\n\
44 --MyBoundaryString\r\n\
45 content-disposition: inline\r\n\
48 --MyBoundaryString\r\n\
50 Headerless body with trailing CRLF\r\n\
52 --MyBoundaryString--\r\n\
53 ThisIsIgnoredEpilogue";
54 printf(
"Multipart parsing test commencing.\n");
55 Parser.Parse(Data,
sizeof(Data) - 1);
57 printf(
"Multipart parsing test finished\n");
62 printf(
"Starting a new part\n");
68 printf(
" Hdr: \"%s\"=\"%s\"\n", a_Key.c_str(), a_Value.c_str());
72 virtual void OnPartData(
const char * a_Data,
int a_Size)
override
74 printf(
" Data: %d bytes, \"%.*s\"\n", a_Size, a_Size, a_Data);
95 m_Callbacks(a_Callbacks),
97 m_EnvelopeParser(*this),
101 AString ContentType(a_ContentType);
102 if (strncmp(ContentType.c_str(),
"multipart/", 10) != 0)
107 size_t idxSC = ContentType.find(
';', 10);
108 if (idxSC == AString::npos)
115 ContentType.erase(0, idxSC + 1);
162 if (BytesConsumed == AString::npos)
177 if (idxBoundary == AString::npos)
195 if (LineEnd == AString::npos)
207 (LineEnd - idxBoundary !=
m_Boundary.size()) &&
208 (LineEnd - idxBoundary !=
m_Boundary.size() + 2)
221 size_t idxSlash = idxBoundary +
m_Boundary.size();
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.
void SetIsInHeaders(bool a_IsInHeaders)
Sets the IsInHeaders flag; used by cMultipartParser to simplify the parser initial conditions.
bool IsInHeaders(void) const
Returns true if more input is expected for the envelope header.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value) override
Called when a full header line is parsed.
cCallbacks & m_Callbacks
The callbacks to call for various parsing events.
bool m_IsValid
True if the data parsed so far is valid; if false, further parsing is skipped.
cMultipartParser(const AString &a_ContentType, cCallbacks &a_Callbacks)
Creates the parser, expects to find the boundary in a_ContentType.
cEnvelopeParser m_EnvelopeParser
Parser for each part's envelope.
AString m_Boundary
The boundary, excluding both the initial "--" and the terminating CRLF.
void Parse(const char *a_Data, size_t a_Size)
Parses more incoming data.
AString m_IncomingData
Buffer for the incoming data until it is parsed.
virtual void OnPartHeader(const AString &a_Key, const AString &a_Value)=0
Called when a complete header line is received for a part.
virtual void OnPartStart(void)=0
Called when a new part starts.
virtual void OnPartEnd(void)=0
Called when the current part ends.
virtual void OnPartData(const char *a_Data, size_t a_Size)=0
Called when body for a part is received.
bool Finish(void)
Notifies the parser that no more data will be coming.
bool IsValid(void) const
Returns true if the data parsed so far was valid.