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

Changeset 8628

Show
Ignore:
Timestamp:
01/11/08 22:07:04 (1 year ago)
Author:
bitsweat
Message:

Introduce send_file :x_sendfile => true to send an X-Sendfile response header.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8626 r8628  
    11*SVN* 
     2 
     3* Introduce send_file :x_sendfile => true to send an X-Sendfile response header.  [Jeremy Kemper] 
    24 
    35* Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used #10739 [jeremyevans] 
  • trunk/actionpack/lib/action_controller/streaming.rb

    r8075 r8628  
    55      :type         => 'application/octet-stream'.freeze, 
    66      :disposition  => 'attachment'.freeze, 
    7       :stream       => true,  
    8       :buffer_size  => 4096 
     7      :stream       => true, 
     8      :buffer_size  => 4096, 
     9      :x_sendfile   => false 
    910    }.freeze 
     11 
     12    X_SENDFILE_HEADER = 'X-Sendfile'.freeze 
    1013 
    1114    protected 
     
    2326      # * <tt>:type</tt> - specifies an HTTP content type. 
    2427      #   Defaults to 'application/octet-stream'. 
    25       # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.   
     28      # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 
    2629      #   Valid values are 'inline' and 'attachment' (default). 
    2730      # * <tt>:stream</tt> - whether to send the file to the user agent as it is read (true) 
     
    3033      #   Defaults to 4096. 
    3134      # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'. 
    32       # * <tt>:url_based_filename</tt> - set to true if you want the browser guess the filename from  
    33       #   the URL, which is necessary for i18n filenames on certain browsers  
     35      # * <tt>:url_based_filename</tt> - set to true if you want the browser guess the filename from 
     36      #   the URL, which is necessary for i18n filenames on certain browsers 
    3437      #   (setting :filename overrides this option). 
    3538      # 
     
    6871        @performed_render = false 
    6972 
    70         if options[:stream] 
    71           render :status => options[:status], :text => Proc.new { |response, output| 
    72             logger.info "Streaming file #{path}" unless logger.nil? 
    73             len = options[:buffer_size] || 4096 
    74             File.open(path, 'rb') do |file| 
    75               while buf = file.read(len) 
    76                 output.write(buf) 
     73        if options[:x_sendfile] 
     74          logger.info "Sending #{X_SENDFILE_HEADER} header #{path}" if logger 
     75          head options[:status], X_SENDFILE_HEADER => path 
     76        else 
     77          if options[:stream] 
     78            render :status => options[:status], :text => Proc.new { |response, output| 
     79              logger.info "Streaming file #{path}" unless logger.nil? 
     80              len = options[:buffer_size] || 4096 
     81              File.open(path, 'rb') do |file| 
     82                while buf = file.read(len) 
     83                  output.write(buf) 
     84                end 
    7785              end 
    78             end 
    79           } 
    80         else 
    81           logger.info "Sending file #{path}" unless logger.nil? 
    82           File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read } 
     86            } 
     87          else 
     88            logger.info "Sending file #{path}" unless logger.nil? 
     89            File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read } 
     90          end 
    8391        end 
    8492      end 
     
    9199      # * <tt>:type</tt> - specifies an HTTP content type. 
    92100      #   Defaults to 'application/octet-stream'. 
    93       # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.   
     101      # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 
    94102      #   Valid values are 'inline' and 'attachment' (default). 
    95103      # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'. 
     
    106114      # See +send_file+ for more information on HTTP Content-* headers and caching. 
    107115      def send_data(data, options = {}) #:doc: 
    108         logger.info "Sending data #{options[:filename]}" unless logger.nil? 
     116        logger.info "Sending data #{options[:filename]}" if logger 
    109117        send_file_headers! options.merge(:length => data.size) 
    110118        @performed_render = false 
     
    131139 
    132140        # Fix a problem with IE 6.0 on opening downloaded files: 
    133         # If Cache-Control: no-cache is set (which Rails does by default),  
    134         # IE removes the file it just downloaded from its cache immediately  
    135         # after it displays the "open/save" dialog, which means that if you  
    136         # hit "open" the file isn't there anymore when the application that  
     141        # If Cache-Control: no-cache is set (which Rails does by default), 
     142        # IE removes the file it just downloaded from its cache immediately 
     143        # after it displays the "open/save" dialog, which means that if you 
     144        # hit "open" the file isn't there anymore when the application that 
    137145        # is called for handling the download is run, so let's workaround that 
    138146        headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache' 
  • trunk/actionpack/test/controller/send_file_test.rb

    r8564 r8628  
    2828 
    2929  Mime::Type.register "image/png", :png unless defined? Mime::PNG 
    30    
     30 
    3131  def setup 
    3232    @controller = SendFileController.new 
     
    5656    assert_equal file_data, output.string 
    5757  end 
    58    
     58 
    5959  def test_file_url_based_filename 
    6060    @controller.options = { :url_based_filename => true } 
     
    6363    assert_not_nil response 
    6464    assert_equal "attachment", response.headers["Content-Disposition"] 
     65  end 
     66 
     67  def test_x_sendfile_header 
     68    @controller.options = { :x_sendfile => true } 
     69 
     70    response = nil 
     71    assert_nothing_raised { response = process('file') } 
     72    assert_not_nil response 
     73 
     74    assert_equal @controller.file_path, response.headers['X-Sendfile'] 
     75    assert response.body.blank? 
    6576  end 
    6677