From 88098cedef2204f27acff4608c67270b0054e77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 9 Mar 2021 15:26:48 +0100 Subject: [PATCH 1/2] Use each_value instead of values.each --- src/http/cookie.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http/cookie.cr b/src/http/cookie.cr index 65879c6cbdb8..8660eba9f754 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -272,8 +272,8 @@ module HTTP end # Yields each `HTTP::Cookie` in the collection. - def each(&block : Cookie ->) - @cookies.values.each do |cookie| + def each(& : Cookie ->) + @cookies.each_value do |cookie| yield cookie end end From 0f0640583e18e4bc29b31f97ccdbff3a8a5cd899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 9 Mar 2021 15:28:52 +0100 Subject: [PATCH 2/2] Override headers["Cookie"] and build string without intermediate --- src/http/cookie.cr | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/http/cookie.cr b/src/http/cookie.cr index 8660eba9f754..fcf79b73431b 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -297,8 +297,15 @@ module HTTP # given `HTTP::Headers` instance and returns it. Removes any existing # `Cookie` headers in it. def add_request_headers(headers) - headers.delete("Cookie") - headers.add("Cookie", map(&.to_cookie_header).join("; ")) unless empty? + if empty? + headers.delete("Cookie") + else + capacity = sum { |cookie| cookie.name.bytesize + cookie.value.bytesize + 1 } + capacity += (size - 1) * 2 + headers["Cookie"] = String.build(capacity) do |io| + join(io, "; ", &.to_cookie_header(io)) + end + end headers end