dhttp 0.1.0

The True Internet
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
//! Server request and response message API.
//!
//! Low-level stream access is intentionally not part of the high-level
//! request/response surface. Use `read`/`write`, buffered helpers, and
//! stream adapters instead. A future dedicated low-level API can own the raw
//! stream handles without aliasing them through these high-level wrappers.
//!
//! ```compile_fail
//! fn request_must_not_expose_raw_read_stream(
//!     request: &mut dhttp::endpoint::server::Request,
//! ) {
//!     let _ = request.read_stream();
//! }
//! ```
//!
//! ```compile_fail
//! fn response_must_not_expose_raw_write_stream(
//!     response: &mut dhttp::endpoint::server::Response,
//! ) {
//!     let _ = response.write_stream();
//! }
//! ```
//!
//! ```compile_fail
//! fn request_target_parts_must_use_uri(request: &dhttp::endpoint::server::Request) {
//!     let _ = request.scheme();
//!     let _ = request.path();
//!     let _ = request.target_authority();
//! }
//! ```
//!
//! ```compile_fail
//! fn server_message_identity_uses_authority(
//!     request: &dhttp::endpoint::server::Request,
//!     response: &dhttp::endpoint::server::Response,
//! ) {
//!     let _ = request.remote_authority();
//!     let _ = response.local_authority();
//! }
//! ```

use bytes::{Buf, Bytes};
use dhttp_identity::identity as authority;
use futures::{Stream, StreamExt};
use http::{
    HeaderMap, HeaderValue, Method, Uri,
    header::{AsHeaderName, IntoHeaderName},
};
use snafu::{OptionExt, Report, ResultExt, Snafu};
use std::{future::Future, sync::Arc};
use tracing::Instrument;

use crate::{
    h3x::{
        dhttp::message::{MessageReader, MessageStreamError, MessageWriter},
        endpoint::UnresolvedRequest,
        error::Code,
        protocol::Protocols,
        qpack::field::Protocol,
        stream_id::StreamId,
    },
    message::{
        Body, IntoBody, MessageOperationError, ReadBufferedBodyError, ReadStreamingBodyError,
        ReadToStringError, ReadTrailersError, RequestMessage, ResponseMessage,
        WriteStreamingBodyError,
    },
};

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum ResolveError {
    #[snafu(display("failed to read server local agent"))]
    LocalAuthority {
        source: crate::h3x::quic::ConnectionError,
    },
    #[snafu(display("server request is missing local agent"))]
    MissingLocalAuthority,
    #[snafu(display("failed to read server remote authority"))]
    RemoteAuthority {
        source: crate::h3x::quic::ConnectionError,
    },
    #[snafu(display("failed to read request header"))]
    ReadHeader { source: MessageStreamError },
}

pub async fn resolve(request: UnresolvedRequest) -> Result<(Request, Response), ResolveError> {
    let UnresolvedRequest {
        stream_id,
        read_stream,
        write_stream,
        connection,
    } = request;
    // Authorities are backed by a watch channel — fetching them per-request
    // is effectively a clone once the handshake has completed.
    let local_authority = connection
        .local_authority()
        .await
        .context(resolve_error::LocalAuthoritySnafu)?
        .context(resolve_error::MissingLocalAuthoritySnafu)?;
    let remote_authority = connection
        .remote_authority()
        .await
        .context(resolve_error::RemoteAuthoritySnafu)?;
    let protocols = connection.protocols().clone();

    let mut read_stream = read_stream;
    let request_message = RequestMessage::read_from(&mut read_stream)
        .await
        .context(resolve_error::ReadHeaderSnafu)?;
    let request = Request {
        message: request_message,
        stream: read_stream,
        authority: remote_authority,
        stream_id,
        protocols: protocols.clone(),
    };
    let response = Response {
        message: Some(ResponseMessage::default()),
        stream: Some(write_stream),
        authority: local_authority,
        stream_id,
        protocols,
    };
    Ok((request, response))
}

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum ResponseWriteError {
    #[snafu(display("response is already finalized"))]
    ResponseFinalized,
    #[snafu(display("response message operation failed"))]
    MessageOperation { source: MessageOperationError },
    #[snafu(transparent)]
    Body { source: WriteStreamingBodyError },
}

