Skip to main content

WebExt

Struct WebExt 

Source
pub struct WebExt;
Expand description

Web 解析工具

提供 URL 解析、真实 IP 获取、私网 IP 判断、查询字符串构造等功能。

Implementations§

Source§

impl WebExt

Source

pub fn domain(url_str: &str) -> Option<String>

解析 URL 获取域名(自研实现,替代 url crate)

支持 userinfo、IPv6 括号形式;端口会被剥离,IPv6 返回无括号形式。

Source

pub fn path(url_str: &str) -> Option<String>

解析 URL 获取路径(含开头的 /,不含 query/fragment;自研实现,替代 url crate)

Source

pub fn real_ip(headers: &[(String, String)], remote_addr: &str) -> String

从请求头获取真实 IP(X-Forwarded-For 或 X-Real-IP)。

§安全警告

本方法无条件信任 X-Forwarded-For / X-Real-IP 头,任何客户端都能 伪造这些头来伪装来源 IP。切勿用于限流、鉴权、审计等安全敏感场景。 仅当服务确定运行在可信反向代理之后、且该代理会覆盖这些头时才可使用。

安全场景请改用 extract_client_ip_trusted,它会校验直连对端是否为 可信代理网段后再读取头部。

Source

pub fn is_private_ip(ip: &str) -> bool

检查是否为私有 IP(委托给公共函数 is_private_ip)

Source

pub fn build_query(params: &[(&str, &str)]) -> String

构建 URL 查询字符串

Source

pub fn percent_encode(seg: &str) -> String

URL 路径段/查询参数编码:仅保留字母数字与 - . _ ~,其余字节转 %XX(大写)。

对应 RFC 3986 unreserved 字符集,与 SigV4 canonical URI/query 编码要求一致。 逐字节直接拼装,不为每个转义字节分配临时字符串。

§示例
use wordval_utils::WebExt;

assert_eq!(WebExt::percent_encode("a-b_c.d~"), "a-b_c.d~");
assert_eq!(WebExt::percent_encode("a b"), "a%20b");
assert_eq!(WebExt::percent_encode("/"), "%2F");
assert_eq!(WebExt::percent_encode("中文"), "%E4%B8%AD%E6%96%87");
Source

pub fn mime_guess(filename: &str) -> &'static str

按扩展名推断 MIME 类型(返回静态字符串,零分配;需要所有权时自行 to_string())

§示例
use wordval_utils::WebExt;

assert_eq!(WebExt::mime_guess("a.PNG"), "image/png");
assert_eq!(WebExt::mime_guess("noext"), "application/octet-stream");
Source

pub fn url_decode(s: &str) -> String

URL 解码(%E4%BD%A0 → "你")

支持 %XX 十六进制编码和 + 作为空格。 非法编码(如 %ZZ 或不完整的 %X)会原样保留。

Source

pub fn content_type_mime(content_type: &str) -> Option<String>

解析 Content-Type 头,提取 MIME 类型

§示例
use wordval_utils::WebExt;

assert_eq!(
    WebExt::content_type_mime("application/json; charset=utf-8"),
    Some("application/json".to_string())
);
assert_eq!(
    WebExt::content_type_mime("text/html"),
    Some("text/html".to_string())
);

Auto Trait Implementations§

§

impl Forget for WebExt

§

impl Freeze for WebExt

§

impl Move for WebExt

§

impl RefUnwindSafe for WebExt

§

impl Send for WebExt

§

impl Sync for WebExt

§

impl Unpin for WebExt

§

impl UnsafeUnpin for WebExt

§

impl UnwindSafe for WebExt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more