Skip to content

Commit

Permalink
fix: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Dec 15, 2024
1 parent 65da8e9 commit 307fff4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/deferred_data/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
17 changes: 16 additions & 1 deletion src/did/src/common/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ pub struct HttpResponse {
impl HttpResponse {
pub fn new(
status_code: u16,
headers: HashMap<Cow<'static, str>, Cow<'static, str>>,
mut headers: HashMap<Cow<'static, str>, Cow<'static, str>>,
body: ByteBuf,
upgrade: Option<bool>,
) -> 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,
Expand Down

0 comments on commit 307fff4

Please sign in to comment.