1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::Host;

impl Host for http_0_2::Uri {
    fn hostname(&self) -> &str {
        self.host().unwrap_or("")
    }

    fn port(&self) -> Option<u16> {
        match self.port_u16() {
            Some(port) => Some(port),
            None => scheme_to_port(self.scheme_str()),
        }
    }
}

impl Host for http_1::Uri {
    fn hostname(&self) -> &str {
        self.host().unwrap_or("")
    }

    fn port(&self) -> Option<u16> {
        match self.port_u16() {
            Some(port) => Some(port),
            None => scheme_to_port(self.scheme_str()),
        }
    }
}

// Get port from well-known URL schemes.
fn scheme_to_port(scheme: Option<&str>) -> Option<u16> {
    match scheme {
        // HTTP
        Some("http") => Some(80),
        Some("https") => Some(443),

        // WebSockets
        Some("ws") => Some(80),
        Some("wss") => Some(443),

        // Advanced Message Queuing Protocol (AMQP)
        Some("amqp") => Some(5672),
        Some("amqps") => Some(5671),

        // Message Queuing Telemetry Transport (MQTT)
        Some("mqtt") => Some(1883),
        Some("mqtts") => Some(8883),

        // File Transfer Protocol (FTP)
        Some("ftp") => Some(21),
        Some("ftps") => Some(990),

        // Redis
        Some("redis") => Some(6379),

        // MySQL
        Some("mysql") => Some(3306),

        // PostgreSQL
        Some("postgres") => Some(5432),

        _ => None,
    }
}