Rack::ETag does not set headers for Array type response bodies

45 views
Skip to first unread message

Karel Minarik

unread,
Mar 4, 2010, 8:17:37 AM3/4/10
to Rack Development
Hi,

I wanted to `use Rack::ETag` from rack-contrib [1] to automatically
set ETag headers in a Sinatra application, and discovered that it
checks whether the response body is a String. Since Rack response
bodies are _specifically_ required to _not_ be an instance of String
-- as far as I understand [2] -- and eg. Sinatra serves an Array by
default [3], this fails.

The solution would be to just call `body.to_s`, as you can see in this
commit: [4]. I am not sure how it would affect streaming responses,
but the current implementation of Rack::ETag is more or less useless
in Sinatra /and possibly elsewhere/.

Karel

[1] http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/etag.rb
[2] http://rack.rubyforge.org/doc/files/SPEC.html
[3] http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L57-62
[4] http://github.com/karmi/rack-contrib/commit/56f02f3e294cece010caed7865581ba63be6f868#diff-0

Ryan Tomayko

unread,
Mar 4, 2010, 10:48:01 AM3/4/10
to rack-...@googlegroups.com

Rack::ETag has been a part of Rack core for a while now:

http://github.com/rack/rack/blob/master/lib/rack/etag.rb

It fixes the issues mentioned. I'll remove the version contrib.

Thanks,
Ryan

Karel Minarik

unread,
Mar 4, 2010, 12:49:55 PM3/4/10
to Rack Development
Argh, thanks, Ryan!!

Karel

On Mar 4, 4:48 pm, Ryan Tomayko <r...@tomayko.com> wrote:


> On Thu, Mar 4, 2010 at 5:17 AM, Karel Minarik <karel.mina...@gmail.com> wrote:
> > Hi,
>
> > I wanted to `use Rack::ETag` from rack-contrib [1] to automatically
> > set ETag headers in a Sinatra application, and discovered that it
> > checks whether the response body is a String. Since Rack response
> > bodies are _specifically_ required to _not_ be an instance of String
> > -- as far as I understand [2] -- and eg. Sinatra serves an Array by
> > default [3], this fails.
>
> > The solution would be to just call `body.to_s`, as you can see in this
> > commit: [4]. I am not sure how it would affect streaming responses,
> > but the current implementation of Rack::ETag is more or less useless
> > in Sinatra /and possibly elsewhere/.
>
> > Karel
>
> > [1]http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/etag.rb
> > [2]http://rack.rubyforge.org/doc/files/SPEC.html
> > [3]http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L57-62

> > [4]http://github.com/karmi/rack-contrib/commit/56f02f3e294cece010caed786...

Reply all
Reply to author
Forward
0 new messages