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
//! These APIs are all pretty rough for now - only use them after verifying their safety

use uriparser_sys::{self, UriPathSegmentA, UriTextRangeA, UriUriA, uriToStringA, uriToStringCharsRequiredA};
use core::{fmt, ptr, cmp};
fn bool_to_uri(b: bool) -> uriparser_sys::UriBool {
    if b {
        uriparser_sys::URI_TRUE as i32
    } else {
        uriparser_sys::URI_FALSE as i32
    }
}
pub fn escape<'o>(text: &[u8], out: &'o mut [u8], replace_space: bool, normalize_line_breaks: bool) -> &'o mut [u8] {
    assert!(out.len() >= text.len() * if normalize_line_breaks {
        6
    } else {
        3
    } + 1);
    let end = unsafe {
        uriparser_sys::uriEscapeExA(
            text.as_ptr() as *const _, 
            text.as_ptr().add(text.len()) as *const _,
            out.as_mut_ptr() as *mut _, 
            bool_to_uri(replace_space), 
            bool_to_uri(normalize_line_breaks)
        )
    };
    let new_len = end as usize - out.as_ptr() as usize;
    &mut out[..new_len]
}
pub enum LineBreak {
    Cr,
    Lf,
    Crlf,
}
fn hex(v: u8) -> Option<u8> {
    match v {
        b'a'..=b'f' => Some(v - b'a' + 10),
        b'A'..=b'F' => Some(v - b'A' + 10),
        b'0'..=b'9' => Some(v - b'0'),
        _ => None
    }
}
pub fn unescape(text: &mut [u8], replace_plus: bool, line_breaks: LineBreak) -> &mut [u8] {
    let last_byte = match text {
        [.., p, a, b] if *p == b'%' => {
            if let Some(n) = hex(*a).and_then(|a| hex(*b).map(|b| a * 16 + b)) {
                *p = 0;
                n
            } else {
                core::mem::replace(b, 0)
            }
        }
        [.., l] => core::mem::replace(l, 0),
        [] => return text,
    };
    let last = unsafe {
        uriparser_sys::uriUnescapeInPlaceExA(text.as_mut_ptr() as *mut _, bool_to_uri(replace_plus), match line_breaks {
            LineBreak::Cr => uriparser_sys::UriBreakConversionEnum_URI_BR_TO_CR,
            LineBreak::Lf => uriparser_sys::UriBreakConversionEnum_URI_BR_TO_LF,
            LineBreak::Crlf => uriparser_sys::UriBreakConversionEnum_URI_BR_TO_CRLF,
        })
    };
    unsafe {
        *(last as *const u8 as *mut u8) = last_byte;
    }
    let new_len = last as usize - text.as_ptr() as usize;
    &mut text[..=new_len]
}
unsafe fn text_range_as_bytes<'a>(range: &UriTextRangeA) -> Option<&'a [u8]> {
    if range.first.is_null() {
        None
    } else {
        Some(core::slice::from_raw_parts(
            range.first as *const _,
            range.afterLast as usize - range.first as usize,
        ))
    }    
}
#[derive(Debug)]
pub struct Uri<'a> {
    raw: UriUriA,
    marker: core::marker::PhantomData<&'a [u8]>,
}

#[derive(Debug)]
pub struct ParseError {
    errpos: usize,
}

impl ParseError {
    pub fn pos(&self) -> usize {
        self.errpos
    }
    fn from_code(code: u32, errpos: usize) -> Self {
        match code {
            uriparser_sys::URI_ERROR_SYNTAX => Self { errpos },
            uriparser_sys::URI_ERROR_NULL => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_MALLOC => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_OUTPUT_TOO_LARGE => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_NOT_IMPLEMENTED => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_RANGE_INVALID => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_MEMORY_MANAGER_INCOMPLETE => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_TOSTRING_TOO_LONG => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_ADDBASE_REL_BASE => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_REMOVEBASE_REL_BASE => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_REMOVEBASE_REL_SOURCE => panic!("unexpected error"),
            uriparser_sys::URI_ERROR_MEMORY_MANAGER_FAULTY => panic!("unexpected error"),
            err => unimplemented!("unknown error {}", err),
        }
    }
}

impl<'a> Uri<'a> {
    pub fn parse(uri: &'a [u8]) -> Result<Self, ParseError> {
        let mut raw = Default::default();
        let mut errpos = ptr::null();
        match unsafe { uriparser_sys::uriParseSingleUriExA(&mut raw, uri.as_ptr() as *const _, uri.as_ptr().add(uri.len()) as *const _, &mut errpos) } as u32
        {
            uriparser_sys::URI_SUCCESS => Ok(Self {
                raw,
                marker: core::marker::PhantomData,
            }),
            err => Err(ParseError::from_code(err, errpos as usize - uri.as_ptr() as usize)),
        }
    }
    pub fn resolve(&self, reference: &Uri<'_>, strict: bool) -> Result<Uri<'static>, ()> {
        let mut out = Default::default();
        match unsafe {
            uriparser_sys::uriAddBaseUriExA(&mut out, &reference.raw, &self.raw, if strict {
                uriparser_sys::UriResolutionOptionsEnum_URI_RESOLVE_STRICTLY
            } else {
                uriparser_sys::UriResolutionOptionsEnum_URI_RESOLVE_IDENTICAL_SCHEME_COMPAT
            })
        } as u32 {
            uriparser_sys::URI_SUCCESS => Ok(Uri {
                raw: out,
                marker: core::marker::PhantomData,
            }),
            err => Err(())
        }
    }
    pub fn as_relative(&self, base: &Uri<'_>, from_domain_root: bool) -> Result<Uri<'static>, ()> {
        let mut out = Default::default();
        match unsafe {
            uriparser_sys::uriRemoveBaseUriA(&mut out, &self.raw, &base.raw, bool_to_uri(from_domain_root))
        } as u32 {
            uriparser_sys::URI_SUCCESS => Ok(Uri {
                raw: out,
                marker: core::marker::PhantomData,
            }),
            err => Err(())
        }
    }
    pub fn into_owned(mut self) -> Uri<'static> {
        unsafe {
            uriparser_sys::uriMakeOwnerA(&mut self.raw);
        }
        Uri {
            raw: self.raw,
            marker: core::marker::PhantomData,
        }
    }
    pub fn scheme(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.scheme) }.map(|s| core::str::from_utf8(s).unwrap())
    }
    pub fn userinfo(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.userInfo) }.map(|s| core::str::from_utf8(s).unwrap())
    }
    pub fn host(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.hostText) }.map(|s| core::str::from_utf8(s).unwrap())
    }
    pub fn port(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.portText) }.map(|s| core::str::from_utf8(s).unwrap())
    }
    pub fn path(&self) -> Path<'_> {
        let head = core::ptr::NonNull::new(self.raw.pathHead).map(|ptr| unsafe { &*ptr.as_ptr() });
        let is_absolute = self.raw.absolutePath == uriparser_sys::URI_TRUE as i32 || (self.host().is_some() && head.is_some());
        Path {
            is_absolute,
            head
        }
    }

    pub fn query(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.query) }.map(|s| core::str::from_utf8(s).unwrap())
    }
    pub fn fragment(&self) -> Option<&str> {
        unsafe { text_range_as_bytes(&self.raw.fragment) }.map(|s| core::str::from_utf8(s).unwrap())
    }
}
impl PartialEq<Uri<'_>> for Uri<'_> {
    fn eq(&self, other: &Uri<'_>) -> bool {
        (unsafe {
            uriparser_sys::uriEqualsUriA(&self.raw, &other.raw)
        }) as u32 == uriparser_sys::URI_TRUE
    }
}
impl ToString for Uri<'_> {
    fn to_string(&self) -> String {
        let mut capacity = 0;
        assert_eq!(unsafe {
            uriToStringCharsRequiredA(&self.raw, &mut capacity)
        } as u32, uriparser_sys::URI_SUCCESS);
        let mut s = String::with_capacity(capacity as usize);
        let mut written = 0;
        assert_eq!(unsafe {
            uriToStringA(s.as_bytes_mut().as_mut_ptr() as *mut _, &self.raw, capacity, &mut written)
        } as u32, uriparser_sys::URI_SUCCESS);
        unsafe {
            s.as_mut_vec().set_len(written as usize);
        }
        s
    }
}
pub struct Path<'a> {
    is_absolute: bool,
    head: Option<&'a UriPathSegmentA>,
}
use fmt::Write;
impl fmt::Debug for Path<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_char('"')?;
        if self.is_absolute {
            f.write_char('/')?;
        }
        let mut segments = self.segments();
        if let Some(segment) = segments.next() {
            write!(f, "{}", segment.escape_debug())?;
            for segment in segments {
                f.write_char('/')?;
                write!(f, "{}", segment.escape_debug())?;
            }
        }
        Ok(())
    }
}
impl cmp::PartialEq<[u8]> for Path<'_> {
    fn eq(&self, mut other: &[u8]) -> bool {
        macro_rules! unwrap {
            ($e:expr) => {match $e {
                Some(v) => v,
                None => return false
            }};
        }
        if self.is_absolute {
            other = unwrap!(other.strip_prefix(b"/"));
        }
        let mut segments = self.segments();
        if let Some(segment) = segments.next() {
            other = unwrap!(other.strip_prefix(segment.as_bytes()));
            for segment in segments {
                other = unwrap!(other.strip_prefix(b"/"));
                other = unwrap!(other.strip_prefix(segment.as_bytes()));
            }
        }
        true
    }
}
impl cmp::PartialEq<str> for Path<'_> {
    fn eq(&self, other: &str) -> bool {
        self.eq(other.as_bytes())
    }
}
impl cmp::PartialEq for Path<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.is_absolute.eq(&other.is_absolute) && self.segments().eq(other.segments())
    }
}
impl fmt::Display for Path<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_absolute {
            f.write_char('/')?;
        }
        let mut segments = self.segments();
        if let Some(segment) = segments.next() {
            f.write_str(segment)?;
            for segment in segments {
                f.write_char('/')?;
                f.write_str(segment)?;
            }
        }
        Ok(())
    }
}
impl<'a> Path<'a> {
    pub fn segments(&self) -> impl Iterator<Item = &'a str> + 'a {
        let mut next = self.head;
        core::iter::from_fn(move || {
            next.map(|segment| {
                next = core::ptr::NonNull::new(segment.next).map(|ptr| unsafe { &*ptr.as_ptr() });
                core::str::from_utf8(unsafe { text_range_as_bytes(&segment.text) }.unwrap()).unwrap()
            })
        })
    }
}
impl Uri<'_> {
    fn into_raw(self) -> uriparser_sys::UriUriA {
        self.raw
    }
    unsafe fn from_raw(raw: uriparser_sys::UriUriA) -> Self {
        Self {
            raw,
            marker: core::marker::PhantomData,
        }
    }
    unsafe fn as_raw(&self) -> &uriparser_sys::UriUriA {
        &self.raw
    }
    unsafe fn as_mut_raw(&mut self) -> &mut uriparser_sys::UriUriA {
        &mut self.raw
    }
}

impl Drop for Uri<'_> {
    fn drop(&mut self) {
        unsafe {
            uriparser_sys::uriFreeUriMembersA(&mut self.raw);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Uri;

    #[test]
    fn parsing() {
        let uri = Uri::parse(b"https://www.youtube.com/watch?v=HOJ1NVtlnyQ").unwrap();
        assert_eq!(Some("v=HOJ1NVtlnyQ"), uri.query());
        Uri::parse(b"foobar://abc.com/ooh##").expect_err("no hashes in fragment");
    }
}