Skip to content

Commit 9631fde

Browse files
samuel-williams-shopifyioquatix
authored andcommitted
Better handling of HEAD requests.
1 parent 4ae402a commit 9631fde

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/protocol/rack/body.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def self.no_content?(status)
3737
# @parameter headers [Hash] The response headers.
3838
# @parameter body [Object] The response body to wrap.
3939
# @parameter input [Object] Optional input for streaming bodies.
40+
# @parameter head [Boolean] Indicates if this is a HEAD request, which should not have a body.
4041
# @returns [Protocol::HTTP::Body] The wrapped response body.
41-
def self.wrap(env, status, headers, body, input = nil)
42+
def self.wrap(env, status, headers, body, input = nil, head = false)
4243
# In no circumstance do we want this header propagating out:
4344
if length = headers.delete(CONTENT_LENGTH)
4445
# We don't really trust the user to provide the right length to the transport.
@@ -84,6 +85,14 @@ def self.wrap(env, status, headers, body, input = nil)
8485
end
8586
end
8687

88+
if head
89+
if body
90+
body = ::Protocol::HTTP::Body::Head.for(body)
91+
elsif length
92+
body = ::Protocol::HTTP::Body::Head.new(length)
93+
end
94+
end
95+
8796
return body
8897
end
8998

lib/protocol/rack/response.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ def self.wrap(env, status, headers, meta, body, request = nil)
5050
body = hijack_body
5151
end
5252

53-
body = Body.wrap(env, status, headers, body, request&.body)
54-
55-
if request&.head?
56-
# I thought about doing this in Output.wrap, but decided the semantics are too tricky. Specifically, the various ways a rack response body can be wrapped, and the need to invoke #close at the right point.
57-
body = ::Protocol::HTTP::Body::Head.for(body)
58-
end
53+
body = Body.wrap(env, status, headers, body, request&.body, request&.head?)
5954

6055
protocol = meta[RACK_PROTOCOL]
6156

0 commit comments

Comments
 (0)