-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfairings.rs
43 lines (37 loc) · 1.39 KB
/
fairings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// fairings.rs
// Copyright (C) 2021-2025 Óscar García Amor <[email protected]>
// Distributed under terms of the GNU GPLv3 license.
//
use rocket::fairing::{Fairing, Info, Kind};
use rocket::{http::{ContentType, Header}, Data, Request, Response};
pub struct Cors;
pub struct ForceContentType(pub ContentType);
#[rocket::async_trait]
impl Fairing for Cors {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response
}
}
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
// Add CORS headers to allow all origins to all outgoing requests
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Methods", "DELETE, GET, OPTIONS, PATCH, POST, PUT"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}
#[rocket::async_trait]
impl Fairing for ForceContentType {
fn info(&self) -> Info {
Info {
name: "Force a ContentType to requests",
kind: Kind::Request
}
}
async fn on_request(&self, request: &mut Request<'_>, _data: &mut Data<'_>) {
request.replace_header(Header::new("Accept", format!("{}", self.0)));
}
}