pub struct Request {
    message: RequestMessage,
    stream: MessageReader,
    authority: Option<Arc<dyn authority::RemoteAuthority>>,
    stream_id: StreamId,
    protocols: Arc<Protocols>,
}

impl Request {
    pub fn method(&self) -> Method {
        self.message.method().clone()
    }

    pub fn protocol(&self) -> Option<Protocol> {
        self.message.header().protocol().cloned()
    }

    pub fn uri(&self) -> Uri {
        self.message.uri()
    }

    pub fn headers(&self) -> &http::HeaderMap {
        self.message.header().header_map()
    }

    pub fn header(&self, name: impl AsHeaderName) -> Option<&HeaderValue> {
        self.headers().get(name)
    }

    pub async fn read(&mut self) -> Option<Result<Bytes, ReadStreamingBodyError>> {
        self.message
            .read_streaming_body_from(&mut self.stream)
            .await
    }

    pub async fn read_all(&mut self) -> Result<impl Buf, ReadBufferedBodyError> {
        self.message.read_buffered_body_from(&mut self.stream).await
    }

    pub async fn read_to_bytes(&mut self) -> Result<Bytes, ReadBufferedBodyError> {
        self.message.collect_bytes_body_from(&mut self.stream).await
    }

    pub async fn read_to_string(&mut self) -> Result<String, ReadToStringError> {
        self.message
            .collect_string_body_from(&mut self.stream)
            .await
    }

    pub async fn as_stream(&mut self) -> impl Stream<Item = Result<Bytes, ReadStreamingBodyError>> {
        futures::stream::unfold(self, async |this| {
            this.read().await.map(|item| (item, this))
        })
        .fuse()
    }

    pub async fn into_stream(self) -> impl Stream<Item = Result<Bytes, ReadStreamingBodyError>> {
        futures::stream::unfold(self, async |mut this| {
            this.read().await.map(|item| (item, this))
        })
        .fuse()
    }

    pub async fn trailers(&mut self) -> Result<&HeaderMap, ReadTrailersError> {
        self.message.read_trailers_from(&mut self.stream).await
    }

    pub async fn stop(&mut self, code: Code) -> Result<(), MessageStreamError> {
        self.stream.stop(code).await
    }

    pub fn authority(&self) -> Option<&Arc<dyn authority::RemoteAuthority>> {
        self.authority.as_ref()
    }

    /// Returns the QUIC stream identifier for this request.
    ///
    /// The stream ID uniquely identifies the request stream within its QUIC connection.
    /// Combined with [`protocols()`](Self::protocols), it serves as the per-stream key
    /// for deriving protocol-specific session handles from connection-scoped protocol
    /// state:
    ///
    /// ```ignore
    /// let proto = request.protocols().get::<MyProtocol>().unwrap();
    /// let session = proto.create_session(request.stream_id());
    /// ```
    pub fn stream_id(&self) -> StreamId {
        self.stream_id
    }

    /// Returns the connection-scoped protocol registry.
    ///
    /// The returned `Arc<Protocols>` is shared across all request handlers on the same
    /// QUIC connection. Use [`Protocols::get`] to look up a concrete protocol runtime
    /// by type, then derive per-request handles using [`stream_id()`](Self::stream_id):
    ///
    /// ```ignore
    /// let dhttp = request.protocols().get::<DHttpProtocol>().unwrap();
    /// let qpack = request.protocols().get::<QPackProtocol>();
    /// ```
    pub fn protocols(&self) -> &Arc<Protocols> {
        &self.protocols
    }
}

pub struct Response {
    message: Option<ResponseMessage>,
    stream: Option<MessageWriter>,
    authority: Arc<dyn authority::LocalAuthority>,
    stream_id: StreamId,
    protocols: Arc<Protocols>,
}

