rama-http 0.4.0

rama http layers, services and other utilities
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
//! Asynchronous streaming request and response body capture.
//!
//! [`BodyCaptureLayer`] creates a dedicated [`BodyCaptureSink`] for each
//! selected HTTP message. Each sink receives owned frame copies while the
//! original body continues downstream.

use std::{fmt, future::Future};

use rama_core::{Layer, Service, bytes::Bytes, error::BoxError};
use rama_http_types::{
    Body, BodyCaptureSink, CaptureBody, Request, Response, StreamingBody, body::util::BodyExt as _,
    request, response,
};
use rama_utils::macros::define_inner_service_accessors;

/// The HTTP head associated with a captured body stream.
#[derive(Clone)]
pub enum CapturedHead {
    /// An inbound request head.
    Request(request::Parts),
    /// An outbound response head.
    Response(response::Parts),
}

impl fmt::Debug for CapturedHead {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Request(parts) => f.debug_tuple("Request").field(parts).finish(),
            Self::Response(parts) => f.debug_tuple("Response").field(parts).finish(),
        }
    }
}

/// Creates a dedicated streaming capture sink for an HTTP message.
///
/// Returning `None` skips body capture for that message. The factory future is
/// awaited after the HTTP head is available and before its body starts flowing.
pub trait MakeBodyCaptureSink: Send + Sync + 'static {
    /// The per-message capture sink.
    type Sink: BodyCaptureSink;

    /// Create a capture sink from the request or response head.
    fn make_sink(&self, head: CapturedHead)
    -> impl Future<Output = Option<Self::Sink>> + Send + '_;
}

impl<F, Fut, S> MakeBodyCaptureSink for F
where
    F: Fn(CapturedHead) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Option<S>> + Send + 'static,
    S: BodyCaptureSink,
{
    type Sink = S;

    fn make_sink(
        &self,
        head: CapturedHead,
    ) -> impl Future<Output = Option<Self::Sink>> + Send + '_ {
        self(head)
    }
}

/// Layer that captures request bodies, response bodies, or both while they
/// continue streaming through the service.
#[derive(Clone)]
pub struct BodyCaptureLayer<M> {
    make_sink: M,
    capture_request: bool,
    capture_response: bool,
}

impl<M> BodyCaptureLayer<M> {
    /// Capture request bodies.
    #[must_use]
    pub const fn request(make_sink: M) -> Self {
        Self {
            make_sink,
            capture_request: true,
            capture_response: false,
        }
    }

    /// Capture response bodies.
    #[must_use]
    pub const fn response(make_sink: M) -> Self {
        Self {
            make_sink,
            capture_request: false,
            capture_response: true,
        }
    }

    /// Capture request and response bodies.
    #[must_use]
    pub const fn bidirectional(make_sink: M) -> Self {
        Self {
            make_sink,
            capture_request: true,
            capture_response: true,
        }
    }

    /// Return the capture sink factory.
    #[must_use]
    pub const fn make_sink(&self) -> &M {
        &self.make_sink
    }

    /// Return whether request capture is enabled.
    #[must_use]
    pub const fn captures_requests(&self) -> bool {
        self.capture_request
    }

    /// Return whether response capture is enabled.
    #[must_use]
    pub const fn captures_responses(&self) -> bool {
        self.capture_response
    }
}

impl<M: fmt::Debug> fmt::Debug for BodyCaptureLayer<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BodyCaptureLayer")
            .field("make_sink", &self.make_sink)
            .field("capture_request", &self.capture_request)
            .field("capture_response", &self.capture_response)
            .finish()
    }
}

impl<M, S> Layer<S> for BodyCaptureLayer<M>
where
    M: Clone,
{
    type Service = BodyCaptureService<M, S>;

    fn layer(&self, inner: S) -> Self::Service {
        BodyCaptureService {
            inner,
            make_sink: self.make_sink.clone(),
            capture_request: self.capture_request,
            capture_response: self.capture_response,
        }
    }

    fn into_layer(self, inner: S) -> Self::Service {
        BodyCaptureService {
            inner,
            make_sink: self.make_sink,
            capture_request: self.capture_request,
            capture_response: self.capture_response,
        }
    }
}

