From 5fc414d59c0359e2c5e6c68ad5470d0e79b05866 Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Mon, 21 Mar 2022 13:24:12 +0800 Subject: [PATCH 1/2] remove stage from generated http request url --- lambda-http/src/request.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lambda-http/src/request.rs b/lambda-http/src/request.rs index cff30fc7..70d62dde 100644 --- a/lambda-http/src/request.rs +++ b/lambda-http/src/request.rs @@ -75,7 +75,7 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request http::Request { let builder = http::Request::builder() .uri({ let host = ag.headers.get(http::header::HOST).and_then(|s| s.to_str().ok()); - let path = apigw_path_with_stage(&ag.request_context.stage, &ag.path.unwrap_or_default()); + let path = ag.path.unwrap_or_default(); let mut url = match host { None => path, @@ -241,7 +241,7 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request< let builder = http::Request::builder() .uri({ let host = ag.headers.get(http::header::HOST).and_then(|s| s.to_str().ok()); - let path = apigw_path_with_stage(&ag.request_context.stage, &ag.path.unwrap_or_default()); + let path = ag.path.unwrap_or_default(); let mut url = match host { None => path, @@ -299,14 +299,6 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request< req } -fn apigw_path_with_stage(stage: &Option, path: &str) -> String { - match stage { - None => path.into(), - Some(stage) if stage == "$default" => path.into(), - Some(stage) => format!("/{}{}", stage, path), - } -} - /// Event request context as an enumeration of request contexts /// for both ALB and API Gateway and HTTP API events #[derive(Deserialize, Debug, Clone)] @@ -476,7 +468,7 @@ mod tests { assert_eq!(req.method(), "GET"); assert_eq!( req.uri(), - "https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/test/hello?name=me" + "https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello?name=me" ); // Ensure this is an APIGW request @@ -603,6 +595,6 @@ mod tests { ); let req = result.expect("failed to parse request"); assert_eq!(req.method(), "GET"); - assert_eq!(req.uri(), "/test/test/hello?name=me"); + assert_eq!(req.uri(), "/test/hello?name=me"); } } From 6c4609d68adad642a244226ac0ff4c9f8ad16724 Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Tue, 22 Mar 2022 10:28:31 +0800 Subject: [PATCH 2/2] allow customized User_Agent for lambda runtime api client --- lambda-runtime-api-client/README.md | 5 +++++ lambda-runtime-api-client/src/lib.rs | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lambda-runtime-api-client/README.md b/lambda-runtime-api-client/README.md index 530fefdd..f193a537 100644 --- a/lambda-runtime-api-client/README.md +++ b/lambda-runtime-api-client/README.md @@ -33,3 +33,8 @@ async fn main() -> Result<(), Error> { client.call(request).await } ``` + +## Custom User Agent + +To customize User Agent header sent to Lambda Runtime API, you can configure an environment variable `LAMBDA_RUNTIME_USER_AGENT` at compiling time. +This will overide the default User Agent. diff --git a/lambda-runtime-api-client/src/lib.rs b/lambda-runtime-api-client/src/lib.rs index 83d789cc..4585f2a2 100644 --- a/lambda-runtime-api-client/src/lib.rs +++ b/lambda-runtime-api-client/src/lib.rs @@ -14,7 +14,8 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tower_service::Service; const USER_AGENT_HEADER: &str = "User-Agent"; -const USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION")); +const DEFAULT_USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION")); +const CUSTOM_USER_AGENT: Option<&str> = option_env!("LAMBDA_RUNTIME_USER_AGENT"); /// Error type that lambdas may result in pub type Error = Box; @@ -127,8 +128,13 @@ where /// Create a request builder. /// This builder uses `aws-lambda-rust/CRATE_VERSION` as -/// the default User-Agent. +/// the default User-Agent. Configure environment variable `LAMBDA_RUNTIME_USER_AGENT` +/// at compile time to modify User-Agent value. pub fn build_request() -> http::request::Builder { + const USER_AGENT: &str = match CUSTOM_USER_AGENT { + Some(value) => value, + None => DEFAULT_USER_AGENT, + }; http::Request::builder().header(USER_AGENT_HEADER, USER_AGENT) }