cache protocol on the stack to reduce options hash lookups

This commit is contained in:
Aaron Patterson 2014-06-11 11:36:50 -07:00
parent 1c432d1af1
commit aaaff369da

@ -79,17 +79,17 @@ def build_host_url(options)
options[:port] = match[3] unless options.key?(:port) options[:port] = match[3] unless options.key?(:port)
end end
options[:protocol] = normalize_protocol(options) protocol = normalize_protocol options[:protocol]
options[:host] = normalize_host(options) options[:host] = normalize_host(options)
result = options[:protocol].dup result = protocol.dup
if options[:user] && options[:password] if options[:user] && options[:password]
result << "#{Rack::Utils.escape(options[:user])}:#{Rack::Utils.escape(options[:password])}@" result << "#{Rack::Utils.escape(options[:user])}:#{Rack::Utils.escape(options[:password])}@"
end end
result << options[:host] result << options[:host]
normalize_port(options[:port], options[:protocol]) { |port| normalize_port(options[:port], protocol) { |port|
result << ":#{port}" result << ":#{port}"
} }
@ -104,8 +104,8 @@ def same_host?(options)
(options[:subdomain] == true || !options.key?(:subdomain)) && options[:domain].nil? (options[:subdomain] == true || !options.key?(:subdomain)) && options[:domain].nil?
end end
def normalize_protocol(options) def normalize_protocol(protocol)
case options[:protocol] case protocol
when nil when nil
"http://" "http://"
when false, "//" when false, "//"
@ -113,7 +113,7 @@ def normalize_protocol(options)
when PROTOCOL_REGEXP when PROTOCOL_REGEXP
"#{$1}://" "#{$1}://"
else else
raise ArgumentError, "Invalid :protocol option: #{options[:protocol].inspect}" raise ArgumentError, "Invalid :protocol option: #{protocol.inspect}"
end end
end end