Skip to content

Commit

Permalink
add some comments, replace loop with iterator calls
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 15, 2023
1 parent 8dd01dd commit 3120eee
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 24 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion lib/csurf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ http = "0.2.11"
pin-project-lite = "0.2.13"
rand = "0.8.5"
tower = { version = "0.4.13", default-features = false }
tracing = "0.1.40"
zeroize = { version = "1.7.0", features = ["derive"] }

# `axum` feature
Expand Down
3 changes: 0 additions & 3 deletions lib/csurf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![doc = include_str!("../README.md")]

#[macro_use]
extern crate tracing;

pub use self::{
future::ResponseFuture, handle::CsrfHandle, layer::CsrfLayer, newtypes::*, service::CsrfService,
};
Expand Down
24 changes: 5 additions & 19 deletions lib/csurf/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,14 @@ where
}

fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let cookies = req
let csrf_cookie = req
.headers()
.get_all(header::COOKIE)
.into_iter()
.filter_map(|value| value.to_str().ok())
.flat_map(Cookie::split_parse_encoded);

let mut csrf_cookie = None;
for cookie in cookies {
let cookie = match cookie {
Ok(cookie) => cookie,
Err(error) => {
debug!(?error, "failed to decode cookie");
continue;
}
};

if cookie.name() == CSRF_COOKIE_NAME {
csrf_cookie = Some(cookie);
break;
}
}
.filter_map(|value| value.to_str().ok()) // Filter out all the values that aren't valid UTF-8
.flat_map(Cookie::split_parse_encoded) // Parse all the cookie headers and flatten the resulting iterator into a contiguous one
.flatten() // Call `.flatten()` to turn `Result<Cookie, Error>` -> `Cookie`, ignoring all the errors
.find(|cookie| cookie.name() == CSRF_COOKIE_NAME); // Find the cookie with the name of our CSRF cookie

let read_data = if let Some(csrf_cookie) = csrf_cookie {
csrf_cookie
Expand Down

0 comments on commit 3120eee

Please sign in to comment.