Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
.or(ag.request_context.domain_name.as_deref())
.unwrap_or_default();

let path = apigw_path_with_stage(&ag.request_context.stage, ag.raw_path.as_deref().unwrap_or_default());
let path = ag.raw_path.as_deref().unwrap_or_default();
let mut url = format!("{}://{}{}", scheme, host, path);

if let Some(query) = ag.raw_query_string {
Expand Down Expand Up @@ -118,7 +118,7 @@ fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -299,14 +299,6 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
req
}

fn apigw_path_with_stage(stage: &Option<String>, 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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
}
5 changes: 5 additions & 0 deletions lambda-runtime-api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 8 additions & 2 deletions lambda-runtime-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -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)
}

Expand Down