### Create HTTP ACL and reqwest client with middleware Source: https://github.com/hisn-io/http-acl/blob/main/http-acl-reqwest/README.md Shows the full setup: build an HttpAcl denying example.com, create HttpAclMiddleware, attach its DNS resolver and redirect policy to a reqwest client, then wrap with reqwest_middleware. The final assertion confirms that a request to the denied host returns an error. ```rust use http_acl_reqwest::{HttpAcl, HttpAclMiddleware}; use reqwest::Client; use reqwest_middleware::ClientBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Create an HTTP ACL let acl = HttpAcl::builder() .add_denied_host("example.com".to_string()) .unwrap() .build(); // Create the HTTP ACL middleware let middleware = HttpAclMiddleware::new(acl.clone()); // Create a reqwest client with the DNS resolver and redirect policy let client = Client::builder() .dns_resolver(middleware.dns_resolver()) .redirect(middleware.redirect_policy()) .build() .unwrap(); // Create a reqwest client with the middleware let client_with_middleware = ClientBuilder::new(client) .with(middleware) .build(); // Make a request to a denied host assert!(client_with_middleware.get("http://example.com/").send().await.is_err()); Ok(()) } ``` -------------------------------- ### Configure HttpAcl with Request and Response Modification Hooks Source: https://github.com/hisn-io/http-acl/blob/main/README.md Use HttpAclBuilder::build_full with HttpAclHooks to attach closures for modifying outgoing requests and incoming responses. This example demonstrates injecting an API key header and stripping the set-cookie response header. ```rust use http_acl::{HttpAcl, HttpAclHooks}; use std::sync::Arc; let api_key = "super-secret-api-key".to_string(); let acl = HttpAcl::builder().build_full(HttpAclHooks { // Inject a secret the caller never sees or controls. modify_request_fn: Some(Arc::new(move |_scheme, _authority, mutation| { mutation .headers .push(("x-api-key".to_string(), api_key.clone())); })), // Strip a header before the caller ever sees the response. modify_response_fn: Some(Arc::new(|_scheme, _authority, mutation| { mutation.headers.retain(|(name, _)| name != "set-cookie"); })), ..Default::default() }); ``` -------------------------------- ### Configure and verify HttpAcl Source: https://github.com/hisn-io/http-acl/blob/main/README.md Demonstrates creating an HttpAcl instance using the builder pattern to define allowed and denied rules, followed by verification assertions. ```rust use http_acl::{HttpAcl, IpNet}; fn main() -> Result<(), Box> { // Create an HTTP ACL let acl = HttpAcl::builder() .add_allowed_host("example.com".to_string()) .unwrap() .add_allowed_host("*.example.org".to_string()) .unwrap() .add_denied_host("example.net".to_string()) .unwrap() .add_allowed_port_range(8080..=8080) .unwrap() .add_denied_port_range(8443..=8443) .unwrap() .add_allowed_ip_range("1.0.0.0/8".parse::().unwrap()) .unwrap() .add_denied_ip_range("9.0.0.0/8".parse::().unwrap()) .unwrap() .build(); // Check if a request is allowed assert!(acl.is_host_allowed("example.com").is_allowed()); assert!(acl.is_host_allowed("foo.example.org").is_allowed()); assert!(!acl.is_host_allowed("example.net").is_allowed()); assert!(acl.is_port_allowed(8080).is_allowed()); assert!(!acl.is_port_allowed(8443).is_allowed()); assert!(acl.is_ip_allowed(&"1.1.1.1".parse().unwrap()).is_allowed()); assert!(acl.is_ip_allowed(&"9.9.9.9".parse().unwrap()).is_denied()); assert!(acl .is_ip_allowed(&"192.168.1.1".parse().unwrap()) .is_denied()); Ok(()) } ``` -------------------------------- ### Check request parameters against HttpAcl Source: https://github.com/hisn-io/http-acl/blob/main/http-acl/README.md This function demonstrates how to validate request components like scheme, method, authority, headers, and path against an HttpAcl instance before sending the request. ```rust use http_acl::{ HttpAcl, utils::authority::{Authority, Host}, }; fn check_request( acl: &HttpAcl, scheme: &str, method: &str, authority: &str, headers: &[(&str, &str)], url_path: &str, body: Option<&[u8]>, ) -> Result<(), String> { if acl.is_scheme_allowed(scheme).is_denied() { return Err(format!("scheme {scheme} is denied")); } if acl.is_method_allowed(method).is_denied() { return Err(format!("method {method} is denied")); } let authority = Authority::parse(authority).map_err(|_| "invalid host".to_string())?; match &authority.host { Host::Ip(ip) if acl.is_ip_allowed(ip).is_denied() => { return Err(format!("ip {ip} is denied")); } Host::Domain(domain) if acl.is_host_allowed(domain).is_denied() => { return Err(format!("host {domain} is denied")); } _ => {} } if acl.is_port_allowed(authority.port).is_denied() { return Err(format!("port {} is denied", authority.port)); } for (name, value) in headers.iter().copied() { if acl.is_header_allowed(name, value).is_denied() { return Err(format!("header {name} is denied")); } } if acl.is_url_path_allowed(url_path).is_denied() { return Err(format!("path {url_path} is denied")); } if acl .is_valid(scheme, &authority, headers.iter().copied(), body) .is_denied() { return Err("request failed custom validation".to_string()); } Ok(()) } fn main() { let acl = HttpAcl::builder() .add_denied_host("example.com".to_string()) .unwrap() .build(); let result = check_request( &acl, "https", "GET", "example.com:443", &[("accept", "*/*")], "/", None, ); assert!(result.is_err()); } ``` -------------------------------- ### Attach ModifyRequestFn and ModifyResponseFn to HttpAcl Source: https://github.com/hisn-io/http-acl/blob/main/http-acl-reqwest/README.md Creates an HttpAcl with hooks that inject an x-api-key header into requests and strip Set-Cookie from responses. The middleware applies these hooks automatically. Note that ModifyResponseFn forces full response buffering and rebuild, and neither hook applies to redirect hops as expected. ```rust use http_acl_reqwest::{HttpAcl, HttpAclHooks, HttpAclMiddleware}; use std::sync::Arc; let api_key = "super-secret-api-key".to_string(); let acl = HttpAcl::builder().build_full(HttpAclHooks { // Inject a secret the caller building the request never sees or controls. modify_request_fn: Some(Arc::new(move |_scheme, _authority, mutation| { mutation .headers .push(("x-api-key".to_string(), api_key.clone())); })), // Strip `Set-Cookie` before the caller ever sees the response. modify_response_fn: Some(Arc::new(|_scheme, _authority, mutation| { mutation.headers.retain(|(name, _)| name != "set-cookie"); })), ..Default::default() }); let middleware = HttpAclMiddleware::new(acl); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.