Froze rails gems
[depot.git] / vendor / rails / actionpack / lib / action_controller / streaming.rb
1 module ActionController #:nodoc:
2 # Methods for sending files and streams to the browser instead of rendering.
3 module Streaming
4 DEFAULT_SEND_FILE_OPTIONS = {
5 :type => 'application/octet-stream'.freeze,
6 :disposition => 'attachment'.freeze,
7 :stream => true,
8 :buffer_size => 4096,
9 :x_sendfile => false
10 }.freeze
11
12 X_SENDFILE_HEADER = 'X-Sendfile'.freeze
13
14 protected
15 # Sends the file, by default streaming it 4096 bytes at a time. This way the
16 # whole file doesn't need to be read into memory at once. This makes it
17 # feasible to send even large files. You can optionally turn off streaming
18 # and send the whole file at once.
19 #
20 # Be careful to sanitize the path parameter if it is coming from a web
21 # page. <tt>send_file(params[:path])</tt> allows a malicious user to
22 # download any file on your server.
23 #
24 # Options:
25 # * <tt>:filename</tt> - suggests a filename for the browser to use.
26 # Defaults to <tt>File.basename(path)</tt>.
27 # * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'.
28 # * <tt>:length</tt> - used to manually override the length (in bytes) of the content that
29 # is going to be sent to the client. Defaults to <tt>File.size(path)</tt>.
30 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
31 # Valid values are 'inline' and 'attachment' (default).
32 # * <tt>:stream</tt> - whether to send the file to the user agent as it is read (+true+)
33 # or to read the entire file before sending (+false+). Defaults to +true+.
34 # * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used to stream the file.
35 # Defaults to 4096.
36 # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
37 # * <tt>:url_based_filename</tt> - set to +true+ if you want the browser guess the filename from
38 # the URL, which is necessary for i18n filenames on certain browsers
39 # (setting <tt>:filename</tt> overrides this option).
40 # * <tt>:x_sendfile</tt> - uses X-Sendfile to send the file when set to +true+. This is currently
41 # only available with Lighttpd/Apache2 and specific modules installed and activated. Since this
42 # uses the web server to send the file, this may lower memory consumption on your server and
43 # it will not block your application for further requests.
44 # See http://blog.lighttpd.net/articles/2006/07/02/x-sendfile and
45 # http://tn123.ath.cx/mod_xsendfile/ for details. Defaults to +false+.
46 #
47 # The default Content-Type and Content-Disposition headers are
48 # set to download arbitrary binary files in as many browsers as
49 # possible. IE versions 4, 5, 5.5, and 6 are all known to have
50 # a variety of quirks (especially when downloading over SSL).
51 #
52 # Simple download:
53 #
54 # send_file '/path/to.zip'
55 #
56 # Show a JPEG in the browser:
57 #
58 # send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
59 #
60 # Show a 404 page in the browser:
61 #
62 # send_file '/path/to/404.html', :type => 'text/html; charset=utf-8', :status => 404
63 #
64 # Read about the other Content-* HTTP headers if you'd like to
65 # provide the user with more information (such as Content-Description) in
66 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11.
67 #
68 # Also be aware that the document may be cached by proxies and browsers.
69 # The Pragma and Cache-Control headers declare how the file may be cached
70 # by intermediaries. They default to require clients to validate with
71 # the server before releasing cached responses. See
72 # http://www.mnot.net/cache_docs/ for an overview of web caching and
73 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
74 # for the Cache-Control header spec.
75 def send_file(path, options = {}) #:doc:
76 raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)
77
78 options[:length] ||= File.size(path)
79 options[:filename] ||= File.basename(path) unless options[:url_based_filename]
80 send_file_headers! options
81
82 @performed_render = false
83
84 if options[:x_sendfile]
85 logger.info "Sending #{X_SENDFILE_HEADER} header #{path}" if logger
86 head options[:status], X_SENDFILE_HEADER => path
87 else
88 if options[:stream]
89 render :status => options[:status], :text => Proc.new { |response, output|
90 logger.info "Streaming file #{path}" unless logger.nil?
91 len = options[:buffer_size] || 4096
92 File.open(path, 'rb') do |file|
93 while buf = file.read(len)
94 output.write(buf)
95 end
96 end
97 }
98 else
99 logger.info "Sending file #{path}" unless logger.nil?
100 File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read }
101 end
102 end
103 end
104
105 # Send binary data to the user as a file download. May set content type, apparent file name,
106 # and specify whether to show data inline or download as an attachment.
107 #
108 # Options:
109 # * <tt>:filename</tt> - suggests a filename for the browser to use.
110 # * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'.
111 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
112 # Valid values are 'inline' and 'attachment' (default).
113 # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
114 #
115 # Generic data download:
116 #
117 # send_data buffer
118 #
119 # Download a dynamically-generated tarball:
120 #
121 # send_data generate_tgz('dir'), :filename => 'dir.tgz'
122 #
123 # Display an image Active Record in the browser:
124 #
125 # send_data image.data, :type => image.content_type, :disposition => 'inline'
126 #
127 # See +send_file+ for more information on HTTP Content-* headers and caching.
128 def send_data(data, options = {}) #:doc:
129 logger.info "Sending data #{options[:filename]}" if logger
130 send_file_headers! options.merge(:length => data.size)
131 @performed_render = false
132 render :status => options[:status], :text => data
133 end
134
135 private
136 def send_file_headers!(options)
137 options.update(DEFAULT_SEND_FILE_OPTIONS.merge(options))
138 [:length, :type, :disposition].each do |arg|
139 raise ArgumentError, ":#{arg} option required" if options[arg].nil?
140 end
141
142 disposition = options[:disposition].dup || 'attachment'
143
144 disposition <<= %(; filename="#{options[:filename]}") if options[:filename]
145
146 headers.update(
147 'Content-Length' => options[:length],
148 'Content-Type' => options[:type].to_s.strip, # fixes a problem with extra '\r' with some browsers
149 'Content-Disposition' => disposition,
150 'Content-Transfer-Encoding' => 'binary'
151 )
152
153 # Fix a problem with IE 6.0 on opening downloaded files:
154 # If Cache-Control: no-cache is set (which Rails does by default),
155 # IE removes the file it just downloaded from its cache immediately
156 # after it displays the "open/save" dialog, which means that if you
157 # hit "open" the file isn't there anymore when the application that
158 # is called for handling the download is run, so let's workaround that
159 headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache'
160 end
161 end
162 end