Skip to content

Commit

Permalink
chore(middleware-content-length): set transfer-encoding if content-le…
Browse files Browse the repository at this point in the history
…ngth is not set (#3403)
  • Loading branch information
trivikr authored Mar 9, 2022
1 parent a4beeba commit 7e4abf7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/middleware-content-length/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@aws-sdk/types";

const CONTENT_LENGTH_HEADER = "content-length";
const TRANSFER_ENCODING_HEADER = "transfer-encoding";

export function contentLengthMiddleware(bodyLengthChecker: BodyLengthCalculator): BuildMiddleware<any, any> {
return <Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
Expand All @@ -24,12 +25,17 @@ export function contentLengthMiddleware(bodyLengthChecker: BodyLengthCalculator)
.map((str) => str.toLowerCase())
.indexOf(CONTENT_LENGTH_HEADER) === -1
) {
const length = bodyLengthChecker(body);
if (length !== undefined) {
try {
const length = bodyLengthChecker(body);
request.headers = {
...request.headers,
[CONTENT_LENGTH_HEADER]: String(length),
};
} catch (error) {
request.headers = {
...request.headers,
[TRANSFER_ENCODING_HEADER]: "chunked",
};
}
}
}
Expand Down

0 comments on commit 7e4abf7

Please sign in to comment.