Updated Branches: refs/heads/master 498003b5e -> b55801c81 TS-1094: Handle buffer with just CR in it correctly for MIME parsing. Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b55801c8 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b55801c8 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b55801c8 Branch: refs/heads/master Commit: b55801c814dff141b3d287f42e943e21a850a993 Parents: 498003b Author: Alan M. Carroll Authored: Wed Feb 1 10:51:02 2012 -0600 Committer: Alan M. Carroll Committed: Wed Feb 1 19:19:38 2012 -0600 ---------------------------------------------------------------------- proxy/hdrs/MIME.cc | 34 ++++++++++++++++++++++++++++++---- proxy/hdrs/MIME.h | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b55801c8/proxy/hdrs/MIME.cc ---------------------------------------------------------------------- diff --git a/proxy/hdrs/MIME.cc b/proxy/hdrs/MIME.cc index e2e3cbb..2fa5f66 100644 --- a/proxy/hdrs/MIME.cc +++ b/proxy/hdrs/MIME.cc @@ -2196,6 +2196,8 @@ mime_scanner_get(MIMEScanner *S, { const char *raw_input_c, *lf_ptr; MIMEParseResult zret = PARSE_CONT; + // Need this for handling dangling CR. + static char const RAW_CR = ParseRules::CHAR_CR; ink_debug_assert((raw_input_s != NULL) && (*raw_input_s != NULL)); ink_debug_assert(raw_input_e != NULL); @@ -2206,11 +2208,35 @@ mime_scanner_get(MIMEScanner *S, ptrdiff_t runway = raw_input_e - raw_input_c; // remaining input. switch (S->m_state) { case MIME_PARSE_BEFORE: // waiting to find a field. - // If we find leading CR LF then it's the last line of the header. - if (ParseRules::is_cr(*raw_input_c) && runway >= 2 && ParseRules::is_lf(raw_input_c[1])) { - raw_input_c += 2; - zret = PARSE_OK; + if (ParseRules::is_cr(*raw_input_c)) { + ++raw_input_c; + if (runway >= 2 && ParseRules::is_lf(*raw_input_c)) { + // optimize a bit - this happens >99% of the time after a CR. + ++raw_input_c; + zret = PARSE_DONE; + } else { + S->m_state = MIME_PARSE_FOUND_CR; + } + } else if (ParseRules::is_lf(*raw_input_c)) { + ++raw_input_c; + zret = PARSE_DONE; // Required by regression test. + } else { + // consume this character in the next state. + S->m_state = MIME_PARSE_INSIDE; + } + break; + case MIME_PARSE_FOUND_CR: + // Looking for a field and found a CR, which should mean terminating + // the header. Note that we've left the CR in the input so we have + // to skip over it. + if (ParseRules::is_lf(*raw_input_c)) { + // Header terminated. + ++raw_input_c; + zret = PARSE_DONE; } else { + // This really should be an error (spec doesn't permit lone CR) + // but the regression tests require it. + mime_scanner_append(S, &RAW_CR, 1); S->m_state = MIME_PARSE_INSIDE; } break; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b55801c8/proxy/hdrs/MIME.h ---------------------------------------------------------------------- diff --git a/proxy/hdrs/MIME.h b/proxy/hdrs/MIME.h index 5af9e09..49a6b98 100644 --- a/proxy/hdrs/MIME.h +++ b/proxy/hdrs/MIME.h @@ -55,6 +55,7 @@ enum /// Parsing state. enum MimeParseState { MIME_PARSE_BEFORE, ///< Before a field. + MIME_PARSE_FOUND_CR, ///< Before a field, found a CR. MIME_PARSE_INSIDE, ///< Inside a field. MIME_PARSE_AFTER, ///< After a field. };