Server cleanup

This commit is contained in:
Alex Yatskov 2021-07-18 10:34:13 -07:00
parent 6a68046fee
commit 3a4814d3a8

View File

@ -19,6 +19,7 @@ import socket
from . import util from . import util
# #
# WebRequest # WebRequest
# #
@ -66,8 +67,6 @@ class WebClient:
self.writeBuff += self.handler(req) self.writeBuff += self.handler(req)
break break
if wlist and self.writeBuff: if wlist and self.writeBuff:
try: try:
length = self.sock.send(self.writeBuff) length = self.sock.send(self.writeBuff)
@ -154,7 +153,6 @@ class WebServer:
def handlerWrapper(self, req): def handlerWrapper(self, req):
# handle multiple cors origins by checking the 'origin'-header against the allowed origin list from the config # handle multiple cors origins by checking the 'origin'-header against the allowed origin list from the config
webCorsOriginList = util.setting('webCorsOriginList') webCorsOriginList = util.setting('webCorsOriginList')
@ -166,7 +164,7 @@ class WebServer:
allowed = False allowed = False
corsOrigin = 'http://localhost' corsOrigin = 'http://localhost'
allowAllCors = '*' in webCorsOriginList # allow CORS for all domains allowAllCors = '*' in webCorsOriginList # allow CORS for all domains
if allowAllCors: if allowAllCors:
corsOrigin = '*' corsOrigin = '*'
allowed = True allowed = True
@ -175,7 +173,7 @@ class WebServer:
if originStr in webCorsOriginList : if originStr in webCorsOriginList :
corsOrigin = originStr corsOrigin = originStr
allowed = True allowed = True
elif 'http://localhost' in webCorsOriginList and ( elif 'http://localhost' in webCorsOriginList and (
originStr == 'http://127.0.0.1' or originStr == 'https://127.0.0.1' or # allow 127.0.0.1 if localhost allowed originStr == 'http://127.0.0.1' or originStr == 'https://127.0.0.1' or # allow 127.0.0.1 if localhost allowed
originStr.startswith('http://127.0.0.1:') or originStr.startswith('http://127.0.0.1:') or originStr.startswith('http://127.0.0.1:') or originStr.startswith('http://127.0.0.1:') or
originStr.startswith('chrome-extension://') or originStr.startswith('moz-extension://') ) : # allow chrome and firefox extension if localhost allowed originStr.startswith('chrome-extension://') or originStr.startswith('moz-extension://') ) : # allow chrome and firefox extension if localhost allowed
@ -184,10 +182,9 @@ class WebServer:
else: else:
allowed = True allowed = True
resp = bytes()
paramsError = False
try: try:
params = json.loads(req.body.decode('utf-8')) params = json.loads(req.body.decode('utf-8'))
paramsError = False
except ValueError: except ValueError:
body = json.dumps(None).encode('utf-8') body = json.dumps(None).encode('utf-8')
paramsError = True paramsError = True
@ -202,9 +199,9 @@ class WebServer:
params['params']['origin'] = b'origin' in req.headers and req.headers[b'origin'].decode() or '' params['params']['origin'] = b'origin' in req.headers and req.headers[b'origin'].decode() or ''
if not allowed : if not allowed :
corsOrigin = params['params']['origin'] corsOrigin = params['params']['origin']
body = json.dumps(self.handler(params)).encode('utf-8') body = json.dumps(self.handler(params)).encode('utf-8')
headers = [ headers = [
['HTTP/1.1 200 OK', None], ['HTTP/1.1 200 OK', None],
['Content-Type', 'text/json'], ['Content-Type', 'text/json'],
@ -220,6 +217,8 @@ class WebServer:
] ]
body = ''.encode('utf-8'); body = ''.encode('utf-8');
resp = bytes()
for key, value in headers: for key, value in headers:
if value is None: if value is None:
resp += '{}\r\n'.format(key).encode('utf-8') resp += '{}\r\n'.format(key).encode('utf-8')