Package pyamf :: Package remoting :: Module amf0

[hide private]
[frames] | no frames]

# Source Code for Module pyamf.remoting.amf0

      1  # Copyright (c) 2007-2009 The PyAMF Project.
      2  # See LICENSE.txt for details.
      3
      4  """
      5  AMF0 Remoting support.
      6
      7  @since: 0.1.0
      8  """
      9
     10  import traceback
     11  import sys
     12
     13  from pyamf import remoting
     14  from pyamf.remoting import gateway
     15
     16
     17 -class RequestProcessor(object):
     18 -    def __init__(self, gateway):
     19          self.gateway = gateway
     20
     21 -    def authenticateRequest(self, request, service_request, *args, **kwargs):
     22          """
     23          Authenticates the request against the service.
     24
     25          @param request: The AMF request
     26          @type request: L{Request<pyamf.remoting.Request>}
     27          """
     28          username = password = None
     29
     30          if 'Credentials' in request.headers:
     31              cred = request.headers['Credentials']
     32
     33              username = cred['userid']
     34              password = cred['password']
     35
     36          return self.gateway.authenticateRequest(service_request, username,
     37              password, *args, **kwargs)
     38
     39 -    def buildErrorResponse(self, request, error=None):
     40          """
     41          Builds an error response.
     42
     43          @param request: The AMF request
     44          @type request: L{Request<pyamf.remoting.Request>}
     45          @return: The AMF response
     46          @rtype: L{Response<pyamf.remoting.Response>}
     47          """
     48          if error is not None:
     49              cls, e, tb = error
     50          else:
     51              cls, e, tb = sys.exc_info()
     52
     53          return remoting.Response(build_fault(cls, e, tb, self.gateway.debug),
     54              status=remoting.STATUS_ERROR)
     55
     56 -    def _getBody(self, request, response, service_request, **kwargs):
     57          if 'DescribeService' in request.headers:
     58              return service_request.service.description
     59
     60          return self.gateway.callServiceRequest(service_request, *request.body,
     61              **kwargs)
     62
     63 -    def __call__(self, request, *args, **kwargs):
     64          """
     65          Processes an AMF0 request.
     66
     67          @param request: The request to be processed.
     68          @type request: L{Request<pyamf.remoting.Request>}
     69
     70          @return: The response to the request.
     71          @rtype: L{Response<pyamf.remoting.Response>}
     72          """
     73          response = remoting.Response(None)
     74
     75          try:
     76              service_request = self.gateway.getServiceRequest(request,
     77                  request.target)
     78          except gateway.UnknownServiceError:
     79              return self.buildErrorResponse(request)
     80
     81          # we have a valid service, now attempt authentication
     82          try:
     83              authd = self.authenticateRequest(request, service_request, *args,
     84                  **kwargs)
     85          except (SystemExit, KeyboardInterrupt):
     86              raise
     87          except:
     88              return self.buildErrorResponse(request)
     89
     90          if not authd:
     91              # authentication failed
     92              response.status = remoting.STATUS_ERROR
     93              response.body = remoting.ErrorFault(code='AuthenticationError',
     94                  description='Authentication failed')
     95
     96              return response
     97
     98          # authentication succeeded, now fire the preprocessor (if there is one)
     99          try:
    100              self.gateway.preprocessRequest(service_request, *args, **kwargs)
    101          except (SystemExit, KeyboardInterrupt):
    102              raise
    103          except:
    104              return self.buildErrorResponse(request)
    105
    106          try:
    107              response.body = self._getBody(request, response, service_request,
    108                  *args, **kwargs)
    109
    110              return response
    111          except (SystemExit, KeyboardInterrupt):
    112              raise
    113          except:
    114              return self.buildErrorResponse(request)
    115
    116
    117 -def build_fault(cls, e, tb, include_traceback=False):
    118      """
    119      Builds a L{ErrorFault<pyamf.remoting.ErrorFault>} object based on the last
    120      exception raised.
    121
    122      If include_traceback is C{False} then the traceback will not be added to
    123      the L{remoting.ErrorFault}.
    124      """
    125      if hasattr(cls, '_amf_code'):
    126          code = cls._amf_code
    127      else:
    128          code = cls.__name__
    129
    130      details = None
    131
    132      if include_traceback:
    133          details = str(traceback.format_exception(cls, e, tb)).replace("\\n", '')
    134
    135      return remoting.ErrorFault(code=code, description=str(e), details=details)
    136