Exemple #1
0
    def render(self, request):
        """
        Render a given resource. See L{IResource}'s render method.

        I delegate to methods of self with the form 'render_METHOD'
        where METHOD is the HTTP that was used to make the
        request. Examples: render_GET, render_HEAD, render_POST, and
        so on. Generally you should implement those methods instead of
        overriding this one.

        render_METHOD methods are expected to return a byte string which will be
        the rendered page, unless the return value is C{server.NOT_DONE_YET}, in
        which case it is this class's responsibility to write the results using
        C{request.write(data)} and then call C{request.finish()}.

        Old code that overrides render() directly is likewise expected
        to return a byte string or NOT_DONE_YET.

        @see: L{IResource.render}
        """
        m = getattr(self, "render_" + nativeString(request.method), None)
        if not m:
            try:
                allowedMethods = self.allowedMethods
            except AttributeError:
                allowedMethods = _computeAllowedMethods(self)
            raise UnsupportedMethod(allowedMethods)
        return m(request)
Exemple #2
0
    def render(self, request):
        """
        Render a given resource. See L{IResource}'s render method.

        I delegate to methods of self with the form 'render_METHOD'
        where METHOD is the HTTP that was used to make the
        request. Examples: render_GET, render_HEAD, render_POST, and
        so on. Generally you should implement those methods instead of
        overriding this one.

        render_METHOD methods are expected to return a string which
        will be the rendered page, unless the return value is
        twisted.web.server.NOT_DONE_YET, in which case it is this
        class's responsibility to write the results to
        request.write(data), then call request.finish().

        Old code that overrides render() directly is likewise expected
        to return a string or NOT_DONE_YET.
        """
        m = getattr(self, 'render_' + request.method, None)
        if not m:
            # This needs to be here until the deprecated subclasses of the
            # below three error resources in twisted.web.error are removed.
            from twisted.web.error import UnsupportedMethod
            raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))
        return m(request)
Exemple #3
0
 def render(self, request):
     allowedMethods = set()
     for res in self.resources:
         try:
             return res.render(request)
         except UnsupportedMethod as e:
             allowedMethods.update(e.allowedMethods)
     raise UnsupportedMethod(list(allowedMethods))
 def _extract_render_method(self, request):
     # from twisted.web.resource.Resource
     render_method = getattr(self, 'render_' + nativeString(request.method),
                             None)
     if not render_method:
         try:
             allowedMethods = self.allowedMethods
         except AttributeError:
             allowedMethods = _computeAllowedMethods(self)
         raise UnsupportedMethod(allowedMethods)
     return render_method
Exemple #5
0
    def render(self, request):
        if request.method not in self._allowedMethods:
            raise UnsupportedMethod(self._allowedMethods)

        # if it finishes prematurely then cancel the command
        finishedDeferred = request.notifyFinish()
        finishedDeferred.addErrback(self.dont_render)

        request.setHeader("content-type", "text/plain")

        self._delayedRender(request)

        return NOT_DONE_YET
    def render(self,
               request):  # Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
        """
        Adds support for deferred render methods
        """
        auth_header = request.getHeader('Authorization')

        if self.username or self.password:
            authenticated = False
            if auth_header:
                auth_header = auth_header.split(' ')
                if len(auth_header) > 1 and auth_header[0] == 'Basic':
                    userpass = auth_header[1].decode('base64').split(':')
                    if len(userpass) == 2:
                        username, password = userpass
                        if self.username == username and self.password == password:
                            authenticated = True

            if not authenticated:
                print auth_header
                print self.username, self.password
                request.setResponseCode(401)
                return 'Unauthorized'

        m = getattr(self, 'render_' + request.method, None)
        if not m:
            # This needs to be here until the deprecated subclasses of the
            # below three error resources in twisted.web.error are removed.
            from twisted.web.error import UnsupportedMethod
            allowedMethods = (getattr(self, 'allowedMethods', 0)
                              or _computeAllowedMethods(self))
            raise UnsupportedMethod(allowedMethods)

        result = defer.maybeDeferred(m, request)

        def write_rest(defer_result, request):
            request.write(defer_result)
            request.finish()

        def err_rest(defer_result=None):
            defer_result.printTraceback()
            request.finish()

        result.addCallback(write_rest, request)
        result.addErrback(err_rest)

        return server.NOT_DONE_YET
Exemple #7
0
 def test_error_not_supported_method(self, log_msg_mock):
     exc = UnsupportedMethod(['GET'])
     result = self.resource.handle_error(exc, self.request)
     self.assertEqual(self.request.code, 405)
     self.assertFalse(log_msg_mock.called)
     self.assertIn('GET', result['message'])
Exemple #8
0
 def render(self, request):
     raise UnsupportedMethod(())
 def render_POST(self, request):
     raise UnsupportedMethod(allowedMethods)