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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::borrow::Cow;

use crate::ext::IntoOwned;
use crate::parse::{Extent, IndexedStr};
use crate::uri::{Authority, Path, Query, Data, Error, as_utf8_unchecked, fmt};

/// A URI with a scheme, authority, path, and query.
///
/// # Structure
///
/// The following diagram illustrates the syntactic structure of an absolute
/// URI with all optional parts:
///
/// ```text
///  http://user:pass@domain.com:4444/foo/bar?some=query
///  |--|  |------------------------||------| |--------|
/// scheme          authority          path      query
/// ```
///
/// Only the scheme part of the URI is required.
///
/// # Normalization
///
/// Rocket prefers _normalized_ absolute URIs, an absolute URI with the
/// following properties:
///
///   * If there is an authority, the path is empty or absolute.
///   * The path and query, if any, are normalized with no empty segments except
///     optionally for one trailing slash.
///
/// The [`Absolute::is_normalized()`] method checks for normalization while
/// [`Absolute::into_normalized()`] normalizes any absolute URI.
///
/// As an example, the following URIs are all valid, normalized URIs:
///
/// ```rust
/// # extern crate rocket;
/// # use rocket::http::uri::Absolute;
/// # let valid_uris = [
/// "http://rocket.rs",
/// "http://rocket.rs/",
/// "ftp:/a/b/",
/// "ftp:/a/b/?",
/// "scheme:/foo/bar",
/// "scheme:/foo/bar/",
/// "scheme:/foo/bar/?",
/// "scheme:/foo/bar/?abc",
/// # ];
/// # for uri in &valid_uris {
/// #     let uri = Absolute::parse(uri).unwrap();
/// #     assert!(uri.is_normalized(), "{} non-normal?", uri);
/// # }
/// ```
///
/// By contrast, the following are valid but non-normal URIs:
///
/// ```rust
/// # extern crate rocket;
/// # use rocket::http::uri::Absolute;
/// # let invalid = [
/// "ftp:/a//c//d",         // two empty segments
/// "ftp:/?foo&",           // trailing empty query segment
/// "ftp:/?fooa&&b",        // empty query segment
/// # ];
/// # for uri in &invalid {
/// #   assert!(!Absolute::parse(uri).unwrap().is_normalized());
/// # }
/// ```
///
/// # (De)serialization
///
/// `Absolute` is both `Serialize` and `Deserialize`:
///
/// ```rust
/// # #[cfg(feature = "serde")] mod serde {
/// # use serde_ as serde;
/// use serde::{Serialize, Deserialize};
/// use rocket::http::uri::Absolute;
///
/// #[derive(Deserialize, Serialize)]
/// # #[serde(crate = "serde_")]
/// struct UriOwned {
///     uri: Absolute<'static>,
/// }
///
/// #[derive(Deserialize, Serialize)]
/// # #[serde(crate = "serde_")]
/// struct UriBorrowed<'a> {
///     uri: Absolute<'a>,
/// }
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Absolute<'a> {
    pub(crate) source: Option<Cow<'a, str>>,
    pub(crate) scheme: IndexedStr<'a>,
    pub(crate) authority: Option<Authority<'a>>,
    pub(crate) path: Data<'a, fmt::Path>,
    pub(crate) query: Option<Data<'a, fmt::Query>>,
}

impl<'a> Absolute<'a> {
    /// Parses the string `string` into an `Absolute`. Parsing will never
    /// allocate. Returns an `Error` if `string` is not a valid absolute URI.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// use rocket::http::uri::Absolute;
    ///
    /// // Parse a valid authority URI.
    /// let uri = Absolute::parse("https://rocket.rs").expect("valid URI");
    /// assert_eq!(uri.scheme(), "https");
    /// assert_eq!(uri.authority().unwrap().host(), "rocket.rs");
    /// assert_eq!(uri.path(), "");
    /// assert!(uri.query().is_none());
    ///
    /// // Prefer to use `uri!()` when the input is statically known:
    /// let uri = uri!("https://rocket.rs");
    /// assert_eq!(uri.scheme(), "https");
    /// assert_eq!(uri.authority().unwrap().host(), "rocket.rs");
    /// assert_eq!(uri.path(), "");
    /// assert!(uri.query().is_none());
    /// ```
    pub fn parse(string: &'a str) -> Result<Absolute<'a>, Error<'a>> {
        crate::parse::uri::absolute_from_str(string)
    }

    /// Parses the string `string` into an `Absolute`. Allocates minimally on
    /// success and error.
    ///
    /// This method should be used instead of [`Absolute::parse()`] when the
    /// source URI is already a `String`. Returns an `Error` if `string` is not
    /// a valid absolute URI.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate rocket;
    /// use rocket::http::uri::Absolute;
    ///
    /// let source = format!("https://rocket.rs/foo/{}/three", 2);
    /// let uri = Absolute::parse_owned(source).expect("valid URI");
    /// assert_eq!(uri.authority().unwrap().host(), "rocket.rs");
    /// assert_eq!(uri.path(), "/foo/2/three");
    /// assert!(uri.query().is_none());
    /// ```
    // TODO: Avoid all allocations.
    pub fn parse_owned(string: String) -> Result<Absolute<'static>, Error<'static>> {
        let absolute = Absolute::parse(&string).map_err(|e| e.into_owned())?;
        debug_assert!(absolute.source.is_some(), "Absolute parsed w/o source");

        let absolute = Absolute {
            scheme: absolute.scheme.into_owned(),
            authority: absolute.authority.into_owned(),
            query: absolute.query.into_owned(),
            path: absolute.path.into_owned(),
            source: Some(Cow::Owned(string)),
        };

        Ok(absolute)
    }

    /// Returns the scheme part of the absolute URI.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let uri = uri!("ftp://127.0.0.1");
    /// assert_eq!(uri.scheme(), "ftp");
    /// ```
    #[inline(always)]
    pub fn scheme(&self) -> &str {
        self.scheme.from_cow_source(&self.source)
    }

    /// Returns the authority part of the absolute URI, if there is one.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let uri = uri!("https://rocket.rs:80");
    /// assert_eq!(uri.scheme(), "https");
    /// let authority = uri.authority().unwrap();
    /// assert_eq!(authority.host(), "rocket.rs");
    /// assert_eq!(authority.port(), Some(80));
    ///
    /// let uri = uri!("file:/web/home");
    /// assert_eq!(uri.authority(), None);
    /// ```
    #[inline(always)]
    pub fn authority(&self) -> Option<&Authority<'a>> {
        self.authority.as_ref()
    }

    /// Returns the path part. May be empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let uri = uri!("ftp://rocket.rs/foo/bar");
    /// assert_eq!(uri.path(), "/foo/bar");
    ///
    /// let uri = uri!("ftp://rocket.rs");
    /// assert!(uri.path().is_empty());
    /// ```
    #[inline(always)]
    pub fn path(&self) -> Path<'_> {
        Path { source: &self.source, data: &self.path }
    }

    /// Returns the query part with the leading `?`. May be empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let uri = uri!("ftp://rocket.rs/foo?bar");
    /// assert_eq!(uri.query().unwrap(), "bar");
    ///
    /// let uri = uri!("ftp://rocket.rs");
    /// assert!(uri.query().is_none());
    /// ```
    #[inline(always)]
    pub fn query(&self) -> Option<Query<'_>> {
        self.query.as_ref().map(|data| Query { source: &self.source, data })
    }

    /// Removes the query part of this URI, if there is any.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let mut uri = uri!("ftp://rocket.rs/foo?bar");
    /// assert_eq!(uri.query().unwrap(), "bar");
    ///
    /// uri.clear_query();
    /// assert!(uri.query().is_none());
    /// ```
    #[inline(always)]
    pub fn clear_query(&mut self) {
        self.set_query(None);
    }

    /// Returns `true` if `self` is normalized. Otherwise, returns `false`.
    ///
    /// See [Normalization](#normalization) for more information on what it
    /// means for an absolute URI to be normalized. Note that `uri!()` always
    /// returns a normalized version of its static input.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// use rocket::http::uri::Absolute;
    ///
    /// assert!(uri!("http://rocket.rs").is_normalized());
    /// assert!(uri!("http://rocket.rs///foo////bar").is_normalized());
    ///
    /// assert!(Absolute::parse("http:/").unwrap().is_normalized());
    /// assert!(Absolute::parse("http://").unwrap().is_normalized());
    /// assert!(Absolute::parse("http://foo.rs/foo/bar").unwrap().is_normalized());
    /// assert!(Absolute::parse("foo:bar").unwrap().is_normalized());
    /// assert!(Absolute::parse("git://rocket.rs/").unwrap().is_normalized());
    ///
    /// assert!(!Absolute::parse("http:/foo//bar").unwrap().is_normalized());
    /// assert!(!Absolute::parse("foo:bar?baz&&bop").unwrap().is_normalized());
    /// ```
    pub fn is_normalized(&self) -> bool {
        let normalized_query = self.query().map_or(true, |q| q.is_normalized());
        if self.authority().is_some() && !self.path().is_empty() {
            self.path().is_normalized(true) && normalized_query
        } else {
            self.path().is_normalized(false) && normalized_query
        }
    }

    /// Normalizes `self` in-place. Does nothing if `self` is already
    /// normalized.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rocket::http::uri::Absolute;
    ///
    /// let mut uri = Absolute::parse("git://rocket.rs").unwrap();
    /// assert!(uri.is_normalized());
    ///
    /// let mut uri = Absolute::parse("git://rocket.rs/").unwrap();
    /// assert!(uri.is_normalized());
    ///
    /// let mut uri = Absolute::parse("http:/foo//bar").unwrap();
    /// assert!(!uri.is_normalized());
    /// uri.normalize();
    /// assert!(uri.is_normalized());
    ///
    /// let mut uri = Absolute::parse("foo:bar?baz&&bop").unwrap();
    /// assert!(!uri.is_normalized());
    /// uri.normalize();
    /// assert!(uri.is_normalized());
    /// ```
    pub fn normalize(&mut self) {
        if self.authority().is_some() && !self.path().is_empty() {
            if !self.path().is_normalized(true) {
                self.path = self.path().to_normalized(true, true);
            }
        } else {
            if !self.path().is_normalized(false) {
                self.path = self.path().to_normalized(false, true);
            }
        }

        if let Some(query) = self.query() {
            if !query.is_normalized() {
                self.query = Some(query.to_normalized());
            }
        }
    }

    /// Normalizes `self`. This is a no-op if `self` is already normalized.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rocket::http::uri::Absolute;
    ///
    /// let mut uri = Absolute::parse("git://rocket.rs/").unwrap();
    /// assert!(uri.is_normalized());
    ///
    /// let mut uri = Absolute::parse("http:/foo//bar").unwrap();
    /// assert!(!uri.is_normalized());
    /// assert!(uri.into_normalized().is_normalized());
    ///
    /// let mut uri = Absolute::parse("foo:bar?baz&&bop").unwrap();
    /// assert!(!uri.is_normalized());
    /// assert!(uri.into_normalized().is_normalized());
    /// ```
    pub fn into_normalized(mut self) -> Self {
        self.normalize();
        self
    }

    /// Sets the authority in `self` to `authority`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let mut uri = uri!("https://rocket.rs:80");
    /// let authority = uri.authority().unwrap();
    /// assert_eq!(authority.host(), "rocket.rs");
    /// assert_eq!(authority.port(), Some(80));
    ///
    /// let new_authority = uri!("rocket.rs:443");
    /// uri.set_authority(new_authority);
    /// let authority = uri.authority().unwrap();
    /// assert_eq!(authority.host(), "rocket.rs");
    /// assert_eq!(authority.port(), Some(443));
    /// ```
    #[inline(always)]
    pub fn set_authority(&mut self, authority: Authority<'a>) {
        self.authority = Some(authority);
    }

    /// Sets the authority in `self` to `authority` and returns `self`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[macro_use] extern crate rocket;
    /// let uri = uri!("https://rocket.rs:80");
    /// let authority = uri.authority().unwrap();
    /// assert_eq!(authority.host(), "rocket.rs");
    /// assert_eq!(authority.port(), Some(80));
    ///
    /// let new_authority = uri!("rocket.rs");
    /// let uri = uri.with_authority(new_authority);
    /// let authority = uri.authority().unwrap();
    /// assert_eq!(authority.host(), "rocket.rs");
    /// assert_eq!(authority.port(), None);
    /// ```
    #[inline(always)]
    pub fn with_authority(mut self, authority: Authority<'a>) -> Self {
        self.set_authority(authority);
        self
    }
}

