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
pub mod client;
pub mod server;

pub mod status {
    use core::ops::Range;

    pub const INFO: Range<u16> = 100..200;
    pub const OK: Range<u16> = 200..300;
    pub const REDIRECT: Range<u16> = 300..400;
    pub const CLIENT_ERROR: Range<u16> = 400..500;
    pub const SERVER_ERROR: Range<u16> = 500..600;
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "std", derive(Hash))]
pub enum Method {
    Delete,
    Get,
    Head,
    Post,
    Put,
    Connect,
    Options,
    Trace,
    Copy,
    Lock,
    MkCol,
    Move,
    Propfind,
    Proppatch,
    Search,
    Unlock,
    Bind,
    Rebind,
    Unbind,
    Acl,
    Report,
    MkActivity,
    Checkout,
    Merge,
    MSearch,
    Notify,
    Subscribe,
    Unsubscribe,
    Patch,
    Purge,
    MkCalendar,
    Link,
    Unlink,
}

pub trait Headers {
    fn header(&self, name: &str) -> Option<&'_ str>;

    fn content_type(&self) -> Option<&'_ str> {
        self.header("Content-Type")
    }

    fn content_len(&self) -> Option<u64> {
        self.header("Content-Length")
            .and_then(|v| v.parse::<u64>().ok())
    }

    fn content_encoding(&self) -> Option<&'_ str> {
        self.header("Content-Encoding")
    }

    fn transfer_encoding(&self) -> Option<&'_ str> {
        self.header("Transfer-Encoding")
    }

    fn host(&self) -> Option<&'_ str> {
        self.header("Host")
    }

    fn connection(&self) -> Option<&'_ str> {
        self.header("Connection")
    }

    fn cache_control(&self) -> Option<&'_ str> {
        self.header("Cache-Control")
    }

    fn upgrade(&self) -> Option<&'_ str> {
        self.header("Upgrade")
    }
}

impl<H> Headers for &H
where
    H: Headers,
{
    fn header(&self, name: &str) -> Option<&'_ str> {
        (*self).header(name)
    }
}

impl<H> Headers for &mut H
where
    H: Headers,
{
    fn header(&self, name: &str) -> Option<&'_ str> {
        (**self).header(name)
    }
}

pub trait Status {
    fn status(&self) -> u16;

    fn status_message(&self) -> Option<&'_ str>;
}

impl<S> Status for &S
where
    S: Status,
{
    fn status(&self) -> u16 {
        (*self).status()
    }

    fn status_message(&self) -> Option<&'_ str> {
        (*self).status_message()
    }
}

impl<S> Status for &mut S
where
    S: Status,
{
    fn status(&self) -> u16 {
        (**self).status()
    }

    fn status_message(&self) -> Option<&'_ str> {
        (**self).status_message()
    }
}

pub trait Query {
    fn uri(&self) -> &'_ str;

    fn method(&self) -> Method;
}

impl<Q> Query for &Q
where
    Q: Query,
{
    fn uri(&self) -> &'_ str {
        (*self).uri()
    }

    fn method(&self) -> Method {
        (*self).method()
    }
}

impl<Q> Query for &mut Q
where
    Q: Query,
{
    fn uri(&self) -> &'_ str {
        (**self).uri()
    }

    fn method(&self) -> Method {
        (**self).method()
    }
}

pub mod headers {
    pub type ContentLenParseBuf = heapless::String<20>;

    pub fn content_type(ctype: &str) -> (&str, &str) {
        ("Content-Type", ctype)
    }

    pub fn content_len(len: u64, buf: &mut ContentLenParseBuf) -> (&str, &str) {
        *buf = ContentLenParseBuf::try_from(len).unwrap();

        ("Content-Length", buf.as_str())
    }

    pub fn content_encoding(encoding: &str) -> (&str, &str) {
        ("Content-Encoding", encoding)
    }

    pub fn transfer_encoding(encoding: &str) -> (&str, &str) {
        ("Transfer-Encoding", encoding)
    }

    pub fn transfer_encoding_chunked<'a>() -> (&'a str, &'a str) {
        transfer_encoding("Chunked")
    }

    pub fn host(host: &str) -> (&str, &str) {
        ("Host", host)
    }

    pub fn connection(connection: &str) -> (&str, &str) {
        ("Connection", connection)
    }

    pub fn connection_upgrade<'a>() -> (&'a str, &'a str) {
        connection("Upgrade")
    }

    pub fn connection_keepalive<'a>() -> (&'a str, &'a str) {
        connection("Keep-Alive")
    }

    pub fn connection_close<'a>() -> (&'a str, &'a str) {
        connection("Close")
    }

    pub fn cache_control(cache: &str) -> (&str, &str) {
        ("Cache-Control", cache)
    }

    pub fn cache_control_no_cache<'a>() -> (&'a str, &'a str) {
        cache_control("No-Cache")
    }

    pub fn location(location: &str) -> (&str, &str) {
        ("Location", location)
    }

    pub fn upgrade(upgrade: &str) -> (&str, &str) {
        ("Upgrade", upgrade)
    }

    pub fn upgrade_websocket<'a>() -> (&'a str, &'a str) {
        upgrade("websocket")
    }
}

pub mod asynch {
    pub use super::*;
}