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
#[derive(Debug)]
pub enum RequestTarget<'a> {
Origin(&'a str),
Absolute {
scheme: &'a str,
authority: &'a str,
path: &'a str
},
Authority(&'a str),
Asterisk,
}
fn authority_end_char(&x: &u8) -> bool {
x == b'/' || x == b'?' || x == b'#' || x == b'@'
}
pub fn parse(s: &str) -> Option<RequestTarget> {
use self::RequestTarget::*;
if s.len() == 0 {
return None;
}
if s.starts_with("/") {
return Some(Origin(s));
}
if s.starts_with("http://") {
let auth_end = s[7..].as_bytes().iter()
.position(authority_end_char)
.unwrap_or(s.len()-7);
return Some(Absolute {
scheme: "http",
authority: &s[7..7+auth_end],
path: &s[7+auth_end..],
});
}
if s.starts_with("https://") {
let auth_end = s[8..].as_bytes().iter()
.position(authority_end_char)
.unwrap_or(s.len()-8);
return Some(Absolute {
scheme: "https",
authority: &s[8..8+auth_end],
path: &s[8+auth_end..],
});
}
if s == "*" {
return Some(Asterisk);
}
if s.as_bytes().iter().position(authority_end_char).is_none() {
return Some(Authority(s));
}
return None;
}
#[cfg(test)]
mod test {
use super::RequestTarget::*;
use super::parse;
#[test]
fn test_empty() {
assert_matches!(parse(""), None);
}
#[test]
fn test_path() {
assert_matches!(parse("/hello"),
Some(Origin("/hello")));
}
#[test]
fn test_path_query() {
assert_matches!(parse("/hello?xxx"),
Some(Origin("/hello?xxx")));
}
#[test]
fn test_star() {
assert_matches!(parse("*"), Some(Asterisk));
}
#[test]
fn test_strange_path() {
assert_matches!(parse("/http://x"),
Some(Origin("/http://x")));
}
#[test]
fn test_plain_authority_uri() {
assert_matches!(parse("http://x"),
Some(Absolute { scheme: "http", authority: "x",
path: "" }));
}
#[test]
fn test_uri() {
assert_matches!(parse("http://x/"),
Some(Absolute { scheme: "http", authority: "x",
path: "/" }));
}
#[test]
fn test_bigger_uri() {
assert_matches!(parse("http://x:932/hello?world"),
Some(Absolute { scheme: "http", authority: "x:932",
path: "/hello?world" }));
}
}