pavex/connection.rs
1//! Extract data concerning the HTTP connection.
2use std::net::SocketAddr;
3
4/// Information relating to the current underlying HTTP connection.
5///
6/// It includes the [peer address](SocketAddr).
7///
8/// # Guide
9///
10/// Check out [the guide](https://pavex.dev/docs/guide/request_data/connection_info/)
11/// for more details on `ConnectionInfo`.
12#[derive(Clone, Debug)]
13pub struct ConnectionInfo {
14 pub(crate) peer_addr: SocketAddr,
15}
16
17impl ConnectionInfo {
18 /// Returns the peer address.
19 ///
20 /// # Example
21 ///
22 /// ```rust
23 /// use pavex::connection::ConnectionInfo;
24 /// use pavex::Response;
25 ///
26 /// // The `ConnectionInfo` extractor can be used to access a peer's address within a handler.
27 /// pub fn echo_ip(conn_info: &ConnectionInfo) -> Response {
28 /// let body = format!(
29 /// "The peer address for this connection is {}",
30 /// conn_info.peer_addr()
31 /// );
32 /// Response::ok().set_typed_body(body)
33 /// }
34 /// ```
35 pub fn peer_addr(&self) -> SocketAddr {
36 self.peer_addr
37 }
38}