lib/sinatra.rb in sinatra-0.3.1 vs lib/sinatra.rb in sinatra-0.3.2

- old
+ new

@@ -47,22 +47,17 @@ end module Utils extend self end - - module Handler - autoload :Mongrel, ::File.dirname(__FILE__) + "/sinatra/rack/handler/mongrel" - end - end module Sinatra extend self - VERSION = '0.3.0' + VERSION = '0.3.2' class NotFound < RuntimeError def self.code ; 404 ; end end class ServerError < RuntimeError @@ -200,39 +195,34 @@ end class Static include Rack::Utils + def initialize(app) + @app = app + end + def invoke(request) - return unless File.file?( - Sinatra.application.options.public + unescape(request.path_info) - ) + path = @app.options.public + unescape(request.path_info) + return unless File.file?(path) + block = Proc.new { send_file path, :disposition => nil } Result.new(block, {}, 200) end - - def block - Proc.new do - send_file Sinatra.application.options.public + - unescape(request.path_info), :disposition => nil - end - end - end # Methods for sending files and streams to the browser instead of rendering. module Streaming DEFAULT_SEND_FILE_OPTIONS = { :type => 'application/octet-stream'.freeze, :disposition => 'attachment'.freeze, :stream => true, - :buffer_size => 4096 + :buffer_size => 8192 }.freeze class MissingFile < RuntimeError; end class FileStreamer - attr_reader :path, :options def initialize(path, options) @path, @options = path, options end @@ -240,22 +230,21 @@ def to_result(cx, *args) self end def each + size = options[:buffer_size] File.open(path, 'rb') do |file| - while buf = file.read(options[:buffer_size]) + while buf = file.read(size) yield buf end end end - end protected - - # Sends the file by streaming it 4096 bytes at a time. This way the + # Sends the file by streaming it 8192 bytes at a time. This way the # whole file doesn't need to be read into memory at once. This makes # it feasible to send even large files. # # Be careful to sanitize the path parameter if it coming from a web # page. send_file(params[:path]) allows a malicious user to @@ -272,11 +261,11 @@ # Content-Transfer-Encoding headers are omitted entirely. # * <tt>:stream</tt> - whether to send the file to the user agent as it # is read (true) or to read the entire file before sending (false). # Defaults to true. # * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used - # to stream the file. Defaults to 4096. + # to stream the file. Defaults to 8192. # * <tt>:status</tt> - specifies the status code to send with the # response. Defaults to '200 OK'. # * <tt>:last_modified</tt> - an optional RFC 2616 formatted date value # (See Time#httpdate) indicating the last modified time of the file. # If the request includes an If-Modified-Since header that matches this @@ -317,16 +306,18 @@ options[:length] ||= File.size(path) options[:filename] ||= File.basename(path) options[:type] ||= Rack::File::MIME_TYPES[File.extname(options[:filename])[1..-1]] || 'text/plain' options[:last_modified] ||= File.mtime(path).httpdate + options[:stream] = true unless options.key?(:stream) + options[:buffer_size] ||= DEFAULT_SEND_FILE_OPTIONS[:buffer_size] send_file_headers! options if options[:stream] throw :halt, [options[:status] || 200, FileStreamer.new(path, options)] else - File.open(path, 'rb') { |file| throw :halt, [options[:status] || 200, file.read] } + File.open(path, 'rb') { |file| throw :halt, [options[:status] || 200, [file.read]] } end end # Send binary data to the user as a file download. May set content type, # apparent file name, and specify whether to show data inline or download @@ -357,11 +348,11 @@ # :disposition => 'inline' # # See +send_file+ for more information on HTTP Content-* headers and caching. def send_data(data, options = {}) #:doc: send_file_headers! options.merge(:length => data.size) - throw :halt, [options[:status] || 200, data] + throw :halt, [options[:status] || 200, [data]] end private def send_file_headers!(options) @@ -1283,11 +1274,11 @@ private # Called immediately after the application is initialized or reloaded to # register default events, templates, and error handlers. def load_default_configuration! - events[:get] << Static.new + events[:get] << Static.new(self) configure do error do '<h1>Internal Server Error</h1>' end not_found { '<h1>Not Found</h1>'} @@ -1465,12 +1456,10 @@ end end at_exit do raise $! if $! - if Sinatra.application.options.run - Sinatra.run - end + Sinatra.run if Sinatra.application.options.run end mime :xml, 'application/xml' mime :js, 'application/javascript' mime :png, 'image/png'