Handling binary files with mod_ruby and cgi

Z

zak.wilson

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')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top