/// PRIVATE API.
#[doc(hidden)]
impl<'a> Absolute<'a> {
    /// PRIVATE. Used by parser.
    ///
    /// SAFETY: `source` must be valid UTF-8.
    /// CORRECTNESS: `scheme` must be non-empty.
    #[inline]
    pub(crate) unsafe fn raw(
        source: Cow<'a, [u8]>,
        scheme: Extent<&'a [u8]>,
        authority: Option<Authority<'a>>,
        path: Extent<&'a [u8]>,
        query: Option<Extent<&'a [u8]>>,
    ) -> Absolute<'a> {
        Absolute {
            source: Some(as_utf8_unchecked(source)),
            scheme: scheme.into(),
            authority,
            path: Data::raw(path),
            query: query.map(Data::raw)
        }
    }

    /// PRIVATE. Used by tests.
    #[cfg(test)]
    pub fn new(
        scheme: &'a str,
        authority: impl Into<Option<Authority<'a>>>,
        path: &'a str,
        query: impl Into<Option<&'a str>>,
    ) -> Absolute<'a> {
        assert!(!scheme.is_empty());
        Absolute::const_new(scheme, authority.into(), path, query.into())
    }

    /// PRIVATE. Used by codegen and `Host`.
    #[doc(hidden)]
    pub const fn const_new(
        scheme: &'a str,
        authority: Option<Authority<'a>>,
        path: &'a str,
        query: Option<&'a str>,
    ) -> Absolute<'a> {
        Absolute {
            source: None,
            scheme: IndexedStr::Concrete(Cow::Borrowed(scheme)),
            authority,
            path: Data {
                value: IndexedStr::Concrete(Cow::Borrowed(path)),
                decoded_segments: state::InitCell::new(),
            },
            query: match query {
                Some(query) => Some(Data {
                    value: IndexedStr::Concrete(Cow::Borrowed(query)),
                    decoded_segments: state::InitCell::new(),
                }),
                None => None,
            },
        }
    }

    // TODO: Have a way to get a validated `path` to do this. See `Path`?
    pub(crate) fn set_path<P>(&mut self, path: P)
        where P: Into<Cow<'a, str>>
    {
        self.path = Data::new(path.into());
    }

    // TODO: Have a way to get a validated `query` to do this. See `Query`?
    pub(crate) fn set_query<Q: Into<Option<Cow<'a, str>>>>(&mut self, query: Q) {
        self.query = query.into().map(Data::new);
    }
}

impl_serde!(Absolute<'a>, "an absolute-form URI");

impl_traits!(Absolute, scheme, authority, path, query);

impl std::fmt::Display for Absolute<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:", self.scheme())?;
        if let Some(authority) = self.authority() {
            write!(f, "//{}", authority)?;
        }

        write!(f, "{}", self.path())?;
        if let Some(query) = self.query() {
            write!(f, "?{}", query)?;
        }

        Ok(())
    }
}