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
//! Response types

use crate::request::RequestOrigin;
#[cfg(feature = "alb")]
use aws_lambda_events::alb::AlbTargetGroupResponse;
#[cfg(any(feature = "apigw_rest", feature = "apigw_websockets"))]
use aws_lambda_events::apigw::ApiGatewayProxyResponse;
#[cfg(feature = "apigw_http")]
use aws_lambda_events::apigw::ApiGatewayV2httpResponse;
use aws_lambda_events::encodings::Body;
use encoding_rs::Encoding;
use http::header::CONTENT_ENCODING;
use http::HeaderMap;
use http::{header::CONTENT_TYPE, Response, StatusCode};
use http_body::Body as HttpBody;
use hyper::body::to_bytes;
use mime::{Mime, CHARSET};
use serde::Serialize;
use std::borrow::Cow;
use std::future::ready;
use std::{fmt, future::Future, pin::Pin};

const X_LAMBDA_HTTP_CONTENT_ENCODING: &str = "x-lambda-http-content-encoding";

// See list of common MIME types:
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// - https://github.com/ietf-wg-httpapi/mediatypes/blob/main/draft-ietf-httpapi-yaml-mediatypes.md
const TEXT_ENCODING_PREFIXES: [&str; 5] = [
    "text",
    "application/json",
    "application/javascript",
    "application/xml",
    "application/yaml",
];

const TEXT_ENCODING_SUFFIXES: [&str; 3] = ["+xml", "+yaml", "+json"];

/// Representation of Lambda response
#[doc(hidden)]
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum LambdaResponse {
    #[cfg(any(feature = "apigw_rest", feature = "apigw_websockets"))]
    ApiGatewayV1(ApiGatewayProxyResponse),
    #[cfg(feature = "apigw_http")]
    ApiGatewayV2(ApiGatewayV2httpResponse),
    #[cfg(feature = "alb")]
    Alb(AlbTargetGroupResponse),
}

/// Transformation from http type to internal type
impl LambdaResponse {
    pub(crate) fn from_response(request_origin: &RequestOrigin, value: Response<Body>) -> Self {
        let (parts, bod) = value.into_parts();
        let (is_base64_encoded, body) = match bod {
            Body::Empty => (false, None),
            b @ Body::Text(_) => (false, Some(b)),
            b @ Body::Binary(_) => (true, Some(b)),
        };

        let headers = parts.headers;
        let status_code = parts.status.as_u16();

        match request_origin {
            #[cfg(feature = "apigw_rest")]
            RequestOrigin::ApiGatewayV1 => LambdaResponse::ApiGatewayV1(ApiGatewayProxyResponse {
                body,
                status_code: status_code as i64,
                is_base64_encoded: Some(is_base64_encoded),
                headers: headers.clone(),
                multi_value_headers: headers,
            }),
            #[cfg(feature = "apigw_http")]
            RequestOrigin::ApiGatewayV2 => {
                use http::header::SET_COOKIE;
                let mut headers = headers;
                // ApiGatewayV2 expects the set-cookies headers to be in the "cookies" attribute,
                // so remove them from the headers.
                let cookies = headers
                    .get_all(SET_COOKIE)
                    .iter()
                    .cloned()
                    .map(|v| v.to_str().ok().unwrap_or_default().to_string())
                    .collect();
                headers.remove(SET_COOKIE);

                LambdaResponse::ApiGatewayV2(ApiGatewayV2httpResponse {
                    body,
                    status_code: status_code as i64,
                    is_base64_encoded: Some(is_base64_encoded),
                    cookies,
                    headers: headers.clone(),
                    multi_value_headers: headers,
                })
            }
            #[cfg(feature = "alb")]
            RequestOrigin::Alb => LambdaResponse::Alb(AlbTargetGroupResponse {
                body,
                status_code: status_code as i64,
                is_base64_encoded,
                headers: headers.clone(),
                multi_value_headers: headers,
                status_description: Some(format!(
                    "{} {}",
                    status_code,
                    parts.status.canonical_reason().unwrap_or_default()
                )),
            }),
            #[cfg(feature = "apigw_websockets")]
            RequestOrigin::WebSocket => LambdaResponse::ApiGatewayV1(ApiGatewayProxyResponse {
                body,
                status_code: status_code as i64,
                is_base64_encoded: Some(is_base64_encoded),
                headers: headers.clone(),
                multi_value_headers: headers,
            }),
        }
    }
}

