-
-
Save almokhtarbr/2aee4b3295f6361d58cd16f18ddb5eff to your computer and use it in GitHub Desktop.
Revisions
-
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ 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. # @@ -9,6 +12,7 @@ # # www.example.org => example.myapp.com # # Credit: Inspired by http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/ class CustomDomain @@cache = {} -
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,15 +25,14 @@ def call(env) 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) @@ -45,4 +44,9 @@ def cname_lookup(host) @@cache[host] end private def logger RAILS_DEFAULT_LOGGER || Logger.new(STDOUT) end end -
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,11 +23,7 @@ def call(env) # 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(', ') RAILS_DEFAULT_LOGGER.info("CustomDomain: mapped #{host} => #{domain}") end -
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 12 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,12 +23,16 @@ def call(env) # modify Environment variable HTTP_HOST if custom domain is found if custom_domain?(host) domain = cname_lookup(host) env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain # Rails overwrite request.host with this particular header # env["HTTP_X_FORWARDED_HOST"] = host RAILS_DEFAULT_LOGGER.info("CustomDomain: mapped #{host} => #{domain}") end @app.call(env) end def custom_domain?(host) @@ -37,13 +41,12 @@ def custom_domain?(host) 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 end -
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,7 @@ # overwrite HTTP_HOST if needed # # www.example.org => example.myapp.com # class CustomDomain @@cache = {} @@ -29,7 +27,8 @@ def call(env) env["HTTP_X_FORWARDED_HOST"] = host end env # @app.call(env) end def custom_domain?(host) @@ -42,9 +41,9 @@ def cname_lookup(host) return host if @@cache[host] Net::DNS::Resolver.new.query(host).each_cname do |cname| result = cname if cname.include?(@default_domain) end @@cache[host] = result end end -
speedmax revised this gist
Aug 12, 2009 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,9 @@ # overwrite HTTP_HOST if needed # # www.example.org => example.myapp.com # # Base on : http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/ # class CustomDomain @@cache = {} @@ -24,7 +26,7 @@ def call(env) if custom_domain?(host) domain = cname_lookup(host) env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain env["HTTP_X_FORWARDED_HOST"] = host end @app.call(env) -
speedmax created this gist
Aug 11, 2009 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ require File.dirname(__FILE__) + '/../../vendor/gems/net-dns-0.4/lib/net/dns/resolver' # Custom Domain # 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 # 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_HOST"] = port ? "#{domain}:#{port}" : domain env["rack.custom_domain"] = host end @app.call(env) end def custom_domain?(host) domain = @default_domain.sub(/^\./, '') host !~ Regexp.new("#{domain}$", Regexp::IGNORECASE) end def cname_lookup(host) result = nil return host if @@cache[host] Net::DNS::Resolver.new.query(host).each_cname do |cname| result = $0 if cname.include?(@default_domain) end @@cache[host] = result end end