aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-06 07:38:27 +0000
committerFuwn <[email protected]>2023-04-06 07:38:27 +0000
commit50601e3248865f4c4735ea44ae0ebd253be96397 (patch)
treeb3ce54c417a8996e51e6e92c7bbf2c85e4ca7c3d
parentrefactor(router): simplify context creation (diff)
downloadwindmark-50601e3248865f4c4735ea44ae0ebd253be96397.tar.xz
windmark-50601e3248865f4c4735ea44ae0ebd253be96397.zip
feat(context): bring back peer address
-rw-r--r--examples/windmark.rs32
-rw-r--r--src/context/error.rs12
-rw-r--r--src/context/hook.rs11
-rw-r--r--src/context/route.rs11
-rw-r--r--src/router.rs10
5 files changed, 47 insertions, 29 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs
index 687704d..0cdecd5 100644
--- a/examples/windmark.rs
+++ b/examples/windmark.rs
@@ -80,22 +80,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
r.mount("/module", success!("This is a module!"));
});
router.attach_async(Clicker::default());
- // router.set_pre_route_callback(|context| {
- // info!(
- // "accepted connection from {} to {}",
- // context.tcp.peer_addr().unwrap().ip(),
- // context.url.to_string()
- // )
- // });
- // router.set_post_route_callback(|context, content| {
- // content.content =
- // content.content.replace("Welcome!", "Welcome to Windmark!");
- //
- // info!(
- // "closed connection from {}",
- // context.tcp.peer_addr().unwrap().ip()
- // )
- // });
+ router.set_pre_route_callback(|context| {
+ info!(
+ "accepted connection from {} to {}",
+ context.peer_address.unwrap().ip(),
+ context.url.to_string()
+ )
+ });
+ router.set_post_route_callback(|context, content| {
+ content.content =
+ content.content.replace("Welcome!", "Welcome to Windmark!");
+
+ info!(
+ "closed connection from {}",
+ context.peer_address.unwrap().ip()
+ )
+ });
router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string());
router.add_footer(|_| "Copyright 2022".to_string());
router.add_footer(|context| {
diff --git a/src/context/error.rs b/src/context/error.rs
index 7e02b1a..3540a45 100644
--- a/src/context/error.rs
+++ b/src/context/error.rs
@@ -22,14 +22,20 @@ use url::Url;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
pub struct ErrorContext {
- pub url: Url,
- pub certificate: Option<X509>,
+ pub peer_address: Option<std::net::SocketAddr>,
+ pub url: Url,
+ pub certificate: Option<X509>,
}
impl ErrorContext {
#[must_use]
- pub const fn new(url: Url, certificate: Option<X509>) -> Self {
+ pub fn new(
+ peer_address: std::io::Result<std::net::SocketAddr>,
+ url: Url,
+ certificate: Option<X509>,
+ ) -> Self {
Self {
+ peer_address: peer_address.ok(),
url,
certificate,
}
diff --git a/src/context/hook.rs b/src/context/hook.rs
index 94a6908..6a4fe72 100644
--- a/src/context/hook.rs
+++ b/src/context/hook.rs
@@ -23,19 +23,22 @@ use url::Url;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
pub struct HookContext<'a> {
- pub url: Url,
- pub params: Option<Params<'a, 'a>>,
- pub certificate: Option<X509>,
+ pub peer_address: Option<std::net::SocketAddr>,
+ pub url: Url,
+ pub params: Option<Params<'a, 'a>>,
+ pub certificate: Option<X509>,
}
impl<'a> HookContext<'a> {
#[must_use]
- pub const fn new(
+ pub fn new(
+ peer_address: std::io::Result<std::net::SocketAddr>,
url: Url,
params: Option<Params<'a, 'a>>,
certificate: Option<X509>,
) -> Self {
Self {
+ peer_address: peer_address.ok(),
url,
params,
certificate,
diff --git a/src/context/route.rs b/src/context/route.rs
index b3b105d..9ff0b03 100644
--- a/src/context/route.rs
+++ b/src/context/route.rs
@@ -23,19 +23,22 @@ use url::Url;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
pub struct RouteContext<'a> {
- pub url: Url,
- pub params: Params<'a, 'a>,
- pub certificate: Option<X509>,
+ pub peer_address: Option<std::net::SocketAddr>,
+ pub url: Url,
+ pub params: Params<'a, 'a>,
+ pub certificate: Option<X509>,
}
impl<'a> RouteContext<'a> {
#[must_use]
- pub const fn new(
+ pub fn new(
+ peer_address: std::io::Result<std::net::SocketAddr>,
url: Url,
params: Params<'a, 'a>,
certificate: Option<X509>,
) -> Self {
Self {
+ peer_address: peer_address.ok(),
url,
params,
certificate,
diff --git a/src/router.rs b/src/router.rs
index 5b674a5..c865fa9 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -346,6 +346,7 @@ impl Router {
let route = &mut self.routes.at(&fixed_path);
let peer_certificate = stream.ssl().peer_certificate();
let hook_context = HookContext::new(
+ stream.get_ref().peer_addr(),
url.clone(),
route
.as_ref()
@@ -365,8 +366,12 @@ impl Router {
let mut content = if let Ok(ref route) = route {
let footers_length = (*self.footers.lock().unwrap()).len();
- let route_context =
- RouteContext::new(url.clone(), route.params.clone(), peer_certificate);
+ let route_context = RouteContext::new(
+ stream.get_ref().peer_addr(),
+ url.clone(),
+ route.params.clone(),
+ peer_certificate,
+ );
for partial_header in &mut *self.headers.lock().unwrap() {
header
@@ -394,6 +399,7 @@ impl Router {
handler.await
} else {
(*self.error_handler).lock().unwrap()(ErrorContext::new(
+ stream.get_ref().peer_addr(),
url.clone(),
peer_certificate,
))