summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-http3/src/request_target.rs
blob: 28bc22ac2d7304935f4135b9a4c30c29c4e18c16 (plain)
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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(clippy::module_name_repetitions)]

use std::fmt::{Debug, Formatter};

use url::{ParseError, Url};

pub trait RequestTarget: Debug {
    fn scheme(&self) -> &str;
    fn authority(&self) -> &str;
    fn path(&self) -> &str;
}

pub struct RefRequestTarget<'s, 'a, 'p> {
    scheme: &'s str,
    authority: &'a str,
    path: &'p str,
}

impl RequestTarget for RefRequestTarget<'_, '_, '_> {
    fn scheme(&self) -> &str {
        self.scheme
    }

    fn authority(&self) -> &str {
        self.authority
    }

    fn path(&self) -> &str {
        self.path
    }
}

impl<'s, 'a, 'p> RefRequestTarget<'s, 'a, 'p> {
    #[must_use]
    pub fn new(scheme: &'s str, authority: &'a str, path: &'p str) -> Self {
        Self {
            scheme,
            authority,
            path,
        }
    }
}

impl Debug for RefRequestTarget<'_, '_, '_> {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        write!(f, "{}://{}{}", self.scheme, self.authority, self.path)
    }
}

/// `AsRequestTarget` is a trait that produces a `RequestTarget` that
/// refers to the identified object.
pub trait AsRequestTarget<'x> {
    type Target: RequestTarget;
    type Error;
    /// Produce a `RequestTarget` that refers to `self`.
    ///
    /// # Errors
    ///
    /// This method can generate an error of type `Self::Error`
    /// if the conversion is unsuccessful.
    fn as_request_target(&'x self) -> Result<Self::Target, Self::Error>;
}

impl<'x, S, A, P> AsRequestTarget<'x> for (S, A, P)
where
    S: AsRef<str> + 'x,
    A: AsRef<str> + 'x,
    P: AsRef<str> + 'x,
{
    type Target = RefRequestTarget<'x, 'x, 'x>;
    type Error = ();
    fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> {
        Ok(RefRequestTarget::new(
            self.0.as_ref(),
            self.1.as_ref(),
            self.2.as_ref(),
        ))
    }
}

impl<'x> AsRequestTarget<'x> for Url {
    type Target = RefRequestTarget<'x, 'x, 'x>;
    type Error = ();
    fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> {
        Ok(RefRequestTarget::new(
            self.scheme(),
            self.host_str().unwrap_or(""),
            self.path(),
        ))
    }
}

pub struct UrlRequestTarget {
    url: Url,
}

impl RequestTarget for UrlRequestTarget {
    fn scheme(&self) -> &str {
        self.url.scheme()
    }

    fn authority(&self) -> &str {
        self.url.host_str().unwrap_or("")
    }

    fn path(&self) -> &str {
        self.url.path()
    }
}

impl Debug for UrlRequestTarget {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        self.url.fmt(f)
    }
}

impl<'x> AsRequestTarget<'x> for str {
    type Target = UrlRequestTarget;
    type Error = ParseError;
    fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> {
        let url = Url::parse(self)?;
        Ok(UrlRequestTarget { url })
    }
}