def referrer_allowed(referrer, referrer_acl): """ Returns True if the referrer should be allowed based on the referrer_acl list (as returned by :func:`parse_acl`). See :func:`clean_acl` for documentation of the standard Chase ACL format. :param referrer: The value of the HTTP Referer header. :param referrer_acl: The list of referrer designations as returned by :func:`parse_acl`. :returns: True if the referrer should be allowed; False if not. """ allow = False if referrer_acl: rhost = urlparse(referrer or '').hostname or 'unknown' for mhost in referrer_acl: if mhost[0] == '-': mhost = mhost[1:] if mhost == rhost or \ (mhost[0] == '.' and rhost.endswith(mhost)): allow = False elif mhost == '*' or mhost == rhost or \ (mhost[0] == '.' and rhost.endswith(mhost)): allow = True return allow
def test_urlparse(self): parsed = utils.urlparse('http://127.0.0.1/') self.assertEquals(parsed.scheme, 'http') self.assertEquals(parsed.hostname, '127.0.0.1') self.assertEquals(parsed.path, '/') parsed = utils.urlparse('http://127.0.0.1:8080/') self.assertEquals(parsed.port, 8080) parsed = utils.urlparse('https://127.0.0.1/') self.assertEquals(parsed.scheme, 'https') parsed = utils.urlparse('http://[::1]/') self.assertEquals(parsed.hostname, '::1') parsed = utils.urlparse('http://[::1]:8080/') self.assertEquals(parsed.hostname, '::1') self.assertEquals(parsed.port, 8080) parsed = utils.urlparse('www.example.com') self.assertEquals(parsed.hostname, '')