Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Expect-100 Continue header #3200

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/puma/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def initialize(io, env=nil)
@http_content_length_limit = nil
@http_content_length_limit_exceeded = false

@http_expect_100_continue_header_present = false

@peerip = nil
@peer_family = nil
@listener = nil
Expand All @@ -104,7 +106,8 @@ def initialize(io, env=nil)
end

attr_reader :env, :to_io, :body, :io, :timeout_at, :ready, :hijacked,
:tempfile, :io_buffer, :http_content_length_limit_exceeded
:tempfile, :io_buffer, :http_content_length_limit_exceeded,
:http_expect_100_continue_header_present

attr_writer :peerip, :http_content_length_limit

Expand Down Expand Up @@ -158,6 +161,7 @@ def reset(fast_check=true)
@peerip = nil if @remote_addr_header
@in_last_chunk = false
@http_content_length_limit_exceeded = false
@http_expect_100_continue_header_present = false

if @buffer
return false unless try_to_parse_proxy_protocol
Expand Down Expand Up @@ -342,10 +346,7 @@ def setup_body
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)

if @env[HTTP_EXPECT] == CONTINUE
# TODO allow a hook here to check the headers before
# going forward
@io << HTTP_11_100
@io.flush
@http_expect_100_continue_header_present = true
Comment on lines -345 to +349
Copy link
Member

@dentarg dentarg Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure we don't want to remove the current behaviour of responding quickly to Expect: 100 Continue and that allowing the app to define the response should be something the user opt-in to (via the Puma config probably)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or at least, we should keep that behavior UNLESS the user/app says they want something else).

end

@read_header = false
Expand Down
10 changes: 7 additions & 3 deletions lib/puma/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def prepare_response(status, headers, res_body, requests, client)
env = client.env
socket = client.io
io_buffer = client.io_buffer
expect_100_continue = client.http_expect_100_continue_header_present

return false if closed_socket?(socket)

Expand All @@ -169,7 +170,7 @@ def prepare_response(status, headers, res_body, requests, client)
@thread_pool.busy_threads < @max_threads ||
!client.listener.to_io.wait_readable(0)

resp_info = str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)
resp_info = str_headers(env, status, headers, res_body, io_buffer, force_keep_alive, expect_100_continue)

close_body = false
response_hijack = nil
Expand Down Expand Up @@ -568,8 +569,7 @@ def fetch_status_code(status)
# @return [Hash] resp_info
# @version 5.0.3
#
def str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)

def str_headers(env, status, headers, res_body, io_buffer, force_keep_alive, expect_100_continue)
line_ending = LINE_END
colon = COLON

Expand All @@ -581,6 +581,9 @@ def str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)
resp_info[:allow_chunked] = true
resp_info[:keep_alive] = env.fetch(HTTP_CONNECTION, "").downcase != CLOSE

if expect_100_continue
io_buffer << HTTP_11_100
end
# An optimization. The most common response is 200, so we can
# reply with the proper 200 status without having to compute
# the response header.
Expand All @@ -592,6 +595,7 @@ def str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)

resp_info[:no_body] ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status]
end

else
resp_info[:allow_chunked] = false
resp_info[:keep_alive] = env.fetch(HTTP_CONNECTION, "").downcase == KEEP_ALIVE
Expand Down
8 changes: 8 additions & 0 deletions test/test_web_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def test_nonexistent_http_method
socket.close
end

def test_expect_100_header

This comment was marked as resolved.

request = "POST /test HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\nExpect: 100-continue\r\nContent-Length: 5\r\n\r\nHello"
socket = do_test(request, request.length)
response = socket.read
assert_match "HTTP/1.1 100 Continue", response
socket.close
end

private

def do_test(string, chunk)
Expand Down