htwrap 0.15.0

Framework-less Hyper client and server
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
463
464
465
466
467
468
469
470
471
472
473
474
use {
    flowcontrol::shed,
    format_bytes::format_bytes,
    http::{
        HeaderMap,
        HeaderValue,
        Uri,
    },
    loga::{
        ea,
        DebugDisplay,
        ResultContext,
    },
    std::{
        borrow::Cow,
        net::{
            IpAddr,
            SocketAddr,
        },
        str::FromStr,
    },
};

pub const FORWARDED_FOR: &str = "X-Forwarded-For";
pub const FORWARDED_PROTO: &str = "X-Forwarded-Proto";
pub const FORWARDED_HOST: &str = "X-Forwarded-Host";
pub const FORWARDED_PATH: &str = "X-Forwarded-Path";

#[derive(PartialEq, Eq, Debug)]
pub struct ForwardedHop<'a> {
    pub for_: Option<(IpAddr, Option<u16>)>,
    pub proto: Option<Cow<'a, [u8]>>,
    pub host: Option<Cow<'a, [u8]>>,
    pub path: Option<Cow<'a, [u8]>>,
}

impl<'a> ForwardedHop<'a> {
    pub fn to_owned(&self) -> ForwardedHop<'static> {
        return ForwardedHop {
            for_: self.for_.clone(),
            proto: self.proto.as_ref().map(|x| Cow::Owned(x.to_vec())),
            host: self.host.as_ref().map(|x| Cow::Owned(x.to_vec())),
            path: self.path.as_ref().map(|x| Cow::Owned(x.to_vec())),
        };
    }

    pub fn uri(&self) -> Result<Uri, loga::Error> {
        let proto = self.proto.as_ref().context("Missing forwarded proto")?;
        let host = self.host.as_ref().clone().context("Missing forwarded host")?;
        let opt_path = Cow::Borrowed(b"" as &[u8]);
        let path = self.path.as_ref().unwrap_or_else(|| &opt_path);
        let uri_str =
            format!(
                "{}://{}{}",
                String::from_utf8(proto.to_vec())
                    .map_err(loga::err)
                    .context_with("Forwarded proto is invalid UTF-8", ea!(proto = String::from_utf8_lossy(proto)))?,
                String::from_utf8(host.to_vec())
                    .map_err(loga::err)
                    .context_with("Forwarded host is invalid UTF-8", ea!(host = String::from_utf8_lossy(host)))?,
                String::from_utf8(path.to_vec())
                    .map_err(loga::err)
                    .context_with("Forwarded path is invalid UTF-8", ea!(path = String::from_utf8_lossy(path)))?
            );
        return Ok(
            Uri::from_str(&uri_str).map_err(loga::err).context_with("Assembled URL is invalid", ea!(url = uri_str))?,
        );
    }
}

pub type Forwarded<'a> = Vec<ForwardedHop<'a>>;

// Parse `X-Forwarded-For`
pub fn parse_forwarded_for(v: &HeaderValue) -> Result<Vec<(IpAddr, Option<u16>)>, loga::Error> {
    let v =
        String::from_utf8(v.as_bytes().to_vec())
            .map_err(loga::err)
            .context_with(format!("Invalid UTF-8 in {} header", FORWARDED_FOR), ea!(value = v.dbg_str()))?;
    let mut out = vec![];
    for addr in v.split(',') {
        let addr =
            IpAddr::from_str(
                addr,
            ).context_with(format!("Failed to parse IP address in {} header", FORWARDED_FOR), ea!(addr = addr))?;
        out.push((addr, None));
    }
    return Ok(out);
}

