Merge pull request #743 from vipulnsward/minor_optimizations
Minor optimizations
This commit is contained in:
commit
7af7d2aaf7
|
@ -22,8 +22,8 @@ module Sinatra
|
||||||
# Returns an array of acceptable media types for the response
|
# Returns an array of acceptable media types for the response
|
||||||
def accept
|
def accept
|
||||||
@env['sinatra.accept'] ||= begin
|
@env['sinatra.accept'] ||= begin
|
||||||
entries = @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS)
|
@env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS).
|
||||||
entries.map { |e| AcceptEntry.new(e) }.sort
|
map! { |e| AcceptEntry.new(e) }.sort
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ module Sinatra
|
||||||
attr_accessor :params
|
attr_accessor :params
|
||||||
|
|
||||||
def initialize(entry)
|
def initialize(entry)
|
||||||
params = entry.scan(HEADER_PARAM).map do |s|
|
params = entry.scan(HEADER_PARAM).map! do |s|
|
||||||
key, value = s.strip.split('=', 2)
|
key, value = s.strip.split('=', 2)
|
||||||
value = value[1..-2].gsub(/\\(.)/, '\1') if value.start_with?('"')
|
value = value[1..-2].gsub(/\\(.)/, '\1') if value.start_with?('"')
|
||||||
[key, value]
|
[key, value]
|
||||||
|
@ -79,7 +79,7 @@ module Sinatra
|
||||||
@entry = entry
|
@entry = entry
|
||||||
@type = entry[/[^;]+/].delete(' ')
|
@type = entry[/[^;]+/].delete(' ')
|
||||||
@params = Hash[params]
|
@params = Hash[params]
|
||||||
@q = @params.delete('q') { "1.0" }.to_f
|
@q = @params.delete('q') { 1.0 }.to_f
|
||||||
end
|
end
|
||||||
|
|
||||||
def <=>(other)
|
def <=>(other)
|
||||||
|
@ -114,6 +114,7 @@ module Sinatra
|
||||||
# http://rack.rubyforge.org/doc/classes/Rack/Response.html
|
# http://rack.rubyforge.org/doc/classes/Rack/Response.html
|
||||||
# http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
|
# http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
|
||||||
class Response < Rack::Response
|
class Response < Rack::Response
|
||||||
|
DROP_BODY_RESPONSES = [204, 205, 304]
|
||||||
def initialize(*)
|
def initialize(*)
|
||||||
super
|
super
|
||||||
headers['Content-Type'] ||= 'text/html'
|
headers['Content-Type'] ||= 'text/html'
|
||||||
|
@ -161,7 +162,7 @@ module Sinatra
|
||||||
end
|
end
|
||||||
|
|
||||||
def drop_body?
|
def drop_body?
|
||||||
[204, 205, 304].include?(status.to_i)
|
DROP_BODY_RESPONSES.include?(status.to_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -453,7 +454,7 @@ module Sinatra
|
||||||
hash.each do |key, value|
|
hash.each do |key, value|
|
||||||
key = key.to_s.tr('_', '-')
|
key = key.to_s.tr('_', '-')
|
||||||
value = value.to_i if key == "max-age"
|
value = value.to_i if key == "max-age"
|
||||||
values << [key, value].join('=')
|
values << "#{key}=#{value}"
|
||||||
end
|
end
|
||||||
|
|
||||||
response['Cache-Control'] = values.join(', ') if values.any?
|
response['Cache-Control'] = values.join(', ') if values.any?
|
||||||
|
@ -512,6 +513,7 @@ module Sinatra
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ETAG_KINDS = [:strong, :weak]
|
||||||
# Set the response entity tag (HTTP 'ETag' header) and halt if conditional
|
# Set the response entity tag (HTTP 'ETag' header) and halt if conditional
|
||||||
# GET matches. The +value+ argument is an identifier that uniquely
|
# GET matches. The +value+ argument is an identifier that uniquely
|
||||||
# identifies the current version of the resource. The +kind+ argument
|
# identifies the current version of the resource. The +kind+ argument
|
||||||
|
@ -527,12 +529,12 @@ module Sinatra
|
||||||
kind = options[:kind] || :strong
|
kind = options[:kind] || :strong
|
||||||
new_resource = options.fetch(:new_resource) { request.post? }
|
new_resource = options.fetch(:new_resource) { request.post? }
|
||||||
|
|
||||||
unless [:strong, :weak].include?(kind)
|
unless ETAG_KINDS.include?(kind)
|
||||||
raise ArgumentError, ":strong or :weak expected"
|
raise ArgumentError, ":strong or :weak expected"
|
||||||
end
|
end
|
||||||
|
|
||||||
value = '"%s"' % value
|
value = '"%s"' % value
|
||||||
value = 'W/' + value if kind == :weak
|
value = "W/#{value}" if kind == :weak
|
||||||
response['ETag'] = value
|
response['ETag'] = value
|
||||||
|
|
||||||
if success? or status == 304
|
if success? or status == 304
|
||||||
|
@ -770,7 +772,7 @@ module Sinatra
|
||||||
def render(engine, data, options = {}, locals = {}, &block)
|
def render(engine, data, options = {}, locals = {}, &block)
|
||||||
# merge app-level options
|
# merge app-level options
|
||||||
engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
|
engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
|
||||||
options = engine_options.merge(options)
|
options.merge!(engine_options) { |key, v1, v2| v1 }
|
||||||
|
|
||||||
# extract generic options
|
# extract generic options
|
||||||
locals = options.delete(:locals) || locals || {}
|
locals = options.delete(:locals) || locals || {}
|
||||||
|
@ -802,8 +804,8 @@ module Sinatra
|
||||||
|
|
||||||
# render layout
|
# render layout
|
||||||
if layout
|
if layout
|
||||||
options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
|
options.merge!(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope).
|
||||||
options.merge! layout_options
|
merge!(layout_options)
|
||||||
catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
|
catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue