From 3b901be7564d4f9d9edd68b2790b081a19f89c57 Mon Sep 17 00:00:00 2001 From: Jean Sorgemoel Date: Fri, 3 Aug 2018 16:51:28 +0200 Subject: [PATCH] manage json decode error, and add method OPTIONS (http request) --- pymanager/manager.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/pymanager/manager.py b/pymanager/manager.py index 3ccb835..1960f53 100755 --- a/pymanager/manager.py +++ b/pymanager/manager.py @@ -199,7 +199,11 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler): self.end_headers() return None msg = self.rfile.read(sizemsg) - msgjson = json.loads(msg.decode()) + try: + msgjson = json.loads(msg.decode()) + except json.decoder.JSONDecodeError as e: + logging.error("Received request with json (%s)" % str(e)) + return None return msgjson def _command_log(self): @@ -442,6 +446,14 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler): logging.debug('delete recieved!') self.send_error(404, 'File Not Found: %s' % self.path) + def do_OPTIONS(self): + """ request OPTIONS received """ + self.send_response(200, "ok") + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS') + self.send_header("Access-Control-Allow-Headers", "*") + self.end_headers() + class khaganatHTTPServer(http.server.HTTPServer): """ @@ -778,6 +790,13 @@ class Manager(): self.users = {} self.passwordfile = None self.serverHttp = None + self.port = 8000 + self.address = '' + self.keyfile = 'crt/key.pem' + self.certfile = 'crt/cert.pem' + self.ca_cert = 'crt/ca_cert.crt' + self.authentification = False + self.method = 'http' def load_config(self, filecfg): if filecfg is None: @@ -812,23 +831,23 @@ class Manager(): try: self.port = int(config[name]['port']) except (TypeError, KeyError, ValueError): - self.port = 8000 + pass try: self.address = config[name]['address'] except (TypeError, KeyError): - self.address = '' + pass try: self.keyfile = config[name]['keyfile'] except (TypeError, KeyError): - self.keyfile = 'crt/key.pem' + pass try: self.certfile = config[name]['certfile'] except (TypeError, KeyError): - self.certfile = 'crt/cert.pem' + pass try: self.ca_cert = config[name]['ca_cert'] except (TypeError, KeyError): - self.ca_cert = 'crt/ca_cert.crt' + pass try: tmp = config[name]['authentification'] if tmp.upper().strip() == 'YES': @@ -836,17 +855,21 @@ class Manager(): else: self.authentification = False except (TypeError, KeyError): - self.authentification = False + pass try: self.passwordfile = config[name]['passwordfile'] except (TypeError, KeyError): - self.passwordfile = None + pass try: self.method = config[name]['method'] except (TypeError, KeyError): - self.method = 'http' + pass else: - head, value = name.split(':', maxsplit=1) + try: + head, value = name.split(':', maxsplit=1) + except ValueError: + logging.warning("ignore bad parameter '%s'" % (name)) + continue if head == 'command' and 'command' in config[name]: logging.debug("read command '%s'" % name) if 'path' in config[name]: