speedmax (owner)

Forks

Revisions

gist: 166195 Download_button fork
public
Public Clone URL: git://gist.github.com/166195.git
Embed All Files: show embed
customdomain.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'net/dns/resolver'
 
# Custom Domain
#
# Require net-dns gem
#
# A Rack middleware to to resolve the custom domain to original subdomain
# for your multi telent application.
#
# It's all transperant to your application, it performs cname lookup and
# overwrite HTTP_HOST if needed
#
# www.example.org => example.myapp.com
#
# Credit: Inspired by http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/
class CustomDomain
  @@cache = {}
 
  def initialize(app, default_domain)
    @app = app
    @default_domain = default_domain
  end
 
  def call(env)
    host, port = env["HTTP_HOST"].split(':')
 
    # modify Environment variable HTTP_HOST if custom domain is found
    if custom_domain?(host)
      domain = cname_lookup(host)
      env['HTTP_X_FORWARDED_HOST'] = [host, domain].join(', ')
      
      logger.info("CustomDomain: mapped #{host} => #{domain}")
    end
    
    @app.call(env)
  end
 
  def custom_domain?(host)
    host !~ /#{@default_domain.sub(/^\./, '')}/i
  end
 
  def cname_lookup(host)
    unless @@cache[host]
      Net::DNS::Resolver.new.query(host).each_cname do |cname|
        @@cache[host] = cname if cname.include?(@default_domain)
      end
    end
 
    @@cache[host]
  end
  
  private
    def logger
      RAILS_DEFAULT_LOGGER || Logger.new(STDOUT)
    end
end