pub struct ClientInfo { /* private fields */ }Expand description
Client request context: IP, user-agent, parsed device fields, and a server-computed browser fingerprint.
Implements FromRequestParts for automatic extraction in handlers.
Requires ClientIpLayer for the ip field;
if the layer is absent, ip will be None.
For non-HTTP contexts (background jobs, CLI tools), use the builder:
use modo::client::ClientInfo;
let info = ClientInfo::new()
.ip("1.2.3.4")
.user_agent("my-script/1.0");To populate device and fingerprint fields from request headers (e.g.
inside middleware that already holds a &HeaderMap), use
ClientInfo::from_headers.
Implementations§
Source§impl ClientInfo
impl ClientInfo
Sourcepub fn user_agent(self, ua: impl Into<String>) -> Self
pub fn user_agent(self, ua: impl Into<String>) -> Self
Set the user-agent string.
Sourcepub fn device_name(self, name: impl Into<String>) -> Self
pub fn device_name(self, name: impl Into<String>) -> Self
Set the parsed device name (e.g. "Chrome on macOS").
Sourcepub fn device_type(self, kind: impl Into<String>) -> Self
pub fn device_type(self, kind: impl Into<String>) -> Self
Set the device type ("desktop", "mobile", or "tablet").
Sourcepub fn fingerprint(self, fp: impl Into<String>) -> Self
pub fn fingerprint(self, fp: impl Into<String>) -> Self
Set the SHA-256 browser fingerprint.
Sourcepub fn from_headers(
ip: Option<String>,
user_agent: &str,
accept_language: &str,
accept_encoding: &str,
) -> Self
pub fn from_headers( ip: Option<String>, user_agent: &str, accept_language: &str, accept_encoding: &str, ) -> Self
Build a fully-populated ClientInfo from the headers a server already
has at hand.
Parses device_name and device_type from user_agent, and computes
the fingerprint from user_agent + accept_language + accept_encoding.
An empty user_agent still yields meaningful values
("Unknown on Unknown" / "desktop" / a stable hash).
Sourcepub fn user_agent_value(&self) -> Option<&str>
pub fn user_agent_value(&self) -> Option<&str>
The client user-agent string, if available.
Sourcepub fn device_name_value(&self) -> Option<&str>
pub fn device_name_value(&self) -> Option<&str>
The parsed human-readable device name, if available.
Sourcepub fn device_type_value(&self) -> Option<&str>
pub fn device_type_value(&self) -> Option<&str>
The parsed device type ("desktop"/"mobile"/"tablet"), if available.
Sourcepub fn fingerprint_value(&self) -> Option<&str>
pub fn fingerprint_value(&self) -> Option<&str>
The server-computed browser fingerprint, if available.
Trait Implementations§
Source§impl Clone for ClientInfo
impl Clone for ClientInfo
Source§fn clone(&self) -> ClientInfo
fn clone(&self) -> ClientInfo
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ClientInfo
impl Debug for ClientInfo
Source§impl Default for ClientInfo
impl Default for ClientInfo
Source§fn default() -> ClientInfo
fn default() -> ClientInfo
Source§impl<S: Send + Sync> FromRequestParts<S> for ClientInfo
impl<S: Send + Sync> FromRequestParts<S> for ClientInfo
Source§async fn from_request_parts(
parts: &mut Parts,
_state: &S,
) -> Result<Self, Self::Rejection>
async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>
Builds ClientInfo from request extensions and headers.
Reads the IP from the ClientIp extension (inserted by
ClientIpLayer) and the user-agent from
the User-Agent header. Device fields are derived only when the
user-agent header is present; when it is absent they remain None
so callers can reliably distinguish “no UA was sent” from
“UA was sent but unrecognized”. The fingerprint is server-computed
from whatever combination of User-Agent, Accept-Language, and
Accept-Encoding headers were present (each defaulting to "")
and is therefore always populated.
§Errors
This extractor never fails — the Result type is required by
FromRequestParts but the implementation always returns Ok.