mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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.
This commit is contained in:
parent
b6d05bc6cd
commit
7c77c2291b
1 changed files with 12 additions and 18 deletions
|
@ -197,20 +197,16 @@ module Sinatra
|
||||||
class Static
|
class Static
|
||||||
include Rack::Utils
|
include Rack::Utils
|
||||||
|
|
||||||
|
def initialize(app)
|
||||||
|
@app = app
|
||||||
|
end
|
||||||
|
|
||||||
def invoke(request)
|
def invoke(request)
|
||||||
return unless File.file?(
|
path = @app.options.public + unescape(request.path_info)
|
||||||
Sinatra.application.options.public + unescape(request.path_info)
|
return unless File.file?(path)
|
||||||
)
|
block = Proc.new { send_file path, :disposition => nil }
|
||||||
Result.new(block, {}, 200)
|
Result.new(block, {}, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
def block
|
|
||||||
Proc.new do
|
|
||||||
send_file Sinatra.application.options.public +
|
|
||||||
unescape(request.path_info), :disposition => nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Methods for sending files and streams to the browser instead of rendering.
|
# Methods for sending files and streams to the browser instead of rendering.
|
||||||
|
@ -219,7 +215,7 @@ module Sinatra
|
||||||
:type => 'application/octet-stream'.freeze,
|
:type => 'application/octet-stream'.freeze,
|
||||||
:disposition => 'attachment'.freeze,
|
:disposition => 'attachment'.freeze,
|
||||||
:stream => true,
|
:stream => true,
|
||||||
:buffer_size => 4096
|
:buffer_size => 8192
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
class MissingFile < RuntimeError; end
|
class MissingFile < RuntimeError; end
|
||||||
|
@ -246,7 +242,7 @@ module Sinatra
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
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
|
# whole file doesn't need to be read into memory at once. This makes
|
||||||
# it feasible to send even large files.
|
# 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).
|
# is read (true) or to read the entire file before sending (false).
|
||||||
# Defaults to true.
|
# Defaults to true.
|
||||||
# * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used
|
# * <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
|
# * <tt>:status</tt> - specifies the status code to send with the
|
||||||
# response. Defaults to '200 OK'.
|
# response. Defaults to '200 OK'.
|
||||||
# * <tt>:last_modified</tt> - an optional RFC 2616 formatted date value
|
# * <tt>:last_modified</tt> - an optional RFC 2616 formatted date value
|
||||||
|
@ -1280,7 +1276,7 @@ module Sinatra
|
||||||
# Called immediately after the application is initialized or reloaded to
|
# Called immediately after the application is initialized or reloaded to
|
||||||
# register default events, templates, and error handlers.
|
# register default events, templates, and error handlers.
|
||||||
def load_default_configuration!
|
def load_default_configuration!
|
||||||
events[:get] << Static.new
|
events[:get] << Static.new(self)
|
||||||
configure do
|
configure do
|
||||||
error do
|
error do
|
||||||
'<h1>Internal Server Error</h1>'
|
'<h1>Internal Server Error</h1>'
|
||||||
|
@ -1462,9 +1458,7 @@ end
|
||||||
|
|
||||||
at_exit do
|
at_exit do
|
||||||
raise $! if $!
|
raise $! if $!
|
||||||
if Sinatra.application.options.run
|
Sinatra.run if Sinatra.application.options.run
|
||||||
Sinatra.run
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mime :xml, 'application/xml'
|
mime :xml, 'application/xml'
|
||||||
|
|
Loading…
Reference in a new issue