0.9 Response.body and Content-Length

30 views
Skip to first unread message

John Mettraux

unread,
Jan 7, 2009, 6:47:34 PM1/7/09
to rack-...@googlegroups.com
Hi,

at first, thanks a lot for Rack. Its simplicity and efficiency are a delight.


This is not really a bug, just a small thing I came across :

I was testing an application I wrote with Rack 0.4.0. With 0.9.0 it
became stuck with a content-length of 0. I tried the new ContentLength
middleware but, still at 0, then I noticed that the header was set
before the ContentLength took a look at the env. So I went back and
the next stop was Rack::Response#finish.

http://github.com/chneukirchen/rack/tree/0.9/lib/rack/response.rb#L103-104

The 0.9.0 Rack::Response.finish sets the Content-Length (to
@length.to_s) if not present. Fine. Since I was using response.body =
"foo" or response.body = [ 'bar' ] and not response.write('foo') the
@length was staying at its initial value of 0

So, the workaround for me is to use Rack::Response#write but since the
response class exposes :body, I was thinking maybe this current
implementation might trap someone else.

While investigating all of that, I also noticed that
Rack::Response#finish is replying with [ status, atts, self ] and that
Rack::ContentLength doesn't seem to handle body that are instances of
Rack::Response (http://github.com/chneukirchen/rack/tree/0.9/lib/rack/content_length.rb#L12-20).
Well, maybe that's completely intentional...


OK, sorry for this mumbling, I wish you guys a happy new year,

--
John Mettraux - http://jmettraux.wordpress.com

rue

unread,
Jan 7, 2009, 6:51:51 PM1/7/09
to Rack Development
On Jan 8, 1:47 am, "John Mettraux" <jmettr...@gmail.com> wrote:
> Hi,
>
> at first, thanks a lot for Rack. Its simplicity and efficiency are a delight.
>
> This is not really a bug, just a small thing I came across :

I would call it a bug :)

> I was testing an application I wrote with Rack 0.4.0. With 0.9.0 it
> became stuck with a content-length of 0. I tried the new ContentLength
> middleware but, still at 0, then I noticed that the header was set
> before the ContentLength took a look at the env. So I went back and
> the next stop was Rack::Response#finish.
>
> http://github.com/chneukirchen/rack/tree/0.9/lib/rack/response.rb#L10...
>
> The 0.9.0 Rack::Response.finish sets the Content-Length (to
> @length.to_s) if not present. Fine. Since I was using response.body =
> "foo" or response.body = [ 'bar' ] and not response.write('foo') the
> @length was staying at its initial value of 0
>
> So, the workaround for me is to use Rack::Response#write but since the
> response class exposes :body, I was thinking maybe this current
> implementation might trap someone else.

If the intent is that ContentLength is a safeguard or fallback,
there must be some other protocol for it to trigger than the
absence of Content-Length since it may be 0 from #finish.

This is off the assumption that the middleware is the more
optional component. If not, the better place to fix this is
in #finish instead.


Regards,
Eero (rue)

rue

unread,
Jan 9, 2009, 11:35:33 AM1/9/09
to Rack Development
On Jan 8, 1:51 am, rue <totallyrandom12...@gmail.com> wrote:
>
> If the intent is that ContentLength is a safeguard or fallback,
> there must be some other protocol for it to trigger than the
> absence of Content-Length since it may be 0 from #finish.
>
> This is off the assumption that the middleware is the more
> optional component. If not, the better place to fix this is
> in #finish instead.

Submitted http://rack.lighthouseapp.com/projects/22435/tickets/14-patch-fix-responsefinish-content-length-handling#ticket-14-1.

Christian Neukirchen

unread,
Jan 9, 2009, 12:12:21 PM1/9/09
to rack-...@googlegroups.com
rue <totallyra...@gmail.com> writes:

Applied.

--
Christian Neukirchen <chneuk...@gmail.com> http://chneukirchen.org

Reply all
Reply to author
Forward
0 new messages