From 6979d9bc170c8a46fe64e7559c1b33898f51c09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Tue, 14 Jul 2015 12:47:03 +0200 Subject: [PATCH] Some improvements over REST api for actor --- actors/src/udsactor/httpserver.py | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/actors/src/udsactor/httpserver.py b/actors/src/udsactor/httpserver.py index ea67d6975..584773f46 100644 --- a/actors/src/udsactor/httpserver.py +++ b/actors/src/udsactor/httpserver.py @@ -51,6 +51,10 @@ startTime = time.time() class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): + protocol_version = 'HTTP/1.0' + server_version = 'UDS Actor Server' + sys_version = '' + uuid = None service = None lock = threading.Lock() @@ -62,6 +66,15 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.wfile.write(json.dumps({'error': message})) return + def sendJsonResponse(self, data): + self.send_response(200) + data = json.dumps(data) + self.send_header('Content-type', 'application/json') + self.send_header('Content-Length', len(data)) + self.end_headers() + # Send the html message + self.wfile.write(data) + def do_GET(self): # Very simple path & params splitter path = self.path.split('?')[0][1:].split('/') @@ -75,11 +88,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): return if len(path) != 2: - self.send_response(200) - self.send_header('Content-type', 'application/json') - self.end_headers() - # Send the html message - self.wfile.write(json.dumps("UDS Actor has been running for {} seconds".format(time.time() - startTime))) + self.sendJsonResponse("UDS Actor has been running for {} seconds".format(time.time() - startTime)) return try: @@ -93,11 +102,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.sendJsonError(500, str(e)) return - self.send_response(200) - self.send_header('Content-type', 'application/json') - self.end_headers() - # Send the html message - self.wfile.write(json.dumps(result)) + self.sendJsonResponse(result) def do_POST(self): path = self.path.split('?')[0][1:].split('/') @@ -113,7 +118,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): HTTPServerHandler.lock.acquire() length = int(self.headers.getheader('content-length')) content = self.rfile.read(length) - print(length, ">>", content, '<<') + logger.debug('length: {}, content >>{}<<'.format(length, content)) params = json.loads(content) operation = getattr(self, 'post_' + path[1]) @@ -128,11 +133,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): 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)) + self.sendJsonResponse(result) def post_logoff(self, params): logger.debug('Sending LOGOFF to clients') @@ -173,6 +174,12 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): # TODO: Return something useful? :) return 'Information' + def log_error(self, fmt, *args): + logger.error('HTTP ' + fmt % args) + + def log_message(self, fmt, *args): + logger.info('HTTP ' + fmt % args) + class HTTPServerThread(threading.Thread): def __init__(self, address, service):