class Rad::Http::Request < Rack::Request
# Returns an array of acceptable media types for the response
# def accept
# @env['HTTP_ACCEPT'].to_s.split(',').map { |a| a.split(';')[0].strip }
# end
#
# def secure?
# (@env['HTTP_X_FORWARDED_PROTO'] || @env['rack.url_scheme']) == 'https'
# end
# Returns all the \subdomains as an array, so ["dev", "www"] would be
# returned for "dev.www.rubyonrails.org". You can specify a different tld_length,
# such as 2 to catch ["www"] instead of ["www", "rubyonrails"]
# in "www.rubyonrails.co.uk".
def subdomains(tld_length = 1)
return [] unless named_host?(host)
parts = host.split('.')
parts[0..-(tld_length+2)]
end
# Returns the \domain part of a \host, such as "rubyonrails.org" in "www.rubyonrails.org". You can specify
# a different tld_length, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".
def domain(tld_length = 1)
return nil unless named_host?(host)
host.split('.').last(1 + tld_length).join('.')
end
def cookies_with_memory
@cookies_with_memory ||= cookies_without_memory
end
alias_method_chain :cookies, :memory
def normalized_domain
return nil unless named_host?(host)
host.sub('www.', '').downcase
end
def from_browser?
# format = workspace.params.format
# (format.present? and !rad.http.browser_generated_formats.include?(format)) or
content_type.present? and rad.http.browser_generated_types.include?(content_type.downcase)
end
#
# Need this to access original, not normalized with Utils.normalize_params params
#
def raw_params
post = {}
@env['rack.input'].read.split('&').each do |tuple|
k, v = tuple.split('=')
post[Rack::Utils.unescape(k || "")] = Rack::Utils.unescape(v || "")
end
self.GET.merge(post)
rescue EOFError
self.GET
end
class << self
def stub url = '/'
env = stub_environment(url)
Rad::Http::Request.new env
end
def stub_environment url = nil
env = {
'rack.url_scheme' => 'http',
'PATH_INFO' => '/',
'HTTP_HOST' => 'test.com',
'rack.input' => StringIO.new
}
if url
uri = Uri.parse url
env.merge!(
'HTTP_HOST' => %(#{uri.host}#{":#{uri.port}" if uri.port.present?}),
# 'REQUEST_PATH' => uri.path,
'PATH_INFO' => uri.path,
# 'REQUEST_URI' => uri.path,
'QUERY_STRING' => uri.query
)
end
env
end
end
protected
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end
end