Esempio n. 1
0
def canonize(request, website):
    """Enforce a certain scheme and hostname.
    """
    if request.path.raw.startswith('/callbacks/'):
        # Don't redirect callbacks
        if request.path.raw[-1] == '/':
            # Remove trailing slash
            l = request.line
            scheme, netloc, path, query, fragment = urlsplit(l.uri)
            assert path[-1] == '/'  # sanity check
            path = path[:-1]
            new_uri = urlunsplit((scheme, netloc, path, query, fragment))
            request.line = Line(l.method.raw, new_uri, l.version.raw)
        return
    scheme = request.headers.get('X-Forwarded-Proto', 'http')
    host = request.headers['Host']
    canonical_host = website.canonical_host
    canonical_scheme = website.canonical_scheme
    bad_scheme = scheme != canonical_scheme
    bad_host = False
    if canonical_host:
        if host == canonical_host:
            pass
        elif host.endswith('.' + canonical_host):
            subdomain = host[:-len(canonical_host) - 1]
            if subdomain in website.locales:
                accept_langs = request.headers.get('Accept-Language', '')
                request.headers[
                    'Accept-Language'] = subdomain + ',' + accept_langs
            else:
                bad_host = True
        else:
            bad_host = True
    if bad_scheme or bad_host:
        url = '%s://%s' % (canonical_scheme,
                           canonical_host if bad_host else host)
        if request.line.method in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
            # Redirect to a particular path for idempotent methods.
            url += request.line.uri.path.raw
            if request.line.uri.querystring:
                url += '?' + request.line.uri.querystring.raw
        else:
            # For non-idempotent methods, redirect to homepage.
            url += '/'
        website.redirect(url)
Esempio n. 2
0
def test_line_works():
    line = Line("GET", "/", "HTTP/0.9")
    assert line == u"GET / HTTP/0.9", line
Esempio n. 3
0
def test_line_has_version():
    line = Line("GET", "/", "HTTP/0.9")
    assert line.version == u"HTTP/0.9", line.version
Esempio n. 4
0
def test_line_has_uri():
    line = Line("GET", "/", "HTTP/0.9")
    assert line.uri == u"/", line.uri
Esempio n. 5
0
def test_line_has_method():
    line = Line("GET", "/", "HTTP/0.9")
    assert line.method == u"GET", line.method