A pit full of rusty nails
at main 34 lines 819 B view raw
1use core::net::SocketAddr; 2 3use hyper::HeaderMap; 4 5pub use crate::maybe_header::*; 6 7mod maybe_header; 8 9#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] 10#[repr(transparent)] 11pub struct IdentifiedPeer(Box<str>); 12 13impl IdentifiedPeer { 14 #[inline] 15 pub fn extract(headers: &HeaderMap, connection: SocketAddr) -> Self { 16 Self( 17 maybe_x_forwarded_for(headers) 18 .or_else(|| maybe_x_real_ip(headers)) 19 .or_else(|| maybe_forwarded_for(headers)) 20 .unwrap_or_else(|| connection.to_string().into()), 21 ) 22 } 23 24 #[inline] 25 pub fn peer(&self) -> &str { 26 &self.0 27 } 28} 29 30impl core::fmt::Display for IdentifiedPeer { 31 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 32 write!(f, "{}", self.0) 33 } 34}