Skip to content

Commit

Permalink
Removed dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Sep 24, 2024
1 parent 0e80f56 commit b8ca132
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 103 deletions.
94 changes: 0 additions & 94 deletions core/serv-api/web/src/middleware/allow_all_cors.rs
Original file line number Diff line number Diff line change
@@ -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<S, B> Transform<S, ServiceRequest> for AllowAllCors
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Transform = AllowAllCorsMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
ok(AllowAllCorsMiddleware {
service: Rc::new(service),
})
}
}

pub struct AllowAllCorsMiddleware<S> {
service: Rc<S>,
}

#[rustfmt::skip]
fn get_full_permissive_headers() -> Vec<(&'static str, &'static str)> {
vec![
Expand All @@ -83,41 +27,3 @@ pub fn add_full_allow_headers(header_map: &mut HeaderMap) {
);
}
}

impl<S, B> Service<ServiceRequest> for AllowAllCorsMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Future = Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>>>>;

fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
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,
)))
}
}
})
}
}
16 changes: 9 additions & 7 deletions core/serv-api/web/src/middleware/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand All @@ -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<S, B> Transform<S, ServiceRequest> for Auth
where
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
Expand All @@ -61,12 +61,12 @@ pub struct AuthMiddleware<S> {

impl<S, B> Service<ServiceRequest> for AuthMiddleware<S>
where
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output=Result<Self::Response, Self::Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.borrow_mut().poll_ready(cx)
Expand Down Expand Up @@ -122,7 +122,8 @@ where
}

Err(actix_web::Error::from(InternalError::from_response(
"Invalid application key", res,
"Invalid application key",
res,
)))
}
},
Expand All @@ -135,7 +136,8 @@ where
}

Err(actix_web::Error::from(InternalError::from_response(
"Missing application key", res,
"Missing application key",
res,
)))
}
}
Expand Down
1 change: 0 additions & 1 deletion core/serv-api/web/src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 1 addition & 1 deletion core/serv/src/server/appkey_cors.rs
Original file line number Diff line number Diff line change
@@ -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<actix_web::dev::Server> {
Expand Down

0 comments on commit b8ca132

Please sign in to comment.