h3x 0.2.0

High-performance zero-copy DHTTP/3 implementation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use std::pin::pin;

use bytes::Bytes;
use futures::{Stream, TryStreamExt};
use http::{
    HeaderMap, Method, StatusCode, Uri,
    header::{self, HeaderName, HeaderValue},
    method, status,
    uri::{self, Authority, PathAndQuery, Scheme},
};
use snafu::{ResultExt, Snafu, ensure};

use super::{FieldLine, Protocol, PseudoHeaders};
use crate::{
    codec::DecodeFrom,
    connection::StreamError,
    error::{Code, ErrorScope, H3Error},
};

#[derive(Debug, Snafu)]
#[snafu(module, visibility(pub))]
pub enum MalformedHeaderSection {
    #[snafu(display("invalid pseudo-header field with name {name:?}"))]
    InvalidPseudoHeader { name: Bytes },
    #[snafu(display("duplicate pseudo-header field with name {name:?}"))]
    DuplicatePseudoHeader { name: Bytes },
    // TODO: merge this error into following two errors
    #[snafu(display("field section contains both request and response pseudo-header fields"))]
    MixedRequestResponsePseudoHeader,
    #[snafu(display("request pseudo-header field in response"))]
    RequestPseudoHeaderInResponse,
    #[snafu(display("response pseudo-header field in request"))]
    ResponsePseudoHeaderInRequest,
    #[snafu(display("field section contains pseudo-header fields in trailers"))]
    PseudoHeaderInTrailer,
    #[snafu(display("field section too large"))]
    FieldSectionTooLarge,
    #[snafu(display(
        "empty :authority or host header field for scheme with mandatory authority component"
    ))]
    EmptyAuthorityOrHost,
    #[snafu(display("invalid host header field: invalid ASCII"))]
    InvalidHostHeader,
    #[snafu(display("mismatch between :authority and host header fields"))]
    AuthorityHostMismatch,
    #[snafu(display("absence of mandatory pseudo-header fields"))]
    AbsenceOfMandatoryPseudoHeaders { backtrace: snafu::Backtrace },
    #[snafu(display("unexpected :scheme pseudo-header for CONNECT request"))]
    UnexpectedSchemeForConnectRequest,
    #[snafu(display("unexpected :path pseudo-header for CONNECT request"))]
    UnexpectedPathForConnectRequest,
    #[snafu(display("missing :authority pseudo-header for CONNECT request"))]
    MissingAuthorityForConnectRequest,
    #[snafu(display("missing port for CONNECT :authority pseudo-header"))]
    MissingPortForConnectAuthority,
    #[snafu(display("unexpected :protocol pseudo-header in non-CONNECT request"))]
    ProtocolInNonConnectRequest,
    #[snafu(display("invalid :protocol pseudo-header token"))]
    InvalidProtocolToken { source: std::str::Utf8Error },

    #[snafu(transparent)]
    InvalidMessage { source: http::Error },
}

impl H3Error for MalformedHeaderSection {
    fn code(&self) -> Code {
        Code::H3_MESSAGE_ERROR
    }
    fn scope(&self) -> ErrorScope {
        ErrorScope::Stream
    }
}

impl From<method::InvalidMethod> for MalformedHeaderSection {
    fn from(error: method::InvalidMethod) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

impl From<uri::InvalidUri> for MalformedHeaderSection {
    fn from(error: uri::InvalidUri) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

impl From<status::InvalidStatusCode> for MalformedHeaderSection {
    fn from(error: status::InvalidStatusCode) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

impl From<header::MaxSizeReached> for MalformedHeaderSection {
    fn from(error: header::MaxSizeReached) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

impl From<header::InvalidHeaderName> for MalformedHeaderSection {
    fn from(error: header::InvalidHeaderName) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

impl From<header::InvalidHeaderValue> for MalformedHeaderSection {
    fn from(error: header::InvalidHeaderValue) -> Self {
        MalformedHeaderSection::InvalidMessage {
            source: error.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldSection {
    pub(crate) pseudo_headers: Option<super::PseudoHeaders>,
    pub(crate) header_map: HeaderMap,
}

impl FieldSection {
    pub fn header(pseudo_headers: PseudoHeaders, header_map: HeaderMap) -> Self {
        Self {
            pseudo_headers: Some(pseudo_headers),
            header_map,
        }
    }

    pub fn trailer(header_map: HeaderMap) -> Self {
        Self {
            pseudo_headers: None,
            header_map,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.pseudo_headers
            .as_ref()
            .is_none_or(|pseudo_headers| pseudo_headers.is_empty())
            && self.header_map.is_empty()
    }

    pub fn iter(&self) -> Iter<'_> {
        IntoIterator::into_iter(self)
    }

    pub fn is_request_header(&self) -> bool {
        matches!(self.pseudo_headers, Some(PseudoHeaders::Request { .. }))
    }

    pub fn is_response_header(&self) -> bool {
        matches!(self.pseudo_headers, Some(PseudoHeaders::Response { .. }))
    }

    pub fn is_trailer(&self) -> bool {
        self.pseudo_headers.is_none()
    }

    pub fn check_pseudo(&self) -> Result<(), MalformedHeaderSection> {
        let Some(pseudo_headers) = self.pseudo_headers.as_ref() else {
            return Ok(());
        };
        match pseudo_headers {
            PseudoHeaders::Request {
                method,
                scheme,
                authority,
                path,
                protocol,
            } => {
                // All HTTP/3 requests MUST include exactly one value for the :method,
                // :scheme, and :path pseudo-header fields, unless the request is a
                // CONNECT request; see Section 4.4.
                let Some(method) = method else {
                    return malformed_header_section::AbsenceOfMandatoryPseudoHeadersSnafu.fail();
                };

                // 4.  The Extended CONNECT Method
                //
                //    Usage of the CONNECT method in HTTP/2 is defined by Section 8.3 of
                //    [RFC7540].  This extension modifies the method in the following ways:
                //
                //    o  A new pseudo-header field :protocol MAY be included on request
                //       HEADERS indicating the desired protocol to be spoken on the tunnel
                //       created by CONNECT.  The pseudo-header field is single valued and
                //       contains a value from the "Hypertext Transfer Protocol (HTTP)
                //       Upgrade Token Registry" located at
                //       <https://www.iana.org/assignments/http-upgrade-tokens/>
                //
                //    o  On requests that contain the :protocol pseudo-header field, the
                //       :scheme and :path pseudo-header fields of the target URI (see
                //       Section 5) MUST also be included.
                //
                //    o  On requests bearing the :protocol pseudo-header field, the
                //       :authority pseudo-header field is interpreted according to
                //       Section 8.1.2.3 of [RFC7540] instead of Section 8.3 of that
                //       document.  In particular, the server MUST NOT create a tunnel to
                //       the host indicated by the :authority as it would with a CONNECT
                //       method request that was not modified by this extension.
                //
                //    Upon receiving a CONNECT request bearing the :protocol pseudo-header
                //    field, the server establishes a tunnel to another service of the
                //    protocol type indicated by the pseudo-header field.  This service may
                //    or may not be co-located with the server.
                //
                // https://datatracker.ietf.org/doc/html/rfc8441#section-4
                if method != Method::CONNECT || protocol.is_some() {
                    let Some(scheme) = scheme else {
                        return malformed_header_section::AbsenceOfMandatoryPseudoHeadersSnafu
                            .fail();
                    };
                    let Some(_path) = path else {
                        return malformed_header_section::AbsenceOfMandatoryPseudoHeadersSnafu
                            .fail();
                    };

                    // If the :scheme pseudo-header field identifies a scheme that has a
                    // mandatory authority component (including "http" and "https"), the
                    // request MUST contain either an :authority pseudo-header field or a
                    // Host header field. If these fields are present, they MUST NOT be
                    // empty. If both fields are present, they MUST contain the same value.
                    // If the scheme does not have a mandatory authority component and none
                    // is provided in the request target, the request MUST NOT contain the
                    // :authority pseudo-header or Host header fields.
                    if scheme == &Scheme::HTTP || scheme == &Scheme::HTTPS {
                        match (authority, self.header_map.get("host")) {
                            (Some(authority), Some(host)) => {
                                let host = host
                                    .to_str()
                                    .map_err(|_| MalformedHeaderSection::InvalidHostHeader)?;
                                if authority.as_str().is_empty() || host.is_empty() {
                                    return Err(MalformedHeaderSection::EmptyAuthorityOrHost);
                                }
                                if authority.as_str() != host {
                                    return Err(MalformedHeaderSection::AuthorityHostMismatch);
                                }
                            }
                            (Some(authority), None) => {
                                if authority.as_str().is_empty() {
                                    return Err(MalformedHeaderSection::EmptyAuthorityOrHost);
                                }
                            }
                            (None, Some(host)) => {
                                let host = host
                                    .to_str()
                                    .map_err(|_| MalformedHeaderSection::InvalidHostHeader)?;
                                if host.is_empty() {
                                    return Err(MalformedHeaderSection::EmptyAuthorityOrHost);
                                }
                            }
                            (None, None) => {
                                return malformed_header_section::AbsenceOfMandatoryPseudoHeadersSnafu.fail();
                            }
                        }
                    }

                    Ok(())
                } else {
                    // A CONNECT request MUST be constructed as follows:
                    //
                    // * The :method pseudo-header field is set to "CONNECT"
                    //
                    // * The :scheme and :path pseudo-header fields are omitted
                    //
                    // * The :authority pseudo-header field contains the host and port to
                    //   connect to (equivalent to the authority-form of the request-target
                    //   of CONNECT requests; see Section 7.1 of [HTTP]).
                    //
                    // https://datatracker.ietf.org/doc/html/rfc9114#name-the-connect-method
                    if scheme.is_some() {
                        return Err(MalformedHeaderSection::UnexpectedSchemeForConnectRequest);
                    }
                    if path.is_some() {
                        return Err(MalformedHeaderSection::UnexpectedPathForConnectRequest);
                    }
                    let Some(authority) = authority else {
                        return Err(MalformedHeaderSection::MissingAuthorityForConnectRequest);
                    };
                    if authority.port().is_none() {
                        return Err(MalformedHeaderSection::MissingPortForConnectAuthority);
                    }
                    Ok(())
                }
            }
            PseudoHeaders::Response { status } => {
                let Some(_status) = status else {
                    return malformed_header_section::AbsenceOfMandatoryPseudoHeadersSnafu.fail();
                };
                Ok(())
            }
        }
    }

    pub fn method(&self) -> Method {
        let Some(PseudoHeaders::Request { method, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        method.clone().expect("method is set for request headers")
    }

    pub fn set_method(&mut self, new: Method) {
        let Some(PseudoHeaders::Request { method, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        *method = Some(new)
    }

    pub fn scheme(&self) -> Option<Scheme> {
        let Some(PseudoHeaders::Request { scheme, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        scheme.clone()
    }

    pub fn set_scheme(&mut self, new: Scheme) {
        let Some(PseudoHeaders::Request { scheme, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        *scheme = Some(new)
    }

    pub fn authority(&self) -> Option<Authority> {
        let Some(PseudoHeaders::Request { authority, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        authority.clone()
    }

    pub fn set_authority(&mut self, new: Authority) {
        let Some(PseudoHeaders::Request { authority, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        *authority = Some(new)
    }

    pub fn path(&self) -> Option<PathAndQuery> {
        let Some(PseudoHeaders::Request { path, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        path.clone()
    }

    pub fn set_path(&mut self, new: PathAndQuery) {
        let Some(PseudoHeaders::Request { path, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        *path = Some(new);
    }

    pub fn protocol(&self) -> Option<Protocol> {
        let Some(PseudoHeaders::Request { protocol, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        protocol.clone()
    }

    pub fn set_protocol(&mut self, new: Protocol) {
        let Some(PseudoHeaders::Request { protocol, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not request headers")
        };
        *protocol = Some(new);
    }

    pub fn uri(&self) -> Uri {
        let mut uri = uri::Parts::default();
        uri.scheme = self.scheme();
        uri.authority = self.authority();
        uri.path_and_query = self.path();
        Uri::from_parts(uri).expect("valid URI parts from request headers")
    }

    pub fn set_uri(&mut self, uri: Uri) {
        let uri = uri.into_parts();
        if let Some(scheme) = uri.scheme {
            self.set_scheme(scheme);
        }
        if let Some(authority) = uri.authority {
            self.set_authority(authority);
        }
        if let Some(path) = uri.path_and_query {
            self.set_path(path);
        }
    }

    pub fn status(&self) -> StatusCode {
        let Some(PseudoHeaders::Response { status, .. }) = &self.pseudo_headers else {
            panic!("FieldSection is not response headers")
        };
        status.expect("status is set for response headers")
    }

    pub fn set_status(&mut self, new: StatusCode) {
        let Some(PseudoHeaders::Response { status, .. }) = &mut self.pseudo_headers else {
            panic!("FieldSection is not response headers")
        };
        *status = Some(new)
    }
}

impl<S: Stream<Item = Result<FieldLine, StreamError>> + Send> DecodeFrom<S> for FieldSection {
    type Error = StreamError;

    async fn decode_from(stream: S) -> Result<Self, Self::Error> {
        let mut stream = pin!(stream);

        let mut pseudo_headers = None;
        let mut header_map = HeaderMap::new();
        while let Some(FieldLine { name, value }) = stream.try_next().await? {
            match name {
                name if name == PseudoHeaders::METHOD => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_request());
                    let method = match &mut pseudo {
                        PseudoHeaders::Request { method, .. } => method,
                        PseudoHeaders::Response { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        method.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *method =
                        Some(Method::from_bytes(&value[..]).map_err(MalformedHeaderSection::from)?);
                    pseudo_headers = Some(pseudo)
                }
                name if name == PseudoHeaders::PROTOOCL => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_request());
                    let protocol = match &mut pseudo {
                        PseudoHeaders::Request { protocol, .. } => protocol,
                        PseudoHeaders::Response { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        protocol.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *protocol = Some(
                        Protocol::try_from(value.clone())
                            .context(malformed_header_section::InvalidProtocolTokenSnafu)?,
                    );
                    pseudo_headers = Some(pseudo)
                }
                name if name == PseudoHeaders::SCHEME => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_request());
                    let scheme = match &mut pseudo {
                        PseudoHeaders::Request { scheme, .. } => scheme,
                        PseudoHeaders::Response { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        scheme.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *scheme =
                        Some(Scheme::try_from(&value[..]).map_err(MalformedHeaderSection::from)?);
                    pseudo_headers = Some(pseudo)
                }
                name if name == PseudoHeaders::AUTHORITY => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_request());
                    let authority = match &mut pseudo {
                        PseudoHeaders::Request { authority, .. } => authority,
                        PseudoHeaders::Response { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        authority.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *authority = Some(
                        Authority::from_maybe_shared(value)
                            .map_err(MalformedHeaderSection::from)?,
                    );
                    pseudo_headers = Some(pseudo)
                }
                name if name == PseudoHeaders::PATH => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_request());

                    let path_field = match &mut pseudo {
                        PseudoHeaders::Request { path, .. } => path,
                        PseudoHeaders::Response { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        path_field.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *path_field = Some(
                        PathAndQuery::from_maybe_shared(value)
                            .map_err(MalformedHeaderSection::from)?,
                    );
                    pseudo_headers = Some(pseudo)
                }
                name if name == PseudoHeaders::STATUS => {
                    let mut pseudo = pseudo_headers
                        .take()
                        .unwrap_or(PseudoHeaders::unresolved_response());

                    let status = match &mut pseudo {
                        PseudoHeaders::Response { status, .. } => status,
                        PseudoHeaders::Request { .. } => {
                            return Err(
                                MalformedHeaderSection::MixedRequestResponsePseudoHeader.into()
                            );
                        }
                    };
                    ensure!(
                        status.is_none(),
                        malformed_header_section::DuplicatePseudoHeaderSnafu { name }
                    );
                    *status = Some(
                        StatusCode::try_from(&value[..]).map_err(MalformedHeaderSection::from)?,
                    );
                    pseudo_headers = Some(pseudo)
                }

                name if name.starts_with(b":") => {
                    return Err(MalformedHeaderSection::InvalidPseudoHeader { name }.into());
                }
                name => {
                    header_map
                        .try_append(
                            HeaderName::from_bytes(name.as_ref())
                                .map_err(MalformedHeaderSection::from)?,
                            HeaderValue::from_maybe_shared(value)
                                .map_err(MalformedHeaderSection::from)?,
                        )
                        .map_err(MalformedHeaderSection::from)?;
                }
            }
        }

        Ok(FieldSection {
            pseudo_headers,
            header_map,
        })
    }
}

impl<'s> IntoIterator for &'s FieldSection {
    type Item = FieldLine;
    type IntoIter = Iter<'s>;

    fn into_iter(self) -> Self::IntoIter {
        let header_map_iter = self.header_map.iter();
        Iter {
            pseudo_headers: self.pseudo_headers.clone(),
            header_map_iter,
        }
    }
}

pub struct Iter<'s> {
    pseudo_headers: Option<PseudoHeaders>,
    header_map_iter: http::header::Iter<'s, HeaderValue>,
}

impl Iterator for Iter<'_> {
    type Item = FieldLine;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(pseudo_headers) = self.pseudo_headers.as_mut() {
            match pseudo_headers {
                PseudoHeaders::Request {
                    method,
                    scheme,
                    authority,
                    path,
                    protocol,
                } => {
                    if let Some(method) = method.take() {
                        return Some(method.into());
                    }
                    if let Some(protocol) = protocol.take() {
                        return Some(protocol.into());
                    }
                    if let Some(scheme) = scheme.take() {
                        return Some(scheme.into());
                    }
                    if let Some(authority) = authority.take() {
                        return Some(authority.into());
                    }
                    if let Some(path) = path.take() {
                        return Some(path.into());
                    }
                }
                PseudoHeaders::Response { status } => {
                    if let Some(status) = status.take() {
                        return Some(status.into());
                    }
                }
            }
        };
        self.pseudo_headers = None;

        if let Some((name, value)) = self.header_map_iter.next() {
            return Some(FieldLine {
                name: Bytes::from_owner(name.clone()),
                value: Bytes::from_owner(value.clone()),
            });
        }

        None
    }
}