Skip to content

Commit 096fc67

Browse files
ericproulxclaude
andcommitted
Wrap the router in Rack::Head instead of each endpoint's stack
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>
1 parent e835bb7 commit 096fc67

7 files changed

Lines changed: 44 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
* [#3039](https://github.com/ruby-grape/grape/pull/3039): Hold every module to RuboCop's default length - [@ericproulx](https://github.com/ericproulx).
100100
* [#3031](https://github.com/ruby-grape/grape/pull/3031): Define `InheritableSetting`'s scalar settings and scope flags by key - [@ericproulx](https://github.com/ericproulx).
101101
* [#3032](https://github.com/ruby-grape/grape/pull/3032): Keep each API's content type order when negotiating a browser's `*/*` - [@ericproulx](https://github.com/ericproulx).
102+
* [#3042](https://github.com/ruby-grape/grape/pull/3042): Answer `HEAD` without a body from a mounted Rack app by wrapping the router in `Rack::Head` - [@ericproulx](https://github.com/ericproulx).
102103
* Your contribution here.
103104

104105
### 4.0.1 (2026-09-15)

‎UPGRADING.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ Upgrading Grape
33

44
### Upgrading to >= 4.1.0
55

6+
#### `Rack::Head` wraps the router instead of each endpoint's stack
7+
8+
`Rack::Head` used to be the first middleware in each endpoint's stack. That missed a Rack app mounted with `mount`, which the router calls directly, and a middleware inserted ahead of it with `insert 0`: a `HEAD` request to either got the full body back. A single `Rack::Head` around the router now strips the body of every `HEAD` response the API gives ([#3042](https://github.com/ruby-grape/grape/pull/3042)).
9+
10+
An endpoint's stack now starts with `Grape::Middleware::Error`, and three things follow from that:
11+
12+
* `insert_before Rack::Head` and `insert_after Rack::Head` raise `No such middleware to insert before: Rack::Head` (or `after`) when the API compiles. Use `insert 0` to put a middleware first.
13+
* A middleware inserted by index lands one place further in. `insert 1` used to put it between `Rack::Head` and `Grape::Middleware::Error`; it now puts it inside `Grape::Middleware::Error`, so `rescue_from` handles what it raises. Use `insert 0` to keep it outside.
14+
* A middleware inserted with `insert 0` now sees the full body of a `HEAD` response, as one added with `use` always has.
15+
616
#### A `:jsonl` format you declared now gets Grape's JSON Lines formatter, parser and error formatter
717

818
Grape now renders, parses and reports errors in JSON Lines under `:jsonl` ([#3029](https://github.com/ruby-grape/grape/pull/3029)). An API that declared `content_type :jsonl, ...` itself, with nothing of its own registered for it, gets Grape's:

‎lib/grape/api/instance.rb‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ def initialize
112112

113113
@router.compile!
114114
@router.freeze
115-
@app = lint? ? Rack::Lint.new(@router) : @router
115+
# One Rack::Head around the router strips the body of every HEAD
116+
# response the API gives, including the router's own 404 and a mounted
117+
# Rack app's, which go through no endpoint's stack. Rack::Lint goes
118+
# outside it, where it sees the response that is served.
119+
@app = Rack::Head.new(@router)
120+
@app = Rack::Lint.new(@app) if lint?
116121
@cascade = self.class.cascade
117122
end
118123

‎lib/grape/endpoint.rb‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ def build_stack
462462
content_types = setting.content_types
463463
format = setting.format
464464

465-
stack.use Rack::Head
466465
stack.use Grape::Middleware::Error, **error_middleware_options(format, content_types)
467466

468467
stack.concat setting.middleware

‎lib/grape/router.rb‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def associate_routes(greedy_route)
4343

4444
def call(env)
4545
request_method = env[Rack::REQUEST_METHOD]
46-
with_optimization(request_method) do
46+
with_optimization do
4747
input = Grape::Util::PathNormalizer.call(env[Rack::PATH_INFO])
4848
if @utf8_patterns && !input.ascii_only?
4949
input = utf8_path(input)
@@ -316,17 +316,15 @@ def resolve_capture_groups(union, routes)
316316
union
317317
end
318318

319-
def with_optimization(request_method = nil)
319+
def with_optimization
320320
compile!
321-
yield || default_response(request_method)
321+
yield || default_response
322322
end
323323

324-
# The 404 answered when nothing matched. A HEAD request gets it without a
325-
# body, as Rack requires: every endpoint strips its own with Rack::Head,
326-
# but no endpoint answers a path nothing matched.
327-
def default_response(request_method = nil)
328-
body = request_method == Rack::HEAD ? [] : DEFAULT_RESPONSE_BODY.dup
329-
[404, DEFAULT_RESPONSE_HEADERS.dup, body]
324+
# The 404 answered when nothing matched. The Rack::Head the API wraps
325+
# around the router strips its body from a HEAD request.
326+
def default_response
327+
[404, DEFAULT_RESPONSE_HEADERS.dup, DEFAULT_RESPONSE_BODY.dup]
330328
end
331329

332330
# Which alternative of the union matched is answered by scanning one group

‎spec/grape/api_spec.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,26 @@ def query_request(body, content_type: 'application/json')
13481348
head '/example'
13491349
expect(last_response).to be_bad_request
13501350
end
1351+
1352+
it 'answers HEAD to a mounted Rack app without a body' do
1353+
subject.mount ->(_env) { [200, { 'content-type' => 'text/plain' }, ['mounted']] } => '/rack'
1354+
head '/rack'
1355+
expect(last_response).to be_successful
1356+
expect(last_response.body).to be_empty
1357+
end
1358+
1359+
it 'answers HEAD without the body a middleware inserted ahead of the others gives' do
1360+
overwriter = Class.new(Grape::Middleware::Base) do
1361+
def after
1362+
[200, { 'content-type' => 'text/plain' }, ['Overwritten.']]
1363+
end
1364+
end
1365+
subject.insert 0, overwriter
1366+
subject.get('example') { 'example' }
1367+
head '/example'
1368+
expect(last_response).to be_successful
1369+
expect(last_response.body).to be_empty
1370+
end
13511371
end
13521372

13531373
context 'do_not_route_head!' do

‎spec/grape/router_spec.rb‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,6 @@ def request_path(path)
383383
expect(last_response.status).to eq(404)
384384
end
385385

386-
# No endpoint's stack answers a path nothing matched, so none of their
387-
# Rack::Head middlewares is there to strip the body.
388386
it 'answers HEAD to a path no route matches without a body' do
389387
head '/nothing/here'
390388
expect(last_response.status).to eq(404)

0 commit comments

Comments
 (0)