Exemplo n.º 1
0
class _SecureWrapper(record('urlGenerator wrappedResource')):
    """
    Helper class for L{SecuringWrapper} which preserves wrapping to the
    ultimate resource and which implements HTTPS redirect logic if necessary.

    @ivar urlGenerator: The L{ISiteURLGenerator} which will provide an HTTPS
        URL.
    @ivar wrappedResource: The resource which will be used to locate children
        or which will be rendered.
    """
    implements(IResource)

    def __init__(self, *a, **k):
        super(_SecureWrapper, self).__init__(*a, **k)
        self.wrappedResource = IResource(self.wrappedResource)


    def locateChild(self, context, segments):
        """
        Delegate child lookup to the wrapped resource but wrap whatever results
        in another instance of this wrapper.
        """
        childDeferred = maybeDeferred(
            self.wrappedResource.locateChild, context, segments)
        def childLocated((resource, segments)):
            if (resource, segments) == NotFound:
                return NotFound
            return _SecureWrapper(self.urlGenerator, resource), segments
        childDeferred.addCallback(childLocated)
        return childDeferred


    def renderHTTP(self, context):
        """
        Check to see if the wrapped resource wants to be rendered over HTTPS
        and generate a redirect if this is so, if HTTPS is available, and if
        the request is not already over HTTPS.
        """
        if getattr(self.wrappedResource, 'needsSecure', False):
            request = IRequest(context)
            url = self.urlGenerator.encryptedRoot()
            if url is not None:
                for seg in request.prepath:
                    url = url.child(seg)
                return url
        return self.wrappedResource.renderHTTP(context)