def should_redirect(): ''' Return True if the current flask.request should redirect. ''' referer_url = request.headers.get('Referer') if not referer_url: return False return needs_redirect(request.host, request.path, referer_url)
def should_redirect(): ''' Return True if the current flask.request should redirect. ''' if request.args.get('go') == u'\U0001f44c': return False referer_url = request.headers.get('Referer') if not referer_url: return False return needs_redirect(request.host, request.path, referer_url)
def wrapper(*args, **kwargs): ''' Redirect under repository root based on referer if necessary. ''' GET = Getter((get_token().get('access_token'), 'x-oauth-basic')).get # See if the OK hand sign (U+1F44C) was given. if request.args.get('go') == u'\U0001f44c': return untouched_route(*args, **kwargs) # See if there's a referer at all. referer_url = request.headers.get('Referer') if not referer_url: # No referer, no redirect. return maybe_add_slashes(request.path, GET, *args, **kwargs) # See if the referer path is long enough to suggest a redirect. referer_path = urlparse(referer_url).path split_path = referer_path.lstrip('/').split('/', 2) if len(split_path) != 3: # Not long enough. return maybe_add_slashes(request.path, GET, *args, **kwargs) # Talk to Github about the path and find a ref name. path_owner, path_repo, path_ref = split_path if not repo_exists(path_owner, path_repo, GET): # No repo by this name, no redirect. return maybe_add_slashes(request.path, GET, *args, **kwargs) ref, _ = split_branch_path(path_owner, path_repo, path_ref, GET) if ref is None: # No ref identified, no redirect. return maybe_add_slashes(request.path, GET, *args, **kwargs) # Usually 3, but maybe more? slash_count = 2 + len(ref.split('/')) # See if a redirect is necessary. if needs_redirect(request.host, request.path, referer_url, slash_count): return make_redirect(slash_count) # Otherwise, proceed as normal. return maybe_add_slashes(request.path, GET, *args, **kwargs)