scion-quic 0.8.2

QUIC via SCION as transport
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
// Copyright 2026 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The **read half** of a request stream.

use std::{
    future::Future,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use bytes::{BufMut, Bytes, BytesMut};
use http::HeaderMap;
use http_body::{Body, Frame};

use super::StreamRef;
use crate::{
    h3::{
        client::{
            AbortOnDrop,
            app::{Http3ClientApp, ResponseHead, ResponseHeadState},
            error::RequestError,
        },
        common::{H3Error, ReadState, read::poll_read_frame},
    },
    quic::connection::{QuicScionConn, WeakConnectionHandle},
};

/// A future resolving to the response of a request issued via `initiate_request`.
///
/// It resolves once the response head has arrived, yielding an `http::Response`
/// whose body is the streaming [`H3ResponseBody`]. It owns the stream's read-side
/// guard: dropped before the head arrives it stops the read side; on success the
/// guard is handed to the response body, which continues to own it.
pub struct ResponseFut {
    handle: WeakConnectionHandle<Http3ClientApp>,
    stream_id: u64,
    /// The read-side guard, taken when the head arrives and moved into the
    /// response body. `None` once the future has resolved.
    read_guard: Option<ReadGuard>,
}

impl ResponseFut {
    pub(crate) fn new(read_guard: ReadGuard) -> Self {
        Self {
            handle: read_guard.handle(),
            stream_id: read_guard.stream_id(),
            read_guard: Some(read_guard),
        }
    }
}

impl Future for ResponseFut {
    type Output = Result<http::Response<H3ResponseBody>, RequestError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        match poll_head(&this.handle, this.stream_id, cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(Ok(head)) => {
                let guard = this
                    .read_guard
                    .take()
                    .expect("ResponseFut polled after completion");
                Poll::Ready(Ok(head_into_response(head, H3ResponseBody::new(guard))))
            }
            Poll::Ready(Err(err)) => {
                // The read side is already gone (reset or connection close); mark
                // the guard done so its drop does not signal STOP_SENDING on a
                // dead stream.
                if let Some(guard) = this.read_guard.as_mut() {
                    guard.mark_done();
                }
                Poll::Ready(Err(err))
            }
        }
    }
}

/// Assembles an `http::Response` from a routed head and the streaming body.
fn head_into_response(head: ResponseHead, body: H3ResponseBody) -> http::Response<H3ResponseBody> {
    let mut response = http::Response::builder()
        .status(head.status)
        .body(body)
        .expect("status is valid");
    *response.headers_mut() = head.headers;
    response
}

/// The streaming body of an HTTP/3 response.
///
/// The body owns the stream's read-side guard: dropping it before end-of-stream
/// signals `STOP_SENDING` (cancelling the read side); dropping it after a clean
/// end is a no-op. Either way the shared per-stream bookkeeping is released once
/// the last reference (read or write half) drops.
pub struct H3ResponseBody {
    handle: WeakConnectionHandle<Http3ClientApp>,
    stream_id: u64,
    /// Read-side lifetime guard; its `done` flag suppresses `STOP_SENDING` once
    /// the body has finished cleanly, and its drop tears the read side down.
    read_guard: ReadGuard,
    /// The task uploading the request body, when
    /// [`Http3Client::request`](crate::h3::client::Http3Client::request) drives it. `None` when
    /// the caller owns the writer itself, as with `request_with_writer`.
    upload: Option<AbortOnDrop<()>>,
}

impl H3ResponseBody {
    pub(crate) fn new(read_guard: ReadGuard) -> Self {
        Self {
            handle: read_guard.handle(),
            stream_id: read_guard.stream_id(),
            read_guard,
            upload: None,
        }
    }

    /// Ties `upload` to this body, so that dropping the response ends the request body upload.
    pub(crate) fn attach_upload(&mut self, upload: AbortOnDrop<()>) {
        self.upload = Some(upload);
    }

    /// Returns a future that resolves to the next frame of the response body, or
    /// `None` if the body has finished (EOF, trailers, or an error already observed).
    pub async fn next_frame(&mut self) -> Option<Result<Frame<Bytes>, H3Error>> {
        futures::future::poll_fn(|cx| poll_read_frame(&self.handle, self.stream_id, cx)).await
    }

