Skip to content

Commit

Permalink
Don't send 103 early hints response when only invalid headers are used (
Browse files Browse the repository at this point in the history
  • Loading branch information
MSP-Greg committed May 27, 2023
1 parent 2bb9143 commit 7b7774c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/puma/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def handle_request(client, requests)
if @early_hints
env[EARLY_HINTS] = lambda { |headers|
begin
fast_write_str socket, str_early_hints(headers)
unless (str = str_early_hints headers).empty?
fast_write_str socket, "HTTP/1.1 103 Early Hints\r\n#{str}\r\n"
end
rescue ConnectionError => e
@log_writer.debug_error e
# noop, if we lost the socket we just won't send the early hints
Expand Down Expand Up @@ -529,7 +531,7 @@ def req_env_post_parse(env)
# @version 5.0.3
#
def str_early_hints(headers)
eh_str = +"HTTP/1.1 103 Early Hints\r\n"
eh_str = +""
headers.each_pair do |k, vs|
next if illegal_header_key?(k)

Expand All @@ -538,11 +540,11 @@ def str_early_hints(headers)
next if illegal_header_value?(v)
eh_str << "#{k}: #{v}\r\n"
end
else
elsif !(vs.to_s.empty? || !illegal_header_value?(vs))
eh_str << "#{k}: #{vs}\r\n"
end
end
"#{eh_str}\r\n".freeze
eh_str.freeze
end
private :str_early_hints

Expand Down
6 changes: 5 additions & 1 deletion test/test_response_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def assert_ignore_header(name, value, opts={})
server_run(app: app, early_hints: opts[:early_hints])
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"

refute_match("#{name}: #{value}", data)
if opts[:early_hints]
refute_includes data, "HTTP/1.1 103 Early Hints"
end

refute_includes data, "#{name}: #{value}"
end

# The header must not contain a Status key.
Expand Down

0 comments on commit 7b7774c

Please sign in to comment.