rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
//! Body size limits shared by the imposter listeners, the proxy's upstream read and the admin API
//! (issue #1439).
//!
//! Mountebank has no body cap of its own and serves bodies up to V8's string limit (about
//! 512 MiB), past which it crashes. Rift refuses nothing Mountebank can serve: the default
//! per-body ceiling is that same 512 MiB, and a body over it gets a clean `413` instead of a dead
//! process. The ceiling bounds one body; a shared in-flight **memory budget** bounds them all
//! together, so concurrent large uploads get `503` + `Retry-After` rather than exhausting memory.
//! Both are configurable, so an embedder (rift-cluster) can tighten them for its pod sizes.

use bytes::{Bytes, BytesMut};
use http_body_util::BodyExt;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};

/// Default per-body ceiling: Mountebank's own breaking point, so no body it serves is refused.
pub const DEFAULT_MAX_BODY_BYTES: usize = 512 * 1024 * 1024;

/// Default in-flight memory budget shared by every body a manager is buffering.
pub const DEFAULT_BODY_MEMORY_BUDGET: usize = 1024 * 1024 * 1024;

/// How long a request body with a known length waits for the budget before it is refused. The wait
/// is bounded, so bodies waiting on each other's shares cannot deadlock: the worst case is a `503`
/// after this long.
const RESERVE_WAIT: Duration = Duration::from_secs(2);

/// The budget is counted in KiB: `Semaphore::acquire_many` takes a `u32`, and bytes would cap a
/// single reservation at 4 GiB.
const UNIT: usize = 1024;

/// Validated body limits: a per-body ceiling and an optional shared budget at least as large.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BodyLimits {
    max_body_bytes: NonZeroUsize,
    budget_bytes: Option<NonZeroUsize>,
}

/// Why a [`BodyLimits`] configuration is refused.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum BodyLimitsError {
    #[error("the maximum body size must be at least 1 byte")]
    ZeroCeiling,
    #[error(
        "the body memory budget ({budget} bytes) is smaller than the maximum body size ({max} \
         bytes), so a body under the ceiling could never be admitted"
    )]
    BudgetBelowCeiling { budget: usize, max: usize },
}

impl BodyLimits {
    /// `budget_bytes` of `None` means unbounded. A budget below the ceiling is refused: a body
    /// under the ceiling could then never be admitted.
    pub fn new(
        max_body_bytes: usize,
        budget_bytes: Option<usize>,
    ) -> Result<Self, BodyLimitsError> {
        let max = NonZeroUsize::new(max_body_bytes).ok_or(BodyLimitsError::ZeroCeiling)?;
        let budget = match budget_bytes {
            None => None,
            Some(budget) if budget < max_body_bytes => {
                return Err(BodyLimitsError::BudgetBelowCeiling {
                    budget,
                    max: max_body_bytes,
                });
            }
            Some(budget) => NonZeroUsize::new(budget),
        };
        Ok(Self {
            max_body_bytes: max,
            budget_bytes: budget,
        })
    }

    #[must_use]
    pub fn max_body_bytes(&self) -> usize {
        self.max_body_bytes.get()
    }

    #[must_use]
    pub fn budget_bytes(&self) -> Option<usize> {
        self.budget_bytes.map(NonZeroUsize::get)
    }
}

impl Default for BodyLimits {
    fn default() -> Self {
        Self::new(DEFAULT_MAX_BODY_BYTES, Some(DEFAULT_BODY_MEMORY_BUDGET))
            .expect("the defaults satisfy budget >= ceiling")
    }
}

/// Why a body was not collected.
#[derive(Debug, thiserror::Error)]
pub enum BodyReadError {
    /// The body is over the per-body ceiling: a `413`.
    #[error("body exceeds the maximum size of {limit} bytes")]
    TooLarge { limit: usize },
    /// The shared budget cannot hold the body right now: a `503` with `Retry-After`.
    #[error("not enough body memory is free to accept this body; retry shortly")]
    BudgetExhausted,
    /// The body could not be read to completion: a client transmission failure (#694).
    #[error("failed to read body: {0}")]
    Read(Box<dyn std::error::Error + Send + Sync>),
}

/// A collected body and its share of the budget, which is released when this is dropped. Keep it
/// alive until the response is built: the bytes stay in memory throughout matching.
#[derive(Debug)]
pub struct CollectedBody {
    bytes: Bytes,
    _reservation: Option<OwnedSemaphorePermit>,
}

impl CollectedBody {
    #[must_use]
    pub fn bytes(&self) -> &Bytes {
        &self.bytes
    }
}

impl std::ops::Deref for CollectedBody {
    type Target = Bytes;

