aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGustaf Sjoberg <[email protected]>2017-02-23 11:35:32 +0100
committerLoïc Hoguin <[email protected]>2018-03-05 14:54:12 +0100
commitca3b4d8d1b194d5dd4f2d452eb7bd88932e6617a (patch)
tree67736c0daaec8ccc3e9a94ba62a4a24d10b3bd4d /src
parentfa0357ff752e1ce70191ceb6332e5d58b9f1f18e (diff)
downloadcowlib-ca3b4d8d1b194d5dd4f2d452eb7bd88932e6617a.tar.gz
cowlib-ca3b4d8d1b194d5dd4f2d452eb7bd88932e6617a.tar.bz2
cowlib-ca3b4d8d1b194d5dd4f2d452eb7bd88932e6617a.zip
Remove trailing whitespace from header values
Edited to add a test and use byte_size instead of size.
Diffstat (limited to 'src')
-rw-r--r--src/cow_http.erl18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/cow_http.erl b/src/cow_http.erl
index e79896b..f89e518 100644
--- a/src/cow_http.erl
+++ b/src/cow_http.erl
@@ -145,16 +145,32 @@ parse_hd_value(<< $\r, Rest/bits >>, Acc, Name, SoFar) ->
<< $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
parse_hd_value(Rest2, Acc, Name, << SoFar/binary, C >>);
<< $\n, Rest2/bits >> ->
- parse_header(Rest2, [{Name, SoFar}|Acc])
+ Value = clean_value_ws_end(SoFar, byte_size(SoFar) - 1),
+ parse_header(Rest2, [{Name, Value}|Acc])
end;
parse_hd_value(<< C, Rest/bits >>, Acc, Name, SoFar) ->
parse_hd_value(Rest, Acc, Name, << SoFar/binary, C >>).
+%% This function has been copied from cowboy_http.
+clean_value_ws_end(_, -1) ->
+ <<>>;
+clean_value_ws_end(Value, N) ->
+ case binary:at(Value, N) of
+ $\s -> clean_value_ws_end(Value, N - 1);
+ $\t -> clean_value_ws_end(Value, N - 1);
+ _ ->
+ S = N + 1,
+ << Value2:S/binary, _/bits >> = Value,
+ Value2
+ end.
+
-ifdef(TEST).
parse_headers_test_() ->
Tests = [
{<<"\r\nRest">>,
{[], <<"Rest">>}},
+ {<<"Server: Erlang/R17 \r\n\r\n">>,
+ {[{<<"server">>, <<"Erlang/R17">>}], <<>>}},
{<<"Server: Erlang/R17\r\n"
"Date: Sun, 23 Feb 2014 09:30:39 GMT\r\n"
"Multiline-Header: why hello!\r\n"