in Hacking

Production gotcha: Rails send_file seems to corrupt files

What’s wrong with the following Rails code?

class ResourceController < ApplicationController  
  DEFAULT_OPTIONS = { :disposition => 'inline' }

  def send_file1
    send_file 'flash1.swf', DEFAULT_OPTIONS
  end

  def send_file2
    send_file 'flash2.swf', DEFAULT_OPTIONS
  end
end

This code works perfectly in development mode. In production mode retrieving the two different files the second file gets corrupt / wrong…

After a long search I looked in the rails code:

  def send_file(path, options = {}) #:doc:
    raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)
 
    options[:length]   ||= File.size(path)
    options[:filename] ||= File.basename(path) unless options[:url_based_filename] 
    send_file_headers! options  
    #.... 
  end

OOops… the send_file code modifies my class constant!
And after the first call the length and filename is placed in the class constant…
This is no problem in development mode because the classes are reloaded every time. In production mode every mongrel server has it’s own instance…

Share