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
|
use std::convert::TryFrom;
use super::character_classes::{
HEXDIG,
IPV_FUTURE_LAST_PART,
REG_NAME_NOT_PCT_ENCODED,
};
use super::context::Context;
use super::error::Error;
use super::percent_encoded_character_decoder::PercentEncodedCharacterDecoder;
use super::validate_ipv6_address::validate_ipv6_address;
struct Shared {
host: Vec<u8>,
host_is_reg_name: bool,
ipv6_address: String,
pec_decoder: PercentEncodedCharacterDecoder,
port_string: String,
}
enum State {
NotIpLiteral(Shared),
PercentEncodedCharacter(Shared),
Ipv6Address(Shared),
IpvFutureNumber(Shared),
IpvFutureBody(Shared),
GarbageCheck(Shared),
Port(Shared),
}
impl State{
fn finalize(self) -> Result<(Vec<u8>, Option<u16>), Error> {
match self {
Self::PercentEncodedCharacter(_)
| Self::Ipv6Address(_)
| Self::IpvFutureNumber(_)
| Self::IpvFutureBody(_) => {
// truncated or ended early
Err(Error::TruncatedHost)
},
Self::NotIpLiteral(state)
| Self::GarbageCheck(state)
| Self::Port(state) => {
let mut state = state;
if state.host_is_reg_name {
state.host.make_ascii_lowercase();
}
let port = if state.port_string.is_empty() {
None
} else {
match state.port_string.parse::<u16>() {
Ok(port) => {
Some(port)
},
Err(error) => {
return Err(Error::IllegalPortNumber(error));
}
}
};
Ok((state.host, port))
},
}
}
fn new(host_port_string: &str) -> (Self, &str) {
let mut shared = Shared{
host: Vec::<u8>::new(),
host_is_reg_name: false,
ipv6_address: String::new(),
pec_decoder: PercentEncodedCharacterDecoder::new(),
port_string: String::new(),
};
let mut host_port_string = host_port_string;
if host_port_string.starts_with("[v") {
host_port_string = &host_port_string[2..];
shared.host.push(b'v');
(
Self::IpvFutureNumber(shared),
host_port_string
)
} else if host_port_string.starts_with('[') {
host_port_string = &host_port_string[1..];
(
Self::Ipv6Address(shared),
host_port_string
)
} else {
shared.host_is_reg_name = true;
(
Self::NotIpLiteral(shared),
host_port_string
)
}
}
fn next(self, c: char) -> Result<Self, Error> {
match self {
Self::NotIpLiteral(state) => Self::next_not_ip_literal(state, c),
Self::PercentEncodedCharacter(state) => Self::next_percent_encoded_character(state, c),
Self::Ipv6Address(state) => Self::next_ipv6_address(state, c),
Self::IpvFutureNumber(state) => Self::next_ipv_future_number(state, c),
Self::IpvFutureBody(state) => Self::next_ipv_future_body(state, c),
Self::GarbageCheck(state) => Self::next_garbage_check(state, c),
Self::Port(state) => Self::next_port(state, c),
}
}
fn next_not_ip_literal(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
if c == '%' {
Ok(Self::PercentEncodedCharacter(state))
} else if c == ':' {
Ok(Self::Port(state))
} else if REG_NAME_NOT_PCT_ENCODED.contains(&c) {
state.host.push(u8::try_from(c as u32).unwrap());
Ok(Self::NotIpLiteral(state))
} else {
Err(Error::IllegalCharacter(Context::Host))
}
}
fn next_percent_encoded_character(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
if let Some(ci) = state.pec_decoder.next(c)? {
state.host.push(ci);
Ok(Self::NotIpLiteral(state))
} else {
Ok(Self::PercentEncodedCharacter(state))
}
}
fn next_ipv6_address(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
if c == ']' {
validate_ipv6_address(&state.ipv6_address)?;
state.host = state.ipv6_address.chars().map(
|c| u8::try_from(c as u32).unwrap()
).collect();
Ok(Self::GarbageCheck(state))
} else {
state.ipv6_address.push(c);
Ok(Self::Ipv6Address(state))
}
}
fn next_ipv_future_number(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
if c == '.' {
state.host.push(b'.');
Ok(Self::IpvFutureBody(state))
} else if c == ']' {
Err(Error::TruncatedHost)
} else if HEXDIG.contains(&c) {
state.host.push(u8::try_from(c as u32).unwrap());
Ok(Self::IpvFutureNumber(state))
} else {
Err(Error::IllegalCharacter(Context::IpvFuture))
}
}
fn next_ipv_future_body(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
if c == ']' {
Ok(Self::GarbageCheck(state))
} else if IPV_FUTURE_LAST_PART.contains(&c) {
state.host.push(u8::try_from(c as u32).unwrap());
Ok(Self::IpvFutureBody(state))
} else {
Err(Error::IllegalCharacter(Context::IpvFuture))
}
}
fn next_garbage_check(state: Shared, c: char) -> Result<Self, Error> {
// illegal to have anything else, unless it's a colon,
// in which case it's a port delimiter
if c == ':' {
Ok(Self::Port(state))
} else {
Err(Error::IllegalCharacter(Context::Host))
}
}
fn next_port(state: Shared, c: char) -> Result<Self, Error> {
let mut state = state;
state.port_string.push(c);
Ok(Self::Port(state))
}
}
pub fn parse_host_port<T>(host_port_string: T) -> Result<(Vec<u8>, Option<u16>), Error>
where T: AsRef<str>
{
let (machine, host_port_string) = State::new(host_port_string.as_ref());
host_port_string
.chars()
.try_fold(machine, |machine, c| {
machine.next(c)
})?
.finalize()
}
|