From 7c77c2291bb7b7a69eefdf67e77f783c67848946 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Sun, 2 Nov 2008 00:53:03 -0700 Subject: [PATCH] simplify Static implementation; use 8K chunks instead of 4K The Static event handler is now instantiated with a reference back to the Sinatra app instead of using the global singleton. While here, bump streaming buffer size from 4K to 8K -- word around the campfire is that reading in 8K chunks results in a bit less IO on most modern systems. --- lib/sinatra.rb | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index ab075756..a8d5fcb7 100755 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -197,20 +197,16 @@ module Sinatra 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. @@ -219,7 +215,7 @@ module Sinatra :type => 'application/octet-stream'.freeze, :disposition => 'attachment'.freeze, :stream => true, - :buffer_size => 4096 + :buffer_size => 8192 }.freeze class MissingFile < RuntimeError; end @@ -246,7 +242,7 @@ module Sinatra 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. # @@ -267,7 +263,7 @@ module Sinatra # is read (true) or to read the entire file before sending (false). # Defaults to true. # * :buffer_size - specifies size (in bytes) of the buffer used - # to stream the file. Defaults to 4096. + # to stream the file. Defaults to 8192. # * :status - specifies the status code to send with the # response. Defaults to '200 OK'. # * :last_modified - an optional RFC 2616 formatted date value @@ -1280,7 +1276,7 @@ module Sinatra # 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 '

Internal Server Error

' @@ -1462,9 +1458,7 @@ 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'