From b8ca132b9abcc396726694c219acfd4b725a734d Mon Sep 17 00:00:00 2001 From: scx1332 Date: Tue, 24 Sep 2024 23:29:49 +0200 Subject: [PATCH] Removed dead code --- .../web/src/middleware/allow_all_cors.rs | 94 ------------------- core/serv-api/web/src/middleware/auth/mod.rs | 16 ++-- core/serv-api/web/src/middleware/mod.rs | 1 - core/serv/src/server/appkey_cors.rs | 2 +- 4 files changed, 10 insertions(+), 103 deletions(-) diff --git a/core/serv-api/web/src/middleware/allow_all_cors.rs b/core/serv-api/web/src/middleware/allow_all_cors.rs index ec9566098..82fec1545 100644 --- a/core/serv-api/web/src/middleware/allow_all_cors.rs +++ b/core/serv-api/web/src/middleware/allow_all_cors.rs @@ -1,65 +1,9 @@ -#![allow(clippy::new_without_default)] - -use actix_service::{Service, Transform}; -use actix_web::dev::{ServiceRequest, ServiceResponse}; -use actix_web::error::InternalError; use actix_web::http::header::HeaderMap; use actix_web::http::header::HeaderName; use actix_web::http::header::HeaderValue; -use futures::future::{ok, Ready}; -use std::pin::Pin; -use std::rc::Rc; use std::str::FromStr; -use std::task::{Context, Poll}; use structopt::lazy_static::lazy_static; -// Define Middleware Struct -pub struct AllowAllCors { - _empty: u64, -} - -impl AllowAllCors { - pub fn new() -> Self { - static ATOMIC_U64: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); - - //display message only once thanks to atomic counter - if ATOMIC_U64.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 { - let mut message = - "Using AllowAllCors middleware: following headers will be added to all requests:\n" - .to_string(); - for (header_name, header_value) in get_full_permissive_headers().iter() { - message += &format!("{}: {}\n", header_name, header_value); - } - log::info!("{}", message); - } - - Self { _empty: 0 } - } -} - -// Middleware Implementation -impl Transform for AllowAllCors -where - S: Service, Error = actix_web::Error> + 'static, - B: 'static, -{ - type Response = ServiceResponse; - type Error = actix_web::Error; - type Transform = AllowAllCorsMiddleware; - type InitError = (); - type Future = Ready>; - - fn new_transform(&self, service: S) -> Self::Future { - ok(AllowAllCorsMiddleware { - service: Rc::new(service), - }) - } -} - -pub struct AllowAllCorsMiddleware { - service: Rc, -} - #[rustfmt::skip] fn get_full_permissive_headers() -> Vec<(&'static str, &'static str)> { vec![ @@ -83,41 +27,3 @@ pub fn add_full_allow_headers(header_map: &mut HeaderMap) { ); } } - -impl Service for AllowAllCorsMiddleware -where - S: Service, Error = actix_web::Error> + 'static, - B: 'static, -{ - type Response = ServiceResponse; - type Error = actix_web::Error; - type Future = Pin>>>; - - fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { - self.service.poll_ready(ctx) - } - - fn call(&self, req: ServiceRequest) -> Self::Future { - let fut = self.service.call(req); - - Box::pin(async move { - match fut.await { - Ok(mut res) => { - log::debug!("Adding full allow headers"); - add_full_allow_headers(res.headers_mut()); - Ok(res) - } - Err(err) => { - log::debug!("Adding full allow headers to error response"); - // Create an error response and add the "my-header" - let mut res = err.error_response(); - add_full_allow_headers(res.headers_mut()); - - Err(actix_web::Error::from(InternalError::from_response( - err, res, - ))) - } - } - }) - } -} diff --git a/core/serv-api/web/src/middleware/auth/mod.rs b/core/serv-api/web/src/middleware/auth/mod.rs index 5e06a6a61..e1bb3a1dc 100644 --- a/core/serv-api/web/src/middleware/auth/mod.rs +++ b/core/serv-api/web/src/middleware/auth/mod.rs @@ -5,6 +5,7 @@ pub mod resolver; pub use crate::middleware::auth::ident::Identity; pub use crate::middleware::auth::resolver::AppKeyCache; +use crate::middleware::allow_all_cors::add_full_allow_headers; use actix_service::{Service, Transform}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::error::{Error, InternalError, ParseError}; @@ -16,7 +17,6 @@ use std::cell::RefCell; use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; -use crate::middleware::allow_all_cors::add_full_allow_headers; pub struct Auth { pub(crate) cache: AppKeyCache, @@ -27,14 +27,14 @@ impl Auth { pub fn new(cache: AppKeyCache, allow_cors_on_authentication_failure: bool) -> Auth { Auth { cache, - allow_cors_on_authentication_failure + allow_cors_on_authentication_failure, } } } impl Transform for Auth where - S: Service, Error=Error> + 'static, + S: Service, Error = Error> + 'static, S::Future: 'static, B: 'static, { @@ -61,12 +61,12 @@ pub struct AuthMiddleware { impl Service for AuthMiddleware where - S: Service, Error=Error> + 'static, + S: Service, Error = Error> + 'static, S::Future: 'static, { type Response = ServiceResponse; type Error = Error; - type Future = Pin>>>; + type Future = Pin>>>; fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { self.service.borrow_mut().poll_ready(cx) @@ -122,7 +122,8 @@ where } Err(actix_web::Error::from(InternalError::from_response( - "Invalid application key", res, + "Invalid application key", + res, ))) } }, @@ -135,7 +136,8 @@ where } Err(actix_web::Error::from(InternalError::from_response( - "Missing application key", res, + "Missing application key", + res, ))) } } diff --git a/core/serv-api/web/src/middleware/mod.rs b/core/serv-api/web/src/middleware/mod.rs index 231d8dfaa..c69f2ae76 100644 --- a/core/serv-api/web/src/middleware/mod.rs +++ b/core/serv-api/web/src/middleware/mod.rs @@ -2,5 +2,4 @@ mod allow_all_cors; pub mod auth; pub mod cors; -pub use allow_all_cors::AllowAllCors; pub use auth::{ident::Identity, Auth, AuthMiddleware}; diff --git a/core/serv/src/server/appkey_cors.rs b/core/serv/src/server/appkey_cors.rs index 3223fa537..5f976b6d4 100644 --- a/core/serv/src/server/appkey_cors.rs +++ b/core/serv/src/server/appkey_cors.rs @@ -1,9 +1,9 @@ -use std::sync::Arc; use crate::server::CreateServerArgs; use crate::{dashboard_serve, forward_gsb, me, redirect_to_dashboard, Services}; use actix_web::{middleware, web, App, HttpServer}; use anyhow::Context; use metrics::counter; +use std::sync::Arc; use ya_service_api_web::middleware::auth; pub fn create_server(args: CreateServerArgs) -> anyhow::Result {