Beispiel #1
0
def canonical_redirect(event):
    request = event['request']

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ('GET', 'HEAD'):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get('clincoded.canonical_redirect', True):
        return
    if request.path_info == '/':
        return

    canonical_path = event.rendering_val.get('@id', None)
    if canonical_path is None:
        return
    canonical_path = canonical_path.split('?', 1)[0]

    request_path = _join_path_tuple(('', ) +
                                    split_path_info(request.path_info))
    if (request_path == canonical_path.rstrip('/') and
            request.path_info.endswith('/') == canonical_path.endswith('/')):
        return

    if '/@@' in request.path_info:
        return

    qs = request.query_string
    location = canonical_path + ('?' if qs else '') + qs
    raise HTTPMovedPermanently(location=location)
Beispiel #2
0
def canonical_redirect(event):
    request = event["request"]

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ("GET", "HEAD"):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get("encoded.canonical_redirect", True):
        return
    if request.path_info == "/":
        return

    canonical_path = event.rendering_val.get("@id", None)
    if canonical_path is None:
        return
    canonical_path = canonical_path.split("?", 1)[0]

    request_path = _join_path_tuple(("",) + split_path_info(request.path_info))
    if request_path == canonical_path.rstrip("/") and request.path_info.endswith("/") == canonical_path.endswith("/"):
        return

    if "/@@" in request.path_info:
        return

    qs = request.query_string
    location = canonical_path + ("?" if qs else "") + qs
    raise HTTPMovedPermanently(location=location)
Beispiel #3
0
def canonical_redirect(event):
    request = event['request']

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ('GET', 'HEAD'):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get('encoded.canonical_redirect', True):
        return
    if request.path_info == '/':
        return

    canonical_path = event.rendering_val.get('@id', None)
    if canonical_path is None:
        return
    canonical_path = canonical_path.split('?', 1)[0]

    request_path = _join_path_tuple(('',) + split_path_info(request.path_info))
    if (request_path == canonical_path.rstrip('/') and
            request.path_info.endswith('/') == canonical_path.endswith('/')):
        return

    if '/@@' in request.path_info:
        return

    qs = request.query_string
    location = canonical_path + ('?' if qs else '') + qs
    raise HTTPMovedPermanently(location=location)
Beispiel #4
0
def canonical_redirect(event):
    request = event['request']

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ('GET', 'HEAD'):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get('encoded.canonical_redirect', True):
        return
    if request.path_info == '/':
        return

    if not isinstance(event.rendering_val, dict):
        return

    canonical = event.rendering_val.get('@id', None)
    if canonical is None:
        return
    canonical_path, _, canonical_qs = canonical.partition('?')

    request_path = _join_path_tuple(('', ) +
                                    split_path_info(request.path_info))
    if (request_path == canonical_path.rstrip('/')
            and request.path_info.endswith('/') == canonical_path.endswith('/')
            and (canonical_qs in ('', request.query_string))):
        return

    if '/@@' in request.path_info:
        return

    if (parse_qs(canonical_qs) == parse_qs(request.query_string)
            and '/suggest' in request_path):
        return

    qs = canonical_qs or request.query_string
    location = canonical_path + ('?' if qs else '') + qs
    raise HTTPMovedPermanently(location=location)
Beispiel #5
0
def canonical_redirect(event):
    request = event['request']

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ('GET', 'HEAD'):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get('encoded.canonical_redirect', True):
        return
    if request.path_info == '/':
        return

    if not isinstance(event.rendering_val, dict):
        return

    canonical = event.rendering_val.get('@id', None)
    if canonical is None:
        return
    canonical_path, _, canonical_qs = canonical.partition('?')

    request_path = _join_path_tuple(('',) + split_path_info(request.path_info))
    if (request_path == canonical_path.rstrip('/') and
            request.path_info.endswith('/') == canonical_path.endswith('/') and
            (canonical_qs in ('', request.query_string))):
        return

    if '/@@' in request.path_info:
        return

    qs = canonical_qs or request.query_string
    # add redirect information to the query string, but not for the routes specified below
    if not any(route in canonical_path for route in ['/search/', '/browse/', '/metadata/']):
        redir_qs = (qs + '&' if qs else '') + urlencode([('redirected_from', request.path_info)])
    else:
        redir_qs = qs
    location = canonical_path + ('?' if redir_qs else '') + redir_qs
    raise HTTPMovedPermanently(location=location, detail="Redirected from " + str(request.path_info))
