libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit c36e910f5689c94528beabfa3fc619aa09f5a312
parent 33c06771bdf2b357c0659c6cec35813d3a515db1
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Fri, 13 Jun 2025 16:23:14 +0200

POST parser: optimised large upload processing

Diffstat:
Msrc/mhd2/post_parser_funcs.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 94 insertions(+), 0 deletions(-)

diff --git a/src/mhd2/post_parser_funcs.c b/src/mhd2/post_parser_funcs.c @@ -1457,6 +1457,36 @@ parse_post_mpart (struct MHD_Connection *restrict c, mhd_assert (mhd_POST_INVALID_POS == mf->line_start); do /* Fast local loop */ { +#ifndef MHD_FAVOR_SMALL_CODE + const char *lf_ptr; + size_t lf_pos; + + lf_ptr = (const char *) memchr (buf + i, '\n', *pdata_size - i); + if (NULL == lf_ptr) + { + if ('\r' == buf[*pdata_size - 1]) + mf->st = mhd_POST_MPART_ST_PREAMBL_CR_FOUND; + i = *pdata_size; + break; + } + lf_pos = (size_t) (lf_ptr - buf); + mhd_assert (i <= lf_pos); + mhd_assert (*pdata_size > i); + if (bare_lf_as_crlf) + { + i = lf_pos + 1; + mf->st = mhd_POST_MPART_ST_PREAMBL_LINE_START; + break; + } + else if ((i < lf_pos) && + ('\r' == buf[lf_pos - 1])) + { + i = lf_pos + 1; + mf->st = mhd_POST_MPART_ST_PREAMBL_LINE_START; + break; + } + i = lf_pos; +#else /* MHD_FAVOR_SMALL_CODE */ if ('\r' == buf[i]) { mf->st = mhd_POST_MPART_ST_PREAMBL_CR_FOUND; @@ -1469,6 +1499,7 @@ parse_post_mpart (struct MHD_Connection *restrict c, ++i; /* Go to the next char */ break; } +#endif /* MHD_FAVOR_SMALL_CODE */ } while (*pdata_size > ++i); mhd_assert ((*pdata_size == i) || \ (mhd_POST_MPART_ST_PREAMBL_CR_FOUND == mf->st) || \ @@ -1496,6 +1527,21 @@ parse_post_mpart (struct MHD_Connection *restrict c, mhd_assert (mhd_POST_INVALID_POS == mf->delim_check_start); mhd_assert (mhd_POST_INVALID_POS == mf->line_start); mf->line_start = i; +#ifndef MHD_FAVOR_SMALL_CODE + if (*pdata_size - i >= mf->bound.size + 2) + { + if (('-' == buf[i]) && + ('-' == buf[i + 1]) && + (0 == memcmp (buf + i + 2, mf->bound.data, mf->bound.size))) + { + mf->st = mhd_POST_MPART_ST_FIRST_DELIM_FOUND; + i += 2 + mf->bound.size + 1; + } + else + mf->st = mhd_POST_MPART_ST_BACK_TO_PREAMBL; + continue; + } +#endif /* ! MHD_FAVOR_SMALL_CODE */ mf->st = mhd_POST_MPART_ST_PREAMBL_CHECKING_FOR_DELIM; mhd_FALLTHROUGH; /* Intentional fallthrough */ @@ -1877,6 +1923,38 @@ parse_post_mpart (struct MHD_Connection *restrict c, mhd_assert (mhd_POST_INVALID_POS != p_data->field_start); do /* Fast local loop */ { +#ifndef MHD_FAVOR_SMALL_CODE + const char *lf_ptr; + size_t lf_pos; + + lf_ptr = (const char *) memchr (buf + i, '\n', *pdata_size - i); + if (NULL == lf_ptr) + { + if ('\r' == buf[*pdata_size - 1]) + mf->st = mhd_POST_MPART_ST_VALUE_CR_FOUND; + i = *pdata_size; + break; + } + lf_pos = (size_t) (lf_ptr - buf); + mhd_assert (i <= lf_pos); + mhd_assert (*pdata_size > i); + if ((i < lf_pos) && + ('\r' == buf[lf_pos - 1])) + { + mf->delim_check_start = lf_pos - 1; + mf->st = mhd_POST_MPART_ST_VALUE_LINE_START; + i = lf_pos + 1; + break; + } + else if (bare_lf_as_crlf) + { + mf->delim_check_start = lf_pos; + mf->st = mhd_POST_MPART_ST_VALUE_LINE_START; + i = lf_pos + 1; + break; + } + i = lf_pos; +#else /* MHD_FAVOR_SMALL_CODE */ if ('\r' == buf[i]) { mf->delim_check_start = i; @@ -1891,6 +1969,7 @@ parse_post_mpart (struct MHD_Connection *restrict c, ++i; break; } +#endif /* MHD_FAVOR_SMALL_CODE */ } while (*pdata_size > ++i); mhd_assert ((*pdata_size == i) || \ (mhd_POST_MPART_ST_VALUE_CR_FOUND == mf->st) || \ @@ -1909,6 +1988,21 @@ parse_post_mpart (struct MHD_Connection *restrict c, mhd_assert (mhd_POST_INVALID_POS != mf->delim_check_start); mhd_assert (mhd_POST_INVALID_POS != p_data->field_start); mf->line_start = i; +#ifndef MHD_FAVOR_SMALL_CODE + if (*pdata_size - i >= mf->bound.size + 2) + { + if (('-' == buf[i]) && + ('-' == buf[i + 1]) && + (0 == memcmp (buf + i + 2, mf->bound.data, mf->bound.size))) + { + mf->st = mhd_POST_MPART_ST_DELIM_FOUND; + i += 2 + mf->bound.size; + } + else + mf->st = mhd_POST_MPART_ST_BACK_TO_VALUE; + continue; + } +#endif /* ! MHD_FAVOR_SMALL_CODE */ mf->st = mhd_POST_MPART_ST_VALUE_CHECKING_FOR_DELIM; mhd_FALLTHROUGH; /* Intentional fallthrough */