# ActionController#send\_file

**URL:** <https://www.ruby-forum.com/t/actioncontroller-send-file/52287>\
**Category:** Rails\
**Created:** [January 12, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/actioncontroller-send-file/52287 "2006-01-12T15:44:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotjuneshen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f19dbf/32.png) [@hotjuneshen](https://www.ruby-forum.com/u/hotjuneshen)\
**Post date:** [January 12, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/actioncontroller-send-file/52287/1 "2006-01-12T15:44:11Z")

</div>

Looking at the code for ActionController#send\_file I see that it is  
using ruby to send the file contents. Linux supports a sendfile() OS  
call. The implementation of Linux sendfile() is extremely fast and  
very CPU efficient, far faster than what can be done in user space.

Does Windows support a sendfile() equivalent call? I haven’t been  
working with ruby long enough to know how it handles things like this  
on a per platform basis, how is it done?

–  
Jon S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hotjuneshen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f19dbf/32.png) [@hotjuneshen](https://www.ruby-forum.com/u/hotjuneshen)\
**Post date:** [January 12, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/actioncontroller-send-file/52287/2 "2006-01-12T19:59:36Z")

</div>

The rails send\_file method does not use the OS sendfile() call. It  
opens  
the file, reads in a buffer full (default is 4K), and writes it out to  
the  
client.

I don’t know if the CGI, FCGI, SCGI protocols have a way for an  
application  
to return a filename to the web server, and have it send the file using  
OS  
level calls. I’m guessing this is hard, since at least FCGI apps may  
not  
even be running on the same machine!

---

<div class="post-metadata">

**Author:** ![hotjuneshen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f19dbf/32.png) [@hotjuneshen](https://www.ruby-forum.com/u/hotjuneshen)\
**Post date:** [January 12, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/actioncontroller-send-file/52287/3 "2006-01-12T20:17:48Z")

</div>

On 1/12/06, Tom F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The rails send\_file method does not use the OS sendfile() call. It opens  
> the file, reads in a buffer full (default is 4K), and writes it out to the  
> client.
> 
> I don’t know if the CGI, FCGI, SCGI protocols have a way for an application  
> to return a filename to the web server, and have it send the file using OS  
> level calls. I’m guessing this is hard, since at least FCGI apps may not  
> even be running on the same machine!

The rails app would call Linux sendfile() directly, there should be no  
need to involve the web server. Linux sendfile() only need a handle  
for the network socket and a handle for the file. Rails should have  
both of these. First Rails would write out the HTTP headers to the  
socket and then call sendfile() to send the rest of the file.

But I don’t know enough about how FCGI is implemented, does Rail have  
a socket handle directly to the client or is it a socket to the server  
which then forwards the data to the client. Either way sendfile()  
should be a win since it will reduce the CPU usage of the rails app.

> using ruby to send the file contents. Linux supports a sendfile() OS
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Jon S.  
removed\_email\_address@domain.invalid
