Skip to content

Commit 1f4d8f9

Browse files
huonwalexcrichton
authored andcommitted
url: encode small bytes correctly.
Previously, bytes less than 16 would be encoded as %X, rather than %XX, since the output width was left to be automatic.
1 parent b662aa5 commit 1f4d8f9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/liburl/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ fn encode_inner(s: &str, full_url: bool) -> String {
161161
out.push_char(ch);
162162
}
163163

164-
_ => out.push_str(format!("%{:X}", ch as uint).as_slice())
164+
_ => out.push_str(format!("%{:02X}", ch as uint).as_slice())
165165
}
166166
} else {
167-
out.push_str(format!("%{:X}", ch as uint).as_slice());
167+
out.push_str(format!("%{:02X}", ch as uint).as_slice());
168168
}
169169
}
170170
}
@@ -1178,6 +1178,8 @@ mod tests {
11781178
assert_eq!(encode("@"), "@".to_string());
11791179
assert_eq!(encode("["), "[".to_string());
11801180
assert_eq!(encode("]"), "]".to_string());
1181+
assert_eq!(encode("\0"), "%00".to_string());
1182+
assert_eq!(encode("\n"), "%0A".to_string());
11811183
}
11821184

11831185
#[test]
@@ -1207,6 +1209,8 @@ mod tests {
12071209
assert_eq!(encode_component("@"), "%40".to_string());
12081210
assert_eq!(encode_component("["), "%5B".to_string());
12091211
assert_eq!(encode_component("]"), "%5D".to_string());
1212+
assert_eq!(encode_component("\0"), "%00".to_string());
1213+
assert_eq!(encode_component("\n"), "%0A".to_string());
12101214
}
12111215

12121216
#[test]

0 commit comments

Comments
 (0)