impl Response {
    fn check_message_operation(
        &mut self,
        operation: &str,
        operate: impl FnOnce(&mut ResponseMessage) -> Result<(), MessageOperationError>,
    ) -> bool {
        if self.message.is_none() || self.stream.is_none() {
            tracing::warn!(
                operation,
                "response is already finalized, operation will not affect the response stream",
            );
            return false;
        }
        let message = self
            .message
            .as_mut()
            .expect("response message is present after explicit check");
        if let Err(error) = operate(message) {
            let report = Report::from_error(&error);
            tracing::warn!(
                operation, error = %report,
                "response message operation failed, operation will not affect the response stream",
            );
            return false;
        }
        true
    }

    pub fn headers(&self) -> &http::HeaderMap {
        self.message
            .as_ref()
            .expect("response message is unavailable after finalization")
            .header()
            .header_map()
    }

    pub fn headers_mut(&mut self) -> &mut http::HeaderMap {
        self.check_message_operation("modify_headers", |message| {
            message.header_mut()?;
            Ok(())
        });
        self.message
            .as_mut()
            .expect("response message is unavailable after finalization")
            .header_mut_unchecked()
            .header_map_mut()
    }

    pub fn set_header(&mut self, name: impl IntoHeaderName, value: HeaderValue) -> &mut Self {
        self.check_message_operation("set_header", |message| {
            message.header_mut()?.header_map_mut().insert(name, value);
            Ok(())
        });
        self
    }

    pub fn status(&self) -> Option<http::StatusCode> {
        self.message.as_ref().map(ResponseMessage::status)
    }

    pub fn set_status(&mut self, status: http::StatusCode) -> &mut Self {
        self.check_message_operation("set_status", |message| {
            message.header_mut()?.set_status(status);
            Ok(())
        });
        self
    }

    pub fn set_body(&mut self, content: impl IntoBody) -> &mut Self {
        self.check_message_operation("write_chunked_body", |message| {
            if message.is_interim_response() {
                return Err(MessageOperationError::BodyOrTrailerOnInterimResponse);
            }
            message.set_body(content)?;
            Ok(())
        });
        self
    }

    pub fn write<B>(
        &mut self,
        content: B,
    ) -> impl Future<Output = Result<&mut Self, ResponseWriteError>> + use<'_, B>
    where
        B: IntoBody,
    {
        let content: Body = content.into_body();
        async move {
            let message = self
                .message
                .as_mut()
                .ok_or(ResponseWriteError::ResponseFinalized)?;
            if message.is_interim_response() {
                return Err(
                    Err::<(), _>(MessageOperationError::BodyOrTrailerOnInterimResponse)
                        .context(response_write_error::MessageOperationSnafu)
                        .expect_err("response write message operation conversion must fail"),
                );
            }
            let stream = self
                .stream
                .as_mut()
                .ok_or(ResponseWriteError::ResponseFinalized)?;
            message.write_streaming_body_to(stream, content).await?;
            Ok(self)
        }
    }

    pub async fn flush(&mut self) -> Result<&mut Self, MessageStreamError> {
        let message = self
            .message
            .as_mut()
            .ok_or(MessageStreamError::MessageSendFailed)?;
        let stream = self
            .stream
            .as_mut()
            .ok_or(MessageStreamError::MessageSendFailed)?;
        message.write_all_to(stream).await?;
        stream.flush().await?;
        Ok(self)
    }

    pub fn trailers(&self) -> &HeaderMap {
        self.message
            .as_ref()
            .expect("response message is unavailable after finalization")
            .trailers()
    }

    pub fn trailers_mut(&mut self) -> &mut HeaderMap {
        self.check_message_operation("modify_trailers", |message| {
            if message.is_interim_response() {
                return Err(MessageOperationError::BodyOrTrailerOnInterimResponse);
            }
            message.trailers_mut()?;
            Ok(())
        });
        self.message
            .as_mut()
            .expect("response message is unavailable after finalization")
            .trailers_mut_unchecked()
    }

    pub fn set_trailer(&mut self, name: impl IntoHeaderName, value: HeaderValue) -> &mut Self {
        self.check_message_operation("set_trailer", |message| {
            if message.is_interim_response() {
                return Err(MessageOperationError::BodyOrTrailerOnInterimResponse);
            }
            message.trailers_mut()?.insert(name, value);
            Ok(())
        });
        self
    }

    pub fn set_trailers(&mut self, map: HeaderMap) -> &mut Self {
        self.check_message_operation("set_trailers", |message| {
            if message.is_interim_response() {
                return Err(MessageOperationError::BodyOrTrailerOnInterimResponse);
            }
            *message.trailers_mut()? = map;
            Ok(())
        });
        self
    }

    pub async fn close(&mut self) -> Result<(), MessageStreamError> {
        if let Some(future) = self.finish() {
            future.await
        } else {
            Ok(())
        }
    }

    pub async fn reset(&mut self, code: Code) -> Result<(), MessageStreamError> {
        self.message = None;
        if let Some(mut stream) = self.stream.take() {
            stream.reset(code).await
        } else {
            Ok(())
        }
    }

    pub fn authority(&self) -> &Arc<dyn authority::LocalAuthority> {
        &self.authority
    }

    /// Returns the QUIC stream identifier for this response.
    ///
    /// Same stream ID as the corresponding [`Request::stream_id`]. Useful when the
    /// response handler needs to interact with connection-scoped protocols:
    ///
    /// ```ignore
    /// let proto = response.protocols().get::<MyProtocol>().unwrap();
    /// let session = proto.create_session(response.stream_id());
    /// ```
    pub fn stream_id(&self) -> StreamId {
        self.stream_id
    }

    /// Returns the connection-scoped protocol registry.
    ///
    /// Same `Arc<Protocols>` as [`Request::protocols`]. See [`Protocols::get`] for
    /// typed protocol lookup.
    pub fn protocols(&self) -> &Arc<Protocols> {
        &self.protocols
    }

    /// Returns a future that completes response finalization, if the response is unfinished.
    ///
    /// Awaiting the returned future writes any buffered response data and closes the response
    /// stream. If this method returns `None`, the response has already been completed or
    /// finalized. Dropping an unfinished response still performs the same finalization in a
    /// best-effort background task.
    pub fn finish(
        &mut self,
    ) -> Option<impl Future<Output = Result<(), MessageStreamError>> + Send + use<>> {
        let mut message = self.message.take()?;
        let mut stream = self
            .stream
            .take()
            .expect("response stream is unavailable while message is unfinished");

        Some(async move {
            if message.is_interim_response() {
                let error = MessageOperationError::FinalResponseRequired;
                let report = Report::from_error(&error);
                tracing::warn!(
                    error = %report,
                    "response stream cannot be closed without a final response",
                );
                _ = stream.reset(Code::H3_MESSAGE_ERROR).await;
                return Err(MessageStreamError::MessageSendFailed);
            }

            message.write_all_to(&mut stream).await?;
            stream.close().await
        })
    }

    /// Async drop the response properly.
    pub(crate) fn drop(
        &mut self,
    ) -> Option<impl Future<Output = Result<(), MessageStreamError>> + Send + use<>> {
        self.finish()
    }
}

impl Drop for Response {
    fn drop(&mut self) {
        if let Some(future) = self.finish() {
            // Inherent termination: the task owns the response message and stream,
            // then exits after writing/canceling and closing the stream.
            tokio::spawn(
                async move {
                    if let Err(error) = future.await {
                        let report = Report::from_error(&error);
                        tracing::debug!(error = %report, "failed to finish response on drop");
                    }
                }
                .in_current_span(),
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn authority_accessors_have_directional_identity_names() {
        let _request_authority = |request: &Request| {
            let _: Option<&Arc<dyn authority::RemoteAuthority>> = request.authority();
        };
        let _response_authority = |response: &Response| {
            let _: &Arc<dyn authority::LocalAuthority> = response.authority();
        };
    }
}