Skip to content

Add continue_callback to answer Expect: 100-continue before the body is read - #4028

Merged
joshuay03 merged 4 commits into
puma:mainfrom
IslamElsayed:expect-continue-callback
Oct 3, 2026
Merged

joshuay03 merged 4 commits into
puma:mainfrom
IslamElsayed:expect-continue-callback

Conversation

@IslamElsayed

Copy link
Copy Markdown
Contributor

Description

Closes #3188.

Puma replies 100 Continue to every request that sends Expect: 100-continue, so an app can't turn a request away before the client uploads the body — redirecting an upload elsewhere or refusing an oversized one means receiving it all first. Client#setup_body has carried # TODO allow a hook here to check the headers before going forward for this.

This implements the shape @nateberkopec proposed and @dentarg agreed in the issue, with false as the no-op per @dentarg's suggestion:

continue_callback do |env|
  env['CONTENT_LENGTH'].to_i > 10_000_000 ? [413, {}, []] : false
end
  • false / nil — unchanged behaviour: 100 Continue is sent and the request reaches the app.
  • A Rack response — sent instead. 100 Continue is not sent, the body is never read, the app is not called, and the connection is closed.

Without the option, nothing changes.

Decisions worth checking

  • It reuses the existing pre-body rejection path. http_content_length_limit already refuses a request before reading its body, by raising from setup_body and letting client_error write the response. This does the same with a new ContinueRejected that carries the callback's response, so it's answered correctly whether setup_body ran on the reactor or in the thread pool — both already rescue into client_error and then close the client. That's also why it adds no logging: answering early is the callback working, not an error.
  • The connection is always closed after a rejection. The client may already have started sending the body (curl does after a short wait), and Puma won't read it, so the connection can't safely be reused. This mirrors what the 413 path does.
  • The callback can run on the reactor thread, which services every connection still waiting for its request, so a slow callback would stall them. The DSL docs say to decide from the headers alone and not do I/O in it. If you'd rather Puma enforce something here, I'm happy to change it.
  • I didn't validate the returned status. The docs say 3xx or 4xx; returning anything else is the app's choice. I can add a guard if you'd prefer one.

This supersedes #3200, which was closed as stale in November 2024.

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed. — It's 146. 79 are tests (five server tests plus two DSL tests), and about 30 of the library lines are the DSL documentation, including two examples. The code change itself is small. I'd rather keep the tests and docs than trim them to fit — say if you want it split.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly. (The DSL docs; I didn't find a separate options page this needs adding to — point me at one if there is.)
  • All new and existing tests passed, including Rubocop. Locally: test_puma_server.rb (160 runs), test_config.rb (111), test_request_invalid_multiple.rb, test_http11.rb and the hijack tests, all green, and Rubocop clean on the changed files. Each part of the change was also broken on purpose to confirm a test fails without it.

Disclosure: I use AI assistance when working on patches, and I check and test everything before submitting.

…is read

Puma replied 100 Continue to every request that asked for it, so an app
had no way to turn a request away before the client sent the body.
Redirecting an upload elsewhere, or refusing one that is too large,
meant receiving the whole body first. The TODO in Client#setup_body
has pointed at this since the header was first supported.

continue_callback receives the Rack env, which has the headers but no
body yet. Returning false or nil keeps today's behaviour. Returning a
Rack response sends that instead: 100 Continue is not sent, the body is
never read, the app is not called, and the connection is closed, since
the client may already have started a body Puma will not read.

The rejection travels as ContinueRejected through the existing
client_error path, the same way the 413 for http_content_length_limit
does, so it is answered from both the reactor and the thread pool.
Without the option nothing changes.

Closes puma#3188
@IslamElsayed
IslamElsayed marked this pull request as ready for review September 27, 2026 12:51
@IslamElsayed

Copy link
Copy Markdown
Contributor Author

Note on the red jobs: the four JRuby failures (TestPumaControlCli#test_control_ssl_ipv4 / _ipv6, "closing inbound before receiving peer's close_notify") also fail on main at the base commit 6d6e2c0 (run 36313590659), so they look like JRuby 10.1.2, not this change. ubuntu-24.04-arm 3.2 died inside test_control_url_and_status with no assertion output and passes on main; I can't re-run it from a fork. Could someone kick it?

@github-actions github-actions Bot added the waiting-for-review Waiting on review from anyone label Sep 27, 2026
Comment thread lib/puma/server.rb Resolved
@github-actions github-actions Bot added waiting-for-changes Waiting on changes from the requestor and removed waiting-for-review Waiting on review from anyone labels Oct 2, 2026
IslamElsayed and others added 2 commits October 3, 2026 12:39
Rack requires the server to close a response body that responds to #close; the callback's response was written but never closed. Also shorten the DSL doc to the callback's contract and one example.

@joshuay03 joshuay03 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@joshuay03
joshuay03 merged commit f055d83 into puma:main Oct 3, 2026
58 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-for-changes Waiting on changes from the requestor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support apps responding to Expect: 100-continue

2 participants