Example #1
0
    def _isAllowed(self, request, uri):
        '''
        Retrieve the access for the authentication.
        '''
        assert isinstance(request, Request), 'Invalid request %s' % request

        req, reqCnt = self._requestProcessing.contexts['request'](), self._requestProcessing.contexts['requestCnt']()
        rsp, rspCnt = self._requestProcessing.contexts['response'](), self._requestProcessing.contexts['responseCnt']()
        assert isinstance(req, RequestHTTP)
        assert isinstance(rsp, ResponseHTTP)
        assert isinstance(rspCnt, ResponseContentHTTP)

        req.methodName = METHOD_GET
        req.scheme = request.scheme
        req.headers = self.accessHeaders
        req.uriRoot = self.accessUriRoot
        req.uri = uri

        requestChain = Chain(self._requestProcessing)
        requestChain.process(request=req, requestCnt=reqCnt, response=rsp, responseCnt=rspCnt).doAll()
        if not rsp.isSuccess: return
        if rspCnt.source is None:
            raise Exception('Problems with security filter access, the URI \'%s\' has a response %s %s, but no content' %
                            (self.accessUriRoot + uri, rsp.code, rsp.text))
        if isinstance(rspCnt.source, IInputStream):
            source = rspCnt.source
        else:
            source = BytesIO()
            for bytes in rspCnt.source: source.write(bytes)
            source.seek(0)
        allowed = json.load(codecs.getreader(self.accessResponseEncoding)(source))
        return allowed['HasAccess'] == 'True'
Example #2
0
    def processParsing(self, request, requestCnt, response, responseCnt, **keyargs):
        '''
        Process the parsing for the provided contexts.
        
        @return: boolean
            True if the parsing has been successfully done on the request content.
        '''
        assert isinstance(request, Request), 'Invalid request %s' % request
        assert isinstance(requestCnt, RequestContent), 'Invalid request content %s' % requestCnt
        assert isinstance(response, Response), 'Invalid response %s' % response
        assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt

        # Resolving the character set
        if RequestContent.charSet in requestCnt:
            try: codecs.lookup(requestCnt.charSet)
            except LookupError: requestCnt.charSet = self.charSetDefault
        else: requestCnt.charSet = self.charSetDefault
        if RequestContent.type not in requestCnt: requestCnt.type = responseCnt.type

        chain = Chain(self.parsingProcessing)
        chain.process(request=request, requestCnt=requestCnt, response=response, responseCnt=responseCnt, **keyargs)
        if not chain.doAll().isConsumed(): return True
        if Response.code not in response or response.code.isSuccess:
            response.code = UNKNOWN_ENCODING
            response.text = 'Content type \'%s\' not supported for parsing' % requestCnt.type
Example #3
0
    def process(self, redirectProcessing, chain, request, response, **keyargs):
        '''
        Process the redirect.
        
        @param redirectProcessing: Processing
            The processing that provides the redirecting chain.
            
        The rest of the parameters are contexts.
        '''
        assert isinstance(redirectProcessing, Processing), 'Invalid redirect processing %s' % redirectProcessing
        assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
        assert isinstance(request, Request), 'Invalid request %s' % request
        assert isinstance(response, Response), 'Invalid response %s' % response

        if Response.code not in response or response.code.isSuccess: # Skip in case the response is in error
            assert isinstance(request.invoker, Invoker), 'Invalid request invoker %s' % request.invoker

            typ = request.invoker.output
            if isinstance(typ, TypeModelProperty): typ = typ.type
            if isinstance(typ, TypeReference):
                redirectChain = Chain(redirectProcessing)
                redirectChain.process(request=request, response=response, **keyargs).doAll()
                if Response.code not in response or response.code.isSuccess:
                    assert isinstance(response.encoderHeader, IEncoderHeader), \
                    'Invalid header encoder %s' % response.encoderHeader
                    assert isinstance(response.encoderPath, IEncoderPath), \
                    'Invalid encoder path %s' % response.encoderPath

                    response.encoderHeader.encode(self.nameLocation, response.encoderPath.encode(response.obj))
                    response.code, response.text = REDIRECT, 'Redirect'
                    return

        chain.proceed()
Example #4
0
    def testMethodInvoker(self):
        handler = MethodInvokerHandler()
        ioc.initialize(handler)

        request, response = Request(), Response()

        node = NodeRoot()
        request.method, request.path = GET, Path([], node)

        def callProcess(chain, **keyargs): handler.process(**keyargs)
        chain = Chain([callProcess])
        chain.process(request=request, response=response).doAll()

        self.assertEqual(response.allows, 0)
        self.assertTrue(response.isSuccess is False)
Example #5
0
    def testTextConversion(self):
        handler = ConversionSetHandler()
        handler.normalizer = Normalizer()
        handler.converter = Converter()
        ioc.initialize(handler)

        requestCnt, response = Content(), Content()
        def callProcess(chain, **keyargs): handler.process(**keyargs)
        chain = Chain([callProcess])
        chain.process(requestCnt=requestCnt, response=response).doAll()

        self.assertEqual(handler.normalizer, requestCnt.normalizer)
        self.assertEqual(handler.normalizer, response.normalizer)

        self.assertEqual(handler.converter, response.converter)
        self.assertEqual(handler.converter, response.converter)
