Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7643 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

X-Sendfile broken, Content-Length always overridden

Reported by: chewi Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: major Keywords:
Cc: f.svehla

Description

Changeset [5622] appears to have broken X-Sendfile, though I'm surprised this hasn't been reported already. I don't think I'm doing anything wrong. I have always used X-Sendfile like this, as I believe most people do.

response.headers["X-Sendfile"] = file
response.headers['Content-Length'] = File.size(file) rescue nil
render :nothing => true

The problem is that Content-Length now gets overridden by Rails using @body.size, which is 1 when using render :nothing => true. This new Rails code has since been moved in changeset [6165]. I haven't tried this version but it looks as though the problem remains. Changing self.headers[["Content-Length"]] = ... to self.headers[["Content-Length"]] ||= ... would probably do the trick though I have always wondered why the web server doesn't set Content-Length itself when using X-Sendfile anyway. lighttpd doesn't appear to, at least not in version 1.4.11. Maybe Apache does and that's why no one else has reported this yet?

Change History

04/19/07 14:06:21 changed by chewi

Hmmm making this change to current SVN breaks render_component. I guess Content-Length should not be set by a component but this part of the code doesn't have any access to the current controller. Ideas?

07/16/07 00:17:22 changed by f.svehla

  • cc set to f.svehla.

12/14/07 22:03:45 changed by orangechicken

X-Sendfile appears to still be broken in latest edge. Confirm?

12/14/07 22:22:32 changed by chewi

I'm not about to try edge anytime soon so I can't confirm that in a hurry but looking at it now, I guess the easiest solution would be to simply not set Content-Length when X-Sendfile is set in the headers. Alternatively it could set Content-Length to the file size of the file given in X-Sendfile but maybe that's best left to the application.

01/13/08 13:12:30 changed by chuyeow

  • status changed from new to closed.
  • resolution set to fixed.

r8628 adds the :x_sendfile option to send_file, and relies on #head (sending a HEAD response) to return a response to the HTTP client. I think this bypasses the issue reported here since it doesn't set the Content-Length header, but do re-open if you are still encountering this problem on edge.