Skip to main content

HttpPolicy

Trait HttpPolicy 

Source
pub trait HttpPolicy:
    Send
    + Sync
    + 'static {
    // Required method
    fn check_url(&self, url: &str, method: &str) -> Result<(), PolicyError>;

    // Provided method
    fn policy_name(&self) -> &'static str { ... }
}
Expand description

Policy that decides whether a given URL may be accessed.

Every function in the http module calls HttpPolicy::check_url before making a request.

§Built-in implementations

TypeBehaviour
UnrestrictedNo checks (default)
HttpAllowListAllow only listed host patterns

§Custom implementations

use mlua_batteries::policy::{HttpPolicy, PolicyError};

struct BlockInternal;

impl HttpPolicy for BlockInternal {
    fn check_url(&self, url: &str, method: &str) -> Result<(), PolicyError> {
        if url.contains("169.254.") || url.contains("localhost") {
            Err(PolicyError::new(format!("{method} denied: internal URL '{url}'")))
        } else {
            Ok(())
        }
    }
}

Required Methods§

Source

fn check_url(&self, url: &str, method: &str) -> Result<(), PolicyError>

Validate url for method (e.g. “GET”, “POST”).

Return Ok(()) to allow, Err(reason) to deny.

Provided Methods§

Source

fn policy_name(&self) -> &'static str

Human-readable name for this policy, used in Debug output.

The default implementation returns std::any::type_name of the concrete type, which works correctly even through trait objects because the vtable dispatches to the concrete implementation.

Implementors§