    /// Copies the entire response body into `buffer`, returning the collected data and any trailing
    /// headers.
    ///
    /// `max_size`, if set, caps the total body size accepted; a body exceeding it is rejected
    /// rather than partially copied.
    ///
    /// Returns Ok((written, trailers)) if the body was successfully collected, where `written` is
    /// the number of bytes written to the buffer and `trailers` is an optional `HeaderMap`
    /// containing any trailing headers.
    ///
    /// Returns [`CollectError::BufferTooSmall`] if `buffer` runs out of spare capacity,
    /// [`CollectError::TooLarge`] if the body exceeds `max_size`, or [`CollectError::H3`] if the
    /// underlying connection encounters an error.
    pub async fn collect_to_buf(
        mut self,
        buffer: &mut impl BufMut,
        max_size: Option<usize>,
    ) -> Result<(usize, Option<HeaderMap>), CollectError> {
        let mut written = 0;

        while let Some(frame) = self.next_frame().await {
            let frame = frame?;

            if frame.is_data() {
                let chunk = frame.into_data().expect("frame is data");
                if chunk.len() > buffer.remaining_mut() {
                    return Err(CollectError::BufferTooSmall);
                }

                if let Some(max) = max_size
                    && written + chunk.len() > max
                {
                    return Err(CollectError::TooLarge);
                }

                buffer.put_slice(&chunk);
                written += chunk.len();
            } else if frame.is_trailers() {
                let trailers = frame.into_trailers().expect("frame is trailers");
                // Got trailers, well-formed response body is complete. Return the collected data
                // and the trailers.
                return Ok((written, Some(trailers)));
            } else {
                // There are no other frame types as of now, but if there are in the future, we
                // should handle them appropriately.
                debug_assert!(false, "Unexpected frame type in response body: {:?}", frame);
            }
        }

        Ok((written, None))
    }

    /// Collects the entire response body into a `BytesMut` buffer, returning the collected data and
    /// any trailing headers.
    ///
    /// Returns Ok((data, trailers)) if the body was successfully collected, where `data` is a
    /// `BytesMut` buffer containing the collected data and `trailers` is an optional `HeaderMap`
    /// containing any trailing headers.
    ///
    /// Returns an error if the body exceeds the given `max_size` or the underlying connection
    /// encounters an error.
    pub async fn bytes(
        self,
        max_size: Option<usize>,
    ) -> Result<(BytesMut, Option<HeaderMap>), CollectError> {
        let mut buffer = BytesMut::new();
        let (_written, trailers) = self.collect_to_buf(&mut buffer, max_size).await?;
        Ok((buffer, trailers))
    }

    /// Collects the entire response body into a `String`, returning the collected data and any
    /// trailing headers.
    ///
    /// Returns Ok((data, trailers)) if the body was successfully collected, where `data` is a
    /// `String` containing the collected data and `trailers` is an optional `HeaderMap` containing
    /// any trailing headers.
    ///
    /// Returns an error if the body exceeds the given `max_size`, the underlying connection
    /// encounters an error, or if the collected data is not valid UTF-8.
    pub async fn text(
        self,
        max_size: Option<usize>,
    ) -> Result<(String, Option<HeaderMap>), CollectToStringError> {
        let (bytes, trailers) = match self.bytes(max_size).await {
            Ok(result) => result,
            Err(e) => {
                match e {
                    CollectError::H3(h3_err) => return Err(CollectToStringError::H3(h3_err)),
                    CollectError::TooLarge => return Err(CollectToStringError::TooLarge),
                    CollectError::BufferTooSmall => {
                        unreachable!(
                            "BytesMut grows automatically, cannot be BufferTooSmall in collect_to_bytes"
                        )
                    }
                }
            }
        };
        let string = String::from_utf8(bytes.into())?;

        Ok((string, trailers))
    }
}

/// An error that can occur while collecting the response body into a buffer.
#[derive(Debug, thiserror::Error)]
pub enum CollectError {
    /// An error that occurred while reading the response body.
    #[error(transparent)]
    H3(#[from] H3Error),
    /// The provided buffer was too small to hold the response body.
    #[error("the provided buffer was too small to hold the response body")]
    BufferTooSmall,
    /// The response body exceeded the maximum size specified.
    #[error("response body exceeded maximum size")]
    TooLarge,
}

/// An error that can occur while collecting the response body into a string.
#[derive(Debug, thiserror::Error)]
pub enum CollectToStringError {
    /// An error that occurred while reading the response body.
    #[error(transparent)]
    H3(#[from] H3Error),
    /// The response body exceeded the maximum size specified.
    #[error("response body exceeded maximum size")]
    TooLarge,
    /// The response body did not contain valid UTF-8.
    #[error("response body did not contain valid UTF-8")]
    Utf8(#[from] std::string::FromUtf8Error),
}

impl Body for H3ResponseBody {
    type Data = Bytes;
    type Error = H3Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        let this = self.get_mut();
        if this.read_guard.is_done() {
            return Poll::Ready(None);
        }
        let res = poll_read_frame(&this.handle, this.stream_id, cx);
        if let Poll::Ready(ready) = &res {
            match ready {
                // EOF, an error, or a trailing header section all terminate the
                // body: the stream is finished on the read side, so suppress the
                // STOP_SENDING-on-drop.
                None => this.read_guard.mark_done(),
                Some(Err(_)) => this.read_guard.mark_done(),
                Some(Ok(frame)) if frame.is_trailers() => this.read_guard.mark_done(),
                Some(Ok(_)) => {}
            }
        }
        res
    }
}

/// The read-side lifetime guard for a client stream. While held it keeps the
/// shared [`StreamRef`] alive; dropped before the read side has finished cleanly
/// (EOF, trailers, or an error already observed) it signals `STOP_SENDING`.
pub(crate) struct ReadGuard {
    stream_ref: Arc<StreamRef>,
    done: bool,
}

impl ReadGuard {
    pub(crate) fn new(stream_ref: Arc<StreamRef>) -> Self {
        Self {
            stream_ref,
            done: false,
        }
    }

    /// A weak handle to the owning connection.
    pub(crate) fn handle(&self) -> WeakConnectionHandle<Http3ClientApp> {
        self.stream_ref.handle()
    }

    /// The stream this guard governs.
    pub(crate) fn stream_id(&self) -> u64 {
        self.stream_ref.stream_id()
    }

    /// Whether the read side has reached a terminal state.
    pub(crate) fn is_done(&self) -> bool {
        self.done
    }

    /// Marks the read side finished cleanly, suppressing `STOP_SENDING` on drop.
    pub(crate) fn mark_done(&mut self) {
        self.done = true;
    }
}

impl Drop for ReadGuard {
    fn drop(&mut self) {
        if !self.done {
            shutdown_read(&self.stream_ref.handle(), self.stream_ref.stream_id());
        }
    }
}

/// Shuts down the *read* side of `stream_id`: discards any unread body and, if
/// the read side has not already finished, signals `STOP_SENDING`. Touches only
/// the QUIC stream, not the per-stream bookkeeping maps (those are released by
/// [`StreamRef`] once the last reference drops).
fn shutdown_read(handle: &WeakConnectionHandle<Http3ClientApp>, stream_id: u64) {
    let Some(handle) = handle.upgrade() else {
        return;
    };
    let mut guard = handle.lock_recovering();
    let _ = guard
        .inner
        .stream_shutdown(stream_id, squiche::Shutdown::Read, 0);
    drop(guard);
    handle.notify();
}

/// Polls the response-head slot for `stream_id`, registering a waker when the
/// head has not yet arrived. Drives the request [`ResponseFut`]; it performs no
/// stream teardown of its own (the caller's guard owns the read side).
pub(crate) fn poll_head(
    handle: &WeakConnectionHandle<Http3ClientApp>,
    stream_id: u64,
    cx: &mut Context<'_>,
) -> Poll<Result<ResponseHead, RequestError>> {
    let Some(handle) = handle.upgrade() else {
        return Poll::Ready(Err(RequestError::ConnectionClosed));
    };
    let mut guard = handle.lock();
    let QuicScionConn { app, .. } = &mut *guard;
    let Http3ClientApp {
        response_heads,
        streams,
        locally_closed,
        ..
    } = app;
    let Some(slot) = response_heads.get_mut(&stream_id) else {
        return Poll::Ready(Err(RequestError::ConnectionClosed));
    };

    match slot.state {
        ResponseHeadState::Arrived(_) => {
            let ResponseHeadState::Arrived(head) =
                std::mem::replace(&mut slot.state, ResponseHeadState::Done)
            else {
                unreachable!("just matched Arrived")
            };
            Poll::Ready(Ok(head))
        }
        ResponseHeadState::Waiting => {
            // A reset or a connection close before the head means the request
            // faulted.
            match streams.get(&stream_id) {
                Some(st) => {
                    // `locally_closed` implies `Closed` on every open stream (see
                    // the field's docs).
                    match st.read_state {
                        ReadState::Closed if *locally_closed => {
                            return Poll::Ready(Err(RequestError::LocallyClosed));
                        }
                        ReadState::Closed => {
                            return Poll::Ready(Err(RequestError::ConnectionClosed));
                        }
                        ReadState::Reset(code) => {
                            return Poll::Ready(Err(RequestError::Reset(code)));
                        }
                        // The stream is still open, so the head may yet arrive:
                        // fall through and register a waker.
                        ReadState::Streaming | ReadState::Trailers(_) | ReadState::Eof => {}
                    }
                }
                None => {
                    return Poll::Ready(Err(RequestError::ConnectionClosed));
                }
            }
            slot.waker = Some(cx.waker().clone());
            Poll::Pending
        }
        ResponseHeadState::Done => {
            // Polled again after the head was already delivered.
            Poll::Ready(Err(RequestError::ConnectionClosed))
        }
    }
}