Skip to content

Commit

Permalink
Two notable changes:
Browse files Browse the repository at this point in the history
1. The software first checks redirect.hostname.domainname.com for a TXT record, since technically, CNAME records may not have sibling resources.  Using the TXT record on the same hostname as a CNAME was causing problems on one known DNS provider (Amazon Route 53).

2. The path is only appended if the location contains two slashes.  Previously the path was always being appended, such that:

Given the location "http://foo.com/xyz", if the user went to "http://abc.com/123", the target would be "http://foo.com/xyz123".

The location field should now be used as following:

location=http://google.com -- if you want domfo to append the path automatically
location=http://google.com/xyz -- if you want to hardcode a certain path
location=http://google.com/ -- if you want to hardcode /
  • Loading branch information
dustball authored and progrium committed Mar 4, 2011
1 parent 9a6468f commit bc30575
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions domfo.py
Expand Up @@ -9,7 +9,7 @@
from twisted.web.resource import Resource
from twisted.python import log
from twisted.names import client
import sys, os
import sys, os, string

def opt(name, default=None, cast=str): return cast(os.environ.get(name, default))

Expand All @@ -20,21 +20,31 @@ def __init__(self, resolver):
self.resolver = client.Resolver(resolver)

def render_GET(self, request):
host = request.requestHeaders.getRawHeaders('Host', [None])[0]
host = 'redirect.' + host.split(':')[0] # Drop the port
print "Looking up (new method): %s" % host
self.resolver.lookupText(host) \
.addCallback(lambda r: self.do_redirect(host, r[0], request)) \
.addErrback(lambda e: self.backwards_compat(request))
return NOT_DONE_YET

def backwards_compat(self, request):
host = request.requestHeaders.getRawHeaders('Host', [None])[0]
host = host.split(':')[0] # Drop the port
print "Looking up: %s" % host
print "Looking up (old method): %s" % host
self.resolver.lookupText(host) \
.addCallback(lambda r: self.do_redirect(host, r[0], request)) \
.addErrback(lambda e: self.do_error(host, e, request))
return NOT_DONE_YET

def do_redirect(self, host, answers, request):
for x in answers:
data = str(x.payload.data[0])
if data.startswith('location='):
location = data.split('=')[1]
location = location[0:-1] if location[-1] == '/' else location
location = location + request.path
if string.count(location,"/") == 2:
location = location + request.path
request.redirect(location)
print "%s -> %s" % (host, location)
request.finish()
Expand Down

0 comments on commit bc30575

Please sign in to comment.