/// Trait for generating responses
///
/// Types that implement this trait can be used as return types for handler functions.
pub trait IntoResponse {
    /// Transform into a Response<Body> Future
    fn into_response(self) -> ResponseFuture;
}

impl<B> IntoResponse for Response<B>
where
    B: ConvertBody + Send + 'static,
{
    fn into_response(self) -> ResponseFuture {
        let (parts, body) = self.into_parts();
        let headers = parts.headers.clone();

        let fut = async { Response::from_parts(parts, body.convert(headers).await) };

        Box::pin(fut)
    }
}

impl IntoResponse for String {
    fn into_response(self) -> ResponseFuture {
        Box::pin(ready(Response::new(Body::from(self))))
    }
}

impl IntoResponse for &str {
    fn into_response(self) -> ResponseFuture {
        Box::pin(ready(Response::new(Body::from(self))))
    }
}

impl IntoResponse for &[u8] {
    fn into_response(self) -> ResponseFuture {
        Box::pin(ready(Response::new(Body::from(self))))
    }
}

impl IntoResponse for Vec<u8> {
    fn into_response(self) -> ResponseFuture {
        Box::pin(ready(Response::new(Body::from(self))))
    }
}

impl IntoResponse for serde_json::Value {
    fn into_response(self) -> ResponseFuture {
        Box::pin(async move {
            Response::builder()
                .header(CONTENT_TYPE, "application/json")
                .body(
                    serde_json::to_string(&self)
                        .expect("unable to serialize serde_json::Value")
                        .into(),
                )
                .expect("unable to build http::Response")
        })
    }
}

impl IntoResponse for (StatusCode, String) {
    fn into_response(self) -> ResponseFuture {
        let (status, body) = self;
        Box::pin(ready(
            Response::builder()
                .status(status)
                .body(Body::from(body))
                .expect("unable to build http::Response"),
        ))
    }
}

impl IntoResponse for (StatusCode, &str) {
    fn into_response(self) -> ResponseFuture {
        let (status, body) = self;
        Box::pin(ready(
            Response::builder()
                .status(status)
                .body(Body::from(body))
                .expect("unable to build http::Response"),
        ))
    }
}

impl IntoResponse for (StatusCode, &[u8]) {
    fn into_response(self) -> ResponseFuture {
        let (status, body) = self;
        Box::pin(ready(
            Response::builder()
                .status(status)
                .body(Body::from(body))
                .expect("unable to build http::Response"),
        ))
    }
}

impl IntoResponse for (StatusCode, Vec<u8>) {
    fn into_response(self) -> ResponseFuture {
        let (status, body) = self;
        Box::pin(ready(
            Response::builder()
                .status(status)
                .body(Body::from(body))
                .expect("unable to build http::Response"),
        ))
    }
}

impl IntoResponse for (StatusCode, serde_json::Value) {
    fn into_response(self) -> ResponseFuture {
        let (status, body) = self;
        Box::pin(async move {
            Response::builder()
                .status(status)
                .header(CONTENT_TYPE, "application/json")
                .body(
                    serde_json::to_string(&body)
                        .expect("unable to serialize serde_json::Value")
                        .into(),
                )
                .expect("unable to build http::Response")
        })
    }
}

pub type ResponseFuture = Pin<Box<dyn Future<Output = Response<Body>> + Send>>;

pub trait ConvertBody {
    fn convert(self, parts: HeaderMap) -> BodyFuture;
}

impl<B> ConvertBody for B
where
    B: HttpBody + Unpin + Send + 'static,
    B::Data: Send,
    B::Error: fmt::Debug,
{
    fn convert(self, headers: HeaderMap) -> BodyFuture {
        if headers.get(CONTENT_ENCODING).is_some() {
            return convert_to_binary(self);
        }

        let content_type = if let Some(value) = headers.get(CONTENT_TYPE) {
            value.to_str().unwrap_or_default()
        } else {
            // Content-Type and Content-Encoding not set, passthrough as utf8 text
            return convert_to_text(self, "utf-8");
        };

        for prefix in TEXT_ENCODING_PREFIXES {
            if content_type.starts_with(prefix) {
                return convert_to_text(self, content_type);
            }
        }

        for suffix in TEXT_ENCODING_SUFFIXES {
            if content_type.ends_with(suffix) {
                return convert_to_text(self, content_type);
            }
        }

        if let Some(value) = headers.get(X_LAMBDA_HTTP_CONTENT_ENCODING) {
            if value == "text" {
                return convert_to_text(self, content_type);
            }
        }

        convert_to_binary(self)
    }
}

fn convert_to_binary<B>(body: B) -> BodyFuture
where
    B: HttpBody + Unpin + Send + 'static,
    B::Data: Send,
    B::Error: fmt::Debug,
{
    Box::pin(async move { Body::from(to_bytes(body).await.expect("unable to read bytes from body").to_vec()) })
}

fn convert_to_text<B>(body: B, content_type: &str) -> BodyFuture
where
    B: HttpBody + Unpin + Send + 'static,
    B::Data: Send,
    B::Error: fmt::Debug,
{
    let mime_type = content_type.parse::<Mime>();

    let encoding = match mime_type.as_ref() {
        Ok(mime) => mime.get_param(CHARSET).unwrap_or(mime::UTF_8),
        Err(_) => mime::UTF_8,
    };

    let label = encoding.as_ref().as_bytes();
    let encoding = Encoding::for_label(label).unwrap_or(encoding_rs::UTF_8);

    // assumes utf-8
    Box::pin(async move {
        let bytes = to_bytes(body).await.expect("unable to read bytes from body");
        let (content, _, _) = encoding.decode(&bytes);

        match content {
            Cow::Borrowed(content) => Body::from(content),
            Cow::Owned(content) => Body::from(content),
        }
    })
}

pub type BodyFuture = Pin<Box<dyn Future<Output = Body> + Send>>;

#[cfg(test)]
mod tests {
    use super::{Body, IntoResponse, LambdaResponse, RequestOrigin, X_LAMBDA_HTTP_CONTENT_ENCODING};
    use http::{
        header::{CONTENT_ENCODING, CONTENT_TYPE},
        Response, StatusCode,
    };
    use hyper::Body as HyperBody;
    use serde_json::{self, json};

    const SVG_LOGO: &str = include_str!("../tests/data/svg_logo.svg");

    #[tokio::test]
    async fn json_into_response() {
        let response = json!({ "hello": "lambda"}).into_response().await;
        match response.body() {
            Body::Text(json) => assert_eq!(json, r#"{"hello":"lambda"}"#),
            _ => panic!("invalid body"),
        }
        assert_eq!(
            response
                .headers()
                .get(CONTENT_TYPE)
                .map(|h| h.to_str().expect("invalid header")),
            Some("application/json")
        )
    }

    #[tokio::test]
    async fn text_into_response() {
        let response = "text".into_response().await;
        match response.body() {
            Body::Text(text) => assert_eq!(text, "text"),
            _ => panic!("invalid body"),
        }
    }

    #[tokio::test]
    async fn bytes_into_response() {
        let response = "text".as_bytes().into_response().await;
        match response.body() {
            Body::Binary(data) => assert_eq!(data, "text".as_bytes()),
            _ => panic!("invalid body"),
        }
    }

    #[tokio::test]
    async fn json_with_status_code_into_response() {
        let response = (StatusCode::CREATED, json!({ "hello": "lambda"})).into_response().await;
        match response.body() {
            Body::Text(json) => assert_eq!(json, r#"{"hello":"lambda"}"#),
            _ => panic!("invalid body"),
        }
        match response.status() {
            StatusCode::CREATED => (),
            _ => panic!("invalid status code"),
        }

        assert_eq!(
            response
                .headers()
                .get(CONTENT_TYPE)
                .map(|h| h.to_str().expect("invalid header")),
            Some("application/json")
        )
    }

    #[tokio::test]
    async fn text_with_status_code_into_response() {
        let response = (StatusCode::CREATED, "text").into_response().await;

        match response.status() {
            StatusCode::CREATED => (),
            _ => panic!("invalid status code"),
        }
        match response.body() {
            Body::Text(text) => assert_eq!(text, "text"),
            _ => panic!("invalid body"),
        }
    }

    #[tokio::test]
    async fn bytes_with_status_code_into_response() {
        let response = (StatusCode::CREATED, "text".as_bytes()).into_response().await;
        match response.status() {
            StatusCode::CREATED => (),
            _ => panic!("invalid status code"),
        }
        match response.body() {
            Body::Binary(data) => assert_eq!(data, "text".as_bytes()),
            _ => panic!("invalid body"),
        }
    }

    #[tokio::test]
    async fn content_encoding_header() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            .header(CONTENT_ENCODING, "gzip")
            .body(HyperBody::from("000000".as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;
        let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);

        let json = serde_json::to_string(&response).expect("failed to serialize to json");
        assert_eq!(
            json,
            r#"{"statusCode":200,"headers":{"content-encoding":"gzip"},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
        )
    }

    #[tokio::test]
    async fn content_type_header() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            .header(CONTENT_TYPE, "application/json")
            .body(HyperBody::from("000000".as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;
        let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);

        let json = serde_json::to_string(&response).expect("failed to serialize to json");
        assert_eq!(
            json,
            r#"{"statusCode":200,"headers":{"content-type":"application/json"},"multiValueHeaders":{"content-type":["application/json"]},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
        )
    }

    #[tokio::test]
    async fn charset_content_type_header() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            .header(CONTENT_TYPE, "application/json; charset=utf-16")
            .body(HyperBody::from("000000".as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;
        let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);

        let json = serde_json::to_string(&response).expect("failed to serialize to json");
        assert_eq!(
            json,
            r#"{"statusCode":200,"headers":{"content-type":"application/json; charset=utf-16"},"multiValueHeaders":{"content-type":["application/json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
        )
    }

    #[tokio::test]
    async fn content_headers_unset() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            .body(HyperBody::from("000000".as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;
        let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);

        let json = serde_json::to_string(&response).expect("failed to serialize to json");
        assert_eq!(
            json,
            r#"{"statusCode":200,"headers":{},"multiValueHeaders":{},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
        )
    }

    #[test]
    fn serialize_multi_value_headers() {
        let res = LambdaResponse::from_response(
            &RequestOrigin::ApiGatewayV1,
            Response::builder()
                .header("multi", "a")
                .header("multi", "b")
                .body(Body::from(()))
                .expect("failed to create response"),
        );
        let json = serde_json::to_string(&res).expect("failed to serialize to json");
        assert_eq!(
            json,
            r#"{"statusCode":200,"headers":{"multi":"a"},"multiValueHeaders":{"multi":["a","b"]},"isBase64Encoded":false}"#
        )
    }

    #[test]
    fn serialize_cookies() {
        let res = LambdaResponse::from_response(
            &RequestOrigin::ApiGatewayV2,
            Response::builder()
                .header("set-cookie", "cookie1=a")
                .header("set-cookie", "cookie2=b")
                .body(Body::from(()))
                .expect("failed to create response"),
        );
        let json = serde_json::to_string(&res).expect("failed to serialize to json");
        assert_eq!(
            "{\"statusCode\":200,\"headers\":{},\"multiValueHeaders\":{},\"isBase64Encoded\":false,\"cookies\":[\"cookie1=a\",\"cookie2=b\"]}",
            json
        )
    }

    #[tokio::test]
    async fn content_type_xml_as_text() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            .header(CONTENT_TYPE, "image/svg+xml")
            .body(HyperBody::from(SVG_LOGO.as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;

        match response.body() {
            Body::Text(body) => assert_eq!(SVG_LOGO, body),
            _ => panic!("invalid body"),
        }
        assert_eq!(
            response
                .headers()
                .get(CONTENT_TYPE)
                .map(|h| h.to_str().expect("invalid header")),
            Some("image/svg+xml")
        )
    }

    #[tokio::test]
    async fn content_type_custom_encoding_as_text() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let response = Response::builder()
            // this CONTENT-TYPE is not standard, and would yield a binary response
            .header(CONTENT_TYPE, "image/svg")
            .header(X_LAMBDA_HTTP_CONTENT_ENCODING, "text")
            .body(HyperBody::from(SVG_LOGO.as_bytes()))
            .expect("unable to build http::Response");
        let response = response.into_response().await;

        match response.body() {
            Body::Text(body) => assert_eq!(SVG_LOGO, body),
            _ => panic!("invalid body"),
        }
        assert_eq!(
            response
                .headers()
                .get(CONTENT_TYPE)
                .map(|h| h.to_str().expect("invalid header")),
            Some("image/svg")
        )
    }

    #[tokio::test]
    async fn content_type_yaml_as_text() {
        // Drive the implementation by using `hyper::Body` instead of
        // of `aws_lambda_events::encodings::Body`
        let yaml = r#"---
foo: bar
        "#;

        let formats = ["application/yaml", "custom/vdn+yaml"];

        for format in formats {
            let response = Response::builder()
                .header(CONTENT_TYPE, format)
                .body(HyperBody::from(yaml.as_bytes()))
                .expect("unable to build http::Response");
            let response = response.into_response().await;

            match response.body() {
                Body::Text(body) => assert_eq!(yaml, body),
                _ => panic!("invalid body"),
            }
            assert_eq!(
                response
                    .headers()
                    .get(CONTENT_TYPE)
                    .map(|h| h.to_str().expect("invalid header")),
                Some(format)
            )
        }
    }
}