/// Service produced by [`BodyCaptureLayer`].
#[derive(Clone)]
pub struct BodyCaptureService<M, S> {
    inner: S,
    make_sink: M,
    capture_request: bool,
    capture_response: bool,
}

impl<M, S> BodyCaptureService<M, S> {
    /// Create a service that captures request and response bodies.
    #[must_use]
    pub const fn bidirectional(inner: S, make_sink: M) -> Self {
        Self {
            inner,
            make_sink,
            capture_request: true,
            capture_response: true,
        }
    }

    define_inner_service_accessors!();
}

impl<M: fmt::Debug, S: fmt::Debug> fmt::Debug for BodyCaptureService<M, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BodyCaptureService")
            .field("inner", &self.inner)
            .field("make_sink", &self.make_sink)
            .field("capture_request", &self.capture_request)
            .field("capture_response", &self.capture_response)
            .finish()
    }
}

impl<M, S, ReqBody, ResBody> Service<Request<ReqBody>> for BodyCaptureService<M, S>
where
    M: MakeBodyCaptureSink,
    S: Service<Request<Body>, Output = Response<ResBody>>,
    ReqBody: StreamingBody<Data = Bytes, Error: Into<BoxError>> + Send + Sync + 'static,
    ResBody: StreamingBody<Data = Bytes, Error: Into<BoxError>> + Send + Sync + 'static,
{
    type Output = Response<Body>;
    type Error = S::Error;

    async fn serve(&self, request: Request<ReqBody>) -> Result<Self::Output, Self::Error> {
        let request = if self.capture_request {
            let (parts, body) = request.into_parts();
            let body = match self
                .make_sink
                .make_sink(CapturedHead::Request(parts.clone()))
                .await
            {
                Some(sink) => Body::new(CaptureBody::new(body.map_err(Into::into), sink)),
                None => Body::new(body),
            };
            Request::from_parts(parts, body)
        } else {
            request.map(Body::new)
        };

        let response = self.inner.serve(request).await?;
        Ok(if self.capture_response {
            let (parts, body) = response.into_parts();
            let body = match self
                .make_sink
                .make_sink(CapturedHead::Response(parts.clone()))
                .await
            {
                Some(sink) => Body::new(CaptureBody::new(body.map_err(Into::into), sink)),
                None => Body::new(body),
            };
            Response::from_parts(parts, body)
        } else {
            response.map(Body::new)
        })
    }
}

#[cfg(test)]
mod tests {
    use std::{
        convert::Infallible,
        sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        },
    };

    use rama_core::{
        Layer as _, Service as _, bytes::Bytes, futures::StreamExt as _, service::service_fn,
    };
    use rama_http_types::{
        Body, BodyCaptureEvent, CaptureOutcome, Request, Response, StatusCode, body::Frame,
        body::util::BodyExt as _,
    };

    use super::{BodyCaptureLayer, BodyCaptureService, CapturedHead};

    type Capture = (
        CapturedHead,
        tokio::sync::mpsc::UnboundedReceiver<BodyCaptureEvent>,
    );

    async fn recv<T>(receiver: &mut tokio::sync::mpsc::UnboundedReceiver<T>) -> T {
        tokio::time::timeout(std::time::Duration::from_secs(1), receiver.recv())
            .await
            .expect("capture should arrive promptly")
            .expect("capture channel should remain open")
    }

    #[tokio::test]
    async fn bidirectional_capture_streams_both_messages() {
        let request_polls = Arc::new(AtomicUsize::new(0));
        let polled = Arc::clone(&request_polls);
        let request_body = Body::from_stream(
            rama_core::futures::stream::iter([
                Ok::<_, Infallible>(Bytes::from_static(b"request ")),
                Ok(Bytes::from_static(b"body")),
            ])
            .inspect(move |_| {
                polled.fetch_add(1, Ordering::Relaxed);
            }),
        );

        let (captures, mut captured) = tokio::sync::mpsc::unbounded_channel::<Capture>();
        let make_sink = move |head| {
            let captures = captures.clone();
            async move {
                let (events, receiver) = tokio::sync::mpsc::unbounded_channel();
                captures.send((head, receiver)).unwrap();
                Some(events)
            }
        };
        let layer = BodyCaptureLayer::bidirectional(make_sink);
        let service_polls = Arc::clone(&request_polls);
        let service = layer.into_layer(service_fn(move |request: Request<Body>| {
            let request_polls = Arc::clone(&service_polls);
            async move {
                assert_eq!(request_polls.load(Ordering::Relaxed), 0);
                assert_eq!(
                    request.into_body().collect().await.unwrap().to_bytes(),
                    Bytes::from_static(b"request body")
                );
                Ok::<_, Infallible>(
                    Response::builder()
                        .status(StatusCode::CREATED)
                        .body(Body::from("response body"))
                        .unwrap(),
                )
            }
        }));

        let response = service
            .serve(
                Request::builder()
                    .method("POST")
                    .uri("https://example.com/")
                    .body(request_body)
                    .unwrap(),
            )
            .await
            .unwrap();

        let (head, mut request_events) = recv(&mut captured).await;
        assert!(matches!(head, CapturedHead::Request(ref parts) if parts.method == "POST"));
        let mut request_bytes = Vec::new();
        loop {
            let event = recv(&mut request_events).await;
            match event {
                BodyCaptureEvent::Frame(frame) => {
                    if let Ok(data) = frame.into_data() {
                        request_bytes.extend_from_slice(&data);
                    }
                }
                BodyCaptureEvent::End(outcome) => {
                    assert_eq!(outcome, CaptureOutcome::Complete);
                    break;
                }
            }
        }
        assert_eq!(request_bytes, b"request body");

        let (head, mut response_events) = recv(&mut captured).await;
        assert!(
            matches!(head, CapturedHead::Response(ref parts) if parts.status == StatusCode::CREATED)
        );
        response_events.try_recv().unwrap_err();

        assert_eq!(
            response.into_body().collect().await.unwrap().to_bytes(),
            Bytes::from_static(b"response body")
        );
        let BodyCaptureEvent::Frame(frame) = recv(&mut response_events).await else {
            panic!("expected a captured response frame");
        };
        assert_eq!(
            frame.into_data().unwrap(),
            Bytes::from_static(b"response body")
        );
        assert!(matches!(
            recv(&mut response_events).await,
            BodyCaptureEvent::End(CaptureOutcome::Complete)
        ));
    }

    #[tokio::test]
    async fn factory_can_skip_capture_from_the_head() {
        let make_sink = |head| async move {
            match head {
                CapturedHead::Request(parts) if parts.method == "GET" => None,
                _ => Some(|_event: BodyCaptureEvent| async {}),
            }
        };
        let service = BodyCaptureLayer::request(make_sink).into_layer(service_fn(
            |request: Request<Body>| async move {
                request.into_body().collect().await.unwrap();
                Ok::<_, Infallible>(Response::new(Body::empty()))
            },
        ));

        service
            .serve(
                Request::builder()
                    .method("GET")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
    }

    #[test]
    fn frame_is_cloneable_when_its_data_is_cloneable() {
        let frame = Frame::data(Bytes::from_static(b"shared"));
        assert_eq!(
            frame.clone().into_data().unwrap(),
            frame.into_data().unwrap()
        );
    }

    #[test]
    fn constructors_and_debug_report_capture_directions() {
        let request = BodyCaptureLayer::request("factory");
        assert_eq!(request.make_sink(), &"factory");
        assert!(request.captures_requests());
        assert!(!request.captures_responses());
        assert!(format!("{request:?}").contains("BodyCaptureLayer"));

        let response = BodyCaptureLayer::response("factory");
        assert!(!response.captures_requests());
        assert!(response.captures_responses());

        let both = BodyCaptureLayer::bidirectional("factory");
        assert!(both.captures_requests());
        assert!(both.captures_responses());

        let service = BodyCaptureService::bidirectional("inner", "factory");
        assert!(format!("{service:?}").contains("BodyCaptureService"));

        let (request_parts, ()) = Request::builder()
            .method("POST")
            .body(())
            .unwrap()
            .into_parts();
        let request_head = format!("{:?}", CapturedHead::Request(request_parts));
        assert!(request_head.starts_with("Request("));
        assert!(request_head.contains("POST"));

        let (response_parts, ()) = Response::builder()
            .status(StatusCode::CREATED)
            .body(())
            .unwrap()
            .into_parts();
        let response_head = format!("{:?}", CapturedHead::Response(response_parts));
        assert!(response_head.starts_with("Response("));
        assert!(response_head.contains("201"));
    }
}