Beispiel #6
0
def canonical_redirect(event):
    request = event['request']

    # Ignore subrequests
    if len(manager.stack) > 1:
        return

    if request.method not in ('GET', 'HEAD'):
        return
    if request.response.status_int != 200:
        return
    if not request.environ.get('encoded.canonical_redirect', True):
        return
    if request.path_info == '/':
        return

    if not isinstance(event.rendering_val, dict):
        return

    canonical = event.rendering_val.get('@id', None)
    if canonical is None:
        return
    canonical_path, _, canonical_qs = canonical.partition('?')

    request_path = _join_path_tuple(('',) + split_path_info(request.path_info))   
    if (request_path == canonical_path.rstrip('/') and
            request.path_info.endswith('/') == canonical_path.endswith('/') and
            (canonical_qs in ('', request.query_string))):
        return

    if '/@@' in request.path_info:
        return

    if (parse_qs(canonical_qs) == parse_qs(request.query_string) and
            '/suggest' in request_path):
        return

    qs = canonical_qs or request.query_string
    location = canonical_path + ('?' if qs else '') + qs
    raise HTTPMovedPermanently(location=location)
Beispiel #7
0
    def _callFUT(self, tup):
        from pyramid.traversal import _join_path_tuple

        return _join_path_tuple(tup)
 def _callFUT(self, tup):
     from pyramid.traversal import _join_path_tuple
     return _join_path_tuple(tup)
Beispiel #9
0
    def es_tween(request):
        if request.method not in ('GET', 'HEAD'):
            return handler(request)

        if request.params.get('datastore',
                              default_datastore) != 'elasticsearch':
            return handler(request)

        frame = request.params.get('frame', 'page')
        if frame not in (
                'object',
                'embedded',
                'page',
        ):
            return handler(request)

        # Normalize path
        path_tuple = split_path_info(request.path_info)
        path = _join_path_tuple(('', ) + path_tuple)

        if path in ignore or path.startswith('/static/'):
            return handler(request)

        query = {'filter': {'term': {'paths': path}}, 'version': True}
        data = es.search(index='encoded', body=query)
        hits = data['hits']['hits']
        if len(hits) != 1:
            return handler(request)

        source = hits[0]['_source']
        edits = dict.get(request.session, 'edits', None)
        if edits is not None:
            version = hits[0]['_version']
            linked_uuids = set(source['linked_uuids'])
            embedded_uuids = set(source['embedded_uuids'])
            for xid, updated, linked in edits:
                if xid < version:
                    continue
                if not embedded_uuids.isdisjoint(updated):
                    return handler(request)
                if not linked_uuids.isdisjoint(linked):
                    return handler(request)

        allowed = set(source['principals_allowed']['view'])
        if allowed.isdisjoint(request.effective_principals):
            raise HTTPForbidden()

        if frame == 'page':
            properties = source['embedded']
            request.root = registry.getUtility(IRootFactory)(request)
            collection = request.root.get(properties['@type'][0])
            rendering_val = collection.Item.expand_page(request, properties)

            # Add actions
            ns = {
                'has_permission': es_permission_checker(source, request),
                'item_uri': source['object']['@id'],
                'item_type': collection.item_type,
            }
            actions = calculate_properties(collection.Item,
                                           request,
                                           ns,
                                           category='action')
            if actions:
                rendering_val['actions'] = list(actions.values())

            if ns['has_permission']('audit'):
                rendering_val['audit'] = source['audit']

        else:
            rendering_val = source[frame]
        return render_to_response(None, rendering_val, request)