Percent-decode a URL query parameter value.
(s: &str)
| 307 | |
| 308 | /// Percent-decode a URL query parameter value. |
| 309 | fn percent_decode(s: &str) -> String { |
| 310 | let mut out = Vec::with_capacity(s.len()); |
| 311 | let mut bytes = s.bytes(); |
| 312 | while let Some(b) = bytes.next() { |
| 313 | if b == b'%' { |
| 314 | let hi = bytes.next().and_then(|b| char::from(b).to_digit(16)); |
| 315 | let lo = bytes.next().and_then(|b| char::from(b).to_digit(16)); |
| 316 | if let (Some(h), Some(l)) = (hi, lo) { |
| 317 | out.push(u8::try_from(h * 16 + l).unwrap_or(b'%')); |
| 318 | } else { |
| 319 | out.push(b'%'); |
| 320 | } |
| 321 | } else if b == b'+' { |
| 322 | out.push(b' '); |
| 323 | } else { |
| 324 | out.push(b); |
| 325 | } |
| 326 | } |
| 327 | String::from_utf8(out).unwrap_or_else(|_| s.to_string()) |
| 328 | } |
| 329 | |
| 330 | /// Callback server state. |
| 331 | struct CallbackState { |
no test coverage detected