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" => Some(80),
24 "https" => Some(443),
25 "ws" => Some(80),
26 "wss" => Some(443),
27 "amqp" => Some(5672),
28 "amqps" => Some(5671),
29 "sb" => Some(5671),
30 "mqtt" => Some(1883),
31 "mqtts" => Some(8883),
32 _ => None,
33 }
34 } else {
35 None
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn port_tests() {
45 for (s, p) in [
46 ("http", 80),
47 ("https", 443),
48 ("ws", 80),
49 ("wss", 443),
50 ("amqp", 5672),
51 ("amqps", 5671),
52 ("sb", 5671),
53 ("mqtt", 1883),
54 ("mqtts", 8883),
55 ] {
56 assert_eq!(port(Some(s)), Some(p))
57 }
58 assert_eq!(port(Some("unknowns")), None);
59 assert_eq!(port(None), None);
60 }
61}