add option to control parser invalid line behavior

This commit is contained in:
bmagistro 2017-02-27 11:04:33 -05:00
parent 05a65a3bfa
commit 68fd4e1e42
3 changed files with 136 additions and 90 deletions

View File

@ -118,6 +118,8 @@ shared_ptr <headerField> headerField::parseNext
if (buffer[pos] != ':') if (buffer[pos] != ':')
{ {
switch (ctx.getHeaderParseErrorRecoveryMethod()) {
case vmime::headerParseRecoveryMethod::SKIP_LINE:
// Humm...does not seem to be a valid header line. // Humm...does not seem to be a valid header line.
// Skip this error and advance to the next line // Skip this error and advance to the next line
pos = nameStart; pos = nameStart;
@ -127,6 +129,16 @@ shared_ptr <headerField> headerField::parseNext
if (pos < end && buffer[pos] == '\n') if (pos < end && buffer[pos] == '\n')
++pos; ++pos;
break;
// case vmime::headerParseRecoveryMethod::APPEND_TO_PREVIOUS_LINE:
// // TODO Implement this...
// break;
case vmime::headerParseRecoveryMethod::ASSUME_END_OF_HEADERS:
return null;
break;
}
} }
else else
{ {

View File

@ -28,13 +28,13 @@ namespace vmime
{ {
parsingContext::parsingContext() parsingContext::parsingContext() : m_headerParseErrorRecovery(vmime::headerParseRecoveryMethod::SKIP_LINE)
{ {
} }
parsingContext::parsingContext(const parsingContext& ctx) parsingContext::parsingContext(const parsingContext& ctx)
: context(ctx) : context(ctx), m_headerParseErrorRecovery(vmime::headerParseRecoveryMethod::SKIP_LINE)
{ {
} }
@ -45,5 +45,16 @@ parsingContext& parsingContext::getDefaultContext()
return ctx; return ctx;
} }
headerParseRecoveryMethod::headerLineError parsingContext::getHeaderParseErrorRecoveryMethod() const
{
return m_headerParseErrorRecovery;
}
void parsingContext::setHeaderParseErrorRecoveryMethod(headerParseRecoveryMethod::headerLineError recoveryMethod)
{
m_headerParseErrorRecovery = recoveryMethod;
}
} // vmime } // vmime

View File

@ -31,6 +31,15 @@
namespace vmime namespace vmime
{ {
/** Provides runtime configurable options to provide flexibility in header parsing
*/
struct headerParseRecoveryMethod {
enum headerLineError {
SKIP_LINE = 0,
/* APPEND_TO_PREVIOUS_LINE = 1, */
ASSUME_END_OF_HEADERS = 2
};
};
/** Holds configuration parameters used for parsing messages. /** Holds configuration parameters used for parsing messages.
*/ */
@ -48,8 +57,22 @@ public:
*/ */
static parsingContext& getDefaultContext(); static parsingContext& getDefaultContext();
/** Sets the recovery method when parsing a header encounters an error such as a failed fold or missing new line.
*
* @param recoveryMethod is one of vmime::headerParseRecoveryMethod. Defaults to vmime::headerParseRecoveryMethod::SKIP_LINE.
*/
void setHeaderParseErrorRecoveryMethod(headerParseRecoveryMethod::headerLineError recoveryMethod);
/** Return the recovery method when parsing a header encounters an error.
*
* @return is an enum from vmime::headerParseRecoveryMethod
*/
headerParseRecoveryMethod::headerLineError getHeaderParseErrorRecoveryMethod() const;
protected: protected:
headerParseRecoveryMethod::headerLineError m_headerParseErrorRecovery;
}; };