Repository navigation
Wrap the router in Rack::Head instead of each endpoint's stack - #3042
Open
ericproulx wants to merge 1 commit into
Open
ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
Rack::Head was the first middleware in each endpoint's stack, so it only saw responses that went through one. A Rack app mounted with `mount` is called by the router directly and skips `build_stack`, and a middleware inserted with `insert 0` sat outside Rack::Head: a HEAD request to either got the full body back. The router's own 404 had to strip its body itself (#2958) for the same reason. A single Rack::Head around the router, inside Rack::Lint, now strips the body of every HEAD response the API gives, and the router's 404 special case goes away. A request that cascades through several routes goes through one Rack::Head instead of one per route tried. Since an endpoint's stack no longer contains Rack::Head, `insert_before`/`insert_after Rack::Head` raise when the API compiles, and a middleware inserted by index lands one place further in. Both are covered in UPGRADING. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix/head-around-router
branch
from
October 24, 2026 08:06
6fb8c7c to
096fc67
Compare
Danger ReportNo issues found. |
2 of 3 tasks
dblock
approved these changes
Oct 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rack::Headwas the first middleware in each endpoint's stack, so it only stripped bodies from responses that went through one. Two kinds didn't:mount: the router calls it directly (config.app || build_stack), so aHEADrequest got the app's body back, and raisedRack::Lint::LintError: Response body was given for HEAD requestunderlint!;insert 0(the README'sCustomOverwriterexample), which sat outsideRack::Head.The router's own 404 had to strip its body itself (#2958) for the same reason.
This puts a single
Rack::Headaround the router inGrape::API::Instance, insideRack::Lintso Lint checks the response that is served, and drops the router's 404 special case. A request that cascades through several routes goes through oneRack::Headinstead of one per route tried.This came up while looking at where
Rack::TempfileReapershould go in #3041: the sameconfig.app || build_stackgap means a per-endpoint middleware never runs for a mounted Rack app.Contract change
An endpoint's stack now starts with
Grape::Middleware::Error, so:insert_before Rack::Head/insert_after Rack::HeadraiseNo such middleware to insert before: Rack::Headwhen the API compiles;insert 1now goes insideGrape::Middleware::Errorinstead of between it andRack::Head;insert 0sees the full body of aHEADresponse, as one added withusealways has.Covered in UPGRADING.
Test plan
HEADto a mounted Rack app, andHEADanswered by aninsert 0middleware, both come back without a body. Both fail against master'slib.bundle exec rake: RuboCop clean, 3158 examples, 0 failures.gemfiles/rack_2_2.gemfile: 3162 examples, 0 failures.🤖 Generated with Claude Code