    fn deref(&self) -> &Bytes {
        &self.bytes
    }
}

/// A share of the budget held by something other than a body ([`BodyGuard::try_reserve`]),
/// released when this is dropped.
#[derive(Debug)]
#[must_use]
pub struct BudgetShare {
    _permit: Option<OwnedSemaphorePermit>,
}

/// The limits plus the budget they share. One per [`ImposterManager`](super::ImposterManager), so
/// every listener, the proxy and the admin API draw on the same budget.
#[derive(Debug)]
pub struct BodyGuard {
    limits: BodyLimits,
    budget: Option<Arc<Semaphore>>,
}

impl BodyGuard {
    #[must_use]
    pub fn new(limits: BodyLimits) -> Self {
        let budget = limits
            .budget_bytes()
            .map(|bytes| Arc::new(Semaphore::new(units(bytes) as usize)));
        Self { limits, budget }
    }

    #[must_use]
    pub fn limits(&self) -> BodyLimits {
        self.limits
    }

    /// Start collecting a request body of `content_length` bytes, if known.
    ///
    /// A known length over the ceiling is refused before a byte is read, so with
    /// `Expect: 100-continue` the client is never told to send it. A known length under it reserves
    /// its whole share up front, waiting up to [`RESERVE_WAIT`] for the budget. An unknown length
    /// (chunked) reserves as it goes and never waits.
    pub async fn start(&self, content_length: Option<u64>) -> Result<BodyCollector, BodyReadError> {
        let (mut collector, length) = self.open(content_length)?;
        if let (Some(budget), Some(length)) = (&self.budget, length) {
            let wanted = units(length);
            if wanted > 0 {
                // The semaphore is never closed, so an `Err` from it cannot happen; it is treated
                // as exhaustion rather than unwrapped.
                let permit = tokio::time::timeout(
                    RESERVE_WAIT,
                    Arc::clone(budget).acquire_many_owned(wanted),
                )
                .await
                .ok()
                .and_then(Result::ok)
                .ok_or(BodyReadError::BudgetExhausted)?;
                collector.hold(permit, wanted);
            }
        }
        collector.reserve_capacity(length);
        Ok(collector)
    }

    /// [`Self::start`] without waiting for the budget, for a body read while another is already
    /// held — the proxy's upstream response, read while the request's body holds its share. Waiting
    /// there would make concurrent proxied requests time out on each other.
    pub async fn start_without_waiting(
        &self,
        content_length: Option<u64>,
    ) -> Result<BodyCollector, BodyReadError> {
        self.try_start(content_length)
    }

    /// [`Self::start_without_waiting`] for a caller that cannot await: a `lookup` CSV file read on
    /// a blocking thread while the request's body holds its share (issue #1654).
    pub fn try_start(&self, content_length: Option<u64>) -> Result<BodyCollector, BodyReadError> {
        let (mut collector, length) = self.open(content_length)?;
        if let (Some(budget), Some(length)) = (&self.budget, length) {
            let wanted = units(length);
            if wanted > 0 {
                let permit = Arc::clone(budget)
                    .try_acquire_many_owned(wanted)
                    .map_err(|_| BodyReadError::BudgetExhausted)?;
                collector.hold(permit, wanted);
            }
        }
        collector.reserve_capacity(length);
        Ok(collector)
    }

    /// Hold `bytes` of the budget without waiting, for memory that is not a body: a `lookup` CSV
    /// parse the cache does not keep, held until its row is served (issue #1654). More than the
    /// whole budget can never be held, so it is [`BodyReadError::TooLarge`] rather than
    /// exhaustion; without a budget nothing is held.
    ///
    /// # Errors
    /// [`BodyReadError::TooLarge`] past the whole budget, [`BodyReadError::BudgetExhausted`] when
    /// it cannot be held now.
    pub fn try_reserve(&self, bytes: usize) -> Result<BudgetShare, BodyReadError> {
        let Some(budget) = &self.budget else {
            return Ok(BudgetShare { _permit: None });
        };
        let wanted = units(bytes);
        if wanted == 0 {
            return Ok(BudgetShare { _permit: None });
        }
        let total = self.limits.budget_bytes().unwrap_or(usize::MAX);
        if bytes > total {
            return Err(BodyReadError::TooLarge { limit: total });
        }
        Arc::clone(budget)
            .try_acquire_many_owned(wanted)
            .map(|permit| BudgetShare {
                _permit: Some(permit),
            })
            .map_err(|_| BodyReadError::BudgetExhausted)
    }

    /// An empty collector, and the known length checked against the ceiling. A known length over
    /// it is refused here, before anything is reserved or read.
    fn open(
        &self,
        content_length: Option<u64>,
    ) -> Result<(BodyCollector, Option<usize>), BodyReadError> {
        let max = self.limits.max_body_bytes();
        let length = content_length
            .map(|length| {
                usize::try_from(length)
                    .ok()
                    .filter(|length| *length <= max)
                    .ok_or(BodyReadError::TooLarge { limit: max })
            })
            .transpose()?;
        let collector = BodyCollector {
            buf: BytesMut::new(),
            max,
            budget: self.budget.clone(),
            reservation: None,
            reserved_units: 0,
        };
        Ok((collector, length))
    }

    /// Collect a whole `hyper` body under the limits. The length comes from the body's own size
    /// hint, which is exact when the request carried `Content-Length`.
    pub async fn collect<B>(&self, body: B) -> Result<CollectedBody, BodyReadError>
    where
        B: hyper::body::Body<Data = Bytes>,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        let mut collector = self.start(body.size_hint().exact()).await?;
        let mut body = std::pin::pin!(body);
        while let Some(frame) = body.frame().await {
            let frame = frame.map_err(|e| BodyReadError::Read(e.into()))?;
            if let Ok(data) = frame.into_data() {
                collector.push(&data)?;
            }
        }
        Ok(collector.finish())
    }
}

impl Default for BodyGuard {
    fn default() -> Self {
        Self::new(BodyLimits::default())
    }
}

/// A body being collected chunk by chunk under a [`BodyGuard`].
#[derive(Debug)]
pub struct BodyCollector {
    buf: BytesMut,
    max: usize,
    budget: Option<Arc<Semaphore>>,
    reservation: Option<OwnedSemaphorePermit>,
    reserved_units: u32,
}

impl BodyCollector {
    /// Take `permit`, worth `units` of the budget, as this body's up-front share.
    fn hold(&mut self, permit: OwnedSemaphorePermit, units: u32) {
        self.reservation = Some(permit);
        self.reserved_units = units;
    }

    /// Allocate a known length once its share is held, never before: a refused body allocates
    /// nothing.
    fn reserve_capacity(&mut self, length: Option<usize>) {
        if let Some(length) = length {
            self.buf = BytesMut::with_capacity(length);
        }
    }

    /// Append a chunk, refusing it past the ceiling or when the budget cannot cover it now.
    pub fn push(&mut self, chunk: &[u8]) -> Result<(), BodyReadError> {
        let length = self.buf.len() + chunk.len();
        if length > self.max {
            return Err(BodyReadError::TooLarge { limit: self.max });
        }
        if let Some(budget) = &self.budget {
            let wanted = units(length);
            if wanted > self.reserved_units {
                let more = Arc::clone(budget)
                    .try_acquire_many_owned(wanted - self.reserved_units)
                    .map_err(|_| BodyReadError::BudgetExhausted)?;
                match &mut self.reservation {
                    Some(held) => held.merge(more),
                    None => self.reservation = Some(more),
                }
                self.reserved_units = wanted;
            }
        }
        self.buf.extend_from_slice(chunk);
        Ok(())
    }

    #[must_use]
    pub fn finish(self) -> CollectedBody {
        CollectedBody {
            bytes: self.buf.freeze(),
            _reservation: self.reservation,
        }
    }
}

/// `bytes` in budget units, rounded up. Saturates at `u32::MAX` units (4 TiB), past any ceiling a
/// semaphore could grant, so an oversized request is refused rather than wrapped.
fn units(bytes: usize) -> u32 {
    u32::try_from(bytes.div_ceil(UNIT)).unwrap_or(u32::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use http_body_util::Full;
    use std::collections::VecDeque;
    use std::pin::Pin;
    use std::task::{Context, Poll};

    /// A body of the given frames with no known length, as a chunked upload is.
    struct Chunks(VecDeque<Result<Bytes, std::io::Error>>);

    impl hyper::body::Body for Chunks {
        type Data = Bytes;
        type Error = std::io::Error;

        fn poll_frame(
            mut self: Pin<&mut Self>,
            _: &mut Context<'_>,
        ) -> Poll<Option<Result<hyper::body::Frame<Bytes>, std::io::Error>>> {
            Poll::Ready(self.0.pop_front().map(|r| r.map(hyper::body::Frame::data)))
        }
    }

    fn guard(max: usize, budget: Option<usize>) -> BodyGuard {
        BodyGuard::new(BodyLimits::new(max, budget).expect("valid limits"))
    }

    fn chunked(chunks: Vec<&'static [u8]>) -> Chunks {
        Chunks(
            chunks
                .into_iter()
                .map(|c| Ok(Bytes::from_static(c)))
                .collect(),
        )
    }

    #[test]
    fn a_budget_below_the_ceiling_is_refused() {
        assert_eq!(
            BodyLimits::new(2048, Some(1024)),
            Err(BodyLimitsError::BudgetBelowCeiling {
                budget: 1024,
                max: 2048
            })
        );
        assert_eq!(BodyLimits::new(0, None), Err(BodyLimitsError::ZeroCeiling));
        assert!(BodyLimits::new(2048, Some(2048)).is_ok());
        assert!(BodyLimits::new(2048, None).is_ok());
    }

    #[test]
    fn the_defaults_are_mountebanks_ceiling_and_a_one_gib_budget() {
        let limits = BodyLimits::default();
        assert_eq!(limits.max_body_bytes(), 536_870_912);
        assert_eq!(limits.budget_bytes(), Some(1_073_741_824));
    }

    #[tokio::test]
    async fn a_body_at_the_ceiling_is_collected_and_one_byte_over_is_refused() {
        let g = guard(4096, Some(8192));
        let at = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("at the ceiling");
        assert_eq!(at.len(), 4096);
        assert!(matches!(
            g.collect(Full::new(Bytes::from(vec![b'x'; 4097]))).await,
            Err(BodyReadError::TooLarge { limit: 4096 })
        ));
    }

    #[tokio::test]
    async fn a_chunked_body_over_the_ceiling_is_refused() {
        let g = guard(4, None);
        assert!(matches!(
            g.collect(chunked(vec![b"ab", b"cd", b"e"])).await,
            Err(BodyReadError::TooLarge { limit: 4 })
        ));
        let ok = g
            .collect(chunked(vec![b"ab", b"cd"]))
            .await
            .expect("at the ceiling");
        assert_eq!(ok.as_ref(), b"abcd");
    }

    /// A known length over the ceiling is refused before the body is polled.
    #[tokio::test]
    async fn a_known_length_over_the_ceiling_is_refused_up_front() {
        let g = guard(1024, Some(4096));
        assert!(matches!(
            g.start(Some(1025)).await,
            Err(BodyReadError::TooLarge { limit: 1024 })
        ));
    }

    /// The budget is held while a collected body lives, refuses a chunked body that does not fit,
    /// and is free again once the body is dropped.
    #[tokio::test]
    async fn the_budget_is_held_until_the_body_is_dropped() {
        let g = guard(4096, Some(4096));
        let held = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("fits the budget");
        assert!(matches!(
            g.collect(chunked(vec![b"y"])).await,
            Err(BodyReadError::BudgetExhausted)
        ));
        drop(held);
        g.collect(chunked(vec![b"y"]))
            .await
            .expect("the budget is free again");
    }

    /// A known length waits for the budget rather than failing at once, and is refused when the
    /// wait expires.
    #[tokio::test(start_paused = true)]
    async fn a_known_length_waits_for_the_budget_then_is_refused() {
        let g = Arc::new(guard(4096, Some(4096)));
        let held = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("fits the budget");
        let waiter = {
            let g = Arc::clone(&g);
            tokio::spawn(async move { g.start(Some(1024)).await.map(BodyCollector::finish) })
        };
        tokio::time::advance(Duration::from_millis(500)).await;
        drop(held);
        assert!(waiter.await.expect("task").is_ok(), "admitted once freed");

        let _held = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("fits the budget");
        assert!(matches!(
            g.start(Some(1024)).await,
            Err(BodyReadError::BudgetExhausted)
        ));
    }

    #[tokio::test]
    async fn starting_without_waiting_refuses_at_once() {
        let g = guard(4096, Some(4096));
        let _held = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("fits the budget");
        let started = std::time::Instant::now();
        assert!(matches!(
            g.start_without_waiting(Some(1024)).await,
            Err(BodyReadError::BudgetExhausted)
        ));
        assert!(started.elapsed() < Duration::from_millis(500));
    }

    #[tokio::test]
    async fn an_unbounded_budget_never_refuses() {
        let g = guard(4096, None);
        let _a = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("a");
        let _b = g
            .collect(Full::new(Bytes::from(vec![b'x'; 4096])))
            .await
            .expect("b");
    }

    #[tokio::test]
    async fn a_read_failure_is_not_a_size_error() {
        let failing = Chunks(VecDeque::from([Err(std::io::Error::new(
            std::io::ErrorKind::ConnectionReset,
            "reset",
        ))]));
        assert!(matches!(
            guard(4096, None).collect(failing).await,
            Err(BodyReadError::Read(_))
        ));
    }
}