Skip to main content

trillium_async_std/
client.rs

1use crate::{AsyncStdRuntime, AsyncStdTransport};
2use async_std::net::{TcpStream, ToSocketAddrs};
3use std::{
4    io::{Error, ErrorKind, Result},
5    net::SocketAddr,
6};
7use trillium_server_common::{
8    Connector, Transport,
9    url::{Host, Url},
10};
11
12/// configuration for the tcp Connector
13#[derive(Default, Debug, Clone, Copy)]
14pub struct ClientConfig {
15    /// disable [nagle's algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm)
16    pub nodelay: Option<bool>,
17
18    /// set a time to live for the tcp protocol
19    pub ttl: Option<u32>,
20}
21
22impl ClientConfig {
23    /// constructs a default ClientConfig
24    pub const fn new() -> Self {
25        Self {
26            nodelay: None,
27            ttl: None,
28        }
29    }
30
31    /// chainable setter to set default nodelay
32    pub const fn with_nodelay(mut self, nodelay: bool) -> Self {
33        self.nodelay = Some(nodelay);
34        self
35    }
36
37    /// chainable setter for ip ttl
38    pub const fn with_ttl(mut self, ttl: u32) -> Self {
39        self.ttl = Some(ttl);
40        self
41    }
42}
43
44impl Connector for ClientConfig {
45    type Runtime = AsyncStdRuntime;
46    type Transport = AsyncStdTransport<TcpStream>;
47    type Udp = crate::AsyncStdUdpSocket;
48
49    async fn connect(&self, url: &Url) -> Result<Self::Transport> {
50        if url.scheme() != "http" {
51            return Err(Error::new(
52                ErrorKind::InvalidInput,
53                format!("unknown scheme {}", url.scheme()),
54            ));
55        }
56
57        let host = url
58            .host()
59            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, format!("{url} missing host")))?;
60
61        let port = url
62            .port_or_known_default()
63            // this should be ok because we already checked that the scheme is http, which has a
64            // default port
65            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, format!("{url} missing port")))?;
66
67        let mut tcp = match host {
68            Host::Domain(domain) => Self::Transport::connect((domain, port)).await?,
69            Host::Ipv4(ip) => Self::Transport::connect((ip, port)).await?,
70            Host::Ipv6(ip) => Self::Transport::connect((ip, port)).await?,
71        };
72
73        if let Some(nodelay) = self.nodelay {
74            tcp.set_nodelay(nodelay)?;
75        }
76
77        if let Some(ttl) = self.ttl {
78            tcp.set_ip_ttl(ttl)?;
79        }
80
81        Ok(tcp)
82    }
83
84    fn runtime(&self) -> Self::Runtime {
85        AsyncStdRuntime::default()
86    }
87
88    async fn resolve(&self, host: &str, port: u16) -> Result<Vec<SocketAddr>> {
89        (host, port).to_socket_addrs().await.map(Iterator::collect)
90    }
91}