Handling binary files with mod_ruby and cgi

I’m trying to write a CGI script that will fetch a named file and send
it back to the browser. No matter what output method I use, it’s
writing two newlines to the end of the file, which corrupts many
binary file formats. I’m sure I’m missing something obvious. Here’s a
simple version of the script (yes, I’m aware it can be used to read
any file on the system - the full version sanitizes the filename and
restricts access to a specific directory):

require ‘mime/types’
cgi = CGI.new()
file = cgi[‘file’].untaint
mimetype = MIME::Types.type_for(file)[0]
cgi.header(mimetype.nil? ? “application/octet-stream” :
mimetype.simplified)
write File.read(‘file’)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs