lwuri 0.1.1

Crate for using and manipulating Uniform Resource Identifiers with a minimal memory footprint and strong typing.
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use core::char::REPLACEMENT_CHARACTER;
use core::fmt;
use core::fmt::Display;
use core::fmt::Write;
use core::iter::FusedIterator;
use core::str::{from_utf8, from_utf8_unchecked};

#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
#[cfg(feature = "alloc")]
use core::convert::TryInto;

/// Used for determining how long a given UTF8 sequence is.
/// Table values come from https://tools.ietf.org/html/rfc3629
static UTF8_CHAR_WIDTH: [u8; 256] = [
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x1F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x3F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x5F
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, // 0x7F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0x9F
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, // 0xBF
    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, // 0xDF
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF
    4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF
];

/// An iterator used to apply URI percent decoding to strings.
///
/// It is constructed via the method [`unescape_uri()`].
/// See the documentation for [`StrExt`] for more information.
///
/// The iterator can be checked for detected errors via the [`first_error()`] method.
/// Otherwise, the iterator will attempt to make a best-effort lossy conversion when
/// faced with errors.
///
/// [`StrExt`]: trait.StrExt.html
/// [`unescape_uri()`]: trait.StrExt.html#tymethod.unescape_uri
/// [`first_error()`]: #method.first_error
#[derive(Debug, Clone)]
pub struct UnescapeUri<'a> {
    pub(super) iter: core::str::Chars<'a>,
    pub(super) iter_index: usize,
    pub(super) next_c: Option<(char, Option<char>)>,
    pub(super) had_error: Option<DecodingError>,
    pub(super) skip_slashes: bool,
}

impl<'a> Display for UnescapeUri<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.clone().try_for_each(|c| f.write_char(c))
    }
}

#[cfg(feature = "alloc")]
impl<'a> From<UnescapeUri<'a>> for Cow<'a, str> {
    fn from(iter: UnescapeUri<'a>) -> Self {
        iter.to_cow()
    }
}

impl<'a> FusedIterator for UnescapeUri<'a> {}

impl<'a> UnescapeUri<'a> {
    /// Returns the first encountered encoding error, if any.
    pub fn first_error(&self) -> Option<UnescapeError> {
        let mut iter = self.clone();
        let begin = iter.index();

        while let Some(_) = iter.next() {
            if iter.had_error.is_some() {
                break;
            }
        }

        if let Some(err) = iter.had_error {
            let end = iter.index();
            let i = (end - begin).saturating_sub(1);

            return Some(UnescapeError::new(err, i));
        }

        None
    }

    /// Indicates the number of characters that have been read by this iterator
    /// from the source string.
    pub fn index(&self) -> usize {
        self.iter_index
    }

    /// Consumes the iterator and returns a new one that will skip encoded slashes.
    /// See [Skipping Slashes] for more information.
    ///
    /// [Skipping Slashes]: trait.StrExt.html#Skipping_Slashes
    pub fn skip_slashes(mut self) -> Self {
        self.skip_slashes = true;
        self
    }

    /// Decodes the string (lossily if necessary), returning it as a copy-on-write type.
    #[cfg(feature = "alloc")]
    pub fn to_cow(&self) -> Cow<'a, str> {
        let as_str = self.iter.as_str();
        if as_str
            .find(|c: char| !c.is_ascii_graphic() || c == '%')
            .is_some()
        {
            Cow::from(self.to_string())
        } else {
            Cow::from(as_str)
        }
    }

    /// Attempts to losslessly decode the string, returning it as a copy-on-write type.
    ///
    /// # Errors
    ///
    /// If the string cannot be decoded losslessly, then a [`UnescapeError`] is
    /// returned, from which the location of the decoding error can be obtained
    /// with [`UnescapeError::index`].
    #[cfg(feature = "alloc")]
    pub fn try_to_cow(&self) -> Result<Cow<'a, str>, UnescapeError> {
        let as_str = self.iter.as_str();
        if as_str
            .find(|c: char| !c.is_ascii_graphic() || c == '%')
            .is_some()
        {
            self.try_to_string().map(Cow::from)
        } else {
            Ok(Cow::from(as_str))
        }
    }

    /// Attempts to losslessly decode the string, returning it as a standard [`String`].
    ///
    /// If the string cannot be decoded losslessly, then the location of the encoding
    /// error is returned as an `Err`.
    ///
    /// [`String`]: std::string::String
    #[cfg(feature = "alloc")]
    pub fn try_to_string(&self) -> Result<String, UnescapeError> {
        self.clone().try_into()
    }

    /// Checks to see if this iterator has the given *unescaped* prefix,
    /// and, if it does, returns the index of the end of the pattern in the haystack.
    ///
    /// Note that this doesn't take a `Pattern` as an argument, because that
    /// trait only works on string slices and not iterators.
    ///
    /// If encoding errors are encountered then the method returns `None`.
    ///
    /// ## Example
    ///
    /// ```
    /// use lwuri::prelude::*;
    /// let path = "bl%C3%A5b%C3%A6r/%2F/syltet%C3%B8y/and/on/and/on";
    /// assert!(path.unescape_uri().starts_with("blåbær///syltetøy").is_some());
    /// ```
    pub fn starts_with<T: AsRef<str>>(&self, unescaped_prefix: T) -> Option<usize> {
        let mut iter_self = self.clone();
        let mut iter_pat = unescaped_prefix.as_ref().chars();

        loop {
            let b = iter_pat.next();

            if b.is_none() {
                break Some(iter_self.index());
            }

            let a = iter_self.next();

            if iter_self.had_error.is_some() {
                break None;
            }

            if a != b {
                break None;
            }
        }
    }
}

#[cfg(feature = "alloc")]
impl<'a> TryInto<String> for UnescapeUri<'a> {
    type Error = UnescapeError;

    fn try_into(mut self) -> Result<String, Self::Error> {
        let mut buffer = String::with_capacity(self.size_hint().0);

        while let Some(ch) = self.next() {
            if let Some(e) = self.had_error {
                return Err(UnescapeError::new(e, self.index()));
            }

            buffer.push(ch);
        }

        buffer.shrink_to_fit();
        Ok(buffer)
    }
}

/// This is returned, when an error occured while decoding.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnescapeError {
    inner: DecodingError,
    /// The index at which the error occured.
    pub index: usize,
}

impl ::core::error::Error for UnescapeError {}

impl fmt::Display for UnescapeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inner)
    }
}

impl UnescapeError {
    pub(crate) fn new(inner: DecodingError, index: usize) -> Self {
        Self { inner, index }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum DecodingError {
    /// found unescaped ascii control char
    UnescapedAsciiControl(char),
    /// found an unescaped space (' ')
    Space,
    /// there is no char after the `%`
    MissingChar(u8),
    /// the char following a `%` must be an ascii character (in the range from 0-9 and a-f or A-F)
    InvalidEscape(char),
    /// We don't allow escaped ascii control codes for security reasons.
    AsciiControl(char),
    InvalidUtf8 {
        buf: [u8; 4],
        len: u8,
    },
    UnfinishedUtf8 {
        buf: [u8; 4],
        len: u8,
    },
}

impl core::error::Error for DecodingError {}

impl fmt::Display for DecodingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnescapedAsciiControl(c) => {
                write!(f, "unescaped ascii control character `{:?}`", c)
            }
            Self::Space => write!(f, "unescaped space ` `"),
            Self::MissingChar(n) => write!(f, "missing {} char after `%`", n),
            Self::InvalidEscape(c) => write!(
                f,
                "the 2 char after `%` must be a valid hex character `{:?}`",
                c
            ),
            Self::AsciiControl(c) => write!(
                f,
                "ascii control chars are forbidden for security reasons `{:?}`",
                c
            ),
            Self::InvalidUtf8 { buf, len } => write!(f, "invalid utf8 {:?}", &buf[..*len as usize]),
            Self::UnfinishedUtf8 { buf, len } => {
                write!(f, "unfinished utf8 {:?}", &buf[..*len as usize])
            }
        }
    }
}

impl<'a> Iterator for UnescapeUri<'a> {
    type Item = char;

    #[inline]
    fn next(&mut self) -> Option<char> {
        let mut utf8_buf = [0u8; 4];
        let mut utf8_len = 0usize;

        if let Some((c, next_c)) = self.next_c.take() {
            if let Some(x) = next_c {
                self.next_c = Some((x, None));
            }

            return Some(c);
        }

        while let Some(c) = self.iter.next() {
            self.iter_index += 1;

            // Immediately drop all unescaped ascii control codes
            if c.is_ascii_control() {
                self.had_error = Some(DecodingError::UnescapedAsciiControl(c));
                continue;
            }

            match c {
                ' ' => {
                    // Mark unescaped spaces as an error, but pass them along.
                    self.had_error = Some(DecodingError::Space);

                    if utf8_len == 0 {
                        return Some(c);
                    } else {
                        self.next_c = Some((c, None));
                        return Some(REPLACEMENT_CHARACTER);
                    }
                }
                '%' => {
                    self.iter_index += 1;

                    let msn = match self.iter.next() {
                        Some(c) if c.is_ascii_hexdigit() => c as u8,
                        Some(c) => {
                            self.had_error = Some(DecodingError::InvalidEscape(c));

                            if utf8_len == 0 {
                                self.next_c = Some((c, None));
                                return Some('%');
                            } else {
                                self.next_c = Some((c, None));
                                return Some(REPLACEMENT_CHARACTER);
                            }
                        }
                        None => {
                            self.iter_index -= 1;
                            self.had_error = Some(DecodingError::MissingChar(2));
                            self.next_c = Some((c, None));
                            return Some(REPLACEMENT_CHARACTER);
                        }
                    };

                    self.iter_index += 1;

                    let lsn = match self.iter.next() {
                        Some(c) if c.is_ascii_hexdigit() => c as u8,
                        Some(c) => {
                            self.had_error = Some(DecodingError::InvalidEscape(c));

                            if utf8_len == 0 {
                                self.next_c = Some((msn as char, Some(c)));
                                return Some('%');
                            } else {
                                self.next_c = Some((c, None));
                                return Some(REPLACEMENT_CHARACTER);
                            }
                        }
                        None => {
                            self.iter_index -= 1;
                            self.had_error = Some(DecodingError::MissingChar(1));
                            self.next_c = Some((c, None));
                            return Some(REPLACEMENT_CHARACTER);
                        }
                    };

                    let buf = [msn, lsn];

                    // SAFETY: This is safe because we just verified that
                    // these two characters were ASCII hex digits.
                    let buf_str = unsafe { from_utf8_unchecked(&buf) };

                    let decoded = u8::from_str_radix(&buf_str, 16).unwrap();

                    if self.skip_slashes && decoded == b'/' {
                        // Skip decoding escaped slashes.
                        self.next_c = Some((msn as char, Some(lsn as char)));
                        return Some('%');
                    }

                    if (decoded as char).is_ascii_control() {
                        // We don't allow escaped ascii control codes for security reasons.
                        // We signal our error and then convert the code to it's visual
                        // equivalent in the `CONTROL_PICTURES` unicode block at 0x2400.
                        const CONTROL_PICTURES: u32 = 0x2400;
                        let c = core::char::from_u32(decoded as u32 + CONTROL_PICTURES).unwrap();
                        self.had_error = Some(DecodingError::AsciiControl(decoded as char));

                        if utf8_len == 0 {
                            return Some(c);
                        } else {
                            self.next_c = Some((c, None));
                            return Some(REPLACEMENT_CHARACTER);
                        }
                    } else if decoded & 0x80 == 0x80 {
                        // This is a UTF8 byte.
                        utf8_buf[utf8_len] = decoded;
                        utf8_len += 1;

                        if utf8_len >= UTF8_CHAR_WIDTH[utf8_buf[0] as usize] as usize {
                            if let Ok(utf8_str) = from_utf8(&utf8_buf[..utf8_len]) {
                                return Some(utf8_str.chars().next().unwrap());
                            } else {
                                self.had_error = Some(DecodingError::InvalidUtf8 {
                                    buf: utf8_buf,
                                    len: utf8_len as u8,
                                });
                                return Some(REPLACEMENT_CHARACTER);
                            }
                        }
                    } else if utf8_len != 0 {
                        self.had_error = Some(DecodingError::UnfinishedUtf8 {
                            buf: utf8_buf,
                            len: (utf8_len * 2 + utf8_len) as u8,
                        });
                        self.next_c = Some((decoded as char, None));
                        return Some(REPLACEMENT_CHARACTER);
                    } else {
                        return Some(decoded as char);
                    }
                }
                c => {
                    if utf8_len != 0 {
                        self.next_c = Some((c, None));
                        self.had_error = Some(DecodingError::UnfinishedUtf8 {
                            buf: utf8_buf,
                            len: (utf8_len * 2 + utf8_len) as u8,
                        });
                        return Some(REPLACEMENT_CHARACTER);
                    }

                    return Some(c);
                }
            }
        }

        None
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.iter.size_hint().0;
        (n, Some(n))
    }
}