Exemplo n.º 1
0
 def parse_request(self, data):
     """ Attempts to load the request, validates it, and calls it. """
     try:
         obj = json.loads(data)
     except ValueError:
         return json.dumps(ProtocolError(-32700).generate_error())
     if not obj:
         return json.dumps(ProtocolError(-32600).generate_error())
     batch = True
     if type(obj) is not list:
         batch = False
         obj = [obj,]
     responses = []
     for req in obj:
         request_error = ProtocolError(-32600)
         if type(req) is not dict:
             responses.append(request_error.generate_error())
         elif 'method' not in req.keys() or \
             type(req['method']) not in types.StringTypes:
             responses.append(request_error.generate_error())
         else:
             result = self.parse_call(req)
             if req.has_key('id'):
                 response = generate_response(result, id=req.get('id'))
                 responses.append(response)
     if not responses:
         # It's either a batch of notifications or a single
         # notification, so return nothing.
         return ''
     else:
         if not batch:
             # Single request
             responses = responses[0]
         return json.dumps(responses)
Exemplo n.º 2
0
 def parse_request(self, data):
     """ Attempts to load the request, validates it, and calls it. """
     try:
         obj = json.loads(data)
     except ValueError:
         return json.dumps(ProtocolError(-32700).generate_error())
     if not obj:
         return json.dumps(ProtocolError(-32600).generate_error())
     batch = True
     if type(obj) is not list:
         batch = False
         obj = [obj,]
     responses = []
     for req in obj:
         request_error = ProtocolError(-32600)
         if type(req) is not dict:
             responses.append(request_error.generate_error())
         elif 'method' not in req.keys() or \
             type(req['method']) not in types.StringTypes:
             responses.append(request_error.generate_error())
         else:
             result = self.parse_call(req)
             if req.has_key('id'):
                 response = generate_response(result, id=req.get('id'))
                 responses.append(response)
     if not responses:
         # It's either a batch of notifications or a single
         # notification, so return nothing.
         return ''
     else:
         if not batch:
             # Single request
             responses = responses[0]
         return json.dumps(responses)
Exemplo n.º 3
0
    def process(self, sock, addr):
        """
        Retrieves the data stream from the socket and validates it.
        """
        self.socket = sock
        self.socket.settimeout(config.timeout)
        self.client_address = addr
        requestlines = []
        while True:
            data = self.get_data()
            if not data:
                break
            if type(data) != type('data'):
                data = data.decode('utf-8')
            requestlines.append(data)
            if len(data) < config.buffer:
                break
        request = ''.join(requestlines)
        response = ''
        crypt_error = False
        if config.secret:
            crypt = config.crypt.new(config.secret)
            try:
                request = crypt.decrypt(request)
            except ValueError:
                crypt_error = True
                error = ProtocolError(-32700, 'Could not decrypt request.')
                response = json.dumps(error.generate_error())
        history.request = request
        logger.debug('SERVER | REQUEST: %s' % request)
        if self.socket_error:
            self.socket.close()
        else:
            if not crypt_error:
                response = self.parse_request(request)
            history.response = response
            logger.debug('SERVER | RESPONSE: %s' % response)
            if config.secret:
                length = config.crypt_chunk_size
                pad_length = length - (len(response) % length)
                response = crypt.encrypt('%s%s' % (response, ' ' * pad_length))

            if type(response) == type('response'):
                response = response.encode('utf-8')
            self.socket.send(response)
        self.socket.close()
Exemplo n.º 4
0
 def process(self, sock, addr):
     """
     Retrieves the data stream from the socket and validates it.
     """
     self.socket = sock
     self.socket.settimeout(config.timeout)
     self.client_address = addr
     requestlines = []
     while True:
         data = self.get_data()
         if not data: 
             break
         requestlines.append(data)
         if len(data) < config.buffer: 
             break
     request = ''.join(requestlines)
     response = ''
     crypt_error = False
     if config.secret:
         crypt = config.crypt.new(config.secret)
         try:
             request = crypt.decrypt(request)
         except ValueError:
             crypt_error = True
             error = ProtocolError(-32700, 'Could not decrypt request.')
             response = json.dumps(error.generate_error())
     history.request = request
     logger.debug('SERVER | REQUEST: %s' % request)
     if self.socket_error:
         self.socket.close()
     else:
         if not crypt_error:
             response = self.parse_request(request)
         history.response = response
         logger.debug('SERVER | RESPONSE: %s' % response)
         if config.secret:
             length = config.crypt_chunk_size
             pad_length = length - (len(response) % length)
             response = crypt.encrypt('%s%s' % (response, ' '*pad_length))
         self.socket.send(response)
     self.socket.close()