You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ro...@apache.org on 2022/08/15 18:33:33 UTC

[couchdb-mochiweb] 02/04: Remove leading and trailing whitespaces

This is an automated email from the ASF dual-hosted git repository.

ronny pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-mochiweb.git

commit f2ec849d8c52284ce699f97ab3108322a58eaec5
Author: Ronny <ro...@apache.org>
AuthorDate: Thu Aug 11 15:47:29 2022 +0200

    Remove leading and trailing whitespaces
    
    from field-values to be RFC 7230 compliant.
---
 src/mochiweb_headers.erl | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/mochiweb_headers.erl b/src/mochiweb_headers.erl
index 0276aee..d9bc3af 100644
--- a/src/mochiweb_headers.erl
+++ b/src/mochiweb_headers.erl
@@ -174,7 +174,7 @@ lookup(K, T) ->
 %% @doc Insert the pair into the headers if it does not already exist.
 default(K, V, T) ->
     K1 = normalize(K),
-    V1 = any_to_list(V),
+    V1 = trim_leading_and_trailing_ws(any_to_list(V)),
     try gb_trees:insert(K1, {K, V1}, T)
     catch
         error:{key_exists, _} ->
@@ -185,7 +185,7 @@ default(K, V, T) ->
 %% @doc Insert the pair into the headers, replacing any pre-existing key.
 enter(K, V, T) ->
     K1 = normalize(K),
-    V1 = any_to_list(V),
+    V1 = trim_leading_and_trailing_ws(any_to_list(V)),
     gb_trees:enter(K1, {K, V1}, T).
 
 %% @spec insert(key(), value(), headers()) -> headers()
@@ -193,7 +193,7 @@ enter(K, V, T) ->
 %%      A merge is done with Value = V0 ++ ", " ++ V1.
 insert(K, V, T) ->
     K1 = normalize(K),
-    V1 = any_to_list(V),
+    V1 = trim_leading_and_trailing_ws(any_to_list(V)),
     try gb_trees:insert(K1, {K, V1}, T)
     catch
         error:{key_exists, _} ->
@@ -215,6 +215,9 @@ tokenize_header_value(undefined) ->
 tokenize_header_value(V) ->
     reversed_tokens(trim_and_reverse(V, false), [], []).
 
+trim_leading_and_trailing_ws(S) ->
+    re:replace(S, "^[ \\t]*|[ \\t]*$", "", [global, {return, list}]).
+
 trim_and_reverse([S | Rest], Reversed) when S=:=$ ; S=:=$\n; S=:=$\t ->
     trim_and_reverse(Rest, Reversed);
 trim_and_reverse(V, false) ->