def do_POST(self): path = self.path.split('?')[0][1:].split('/') if path[0] != HTTPServerHandler.uuid: self.sendJsonError(403, 'Forbidden') return if len(path) != 2: self.sendJsonError(400, 'Invalid request') return try: HTTPServerHandler.lock.acquire() length = int(self.headers.getheader('content-length')) content = self.rfile.read(length) print(length, ">>", content, '<<') params = json.loads(content) operation = getattr(self, 'post_' + path[1]) result = operation(params) # Protect not POST methods except AttributeError: self.sendJsonError(404, 'Method not found') return except Exception as e: logger.error('Got exception executing POST {}: {}'.format(path[1], utils.toUnicode(e.message))) self.sendJsonError(500, str(e)) return finally: HTTPServerHandler.lock.release() self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() # Send the html message self.wfile.write(json.dumps(result))
def do_POST(self): path = self.path.split('?')[0][1:].split('/') if path[0] != HTTPServerHandler.uuid: self.sendJsonError(403, 'Forbidden') return if len(path) != 2: self.sendJsonError(400, 'Invalid request') return try: HTTPServerHandler.lock.acquire() length = int(self.headers.getheader('content-length')) content = self.rfile.read(length) print(length, ">>", content, '<<') params = json.loads(content) operation = getattr(self, 'post_' + path[1]) result = operation(params) # Protect not POST methods except AttributeError: self.sendJsonError(404, 'Method not found') return except Exception as e: logger.error('Got exception executing POST {}: {}'.format( path[1], utils.toUnicode(e.message))) self.sendJsonError(500, str(e)) return finally: HTTPServerHandler.lock.release() self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() # Send the html message self.wfile.write(json.dumps(result))
def run(self): self.running = True while self.running: try: msg = b'' # We look for magic message header while self.running: # Wait for MAGIC try: buf = self.clientSocket.recv(len(MAGIC) - len(msg)) if buf == b'': self.running = False break msg += buf if len(msg) != len(MAGIC): continue # Do not have message if msg != MAGIC: # Skip first byte an continue searchong msg = msg[1:] continue break except socket.timeout: # Timeout is here so we can get stop thread continue if self.running is False: break # Now we get message basic data (msg + datalen) msg = bytearray(self.receiveBytes(3)) # We have the magic header, here comes the message itself if msg is None: continue msgId = msg[0] dataLen = msg[1] + (msg[2] << 8) if msgId not in VALID_MESSAGES: raise Exception('Invalid message id: {}'.format(msgId)) data = self.receiveBytes(dataLen) if data is None: continue self.messages.put((msgId, data)) self.messageReceived() except socket.error as e: if e.errno == errno.EINTR: time.sleep(1) # continue # Ignore interrupted system call logger.error('Communication with server got an error: {}'.format(toUnicode(e.strerror))) # self.running = False return except Exception as e: tb = traceback.format_exc() logger.error('Error: {}, trace: {}'.format(e, tb)) try: self.clientSocket.close() except Exception: pass # If can't close, nothing happens, just end thread
def do_GET(self): # Very simple path & params splitter path = self.path.split('?')[0][1:].split('/') try: params = dict( (v.split('=') for v in self.path.split('?')[1].split('&'))) except Exception: params = {} if path[0] != HTTPServerHandler.uuid: self.sendJsonError(403, 'Forbidden') return if len(path) != 2: self.sendJsonResponse( "UDS Actor has been running for {} seconds".format( time.time() - startTime)) return try: operation = getattr(self, 'get_' + path[1]) result = operation(params) # Protect not POST methods except AttributeError: self.sendJsonError(404, 'Method not found') return except Exception as e: logger.error('Got exception executing GET {}: {}'.format( path[1], utils.toUnicode(e.message))) self.sendJsonError(500, str(e)) return self.sendJsonResponse(result)
def do_POST(self): path = self.path.split('?')[0][1:].split('/') if path[0] != HTTPServerHandler.uuid: self.sendJsonError(403, 'Forbidden') return if len(path) != 2: self.sendJsonError(400, 'Invalid request') return try: HTTPServerHandler.lock.acquire() length = int(self.headers.get('content-length')) content = self.rfile.read(length).decode('utf8') logger.debug('length: {}, content >>{}<<'.format(length, content)) params = json.loads(content) operation = getattr(self, 'post_' + path[1]) result = operation(params) # Protect not POST methods except AttributeError: self.sendJsonError(404, 'Method not found') return except Exception as e: logger.error('Got exception executing POST {}: {}'.format( path[1], utils.toUnicode(e.message))) self.sendJsonError(500, str(e)) return finally: HTTPServerHandler.lock.release() self.sendJsonResponse(result)
def do_GET(self): # Very simple path & params splitter path = self.path.split('?')[0][1:].split('/') try: params = dict((v.split('=') for v in self.path.split('?')[1].split('&'))) except Exception: params = {} if path[0] != HTTPServerHandler.uuid: self.sendJsonError(403, 'Forbidden') return if len(path) != 2: self.sendJsonResponse("UDS Actor has been running for {} seconds".format(time.time() - startTime)) return try: operation = getattr(self, 'get_' + path[1]) result = operation(params) # Protect not POST methods except AttributeError: self.sendJsonError(404, 'Method not found') return except Exception as e: logger.error('Got exception executing GET {}: {}'.format(path[1], utils.toUnicode(e.message))) self.sendJsonError(500, str(e)) return self.sendJsonResponse(result)