Example #6
0
    def testMethodInvoker(self):
        resourcesLocator = DummyResourceLocator()

        handler = MethodInvokerHandler()
        ioc.initialize(handler)

        request, response = Request(), Response()

        node = NodeRoot()
        request.method, request.path = GET, Path(resourcesLocator, [], node)

        def callProcess(chain, **keyargs): handler.process(**keyargs)
        chain = Chain([callProcess])
        chain.process(request=request, response=response)

        self.assertEqual(response.allows, 0)
        self.assertTrue(not response.code.isSuccess)
Example #7
0
    def process(self, chain, request, response, responseCnt, **keyargs):
        """
        Create the render for the response object.
        """
        assert isinstance(chain, Chain), "Invalid processors chain %s" % chain
        assert isinstance(request, Request), "Invalid request %s" % request
        assert isinstance(response, Response), "Invalid response %s" % response
        assert isinstance(responseCnt, ResponseContent), "Invalid response content %s" % responseCnt

        chain.proceed()

        # Resolving the character set
        if ResponseContent.charSet in responseCnt:
            try:
                codecs.lookup(responseCnt.charSet)
            except LookupError:
                responseCnt.charSet = None
        else:
            responseCnt.charSet = None

        if responseCnt.charSet is None:
            for charSet in request.accCharSets or ():
                try:
                    codecs.lookup(charSet)
                except LookupError:
                    continue
                responseCnt.charSet = charSet
                break
            else:
                responseCnt.charSet = self.charSetDefault

        resolved = False
        if ResponseContent.type in responseCnt:
            renderChain = Chain(self.renderingProcessing)
            renderChain.process(request=request, response=response, responseCnt=responseCnt, **keyargs)
            if renderChain.doAll().isConsumed():
                if Response.code not in response or response.code.isSuccess:
                    response.code = UNKNOWN_ENCODING
                    response.text = "Content type '%s' not supported for rendering" % responseCnt.type
            else:
                resolved = True

        if not resolved:
            # Adding None in case some encoder is configured as default.
            for contentType in itertools.chain(request.accTypes or (), self.contentTypeDefaults):
                responseCnt.type = contentType
                renderChain = Chain(self.renderingProcessing)
                renderChain.process(request=request, response=response, responseCnt=responseCnt, **keyargs)
                if not renderChain.doAll().isConsumed():
                    break
            else:
                raise DevelError(
                    "There is no renderer available, this is more likely a setup issues since the "
                    "default content types should have resolved the renderer"
                )
Example #8
0
    def _process(self, method):
        url = urlparse(self.path)
        path = url.path.lstrip('/')
        for regex, processing in self.server.pathProcessing:
            match = regex.match(path)
            if match:
                uriRoot = path[:match.end()]
                if not uriRoot.endswith('/'): uriRoot += '/'

                assert isinstance(processing, Processing), 'Invalid processing %s' % processing
                req, reqCnt = processing.contexts['request'](), processing.contexts['requestCnt']()
                rsp, rspCnt = processing.contexts['response'](), processing.contexts['responseCnt']()

                assert isinstance(req, RequestHTTP), 'Invalid request %s' % req
                assert isinstance(reqCnt, RequestContentHTTPAsyncore), 'Invalid request content %s' % reqCnt
                assert isinstance(rsp, ResponseHTTP), 'Invalid response %s' % rsp
                assert isinstance(rspCnt, ResponseContentHTTP), 'Invalid response content %s' % rspCnt

                req.scheme, req.uriRoot, req.uri = 'http', uriRoot, path[match.end():]
                req.parameters = parse_qsl(url.query, True, False)
                break
        else:
            self.send_response(404)
            self.end_headers()
            self.close()
            return

        req.methodName = method
        req.headers = dict(self.headers)

        def respond():
            assert isinstance(rsp.code, int), 'Invalid response code %s' % rsp.code
    
            if ResponseHTTP.headers in rsp:
                for name, value in rsp.headers.items(): self.send_header(name, value)
    
            if ResponseHTTP.text in rsp: self.send_response(rsp.code, rsp.text)
            else: self.send_response(rsp.code)
    
            self.end_headers()
    
            if rspCnt.source is not None:
                if isinstance(rspCnt.source, IInputStream): source = readGenerator(rspCnt.source, self.bufferSize)
                else: source = rspCnt.source
    
                self._writeq.append((WRITE_ITER, iter(source)))
            self._writeq.append((WRITE_CLOSE, None))
            
        chain = Chain(processing)
        chain.process(request=req, requestCnt=reqCnt, response=rsp, responseCnt=rspCnt)
        chain.callBack(respond)
        
        while True:
            if not chain.do():
                self._next(3)  # Now we proceed to write stage
                break
            if reqCnt.contentReader is not None:
                self._next(2)  # Now we proceed to read stage
                self._reader = reqCnt.contentReader
                break