Exemplo n.º 1
0
 def setUp(self):
     super(SystemAPITest, self).setUp()
     self.mapper = Mapper()
     self.dispatcher = Dispatcher(self.mapper)
     self.context = CallContext(
         user=None, mapper=self.mapper, dispatcher=self.dispatcher)
     self.system_api = SystemAPI(self.context)
Exemplo n.º 2
0
def handler(request, mapper, help_view):
    """
    XML-RPC handler.

    If post data is defined, it assumes it's XML-RPC and tries to process as
    such. Empty POST request and GET requests assumes you're viewing from a
    browser and tells you about the service by redirecting you to a dedicated
    help page. For backwards compatibility the help view defaults to the
    'default_help' that shows what is registered in the global mapper. If you
    want to show help specific to your mapper you must specify help_view. It
    accepts whatever django.shortcuts.redirect() would.
    """
    if len(request.body):
        raw_data = request.body
        dispatcher = Dispatcher(mapper)

        auth_string = request.META.get('HTTP_AUTHORIZATION')

        if auth_string is not None:
            if ' ' not in auth_string:
                return HttpResponse("Invalid HTTP_AUTHORIZATION header",
                                    status=400)
            scheme, value = auth_string.split(" ", 1)
            if scheme != "Basic":
                return HttpResponse(
                    "Unsupported HTTP_AUTHORIZATION header, only Basic scheme is supported",
                    status=400)
            try:
                decoded_value = base64.standard_b64decode(value)
            except TypeError:
                return HttpResponse(
                    "Corrupted HTTP_AUTHORIZATION header, bad base64 encoding",
                    status=400)
            try:
                username, secret = decoded_value.split(":", 1)
            except ValueError:
                return HttpResponse(
                    "Corrupted HTTP_AUTHORIZATION header, no user:pass",
                    status=400)
            user = None
            try:
                user = AuthToken.get_user_for_secret(username, secret)
            except Exception:
                logging.exception("bug")
            if user is None:
                response = HttpResponse("Invalid token", status=401)
                response[
                    'WWW-Authenticate'] = 'Basic realm="XML-RPC Authentication token"'
                return response
        else:
            user = request.user
        result = dispatcher.marshalled_dispatch(raw_data, user, request)
        response = HttpResponse(content_type="application/xml")
        response.write(result)
        response['Content-length'] = str(len(response.content))
        return response
    else:
        return redirect(help_view)
Exemplo n.º 3
0
 def setUp(self):
     super(DispatcherTests, self).setUp()
     self.mapper = Mapper()
     self.mapper.register(TestAPI, '')
     self.dispatcher = Dispatcher(self.mapper)
Exemplo n.º 4
0
 def setUp(self):
     super().setUp()
     self.mapper = Mapper()
     self.mapper.register(TestAPI, "")
     self.dispatcher = Dispatcher(self.mapper)