Prev | Current Page 154 | Next

Brad Ediger

"Advanced Rails"


The X-Sendfile protocol is a very simple standard, first introduced in the Lighttpd
web server, which directs the web server to send a file from the filesystem to the client
rather than a response generated by the application server. Because the web
server is optimized for throwing files at the client, this usually yields a decent speed
improvement over reading the file into memory and sending it from Rails with the
send_file or send_data API calls.
Because the web server requires access to the file in order to send it to the client, you
must use filesystem large object storage. In addition, the files to be sent must have
permissions set so as to be accessible to the web server. However, the files should be
outside of the web root, lest someone guess a filename and have free access to your
private files.
X-Sendfile uses the X-Sendfile HTTPheader pointing to the server??™s path to the file
to send, in conjunction with the other standard HTTPheaders. A typical response
using X-Sendfile would look something like this:
X-Sendfile: /home/rails/sample_application/private/secret_codes_69843.zip
Content-Type: application/octet-stream
Content-Disposition: attachment; file="secret_codes.zip"
Content-Length: 654685
Assuming the web server is properly configured, it will ignore any response body and
stream the file from disk to the client.
106 | Chapter 4: Database
From Rails, you can set the response headers by modifying the response.


Pages:
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166