// Parse `Forwarded` header value.
pub fn parse_forwarded<'a>(v: &'a HeaderValue) -> Result<Forwarded<'a>, loga::Error> {
    let mut out = vec![];
    for hop in v.as_bytes().split(|x| *x == b',') {
        let mut r#for = None;
        let mut proto = None;
        let mut host = None;
        let mut path = None;
        for kv in hop.split(|x| *x == b';') {
            let kv = kv.trim_ascii();
            let mut kv_splits = kv.splitn(2, |x| *x == b'=');
            let k = kv_splits.next().unwrap().to_ascii_lowercase();
            let Some(mut v) = kv_splits.next() else {
                return Err(loga::err_with("Invalid forwarded kv pair", ea!(pair = String::from_utf8_lossy(kv))));
            };
            if let Some(v1) = v.strip_prefix(b"\"") {
                if let Some(v1) = v1.strip_suffix(b"\"") {
                    v = v1;
                }
            }
            match k.as_slice() {
                b"for" => {
                    if r#for.is_some() {
                        return Err(
                            loga::err_with(
                                "Invalid forwarded header hop, has repeated `for`",
                                ea!(for_ = String::from_utf8_lossy(hop)),
                            ),
                        );
                    }
                    let v =
                        String::from_utf8(v.to_vec())
                            .map_err(loga::err)
                            .context_with(
                                "Invalid utf-8 in forwarded `for`",
                                ea!(hop = String::from_utf8_lossy(hop)),
                            )?;
                    let ip_str;
                    let port_str;
                    if let Some(v) = v.strip_prefix("[") {
                        if let Some(v) = v.strip_suffix("]") {
                            ip_str = v;
                            port_str = None;
                        } else {
                            let mut parts = v.rsplitn(2, ':');
                            let part1 = parts.next().unwrap();
                            let part2 = parts.next();
                            let raw_ip_part;
                            if let Some(v) = part2 {
                                raw_ip_part = v;
                                port_str = Some(part1);
                            } else {
                                raw_ip_part = part1;
                                port_str = None;
                            }
                            let Some(v) = raw_ip_part.strip_suffix("]") else {
                                return Err(
                                    loga::err_with(
                                        "Invalid forwarded header hop `for` IPv6 brackets",
                                        ea!(hop = String::from_utf8_lossy(hop)),
                                    ),
                                );
                            };
                            ip_str = v;
                        }
                    } else {
                        let mut parts = v.splitn(2, ':');
                        ip_str = parts.next().unwrap();
                        port_str = parts.next();
                    }
                    let ip =
                        IpAddr::from_str(
                            ip_str,
                        ).context_with("Invalid IP addr in forwarded `for`", ea!(value = v))?;
                    if let Some(port_str) = port_str {
                        let port =
                            u16::from_str(
                                port_str,
                            ).context_with("Invalid port in forwarded `for`", ea!(value = v))?;
                        r#for = Some((ip, Some(port)));
                    } else {
                        r#for = Some((ip, None));
                    }
                },
                b"proto" => {
                    if proto.is_some() {
                        return Err(
                            loga::err_with(
                                "Invalid forwarded header hop, has repeated `proto`",
                                ea!(hop = String::from_utf8_lossy(&hop)),
                            ),
                        );
                    }
                    proto = Some(Cow::Borrowed(v));
                },
                b"host" => {
                    if host.is_some() {
                        return Err(
                            loga::err_with(
                                "Invalid forwarded header hop, has repeated `host`",
                                ea!(hop = String::from_utf8_lossy(hop)),
                            ),
                        );
                    }
                    host = Some(Cow::Borrowed(v));
                },
                b"path" => {
                    if path.is_some() {
                        return Err(
                            loga::err_with(
                                "Invalid forwarded header hop, has repeated `path`",
                                ea!(hop = String::from_utf8_lossy(hop)),
                            ),
                        );
                    }
                    path = Some(Cow::Borrowed(v));
                },
                _ => { },
            }
        }
        out.push(ForwardedHop {
            for_: r#for,
            proto: proto,
            host: host,
            path: path,
        });
    }
    return Ok(out);
}

// Parses all `X-Forwarded-*` or `Forwarded` into a list.
pub fn parse_all_forwarded<'a>(headers: &'a HeaderMap) -> Result<Forwarded<'a>, loga::Error> {
    let mut separate_for = vec![];
    let mut separate_proto = vec![];
    let mut separate_host = vec![];
    let mut separate_path = vec![];
    for v in headers.get_all(FORWARDED_FOR) {
        separate_for.extend(parse_forwarded_for(&v)?);
    }
    for v in headers.get_all(FORWARDED_PROTO) {
        separate_proto.push(v);
    }
    for v in headers.get_all(FORWARDED_HOST) {
        separate_host.push(v);
    }
    for v in headers.get_all(FORWARDED_PATH) {
        separate_path.push(v);
    }
    if headers.contains_key(http::header::FORWARDED) {
        let mut out = vec![];
        for v in headers.get_all(http::header::FORWARDED) {
            out.extend(parse_forwarded(&v)?.into_iter().map(|x| x.to_owned()));
        }
        return Ok(out);
    } else {
        let want_len;
        if !separate_for.is_empty() {
            want_len = separate_for.len();
        } else if !separate_proto.is_empty() {
            want_len = separate_proto.len();
        } else if !separate_host.is_empty() {
            want_len = separate_host.len();
        } else if !separate_path.is_empty() {
            want_len = separate_path.len();
        } else {
            return Ok(vec![]);
        }
        if (!separate_for.is_empty() && separate_for.len() != want_len) ||
            (!separate_proto.is_empty() && separate_proto.len() != want_len) ||
            (!separate_host.is_empty() && separate_host.len() != want_len) ||
            (!separate_path.is_empty() && separate_path.len() != want_len) {
            return Err(loga::err("Mismatched x-forwarded header value counts"));
        }
        separate_for.reverse();
        separate_proto.reverse();
        separate_host.reverse();
        separate_path.reverse();
        let mut out = vec![];
        for _ in 0 .. want_len {
            out.push(ForwardedHop {
                for_: separate_for.pop(),
                proto: separate_proto.pop().map(|x| Cow::Owned(x.as_bytes().to_vec())),
                host: separate_host.pop().map(|x| Cow::Owned(x.as_bytes().to_vec())),
                path: separate_path.pop().map(|x| Cow::Owned(x.as_bytes().to_vec())),
            });
        }
        return Ok(out);
    }
}

pub fn strip_all_forwarded(headers: &mut HeaderMap) {
    while let Some(_) = headers.remove(FORWARDED_FOR) { }
    while let Some(_) = headers.remove(FORWARDED_PROTO) { }
    while let Some(_) = headers.remove(FORWARDED_HOST) { }
    while let Some(_) = headers.remove(FORWARDED_PATH) { }
    while let Some(_) = headers.remove(http::header::FORWARDED) { }
}

/// Prepare the current header into a new forwarded hop in order to continue to
/// forward a message.
pub fn get_forwarded_current<'a>(req_uri: &'a Uri, peer: SocketAddr) -> ForwardedHop<'a> {
    let host;
    match req_uri.authority() {
        Some(authority) => {
            host = authority.as_str().rsplit("@").next();
        },
        None => {
            host = None;
        },
    }
    return ForwardedHop {
        for_: Some((peer.ip(), Some(peer.port()))),
        proto: req_uri.scheme_str().map(|x| Cow::Borrowed(x.as_bytes())),
        host: host.map(|x| Cow::Borrowed(x.as_bytes())),
        path: Some(Cow::Borrowed(req_uri.path().as_bytes())),
    };
}

/// Turn a list of forwarded hops into `Forwarded` header.
pub fn render_to_forwarded(m: &mut HeaderMap, f: &Forwarded) -> Result<(), loga::Error> {
    let mut out = vec![];
    for (hop_i, hop) in f.iter().enumerate() {
        let mut out_hop = vec![];
        if hop_i > 0 {
            out_hop.extend(b"; ");
        }
        let mut kv_i = 0;
        if let Some((addr, port)) = &hop.for_ {
            out_hop.extend(b"for=");
            match addr {
                IpAddr::V4(addr) => {
                    if let Some(port) = port {
                        out_hop.extend(format_bytes!(b"{}:{}", addr.to_string().into_bytes(), *port));
                    } else {
                        out_hop.extend(format_bytes!(b"{}", addr.to_string().into_bytes()));
                    }
                },
                IpAddr::V6(addr) => {
                    if let Some(port) = port {
                        out_hop.extend(format_bytes!(b"\"[{}]:{}\"", addr.to_string().into_bytes(), *port));
                    } else {
                        out_hop.extend(format_bytes!(b"\"[{}]\"", addr.to_string().into_bytes()));
                    }
                },
            }
            #[allow(unused_assignments)]
            {
                kv_i += 1;
            }
        }
        if let Some(proto) = &hop.proto {
            if kv_i > 0 {
                out_hop.extend(b"; ");
            }
            out_hop.extend(b"proto=");
            out_hop.extend(format_bytes!(b"{}", proto));
            #[allow(unused_assignments)]
            {
                kv_i += 1;
            }
        }
        if let Some(host) = &hop.host {
            if kv_i > 0 {
                out_hop.extend(b"; ");
            }
            out_hop.extend(b"host=");
            out_hop.extend(format_bytes!(b"{}", host));
            #[allow(unused_assignments)]
            {
                kv_i += 1;
            }
        }
        if let Some(path) = &hop.path {
            if kv_i > 0 {
                out_hop.extend(b"; ");
            }
            out_hop.extend(b"path=");
            out_hop.extend(format_bytes!(b"{}", path));
            #[allow(unused_assignments)]
            {
                kv_i += 1;
            }
        }
        out.push(out_hop);
    }
    let out_bytes = format_bytes!(b"{}", format_bytes::join(out, b", "));
    m.insert(
        http::header::FORWARDED,
        HeaderValue::from_bytes(
            &out_bytes,
        ).context_with(
            "Forwarded header values produced invalid header",
            ea!(header = String::from_utf8_lossy(&out_bytes)),
        )?,
    );
    return Ok(());
}

/// Turn a list of orwarded hops into a set of `X-Forwarded-*` headers.
pub fn render_to_x_forwarded(m: &mut HeaderMap, f: &Forwarded) -> Result<(), loga::Error> {
    let mut for_out = vec![];
    for hop in f {
        if let Some((addr, _)) = &hop.for_ {
            for_out.push(addr.to_string());
        }
        if let Some(proto) = &hop.proto {
            m.append(
                FORWARDED_PROTO,
                HeaderValue::from_bytes(
                    &proto,
                ).context_with(
                    "Forwarded proto is invalid as header value",
                    ea!(proto = String::from_utf8_lossy(&proto)),
                )?,
            );
        }
        if let Some(host) = &hop.host {
            m.append(
                FORWARDED_HOST,
                HeaderValue::from_bytes(
                    &host,
                ).context_with(
                    "Forwarded host is invalid as header value",
                    ea!(host = String::from_utf8_lossy(&host)),
                )?,
            );
        }
        if let Some(path) = &hop.path {
            m.append(
                FORWARDED_PATH,
                HeaderValue::from_bytes(
                    &path,
                ).context_with(
                    "Forwarded path is invalid as header value",
                    ea!(path = String::from_utf8_lossy(&path)),
                )?,
            );
        }
    }
    if !for_out.is_empty() {
        let joined = for_out.join(", ");
        m.append(
            FORWARDED_FOR,
            HeaderValue::from_str(
                &joined,
            ).context_with("Forwarded for is invalid as header value", ea!(for_ = joined))?,
        );
    }
    return Ok(());
}

/// This gets the IP of the client that initially sent the request
pub fn get_original_peer_ip(forwarded: &Forwarded, current_peer: IpAddr) -> IpAddr {
    shed!{
        let Some(v) = forwarded.iter().next() else {
            break;
        };
        let Some((addr, _)) = v.for_ else {
            break;
        };
        return addr;
    };
    return current_peer;
}

/// Get the absolute base url the client initially sent the request to, before
/// rewrites due to forwarding. This uses the first hop from `Forwarded` or the
/// `X-Forwarded` headers. If empty just returns the directly provided url.
pub fn get_original_base_url(direct: &Uri, forwarded: &Forwarded) -> Result<Uri, loga::Error> {
    let out = shed!{
        let Some(first) = forwarded.first() else {
            let mut parts = direct.clone().into_parts();
            parts.path_and_query = None;
            break Uri::from_parts(parts).unwrap();
        };
        let url = first.uri()?;
        break url;
    };
    if out.host().unwrap_or_default() == "" {
        return Err(
            loga::err(format!("Request doesn't contain enough information to construct absolute base url: {}", out)),
        );
    }
    return Ok(out);
}