deno.land / x / deno@v1.28.2 / cli / args / flags_allow_net.rs

flags_allow_net.rs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::url::Url;use std::net::IpAddr;use std::str::FromStr;
#[derive(Debug, PartialEq, Eq)]pub struct ParsePortError(String);
#[derive(Debug, PartialEq, Eq)]pub struct BarePort(u16);
impl FromStr for BarePort { type Err = ParsePortError; fn from_str(s: &str) -> Result<BarePort, ParsePortError> { if s.starts_with(':') { match s.split_at(1).1.parse::<u16>() { Ok(port) => Ok(BarePort(port)), Err(e) => Err(ParsePortError(e.to_string())), } } else { Err(ParsePortError( "Bare Port doesn't start with ':'".to_string(), )) } }}
pub fn validator(host_and_port: &str) -> Result<(), String> { if Url::parse(&format!("deno://{}", host_and_port)).is_ok() || host_and_port.parse::<IpAddr>().is_ok() || host_and_port.parse::<BarePort>().is_ok() { Ok(()) } else { Err(format!("Bad host:port pair: {}", host_and_port)) }}
/// Expands "bare port" paths (eg. ":8080") into full paths with hosts. It/// expands to such paths into 3 paths with following hosts: `0.0.0.0:port`,/// `127.0.0.1:port` and `localhost:port`.pub fn parse(paths: Vec<String>) -> clap::Result<Vec<String>> { let mut out: Vec<String> = vec![]; for host_and_port in paths.iter() { if Url::parse(&format!("deno://{}", host_and_port)).is_ok() || host_and_port.parse::<IpAddr>().is_ok() { out.push(host_and_port.to_owned()) } else if let Ok(port) = host_and_port.parse::<BarePort>() { // we got bare port, let's add default hosts for host in ["0.0.0.0", "127.0.0.1", "localhost"].iter() { out.push(format!("{}:{}", host, port.0)); } } else { return Err(clap::Error::raw( clap::ErrorKind::InvalidValue, format!("Bad host:port pair: {}", host_and_port), )); } } Ok(out)}
#[cfg(test)]mod bare_port_tests { use super::{BarePort, ParsePortError};
#[test] fn bare_port_parsed() { let expected = BarePort(8080); let actual = ":8080".parse::<BarePort>(); assert_eq!(actual, Ok(expected)); }
#[test] fn bare_port_parse_error1() { let expected = ParsePortError("Bare Port doesn't start with ':'".to_string()); let actual = "8080".parse::<BarePort>(); assert_eq!(actual, Err(expected)); }
#[test] fn bare_port_parse_error2() { let actual = ":65536".parse::<BarePort>(); assert!(actual.is_err()); }
#[test] fn bare_port_parse_error3() { let actual = ":14u16".parse::<BarePort>(); assert!(actual.is_err()); }
#[test] fn bare_port_parse_error4() { let actual = "Deno".parse::<BarePort>(); assert!(actual.is_err()); }
#[test] fn bare_port_parse_error5() { let actual = "deno.land:8080".parse::<BarePort>(); assert!(actual.is_err()); }}
#[cfg(test)]mod tests { use super::parse;
// Creates vector of strings, Vec<String> macro_rules! svec { ($($x:expr),*) => (vec![$($x.to_string()),*]); }
#[test] fn parse_net_args_() { let entries = svec![ "deno.land", "deno.land:80", "::", "::1", "127.0.0.1", "[::1]", "1.2.3.4:5678", "0.0.0.0:5678", "127.0.0.1:5678", "[::]:5678", "[::1]:5678", "localhost:5678", "[::1]:8080", "[::]:8000", "[::1]:8000", "localhost:8000", "0.0.0.0:4545", "127.0.0.1:4545", "999.0.88.1:80" ]; let expected = svec![ "deno.land", "deno.land:80", "::", "::1", "127.0.0.1", "[::1]", "1.2.3.4:5678", "0.0.0.0:5678", "127.0.0.1:5678", "[::]:5678", "[::1]:5678", "localhost:5678", "[::1]:8080", "[::]:8000", "[::1]:8000", "localhost:8000", "0.0.0.0:4545", "127.0.0.1:4545", "999.0.88.1:80" ]; let actual = parse(entries).unwrap(); assert_eq!(actual, expected); }
#[test] fn parse_net_args_expansion() { let entries = svec![":8080"]; let expected = svec!["0.0.0.0:8080", "127.0.0.1:8080", "localhost:8080"]; let actual = parse(entries).unwrap(); assert_eq!(actual, expected); }
#[test] fn parse_net_args_ipv6() { let entries = svec!["::", "::1", "[::1]", "[::]:5678", "[::1]:5678", "::cafe"]; let expected = svec!["::", "::1", "[::1]", "[::]:5678", "[::1]:5678", "::cafe"]; let actual = parse(entries).unwrap(); assert_eq!(actual, expected); }
#[test] fn parse_net_args_ipv6_error1() { let entries = svec![":::"]; assert!(parse(entries).is_err()); }
#[test] fn parse_net_args_ipv6_error2() { let entries = svec!["0123:4567:890a:bcde:fg::"]; assert!(parse(entries).is_err()); }
#[test] fn parse_net_args_ipv6_error3() { let entries = svec!["[::q]:8080"]; assert!(parse(entries).is_err()); }}
deno

Version Info

Tagged at
a year ago