From d1feb3df35e491391a1600cbde00db9406629b3f Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 19 Mar 2025 19:06:45 +0100 Subject: debug method and switch http version --- readme.md | 7 ++++--- src/modules/debug.rs | 3 ++- src/modules/switch.rs | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 56a47b3..e6e7c1c 100644 --- a/readme.md +++ b/readme.md @@ -191,12 +191,13 @@ themselves; in that case the request is passed on. - Decides between two possible routes based on a condition. - `condition`: - `!is_websocket_upgrade`: Checks if a websocket was requested. - - `!is_get`: Checks if this is a GET request - - `!is_post`: Checks if this is a POST request + - `!is_{get,post,put,patch,options}`: Checks if this is a GET/... request - `!path_starts_with `: Checks if the URI path starts with some prefix - `!path_is `: Checks if the URI path is exactly what you specified - `!has_header `: Checks if the request includes a certain header. + - `!http_version [n]`: Checks for HTTP version n where HTTP/0.9 is + considered 1 aswell. - `!any [conditions]`: Checks if any of a set of conditions are satisfied. - `!all [conditions]`: Checks if all conditions are satisfied. - `case_true` Handler with matched requests (module) @@ -258,7 +259,7 @@ themselves; in that case the request is passed on. - **module `debug`** - Replies with information about the request to debug. Includes source - address, HTTP version, URI and headers. + address, HTTP version, method, URI and headers. #### Credentials config format diff --git a/src/modules/debug.rs b/src/modules/debug.rs index f0cfb98..42d023a 100644 --- a/src/modules/debug.rs +++ b/src/modules/debug.rs @@ -29,9 +29,10 @@ impl Node for Debug { ) -> Pin> + Send + Sync + 'a>> { Box::pin(async move { let s = format!( - "address: {:?}\nversion: {:?}\nuri: {:?}\nheaders: {:#?}", + "address: {:?}\nversion: {:?}\nmethod: {:?}\nuri: {:?}\nheaders: {:#?}", context.addr, request.version(), + request.method(), request.uri(), request.headers(), ); diff --git a/src/modules/switch.rs b/src/modules/switch.rs index 289c406..466bdd8 100644 --- a/src/modules/switch.rs +++ b/src/modules/switch.rs @@ -3,6 +3,7 @@ use crate::{config::DynNode, error::ServiceError}; use anyhow::Result; use futures::Future; use headers::{HeaderMapExt, Upgrade}; +use http::Version; use hyper::Method; use serde::Deserialize; use std::{pin::Pin, sync::Arc}; @@ -52,9 +53,13 @@ enum Condition { IsWebsocketUpgrade, IsPost, IsGet, + IsPut, + IsPatch, + IsOptions, HasHeader(String), PathStartsWith(String), PathIs(String), + HttpVersion(u8), } impl Condition { @@ -66,10 +71,22 @@ impl Condition { Condition::HasHeader(name) => req.headers().contains_key(name), Condition::PathStartsWith(path_prefix) => req.uri().path().starts_with(path_prefix), Condition::PathIs(path) => req.uri().path() == path, + Condition::IsPut => req.method() == Method::PUT, + Condition::IsPatch => req.method() == Method::PATCH, + Condition::IsOptions => req.method() == Method::OPTIONS, Condition::IsPost => req.method() == Method::POST, Condition::IsGet => req.method() == Method::GET, Condition::Any(conds) => conds.iter().any(|c| c.test(req)), Condition::All(conds) => conds.iter().all(|c| c.test(req)), + Condition::HttpVersion(n) => { + if req.version() == Version::HTTP_3 { + *n == 3 + } else if req.version() == Version::HTTP_2 { + *n == 2 + } else { + *n == 1 + } + } } } } -- cgit v1.2.3-70-g09d2