From 7c26b1cf7cb65f8bd200a37e9eecac9f81667872 Mon Sep 17 00:00:00 2001 From: Nicolas Moutschen Date: Thu, 11 Nov 2021 13:53:06 +0100 Subject: [PATCH 1/3] feat: inject user agent in Lambda runtime API calls --- lambda-runtime/src/requests.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lambda-runtime/src/requests.rs b/lambda-runtime/src/requests.rs index a89f1689..a67c926b 100644 --- a/lambda-runtime/src/requests.rs +++ b/lambda-runtime/src/requests.rs @@ -20,6 +20,7 @@ impl IntoRequest for NextEventRequest { fn into_req(self) -> Result, Error> { let req = Request::builder() .method(Method::GET) + .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) .uri(Uri::from_static("/2018-06-01/runtime/invocation/next")) .body(Body::empty())?; Ok(req) @@ -57,6 +58,10 @@ fn test_next_event_request() { let req = req.into_req().unwrap(); assert_eq!(req.method(), Method::GET); assert_eq!(req.uri(), &Uri::from_static("/2018-06-01/runtime/invocation/next")); + assert!(match req.headers().get("User-Agent") { + Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + None => false, + }); } // /runtime/invocation/{AwsRequestId}/response @@ -75,7 +80,11 @@ where let body = serde_json::to_vec(&self.body)?; let body = Body::from(body); - let req = Request::builder().method(Method::POST).uri(uri).body(body)?; + let req = Request::builder() + .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) + .method(Method::POST) + .uri(uri) + .body(body)?; Ok(req) } } @@ -90,6 +99,10 @@ fn test_event_completion_request() { let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/response"); assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); + assert!(match req.headers().get("User-Agent") { + Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + None => false, + }); } // /runtime/invocation/{AwsRequestId}/error @@ -108,6 +121,7 @@ impl<'a> IntoRequest for EventErrorRequest<'a> { let req = Request::builder() .method(Method::POST) .uri(uri) + .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) .header("lambda-runtime-function-error-type", "unhandled") .body(body)?; Ok(req) @@ -127,6 +141,10 @@ fn test_event_error_request() { let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/error"); assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); + assert!(match req.headers().get("User-Agent") { + Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + None => false, + }); } // /runtime/init/error @@ -140,6 +158,7 @@ impl IntoRequest for InitErrorRequest { let req = Request::builder() .method(Method::POST) .uri(uri) + .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) .header("lambda-runtime-function-error-type", "unhandled") .body(Body::empty())?; Ok(req) @@ -153,4 +172,8 @@ fn test_init_error_request() { let expected = Uri::from_static("/2018-06-01/runtime/init/error"); assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); + assert!(match req.headers().get("User-Agent") { + Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + None => false, + }); } From ce4b73510b933bf868fbda82293d9b42ab8a489e Mon Sep 17 00:00:00 2001 From: Nicolas Moutschen Date: Fri, 12 Nov 2021 07:34:59 +0100 Subject: [PATCH 2/3] feat: switch from format to concat for User-Agent string --- lambda-runtime/src/requests.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lambda-runtime/src/requests.rs b/lambda-runtime/src/requests.rs index a67c926b..0ac3fd08 100644 --- a/lambda-runtime/src/requests.rs +++ b/lambda-runtime/src/requests.rs @@ -20,7 +20,7 @@ impl IntoRequest for NextEventRequest { fn into_req(self) -> Result, Error> { let req = Request::builder() .method(Method::GET) - .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) + .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) .uri(Uri::from_static("/2018-06-01/runtime/invocation/next")) .body(Body::empty())?; Ok(req) @@ -59,7 +59,7 @@ fn test_next_event_request() { assert_eq!(req.method(), Method::GET); assert_eq!(req.uri(), &Uri::from_static("/2018-06-01/runtime/invocation/next")); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), None => false, }); } @@ -81,7 +81,7 @@ where let body = Body::from(body); let req = Request::builder() - .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) + .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) .method(Method::POST) .uri(uri) .body(body)?; @@ -100,7 +100,7 @@ fn test_event_completion_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), None => false, }); } @@ -121,7 +121,7 @@ impl<'a> IntoRequest for EventErrorRequest<'a> { let req = Request::builder() .method(Method::POST) .uri(uri) - .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) + .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) .header("lambda-runtime-function-error-type", "unhandled") .body(body)?; Ok(req) @@ -142,7 +142,7 @@ fn test_event_error_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), None => false, }); } @@ -158,7 +158,7 @@ impl IntoRequest for InitErrorRequest { let req = Request::builder() .method(Method::POST) .uri(uri) - .header("User-Agent", format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION"))) + .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) .header("lambda-runtime-function-error-type", "unhandled") .body(Body::empty())?; Ok(req) @@ -173,7 +173,7 @@ fn test_init_error_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == format!("aws-lambda-rust/{}", env!("CARGO_PKG_VERSION")), + Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), None => false, }); } From 2e6404d80d14b76f00fafb78321a726d9a596c34 Mon Sep 17 00:00:00 2001 From: Nicolas Moutschen Date: Mon, 15 Nov 2021 09:22:13 +0100 Subject: [PATCH 3/3] feat: use static str for user agent --- lambda-runtime/src/requests.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lambda-runtime/src/requests.rs b/lambda-runtime/src/requests.rs index 0ac3fd08..8aa2edbe 100644 --- a/lambda-runtime/src/requests.rs +++ b/lambda-runtime/src/requests.rs @@ -4,6 +4,8 @@ use hyper::Body; use serde::Serialize; use std::str::FromStr; +const USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION")); + pub(crate) trait IntoRequest { fn into_req(self) -> Result, Error>; } @@ -20,7 +22,7 @@ impl IntoRequest for NextEventRequest { fn into_req(self) -> Result, Error> { let req = Request::builder() .method(Method::GET) - .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) + .header("User-Agent", USER_AGENT) .uri(Uri::from_static("/2018-06-01/runtime/invocation/next")) .body(Body::empty())?; Ok(req) @@ -59,7 +61,7 @@ fn test_next_event_request() { assert_eq!(req.method(), Method::GET); assert_eq!(req.uri(), &Uri::from_static("/2018-06-01/runtime/invocation/next")); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), + Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"), None => false, }); } @@ -81,7 +83,7 @@ where let body = Body::from(body); let req = Request::builder() - .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) + .header("User-Agent", USER_AGENT) .method(Method::POST) .uri(uri) .body(body)?; @@ -100,7 +102,7 @@ fn test_event_completion_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), + Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"), None => false, }); } @@ -121,7 +123,7 @@ impl<'a> IntoRequest for EventErrorRequest<'a> { let req = Request::builder() .method(Method::POST) .uri(uri) - .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) + .header("User-Agent", USER_AGENT) .header("lambda-runtime-function-error-type", "unhandled") .body(body)?; Ok(req) @@ -142,7 +144,7 @@ fn test_event_error_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), + Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"), None => false, }); } @@ -158,7 +160,7 @@ impl IntoRequest for InitErrorRequest { let req = Request::builder() .method(Method::POST) .uri(uri) - .header("User-Agent", ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat()) + .header("User-Agent", USER_AGENT) .header("lambda-runtime-function-error-type", "unhandled") .body(Body::empty())?; Ok(req) @@ -173,7 +175,7 @@ fn test_init_error_request() { assert_eq!(req.method(), Method::POST); assert_eq!(req.uri(), &expected); assert!(match req.headers().get("User-Agent") { - Some(header) => header.to_str().unwrap() == ["aws-lambda-rust/", env!("CARGO_PKG_VERSION")].concat(), + Some(header) => header.to_str().unwrap().starts_with("aws-lambda-rust/"), None => false, }); }