diff --git a/src/vmime/body.cpp b/src/vmime/body.cpp index 8b334352..e5813375 100644 --- a/src/vmime/body.cpp +++ b/src/vmime/body.cpp @@ -128,7 +128,7 @@ size_t body::findNextBoundaryPosition void body::parseImpl - (const parsingContext& /* ctx */, + (const parsingContext& ctx, shared_ptr parser, const size_t position, const size_t end, size_t* newPosition) { @@ -286,7 +286,7 @@ void body::parseImpl if (partEnd > partStart) { vmime::text text; - text.parse(parser, partStart, partEnd); + text.parse(ctx, parser, partStart, partEnd); m_prologText = text.getWholeBuffer(); } @@ -304,7 +304,7 @@ void body::parseImpl if (partEnd < partStart) std::swap(partStart, partEnd); - part->parse(parser, partStart, partEnd, NULL); + part->parse(ctx, parser, partStart, partEnd, NULL); m_parts.push_back(part); } @@ -325,7 +325,7 @@ void body::parseImpl try { - part->parse(parser, partStart, end); + part->parse(ctx, parser, partStart, end); } catch (std::exception&) { @@ -338,7 +338,7 @@ void body::parseImpl else if (partStart < end) { vmime::text text; - text.parse(parser, partStart, end); + text.parse(ctx, parser, partStart, end); m_epilogText = text.getWholeBuffer(); } diff --git a/src/vmime/headerField.cpp b/src/vmime/headerField.cpp index a89704ab..40611a45 100644 --- a/src/vmime/headerField.cpp +++ b/src/vmime/headerField.cpp @@ -118,15 +118,27 @@ shared_ptr headerField::parseNext if (buffer[pos] != ':') { - // Humm...does not seem to be a valid header line. - // Skip this error and advance to the next line - pos = nameStart; + switch (ctx.getHeaderParseErrorRecoveryMethod()) { + case vmime::headerParseRecoveryMethod::SKIP_LINE: + // Humm...does not seem to be a valid header line. + // Skip this error and advance to the next line + pos = nameStart; - while (pos < end && buffer[pos] != '\n') - ++pos; + while (pos < end && buffer[pos] != '\n') + ++pos; - if (pos < end && buffer[pos] == '\n') - ++pos; + if (pos < end && buffer[pos] == '\n') + ++pos; + break; + +// case vmime::headerParseRecoveryMethod::APPEND_TO_PREVIOUS_LINE: +// // TODO Implement this... +// break; + + case vmime::headerParseRecoveryMethod::ASSUME_END_OF_HEADERS: + return null; + break; + } } else { diff --git a/src/vmime/parsingContext.cpp b/src/vmime/parsingContext.cpp index 527f4705..c5da5151 100644 --- a/src/vmime/parsingContext.cpp +++ b/src/vmime/parsingContext.cpp @@ -28,13 +28,13 @@ namespace vmime { -parsingContext::parsingContext() +parsingContext::parsingContext() : m_headerParseErrorRecovery(vmime::headerParseRecoveryMethod::SKIP_LINE) { } parsingContext::parsingContext(const parsingContext& ctx) - : context(ctx) + : context(ctx), m_headerParseErrorRecovery(vmime::headerParseRecoveryMethod::SKIP_LINE) { } @@ -45,5 +45,16 @@ parsingContext& parsingContext::getDefaultContext() return ctx; } +headerParseRecoveryMethod::headerLineError parsingContext::getHeaderParseErrorRecoveryMethod() const +{ + return m_headerParseErrorRecovery; +} + + +void parsingContext::setHeaderParseErrorRecoveryMethod(headerParseRecoveryMethod::headerLineError recoveryMethod) +{ + m_headerParseErrorRecovery = recoveryMethod; +} + } // vmime diff --git a/src/vmime/parsingContext.hpp b/src/vmime/parsingContext.hpp index 27d14a60..1b1cd282 100644 --- a/src/vmime/parsingContext.hpp +++ b/src/vmime/parsingContext.hpp @@ -31,6 +31,15 @@ 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. */ @@ -48,8 +57,22 @@ public: */ 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: + headerParseRecoveryMethod::headerLineError m_headerParseErrorRecovery; };