module Rad
class 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
protected
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end
end
end