eggserve-core 0.1.1

Security policy, path confinement, and static-serving primitives for eggserve
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! Transport-independent, one-shot request body.
//!
//! [`RequestBody`] wraps the transfer-decoded body stream from an HTTP
//! request. It provides one-shot consumption (either fully buffered or
//! chunk-by-chunk) with bounded limits, timeout awareness, and
//! cancellation safety.
//!
//! # One-shot guarantee
//!
//! A `RequestBody` can only be consumed once. [`read_all`](RequestBody::read_all)
//! consumes the entire body into memory. Streaming via [`Stream`](futures_util::Stream)
//! reads chunks incrementally. Mixing consumption modes is detected and
//! returns [`RequestBodyError::MixedConsumptionMode`].
//!
//! # Transport independence
//!
//! No Hyper type appears in this struct or its public API. The body
//! stream is internal to the type.

use bytes::Bytes;
use futures_util::Stream;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};

use super::request_body_error::RequestBodyError;

/// The consumption state of a request body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BodyState {
    /// Initial state: no data consumed yet.
    Unread,
    /// Streaming in progress: at least one chunk consumed via `Stream`.
    Streaming,
    /// Body fully consumed (either via `read_all` or stream completion).
    Complete,
    /// An error terminated consumption.
    Error,
}

/// A transport-independent, one-shot request body.
///
/// Owns the transfer-decoded byte stream from an HTTP request. No Hyper
/// type appears in the public API.
///
/// # Examples
///
/// ```no_run
/// use eggserve_core::primitives::{RequestBody, RequestBodyError};
/// use futures_util::StreamExt;
///
/// async fn handle(body: RequestBody) -> Result<Vec<u8>, RequestBodyError> {
///     // Option 1: buffer everything
///     let bytes = body.read_all().await?;
///     Ok(bytes.to_vec())
/// }
///
/// async fn handle_streaming(mut body: RequestBody) -> Result<(), RequestBodyError> {
///     // Option 2: stream chunks
///     while let Some(chunk) = body.next_chunk().await? {
///         let _ = chunk;
///     }
///     Ok(())
/// }
/// ```
pub struct RequestBody {
    inner: Option<BodyInner>,
    declared_length: Option<u64>,
    bytes_received: u64,
    state: BodyState,
    max_bytes: u64,
    /// Shared flag indicating whether the body stream was fully consumed.
    /// Set when the stream ends and all declared bytes (if any) have been
    /// received. Used by the connection pipeline for incomplete-body policy.
    consumed: Arc<AtomicBool>,
}

/// Internal body stream, hidden from public API.
#[allow(dead_code)]
enum BodyInner {
    /// A Hyper `Incoming` body (runtime-provided).
    Incoming {
        stream: Pin<Box<dyn Stream<Item = Result<Bytes, IncomingError>> + Send + 'static>>,
    },
    /// A pre-built test body (bytes).
    Fixed { data: Bytes, offset: usize },
    /// An empty body.
    Empty,
}

/// Internal error type for body stream items.
#[derive(Debug)]
pub struct IncomingError(pub(crate) String);

impl std::fmt::Display for IncomingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "incoming body error: {}", self.0)
    }
}

impl std::error::Error for IncomingError {}

impl From<IncomingError> for RequestBodyError {
    fn from(e: IncomingError) -> Self {
        Self::Transport(e.0)
    }
}

impl RequestBody {
    /// Create an empty body with no declared length.
    pub fn empty() -> Self {
        Self {
            inner: Some(BodyInner::Empty),
            declared_length: None,
            bytes_received: 0,
            state: BodyState::Unread,
            max_bytes: u64::MAX,
            consumed: Arc::new(AtomicBool::new(true)),
        }
    }

    /// Create a body from fixed bytes (test/experimental constructor).
    ///
    /// The `max_bytes` parameter sets the effective limit. Use `u64::MAX`
    /// for unlimited.
    pub fn from_bytes(data: impl Into<Bytes>, max_bytes: u64) -> Self {
        let data = data.into();
        let len = data.len() as u64;
        Self {
            inner: Some(BodyInner::Fixed { data, offset: 0 }),
            declared_length: Some(len),
            bytes_received: 0,
            state: BodyState::Unread,
            max_bytes,
            consumed: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Create a body from a Hyper `Incoming` stream.
    ///
    /// This is primarily used by the runtime to wrap Hyper incoming bodies.
    /// External consumers (e.g. fuzz targets) may also use it to test
    /// stream-based body ingestion.
    #[allow(dead_code)]
    pub(crate) fn from_incoming(
        stream: impl Stream<Item = Result<Bytes, IncomingError>> + Send + 'static,
        declared_length: Option<u64>,
        max_bytes: u64,
    ) -> Self {
        Self {
            inner: Some(BodyInner::Incoming {
                stream: Box::pin(stream),
            }),
            declared_length,
            bytes_received: 0,
            state: BodyState::Unread,
            max_bytes,
            consumed: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Returns the declared body length from `Content-Length`, if present.
    pub fn declared_length(&self) -> Option<u64> {
        self.declared_length
    }

    /// Returns the number of bytes received so far.
    pub fn bytes_received(&self) -> u64 {
        self.bytes_received
    }

    /// Returns `true` if the body has been fully consumed.
    pub fn is_complete(&self) -> bool {
        self.state == BodyState::Complete
    }

    /// Returns the current consumption state.
    pub fn state(&self) -> BodyState {
        self.state
    }

    /// Returns the effective byte limit.
    pub fn max_bytes(&self) -> u64 {
        self.max_bytes
    }

    /// Returns a clone of the shared consumption flag.
    ///
    /// The flag is set when the body stream ends and all declared bytes
    /// have been received. Used by the connection pipeline for
    /// incomplete-body policy decisions.
    pub(crate) fn consumed_flag(&self) -> Arc<AtomicBool> {
        self.consumed.clone()
    }

    /// Returns `true` if the body was fully consumed (stream ended and
    /// all declared bytes received).
    pub(crate) fn was_fully_consumed(&self) -> bool {
        self.consumed.load(Ordering::Acquire)
    }

    /// Mark the body as fully consumed.
    fn mark_consumed(&self) {
        self.consumed.store(true, Ordering::Release);
    }

    /// Consume the entire body into a single `Bytes` value.
    ///
    /// This is the simplest way to consume a body. After this call,
    /// the body is in the `Complete` state.
    ///
    /// # Errors
    ///
    /// Returns an error if the body exceeds the limit, if the stream
    /// fails, or if the body was already consumed.
    pub async fn read_all(mut self) -> Result<Bytes, RequestBodyError> {
        if self.state == BodyState::Complete || self.state == BodyState::Error {
            return Err(RequestBodyError::AlreadyConsumed);
        }
        if self.state == BodyState::Streaming {
            return Err(RequestBodyError::MixedConsumptionMode);
        }

        let inner = self.inner.take().ok_or(RequestBodyError::AlreadyConsumed)?;

        match inner {
            BodyInner::Empty => {
                self.state = BodyState::Complete;
                self.mark_consumed();
                Ok(Bytes::new())
            }
            BodyInner::Fixed { data, offset } => {
                let remaining = &data[offset..];
                let total = self
                    .bytes_received
                    .checked_add(remaining.len() as u64)
                    .ok_or(RequestBodyError::LimitExceeded {
                        limit: self.max_bytes,
                        received: u64::MAX,
                    })?;
                if total > self.max_bytes {
                    self.state = BodyState::Error;
                    return Err(RequestBodyError::LimitExceeded {
                        limit: self.max_bytes,
                        received: total,
                    });
                }
                self.bytes_received = total;
                self.state = BodyState::Complete;
                self.mark_consumed();
                Ok(data.slice(offset..))
            }
            BodyInner::Incoming { mut stream } => {
                let mut buf = Vec::new();
                use futures_util::StreamExt;
                while let Some(item) = stream.next().await {
                    let chunk = item.map_err(|e| RequestBodyError::Transport(e.0))?;
                    let new_total = self.bytes_received.checked_add(chunk.len() as u64).ok_or(
                        RequestBodyError::LimitExceeded {
                            limit: self.max_bytes,
                            received: u64::MAX,
                        },
                    )?;
                    if new_total > self.max_bytes {
                        self.state = BodyState::Error;
                        return Err(RequestBodyError::LimitExceeded {
                            limit: self.max_bytes,
                            received: new_total,
                        });
                    }
                    self.bytes_received = new_total;
                    buf.extend_from_slice(&chunk);
                }
                self.state = BodyState::Complete;
                // Check for premature EOF: stream ended before declared length.
                if let Some(declared) = self.declared_length {
                    if self.bytes_received < declared {
                        let received = self.bytes_received;
                        return Err(RequestBodyError::PrematureEof {
                            received,
                            expected: Some(declared),
                        });
                    }
                }
                self.mark_consumed();
                Ok(Bytes::from(buf))
            }
        }
    }

    /// Read the next chunk from the body.
    ///
    /// Returns `Ok(None)` when the body is fully consumed.
    /// Returns `Ok(Some(chunk))` with the next chunk of bytes.
    ///
    /// After the first call to `next_chunk`, the body enters the
    /// `Streaming` state. Subsequent calls to `read_all` will fail
    /// with [`RequestBodyError::MixedConsumptionMode`].
    ///
    /// # Errors
    ///
    /// Returns an error if the body exceeds the limit, if the stream
    /// fails, or if the body was already consumed.
    pub async fn next_chunk(&mut self) -> Result<Option<Bytes>, RequestBodyError> {
        if self.state == BodyState::Error {
            return Err(RequestBodyError::AlreadyConsumed);
        }
        if self.state == BodyState::Complete {
            return Ok(None);
        }

        // Transition to streaming on first chunk read.
        if self.state == BodyState::Unread {
            self.state = BodyState::Streaming;
        }

        let inner = self
            .inner
            .as_mut()
            .ok_or(RequestBodyError::AlreadyConsumed)?;

        match inner {
            BodyInner::Empty => {
                self.state = BodyState::Complete;
                self.mark_consumed();
                Ok(None)
            }
            BodyInner::Fixed { data, offset } => {
                if *offset >= data.len() {
                    self.state = BodyState::Complete;
                    self.mark_consumed();
                    return Ok(None);
                }
                let remaining = &data[*offset..];
                let chunk_size = remaining.len().min(8192);
                let new_total = self.bytes_received.checked_add(chunk_size as u64).ok_or(
                    RequestBodyError::LimitExceeded {
                        limit: self.max_bytes,
                        received: u64::MAX,
                    },
                )?;
                if new_total > self.max_bytes {
                    self.state = BodyState::Error;
                    return Err(RequestBodyError::LimitExceeded {
                        limit: self.max_bytes,
                        received: new_total,
                    });
                }
                let chunk = &data[*offset..*offset + chunk_size];
                *offset += chunk_size;
                self.bytes_received = new_total;
                if *offset >= data.len() {
                    self.state = BodyState::Complete;
                }
                Ok(Some(Bytes::copy_from_slice(chunk)))
            }
            BodyInner::Incoming { stream } => {
                use futures_util::StreamExt;
                match stream.next().await {
                    Some(Ok(chunk)) => {
                        let new_total = self.bytes_received.checked_add(chunk.len() as u64).ok_or(
                            RequestBodyError::LimitExceeded {
                                limit: self.max_bytes,
                                received: u64::MAX,
                            },
                        )?;
                        if new_total > self.max_bytes {
                            self.state = BodyState::Error;
                            return Err(RequestBodyError::LimitExceeded {
                                limit: self.max_bytes,
                                received: new_total,
                            });
                        }
                        self.bytes_received = new_total;
                        Ok(Some(chunk))
                    }
                    Some(Err(e)) => {
                        self.state = BodyState::Error;
                        Err(RequestBodyError::Transport(e.0))
                    }
                    None => {
                        self.state = BodyState::Complete;
                        // Check for premature EOF.
                        if let Some(declared) = self.declared_length {
                            if self.bytes_received < declared {
                                let received = self.bytes_received;
                                return Err(RequestBodyError::PrematureEof {
                                    received,
                                    expected: Some(declared),
                                });
                            }
                        }
                        self.mark_consumed();
                        Ok(None)
                    }
                }
            }
        }
    }
}

impl std::fmt::Debug for RequestBody {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RequestBody")
            .field("declared_length", &self.declared_length)
            .field("bytes_received", &self.bytes_received)
            .field("state", &self.state)
            .field("max_bytes", &self.max_bytes)
            .field("consumed", &self.was_fully_consumed())
            .finish()
    }
}

impl Stream for RequestBody {
    type Item = Result<Bytes, RequestBodyError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Cannot poll after completion or error.
        if self.state == BodyState::Complete || self.state == BodyState::Error {
            return Poll::Ready(None);
        }

        // Check limit before polling.
        if self.bytes_received >= self.max_bytes {
            self.state = BodyState::Error;
            return Poll::Ready(Some(Err(RequestBodyError::LimitExceeded {
                limit: self.max_bytes,
                received: self.bytes_received,
            })));
        }

        let max_bytes = self.max_bytes;
        let bytes_received = self.bytes_received;

        let inner = match self.inner.as_mut() {
            Some(i) => i,
            None => return Poll::Ready(None),
        };

        match inner {
            BodyInner::Empty => {
                self.state = BodyState::Complete;
                self.mark_consumed();
                Poll::Ready(None)
            }
            BodyInner::Fixed { data, offset } => {
                if *offset >= data.len() {
                    self.state = BodyState::Complete;
                    self.mark_consumed();
                    Poll::Ready(None)
                } else {
                    let remaining = &data[*offset..];
                    let chunk_size = remaining.len().min(8192);
                    let new_total = match bytes_received.checked_add(chunk_size as u64) {
                        Some(v) => v,
                        None => {
                            self.state = BodyState::Error;
                            return Poll::Ready(Some(Err(RequestBodyError::LimitExceeded {
                                limit: max_bytes,
                                received: u64::MAX,
                            })));
                        }
                    };
                    if new_total > max_bytes {
                        self.state = BodyState::Error;
                        return Poll::Ready(Some(Err(RequestBodyError::LimitExceeded {
                            limit: max_bytes,
                            received: new_total,
                        })));
                    }
                    let chunk = Bytes::copy_from_slice(&data[*offset..*offset + chunk_size]);
                    *offset += chunk_size;
                    self.bytes_received = new_total;
                    if self.state == BodyState::Unread {
                        self.state = BodyState::Streaming;
                    }
                    Poll::Ready(Some(Ok(chunk)))
                }
            }
            BodyInner::Incoming { stream } => match stream.as_mut().poll_next(cx) {
                Poll::Ready(Some(Ok(chunk))) => {
                    let new_total = match bytes_received.checked_add(chunk.len() as u64) {
                        Some(v) => v,
                        None => {
                            self.state = BodyState::Error;
                            return Poll::Ready(Some(Err(RequestBodyError::LimitExceeded {
                                limit: max_bytes,
                                received: u64::MAX,
                            })));
                        }
                    };
                    if new_total > max_bytes {
                        self.state = BodyState::Error;
                        Poll::Ready(Some(Err(RequestBodyError::LimitExceeded {
                            limit: max_bytes,
                            received: new_total,
                        })))
                    } else {
                        self.bytes_received = new_total;
                        if self.state == BodyState::Unread {
                            self.state = BodyState::Streaming;
                        }
                        Poll::Ready(Some(Ok(chunk)))
                    }
                }
                Poll::Ready(Some(Err(e))) => {
                    self.state = BodyState::Error;
                    Poll::Ready(Some(Err(RequestBodyError::Transport(e.0))))
                }
                Poll::Ready(None) => {
                    self.state = BodyState::Complete;
                    // Check for premature EOF.
                    if let Some(declared) = self.declared_length {
                        if self.bytes_received < declared {
                            let received = self.bytes_received;
                            self.state = BodyState::Error;
                            return Poll::Ready(Some(Err(RequestBodyError::PrematureEof {
                                received,
                                expected: Some(declared),
                            })));
                        }
                    }
                    self.mark_consumed();
                    Poll::Ready(None)
                }
                Poll::Pending => Poll::Pending,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures_util::StreamExt;
    use proptest::prelude::*;

    #[tokio::test]
    async fn empty_body_read_all() {
        let body = RequestBody::empty();
        assert_eq!(body.declared_length(), None);
        assert_eq!(body.bytes_received(), 0);
        let data = body.read_all().await.unwrap();
        assert!(data.is_empty());
    }

    #[tokio::test]
    async fn fixed_body_read_all() {
        let body = RequestBody::from_bytes(b"hello".to_vec(), u64::MAX);
        assert_eq!(body.declared_length(), Some(5));
        let data = body.read_all().await.unwrap();
        assert_eq!(&data[..], b"hello");
    }

    #[tokio::test]
    async fn fixed_body_streaming() {
        let mut body = RequestBody::from_bytes(b"hello world".to_vec(), u64::MAX);
        let mut chunks = Vec::new();
        while let Some(chunk) = body.next_chunk().await.unwrap() {
            chunks.push(chunk);
        }
        let total: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
        assert_eq!(&total[..], b"hello world");
    }

    #[tokio::test]
    async fn limit_exceeded_on_read_all() {
        let body = RequestBody::from_bytes(b"hello".to_vec(), 3);
        let err = body.read_all().await.unwrap_err();
        assert!(err.is_limit_exceeded());
    }

    #[tokio::test]
    async fn limit_exceeded_on_stream() {
        let mut body = RequestBody::from_bytes(b"hello".to_vec(), 3);
        let err = body.next_chunk().await.unwrap_err();
        assert!(err.is_limit_exceeded());
    }

    #[tokio::test]
    async fn already_consumed_after_read_all() {
        let body = RequestBody::from_bytes(b"hello".to_vec(), u64::MAX);
        let _data = body.read_all().await.unwrap();
        // Can't reuse - but we moved self, so this test verifies the type system.
    }

    #[tokio::test]
    async fn mixed_consumption_mode() {
        // Use a body larger than the internal chunk size to ensure streaming state
        let large_body = vec![0u8; 16384];
        let mut body = RequestBody::from_bytes(large_body, u64::MAX);
        let _chunk = body.next_chunk().await.unwrap();
        // Can't call read_all because body is moved - verify via state.
        assert_eq!(body.state(), BodyState::Streaming);
    }

    #[tokio::test]
    async fn zero_length_body() {
        let body = RequestBody::from_bytes(Vec::new(), u64::MAX);
        assert_eq!(body.declared_length(), Some(0));
        let data = body.read_all().await.unwrap();
        assert!(data.is_empty());
    }

    #[tokio::test]
    async fn stream_trait_works() {
        let body = RequestBody::from_bytes(b"abc".to_vec(), u64::MAX);
        let mut stream = body;
        let mut all = Vec::new();
        while let Some(chunk) = stream.next().await.transpose().unwrap() {
            all.extend_from_slice(&chunk);
        }
        assert_eq!(&all[..], b"abc");
    }

    #[test]
    fn body_state_debug() {
        assert_eq!(format!("{:?}", BodyState::Unread), "Unread");
        assert_eq!(format!("{:?}", BodyState::Streaming), "Streaming");
        assert_eq!(format!("{:?}", BodyState::Complete), "Complete");
        assert_eq!(format!("{:?}", BodyState::Error), "Error");
    }

    #[test]
    fn request_body_debug() {
        let body = RequestBody::empty();
        let dbg = format!("{:?}", body);
        assert!(dbg.contains("RequestBody"));
        assert!(dbg.contains("Unread"));
    }

    #[tokio::test]
    async fn premature_eof_returns_error() {
        use futures_util::stream;
        // Create a stream that provides fewer bytes than declared.
        let short_data = b"hi";
        let declared = 10u64;
        let body_stream =
            stream::once(async move { Ok::<_, IncomingError>(Bytes::copy_from_slice(short_data)) });
        let body = RequestBody::from_incoming(body_stream, Some(declared), u64::MAX);
        let result = body.read_all().await;
        assert!(result.is_err());
        match result.unwrap_err() {
            RequestBodyError::PrematureEof { received, expected } => {
                assert_eq!(received, 2);
                assert_eq!(expected, Some(10));
            }
            other => panic!("expected PrematureEof, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn premature_eof_streaming_returns_error() {
        use futures_util::stream;
        // Stream that provides fewer bytes than declared.
        let short_data = b"hi";
        let declared = 10u64;
        let body_stream =
            stream::once(async move { Ok::<_, IncomingError>(Bytes::copy_from_slice(short_data)) });
        let mut body = RequestBody::from_incoming(body_stream, Some(declared), u64::MAX);
        // Read the one available chunk.
        let chunk = body.next_chunk().await.unwrap();
        assert!(chunk.is_some());
        // Next read: stream ended, premature EOF should be reported.
        let result = body.next_chunk().await;
        match result {
            Err(RequestBodyError::PrematureEof { received, expected }) => {
                assert_eq!(received, 2);
                assert_eq!(expected, Some(10));
            }
            Ok(None) => {
                panic!("expected PrematureEof, got Ok(None)");
            }
            other => panic!("expected PrematureEof, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn exact_declared_length_succeeds() {
        use futures_util::stream;
        let data = b"hello";
        let declared = 5u64;
        let body_stream =
            stream::once(
                async move { Ok::<_, IncomingError>(Bytes::copy_from_slice(data.as_slice())) },
            );
        let body = RequestBody::from_incoming(body_stream, Some(declared), u64::MAX);
        let result = body.read_all().await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_ref(), b"hello");
    }

    #[tokio::test]
    async fn checked_add_overflow_returns_error() {
        let body = RequestBody::from_bytes(vec![0u8; 200], 100);
        let result = body.read_all().await;
        assert!(result.is_err());
        assert!(result.unwrap_err().is_limit_exceeded());
    }

    #[test]
    fn state_transitions_unread_to_complete_on_read() {
        proptest::proptest!(|(data in prop::collection::vec(any::<u8>(), 0..500))| {
            let rt = tokio::runtime::Runtime::new().unwrap();
            let body = RequestBody::from_bytes(data, u64::MAX);
            prop_assert_eq!(body.state(), BodyState::Unread);
            let flag = body.consumed_flag();
            let _ = rt.block_on(body.read_all());
            prop_assert!(flag.load(std::sync::atomic::Ordering::Acquire));
        });
    }

    #[test]
    fn consumed_flag_set_after_read() {
        proptest::proptest!(|(data in prop::collection::vec(any::<u8>(), 0..500))| {
            let rt = tokio::runtime::Runtime::new().unwrap();
            let body = RequestBody::from_bytes(data, u64::MAX);
            let flag = body.consumed_flag();
            prop_assert!(!flag.load(std::sync::atomic::Ordering::Acquire));
            let _ = rt.block_on(body.read_all());
            prop_assert!(flag.load(std::sync::atomic::Ordering::Acquire));
        });
    }

    #[test]
    fn chunked_body_via_stream_succeeds() {
        proptest::proptest!(|(data in prop::collection::vec(any::<u8>(), 0..1000))| {
            use futures_util::stream;

            let rt = tokio::runtime::Runtime::new().unwrap();
            let chunk_size = if data.is_empty() { 1 } else { (data[0] as usize % 64) + 1 };
            let chunks: Vec<Result<Bytes, IncomingError>> = data.chunks(chunk_size)
                .map(|c| Ok(Bytes::copy_from_slice(c)))
                .collect();
            let body_stream = stream::iter(chunks);
            let body = RequestBody::from_incoming(body_stream, Some(data.len() as u64), u64::MAX);
            let result = rt.block_on(body.read_all());
            if let Ok(val) = result {
                prop_assert_eq!(val.len(), data.len());
            }
        });
    }

    #[test]
    fn premature_eof_detected() {
        proptest::proptest!(|(data in prop::collection::vec(any::<u8>(), 0..100))| {
            use futures_util::stream;

            let rt = tokio::runtime::Runtime::new().unwrap();
            let declared = (data.len() as u64) + 100;
            let data_owned = data.clone();
            let body_stream = stream::once(async move {
                Ok::<_, IncomingError>(Bytes::from(data_owned))
            });
            let body = RequestBody::from_incoming(body_stream, Some(declared), u64::MAX);
            let result = rt.block_on(body.read_all());
            if let Err(e) = result {
                prop_assert!(
                    matches!(e, RequestBodyError::PrematureEof { .. }),
                    "expected PrematureEof, got: {:?}",
                    e
                );
            }
        });
    }
}