Skip to content

Commit 91e826e

Browse files
Add CORS headers to dream server to ease integration with third-party web interfaces
1 parent 751283a commit 91e826e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ldm/dream/server.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class DreamServer(BaseHTTPRequestHandler):
1616
def do_GET(self):
1717
if self.path == "/":
1818
self.send_response(200)
19+
self.send_header("Access-Control-Allow-Origin", "*")
20+
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
1921
self.send_header("Content-type", "text/html")
2022
self.end_headers()
2123
with open("./static/dream_web/index.html", "rb") as content:
@@ -33,6 +35,8 @@ def do_GET(self):
3335
elif self.path == "/cancel":
3436
self.canceled.set()
3537
self.send_response(200)
38+
self.send_header("Access-Control-Allow-Origin", "*")
39+
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
3640
self.send_header("Content-type", "application/json")
3741
self.end_headers()
3842
self.wfile.write(bytes('{}', 'utf8'))
@@ -55,6 +59,8 @@ def do_GET(self):
5559

5660
def do_POST(self):
5761
self.send_response(200)
62+
self.send_header("Access-Control-Allow-Origin", "*")
63+
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
5864
self.send_header("Content-type", "application/json")
5965
self.end_headers()
6066

@@ -196,6 +202,11 @@ def image_progress(sample, step):
196202
print(f"Canceled.")
197203
return
198204

205+
def do_OPTIONS(self):
206+
self.send_response(200)
207+
self.send_header("Access-Control-Allow-Origin", "*")
208+
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
209+
self.end_headers()
199210

200211
class ThreadingDreamServer(ThreadingHTTPServer):
201212
def __init__(self, server_address):

0 commit comments

Comments
 (0)