Add CORS headers to dream server to ease integration with third-party web interfaces

This commit is contained in:
Sebastian Aigner 2022-09-04 10:22:54 +02:00
parent 751283a2de
commit 91e826e5f4

View File

@ -16,6 +16,8 @@ class DreamServer(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
self.send_header("Content-type", "text/html")
self.end_headers()
with open("./static/dream_web/index.html", "rb") as content:
@ -33,6 +35,8 @@ class DreamServer(BaseHTTPRequestHandler):
elif self.path == "/cancel":
self.canceled.set()
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes('{}', 'utf8'))
@ -55,6 +59,8 @@ class DreamServer(BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
self.send_header("Content-type", "application/json")
self.end_headers()
@ -196,6 +202,11 @@ class DreamServer(BaseHTTPRequestHandler):
print(f"Canceled.")
return
def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
self.end_headers()
class ThreadingDreamServer(ThreadingHTTPServer):
def __init__(self, server_address):