HEAD responses contain body

Jonathan Rudenberg jonathan at titanous.com
Thu Jun 13 19:34:31 UTC 2013


On Jun 13, 2013, at 3:28 PM, Jonathan Rudenberg <jonathan at titanous.com> wrote:

> 
> On Jun 13, 2013, at 3:21 PM, Eric Wong <normalperson at yhbt.net> wrote:
> 
>> Jonathan Rudenberg <jonathan at titanous.com> wrote:
>>> On Jun 13, 2013, at 2:22 PM, Eric Wong <normalperson at yhbt.net> wrote:
>>>> Jonathan Rudenberg <jonathan at titanous.com> wrote:
>>>>> RFC 2616 section 9.4[1] states:
>>>>> 
>>>>>> The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.
>>>>> 
>>>>> A HEAD request against this simple Rack app running on unicorn-4.6.2:
>>>>> 
>>>>>  require 'rack'
>>>>> 
>>>> 
>>>> +     use Rack::Head
>>>> 
>>>>>  run lambda { |env| [200, {}, []] }
>>>> 
>>>> The Rack::Head middleware should be used to correctly strip HEAD
>>>> responses of their bodies (frameworks such as Rails/Sinatra should
>>>> already add Rack::Head to the middleware stack for you)
>>> 
>>> This does not change the result, as the Rack::Head implementation looks like this:
>>> 
>>>   def call(env)
>>>     status, headers, body = @app.call(env)
>>> 
>>>     if env["REQUEST_METHOD"] == "HEAD"
>>>       body.close if body.respond_to? :close
>>>       [status, headers, []]
>>>     else
>>>       [status, headers, body]
>>>     end
>>>   end
>> 
>> OK, I think you were hitting another problem because you were lacking
>> Rack::ContentType
>> 
>> Try the following:
>> -----------------------8<---------------------
>> require 'rack'
>> use Rack::ContentLength # less ambiguous than Rack::Chunked adding '0'
>> use Rack::Head
>> use Rack::ContentType
>> run lambda { |env| [200, {}, []] }
>> -----------------------8<---------------------
> 
> Thanks, this stack works.
> 
>> I added the Rack::ContentLength (it's already in the default middleware
>> stack) since I believe Rack::Chunked adding the '0' is a violation of
>> rfc2616... I'll need to read more closely to be sure.
> 
> Hmm, so this is a bug in Rack::Chunked? My reading of the spec says that the '0' is incorrect.

Actually, the solution is that Rack::Head needs to come before Rack::Chunked. Perhaps Rack's default development stack should include this?



More information about the mongrel-unicorn mailing list