From e0a32d9c602ba3dbba6cc4633169cf62b2263ca3 Mon Sep 17 00:00:00 2001 From: "William A. Rowe Jr" Date: Thu, 4 Aug 2016 20:46:45 +0000 Subject: [PATCH] As promised, reduce this logic by net 9 code lines, shifting the burden of killing trailing whitespace to the purpose-agnostic read logic. Whitespace before or after an obs-fold, and before or after a field value have no semantic purpose at all. Because we are building a buffer for all folded values, reducing the size of the newly allocated buffer is always to our advantage. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1755233 13f79535-47bb-0310-9956-ffa450edef68 --- server/protocol.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/server/protocol.c b/server/protocol.c index 91577e378b..524c9b4fd9 100644 --- a/server/protocol.c +++ b/server/protocol.c @@ -834,7 +834,16 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb return; } - if ((*field == '\t') || *field == ' ') { + /* For all header values, and all obs-fold lines, the presence of + * additional whitespace is a no-op, so collapse trailing whitespace + * to save buffer allocation and optimize copy operations. + * Do not remove the last single whitespace under any condition. + */ + while (len > 1 && (field[len-1] == '\t' || field[len-1] == ' ')) { + field[--len] = '\0'; + } + + if (*field == '\t' || *field == ' ') { /* Append any newly-read obs-fold line onto the preceding * last_field line we are processing @@ -954,13 +963,6 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb && (*tmp_field == ' ' || *tmp_field == '\t')) { *tmp_field-- = '\0'; } - - /* Strip LWS after field-value: */ - tmp_field = last_field + last_len - 1; - while (tmp_field > value - && (*tmp_field == ' ' || *tmp_field == '\t')) { - *tmp_field-- = '\0'; - } } else /* Using strict RFC7230 parsing */ { @@ -990,15 +992,6 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb /* Find invalid, non-HT ctrl char, or the trailing NULL */ tmp_field = (char *)ap_scan_http_field_content(value); - /* Strip LWS after field-value, if string not empty */ - if (*value && (*tmp_field == '\0')) { - tmp_field--; - while (*tmp_field == ' ' || *tmp_field == '\t') { - *tmp_field-- = '\0'; - } - ++tmp_field; - } - /* Reject value for all garbage input (CTRLs excluding HT) * e.g. only VCHAR / SP / HT / obs-text are allowed per * RFC7230 3.2.6 - leave all more explicit rule enforcement -- 2.40.0