Permalink
Browse files

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.
  • Loading branch information...
1 parent b6d05bc commit 7c77c2291bb7b7a69eefdf67e77f783c67848946 @rtomayko rtomayko committed Nov 2, 2008
Showing with 12 additions and 18 deletions.
  1. +12 −18 lib/sinatra.rb
View
@@ -197,20 +197,16 @@ def code
class Static
include Rack::Utils
- def invoke(request)
- return unless File.file?(
- Sinatra.application.options.public + unescape(request.path_info)
- )
- Result.new(block, {}, 200)
+ def initialize(app)
+ @app = app
end
- def block
- Proc.new do
- send_file Sinatra.application.options.public +
- unescape(request.path_info), :disposition => nil
- end
+ def invoke(request)
+ 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
-
end
# Methods for sending files and streams to the browser instead of rendering.
@@ -219,7 +215,7 @@ module Streaming
: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 @@ def each
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 @@ def each
# 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
@@ -1280,7 +1276,7 @@ def dispatch(env)
# 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>'
@@ -1462,9 +1458,7 @@ def to_result(cx, *args)
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'

0 comments on commit 7c77c22

Please sign in to comment.