mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-11-04 19:55:11 +02:00
Upgrade to Rocket v0.5.2 (#7890)
Upgrade Rocket to v0.5.2 which has updated all the crate dependencies. Also adjusted some code which I changed with Rkt and during that found some small bugs and fixed those too. Adjusted the ETag Caching a bit. Also updated other crates and GHA. Signed-off-by: BlackDex <black.dex@gmail.com>
This commit is contained in:
1 parent
e5a90968f2
commit
44c4853d37
11 files changed
+262
-260
No files matched your search
+59
-28
@@ -259,14 +259,16 @@ impl<'r, R: 'r + Responder<'r, 'static> + Send> Responder<'r, 'static> for Cache
|
||||
|
||||
pub struct EtagCached<R> {
|
||||
response: R,
|
||||
content_type: ContentType,
|
||||
etag: String,
|
||||
}
|
||||
|
||||
impl<R> EtagCached<R> {
|
||||
/// An `etag` response should always be quoted
|
||||
pub fn new(response: R, etag: &str) -> Self {
|
||||
pub fn new(response: R, content_type: ContentType, etag: &str) -> Self {
|
||||
Self {
|
||||
response,
|
||||
content_type,
|
||||
etag: format!("\"{etag}\""),
|
||||
}
|
||||
}
|
||||
@@ -288,7 +290,8 @@ impl<'r, R: 'r + Responder<'r, 'static> + Send> Responder<'r, 'static> for EtagC
|
||||
self.response.respond_to(request)?
|
||||
};
|
||||
|
||||
// Both 200 (OK) and 304 (Not Modified) need to return the etag and cache-control
|
||||
// Both 200 (OK) and 304 (Not Modified) need to return the Content-Type Etag and Cache-Control
|
||||
res.set_header(self.content_type);
|
||||
res.set_raw_header("Etag", self.etag);
|
||||
res.set_raw_header("Cache-Control", "public, no-cache");
|
||||
Ok(res)
|
||||
@@ -299,19 +302,55 @@ impl<'r, R: 'r + Responder<'r, 'static> + Send> Responder<'r, 'static> for EtagC
|
||||
// Effectively ignores, any static file route, and the alive endpoint
|
||||
const LOGGED_ROUTES: [&str; 7] = ["/api", "/admin", "/identity", "/icons", "/attachments", "/events", "/notifications"];
|
||||
|
||||
// Boolean is extra debug, when true, we ignore the whitelist above and also print the mounts
|
||||
pub struct BetterLogging(pub bool);
|
||||
// When extra is true, we ignore the whitelist above and also print the mounts
|
||||
pub struct BetterLogging {
|
||||
extra: bool,
|
||||
log_requests: bool,
|
||||
log_response: bool,
|
||||
domain_path: String,
|
||||
}
|
||||
|
||||
impl BetterLogging {
|
||||
pub fn new(extra: bool, domain_path: &str) -> Self {
|
||||
Self {
|
||||
extra,
|
||||
log_requests: log_enabled!(target: "request", log::Level::Info),
|
||||
log_response: log_enabled!(target: "response", log::Level::Info),
|
||||
domain_path: rocket::http::RawStr::new(&domain_path).percent_decode_lossy().into_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn log_path<'p>(&self, method: Method, path: &'p rocket::http::RawStr) -> Option<std::borrow::Cow<'p, str>> {
|
||||
if !self.extra && method == Method::Options {
|
||||
return None;
|
||||
}
|
||||
let path = path.percent_decode_lossy();
|
||||
if self.extra {
|
||||
return Some(path);
|
||||
}
|
||||
let subpath = path.strip_prefix(&self.domain_path).unwrap_or(&path);
|
||||
LOGGED_ROUTES.iter().any(|r| subpath.starts_with(r)).then_some(path)
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl Fairing for BetterLogging {
|
||||
fn info(&self) -> Info {
|
||||
let mut kind = Kind::Liftoff;
|
||||
if self.log_requests {
|
||||
kind = kind | Kind::Request;
|
||||
}
|
||||
if self.log_response {
|
||||
kind = kind | Kind::Response;
|
||||
}
|
||||
Info {
|
||||
name: "Better Logging",
|
||||
kind: Kind::Liftoff | Kind::Request | Kind::Response,
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
async fn on_liftoff(&self, rocket: &Rocket<Orbit>) {
|
||||
if self.0 {
|
||||
if self.extra {
|
||||
info!(target: "routes", "Routes loaded:");
|
||||
let mut routes: Vec<_> = rocket.routes().collect();
|
||||
routes.sort_by_key(|r| r.uri.path());
|
||||
@@ -335,36 +374,28 @@ impl Fairing for BetterLogging {
|
||||
}
|
||||
|
||||
async fn on_request(&self, request: &mut Request<'_>, _data: &mut Data<'_>) {
|
||||
let method = request.method();
|
||||
if !self.0 && method == Method::Options {
|
||||
return;
|
||||
}
|
||||
let uri = request.uri();
|
||||
let uri_path = uri.path();
|
||||
let uri_path_str = uri_path.url_decode_lossy();
|
||||
let uri_subpath = uri_path_str.strip_prefix(&CONFIG.domain_path()).unwrap_or(&uri_path_str);
|
||||
if self.0 || LOGGED_ROUTES.iter().any(|r| uri_subpath.starts_with(r)) {
|
||||
match uri.query() {
|
||||
Some(q) => info!(target: "request", "{method} {uri_path_str}?{}", &q[..q.len().min(30)]),
|
||||
None => info!(target: "request", "{method} {uri_path_str}"),
|
||||
let method = request.method();
|
||||
let Some(path) = self.log_path(method, uri.path().raw()) else {
|
||||
return;
|
||||
};
|
||||
match uri.query() {
|
||||
Some(q) => {
|
||||
let q = q.as_str();
|
||||
info!(target: "request", "{method} {}?{}", path.escape_debug(), q[..q.floor_char_boundary(30)].escape_debug());
|
||||
}
|
||||
None => info!(target: "request", "{method} {}", path.escape_debug()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
|
||||
if !self.0 && request.method() == Method::Options {
|
||||
if self.log_path(request.method(), request.uri().path().raw()).is_none() {
|
||||
return;
|
||||
}
|
||||
let uri_path = request.uri().path();
|
||||
let uri_path_str = uri_path.url_decode_lossy();
|
||||
let uri_subpath = uri_path_str.strip_prefix(&CONFIG.domain_path()).unwrap_or(&uri_path_str);
|
||||
if self.0 || LOGGED_ROUTES.iter().any(|r| uri_subpath.starts_with(r)) {
|
||||
let status = response.status();
|
||||
if let Some(route) = request.route() {
|
||||
info!(target: "response", "{route} => {status}");
|
||||
} else {
|
||||
info!(target: "response", "{status}");
|
||||
}
|
||||
let status = response.status();
|
||||
match request.route() {
|
||||
Some(r) => info!(target: "response", "{r} => {status}"),
|
||||
None => info!(target: "response", "{status}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user