Skip to content

Adding "content-length" header to response causes closed connection. #862

@Jimmy-Z

Description

@Jimmy-Z

This is adapted from the streaming example from here
https://github.com/awslabs/aws-lambda-rust-runtime/blob/main/examples/basic-streaming-response/src/main.rs

use lambda_runtime::{
	Error, LambdaEvent, service_fn,
	streaming::{Body, Response},
	tracing,
};
use serde_json::Value;

async fn func(_event: LambdaEvent<Value>) -> Result<Response<Body>, Error> {
	let body = "hello\n";
	let len = body.as_bytes().len();

	let mut resp = Response::from(Body::from(body));
	// resp.metadata_prelude.headers.append("content-type", "text/plain".parse().unwrap());
	resp.metadata_prelude.headers.append("content-length", len.into());

	Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
	// required to enable CloudWatch error logging by the runtime
	tracing::init_default_subscriber();

	lambda_runtime::run(service_fn(func)).await?;
	Ok(())
}

run it with cargo lambda watch, then curl -v

>curl -v http://127.0.0.1:9000
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
> GET / HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.9.1
> Accept: */*
>
* Empty reply from server
* shutting down connection #0
curl: (52) Empty reply from server

remove the header will fix it, but I wonder why?

upload the binary on aws, it works just fine, except the header becomes "x-amzn-remapped-content-length", so I guess it's not lambda-runtime's fault.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions