Skip to content

Commit

Permalink
fix sendfile.type headers
Browse files Browse the repository at this point in the history
  • Loading branch information
superp committed Nov 2, 2011
1 parent 3bafb78 commit cf375a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/simple_captcha/image.rb
Expand Up @@ -74,10 +74,10 @@ def generate_simple_captcha_image(simple_captcha_key) #:nodoc

SimpleCaptcha::Utils::run("convert", params.join(' '))

#dst.close
dst.close

#File.expand_path(dst.path)
dst
File.expand_path(dst.path)
#dst
end
end
end
51 changes: 20 additions & 31 deletions lib/simple_captcha/middleware.rb
Expand Up @@ -3,6 +3,11 @@ module SimpleCaptcha
class Middleware
include SimpleCaptcha::ImageHelpers

DEFAULT_SEND_FILE_OPTIONS = {
:type => 'application/octet-stream'.freeze,
:disposition => 'attachment'.freeze,
}.freeze

def initialize(app, options={})
@app = app
self
Expand All @@ -20,32 +25,15 @@ def call(env) # :nodoc:
def make_image(env, headers = {}, status = 404)
request = Rack::Request.new(env)
code = request.params["code"]
body = []

if !code.blank? && Utils::simple_captcha_value(code)
status = 200
body = generate_simple_captcha_image(code)
headers['Content-Type'] = 'image/jpeg'
#body = File.open(body, "rb")
#status, headers, body = @app.call(env)
#status = 200
#body = generate_simple_captcha_image(code)
#headers['Content-Type'] = 'image/jpeg'

case type = variation(env)
when 'X-Accel-Redirect'
path = File.expand_path(body.to_path)
if url = map_accel_path(env, path)
headers['Content-Length'] = '0'
headers[type] = url
body = []
else
env['rack.errors'].puts "X-Accel-Mapping header missing"
end
when 'X-Sendfile', 'X-Lighttpd-Send-File'
path = File.expand_path(body.to_path)
headers['Content-Length'] = '0'
headers[type] = path
body = []
when '', nil
else
env['rack.errors'].puts "Unknown x-sendfile variation: '#{type}'.\n"
end
return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
end

[status, headers, body]
Expand All @@ -55,15 +43,16 @@ def captcha_path?(request_path)
request_path.include?('/simple_captcha')
end

def variation(env)
env['sendfile.type'] || env['HTTP_X_SENDFILE_TYPE']
end
def send_file(path, options = {})
raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)

def map_accel_path(env, file)
if mapping = env['HTTP_X_ACCEL_MAPPING']
internal, external = mapping.split('=', 2).map{ |p| p.strip }
file.sub(/^#{internal}/i, external)
end
options[:filename] ||= File.basename(path) unless options[:url_based_filename]

status = options[:status] || 200
headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'}
response_body = File.open(path, "rb")

[status, headers, response_body]
end
end
end

0 comments on commit cf375a9

Please sign in to comment.