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
use crate::http_request::url_escape::percent_encode_query;
use http::Uri;
pub(super) struct QueryWriter {
base_uri: Uri,
new_path_and_query: String,
prefix: Option<char>,
}
impl QueryWriter {
pub(super) fn new(uri: &Uri) -> Self {
let new_path_and_query = uri
.path_and_query()
.map(|pq| pq.to_string())
.unwrap_or_default();
let prefix = if uri.query().is_none() {
Some('?')
} else if !uri.query().unwrap_or_default().is_empty() {
Some('&')
} else {
None
};
QueryWriter {
base_uri: uri.clone(),
new_path_and_query,
prefix,
}
}
pub(super) fn clear_params(&mut self) {
if let Some(index) = self.new_path_and_query.find('?') {
self.new_path_and_query.truncate(index);
self.prefix = Some('?');
}
}
pub(super) fn insert(&mut self, k: &str, v: &str) {
if let Some(prefix) = self.prefix {
self.new_path_and_query.push(prefix);
}
self.prefix = Some('&');
self.new_path_and_query.push_str(&percent_encode_query(k));
self.new_path_and_query.push('=');
self.new_path_and_query.push_str(&percent_encode_query(v));
}
pub(super) fn build_query(self) -> String {
self.build_uri().query().unwrap_or_default().to_string()
}
pub(super) fn build_uri(self) -> Uri {
let mut parts = self.base_uri.into_parts();
parts.path_and_query = Some(
self.new_path_and_query
.parse()
.expect("adding query should not invalidate URI"),
);
Uri::from_parts(parts).expect("a valid URL in should always produce a valid URL out")
}
}
#[cfg(test)]
mod test {
use super::QueryWriter;
use http::Uri;
#[test]
fn empty_uri() {
let uri = Uri::from_static("http://www.example.com");
let mut query_writer = QueryWriter::new(&uri);
query_writer.insert("key", "val%ue");
query_writer.insert("another", "value");
assert_eq!(
query_writer.build_uri(),
Uri::from_static("http://www.example.com?key=val%25ue&another=value")
);
}
#[test]
fn uri_with_path() {
let uri = Uri::from_static("http://www.example.com/path");
let mut query_writer = QueryWriter::new(&uri);
query_writer.insert("key", "val%ue");
query_writer.insert("another", "value");
assert_eq!(
query_writer.build_uri(),
Uri::from_static("http://www.example.com/path?key=val%25ue&another=value")
);
}
#[test]
fn uri_with_path_and_query() {
let uri = Uri::from_static("http://www.example.com/path?original=here");
let mut query_writer = QueryWriter::new(&uri);
query_writer.insert("key", "val%ue");
query_writer.insert("another", "value");
assert_eq!(
query_writer.build_uri(),
Uri::from_static(
"http://www.example.com/path?original=here&key=val%25ue&another=value"
)
);
}
#[test]
fn build_query() {
let uri = Uri::from_static("http://www.example.com");
let mut query_writer = QueryWriter::new(&uri);
query_writer.insert("key", "val%ue");
query_writer.insert("ano%ther", "value");
assert_eq!("key=val%25ue&ano%25ther=value", query_writer.build_query());
}
#[test]
fn doesnt_panic_when_adding_query_to_valid_uri() {
let uri = Uri::from_static("http://www.example.com");
let mut problematic_chars = Vec::new();
for byte in u8::MIN..=u8::MAX {
match std::str::from_utf8(&[byte]) {
Err(_) => {
continue;
}
Ok(value) => {
let mut query_writer = QueryWriter::new(&uri);
query_writer.insert("key", value);
if let Err(_) = std::panic::catch_unwind(|| query_writer.build_uri()) {
problematic_chars.push(char::from(byte));
};
}
}
}
if !problematic_chars.is_empty() {
panic!("we got some bad bytes here: {:#?}", problematic_chars)
}
}
#[test]
fn clear_params() {
let uri = Uri::from_static("http://www.example.com/path?original=here&foo=1");
let mut query_writer = QueryWriter::new(&uri);
query_writer.clear_params();
query_writer.insert("new", "value");
assert_eq!("new=value", query_writer.build_query());
}
}