From 307fff4a330f9098c9e79ae446fe23580e8cf6bb Mon Sep 17 00:00:00 2001 From: veeso Date: Sun, 15 Dec 2024 16:53:19 +0100 Subject: [PATCH] fix: cors --- src/deferred_data/src/http.rs | 5 +++++ src/did/src/common/http.rs | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/deferred_data/src/http.rs b/src/deferred_data/src/http.rs index f3d3dab..0f745c3 100644 --- a/src/deferred_data/src/http.rs +++ b/src/deferred_data/src/http.rs @@ -19,6 +19,11 @@ pub struct HttpApi; impl HttpApi { /// Handles an HTTP request pub async fn handle_http_request(req: HttpRequest) -> HttpResponse { + // handle CORS preflight request + if req.method == "OPTIONS" { + return HttpResponse::ok("".to_string()); + } + // must be a GET request if req.method != "GET" { return HttpResponse::bad_request("expected GET method".to_string()); diff --git a/src/did/src/common/http.rs b/src/did/src/common/http.rs index 9e0141f..96d2a5b 100644 --- a/src/did/src/common/http.rs +++ b/src/did/src/common/http.rs @@ -27,10 +27,25 @@ pub struct HttpResponse { impl HttpResponse { pub fn new( status_code: u16, - headers: HashMap, Cow<'static, str>>, + mut headers: HashMap, Cow<'static, str>>, body: ByteBuf, upgrade: Option, ) -> Self { + // Imposta le intestazioni per consentire CORS + let cors_headers = [ + ("Access-Control-Allow-Origin", "*"), + ("Access-Control-Allow-Methods", "GET, POST, OPTIONS"), + ( + "Access-Control-Allow-Headers", + "Content-Type, Authorization", + ), + ]; + + // insert CORS headers + for (k, v) in cors_headers.iter() { + headers.insert(Cow::Borrowed(*k), Cow::Borrowed(*v)); + } + Self { status_code, headers,