manage json decode error, and add method OPTIONS (http request)

This commit is contained in:
Jean Sorgemoel 2018-08-03 16:51:28 +02:00
parent a9d4bbffcd
commit 3b901be756

View file

@ -199,7 +199,11 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
self.end_headers()
return None
msg = self.rfile.read(sizemsg)
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:
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]: