1use ntex_http::Uri;
2
3use super::Address;
4
5impl Address for Uri {
6 fn host(&self) -> &str {
7 self.host().unwrap_or("")
8 }
9
10 fn port(&self) -> Option<u16> {
11 if let Some(port) = self.port_u16() {
12 Some(port)
13 } else {
14 port(self.scheme_str())
15 }
16 }
17}
18
19fn port(scheme: Option<&str>) -> Option<u16> {
21 if let Some(scheme) = scheme {
22 match scheme {
23 "http" | "ws" => Some(80),
24 "https" | "wss" => Some(443),
25 "amqp" => Some(5672),
26 "amqps" | "sb" => Some(5671),
27 "mqtt" => Some(1883),
28 "mqtts" => Some(8883),
29 _ => None,
30 }
31 } else {
32 None
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn port_tests() {
42 for (s, p) in [
43 ("http", 80),
44 ("https", 443),
45 ("ws", 80),
46 ("wss", 443),
47 ("amqp", 5672),
48 ("amqps", 5671),
49 ("sb", 5671),
50 ("mqtt", 1883),
51 ("mqtts", 8883),
52 ] {
53 assert_eq!(port(Some(s)), Some(p));
54 }
55 assert_eq!(port(Some("unknowns")), None);
56 assert_eq!(port(None), None);
57 }
58}