Skip to content

Commit

Permalink
add future
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 10, 2023
1 parent d5c65c3 commit 9c6f1b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/csurf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ blake3 = "1.5.0"
cookie = "0.18.0"
hex-simd = "0.8.0"
http = "0.2.11"
pin-project-lite = "0.2.13"
rand = "0.8.5"
tower = { version = "0.4.13", default-features = false }
zeroize = { version = "1.7.0", features = ["derive"] }
Expand Down
48 changes: 44 additions & 4 deletions lib/csurf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use cookie::Cookie;
use hex_simd::{AsciiCase, Out};
use http::{Request, Response};
use pin_project_lite::pin_project;
use rand::RngCore;
use std::{
fmt::Display,
future::Future,
pin::Pin,
sync::{Arc, Mutex},
task::{self, Poll},
};
Expand All @@ -20,7 +24,7 @@ struct Shared {
set_data: Option<(Hash, Message)>,
}

#[derive(Zeroize, ZeroizeOnDrop)]
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct CsrfHandle {
#[zeroize(skip)]
inner: Arc<Mutex<Shared>>,
Expand Down Expand Up @@ -91,6 +95,29 @@ impl<S> Layer<S> for CsrfLayer {
}
}

pin_project! {
pub struct ResponseFuture<F> {
#[pin]
inner: F,
handle: CsrfHandle,
}
}

impl<F, E, ResBody> Future for ResponseFuture<F>
where
F: Future<Output = Result<Response<ResBody>, E>>,
{
type Output = Result<Response<ResBody>, E>;

fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
let this = self.project();

this.inner.poll(cx).map_ok(|_resp| {
todo!();
})
}
}

#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct CsrfService<S> {
#[zeroize(skip)]
Expand All @@ -109,14 +136,27 @@ where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
{
type Error = S::Error;
type Future = S::Future;
type Future = ResponseFuture<S::Future>;
type Response = S::Response;

fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
self.inner.call(req)
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let handle = CsrfHandle {
inner: Arc::new(Mutex::new(Shared {
read_data: None,
set_data: None,
})),
key: self.key,
};

req.extensions_mut().insert(handle.clone());

ResponseFuture {
inner: self.inner.call(req),
handle,
}
}
}

0 comments on commit 9c6f1b4

Please sign in to comment.