aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

Throw specific error if Content-Length can't be computed

Open trivikr opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe.

S3 throws NotImplemented Error in Content-Length can't be computed in https://github.com/aws/aws-sdk-js-v3/issues/3377 The error can be more specific, i.e. Content-Length can't be computed.

Describe the solution you'd like

Throw error in calculateBodyLength if length can't be computed.

  • https://github.com/aws/aws-sdk-js-v3/blob/main/packages%2Futil-body-length-node%2Fsrc%2FcalculateBodyLength.ts
  • https://github.com/aws/aws-sdk-js-v3/blob/main/packages%2Futil-body-length-browser%2Fsrc%2FcalculateBodyLength.ts

Describe alternatives you've considered

N/A

Additional context

This suggestion was discussed and approved internally.

trivikr avatar Mar 08 '22 21:03 trivikr

As of now, the contentLengthMiddleware skips setting Content-Length header if it is undefined. https://github.com/aws/aws-sdk-js-v3/blob/88f8cc2d3d33f4dc6d06312e75f70725c89d1f17/packages/middleware-content-length/src/index.ts#L27-L33

trivikr avatar Mar 08 '22 21:03 trivikr

From rfc7230 specification 3.3.2

A user agent SHOULD send a Content-Length in a request message when no Transfer-Encoding is sent and the request method defines a meaning for an enclosed payload body. For example, a Content-Length header field is normally sent in a POST request even when the value is 0 (indicating an empty payload body). A user agent SHOULD NOT send a Content-Length header field when the request message does not contain a payload body and the method semantics do not anticipate such a body.

trivikr avatar Mar 08 '22 21:03 trivikr

The contentLengthMiddleware checks if body is defined and Content-Length header is not set before calling bodyLengthChecker. And as per spec it should be fine to throw error if bodyLengthChecker, as user agent should send a Content-Length in case of enclosed payload body.

I've asked this question on internal Slack channel to check behaviors in other SDKs, and will revisit this feature request in future.

trivikr avatar Mar 08 '22 22:03 trivikr

The AWS SDK for Python and CLI sets Transfer-Encoding to chunked if they're not able to determine content-length https://github.com/boto/botocore/blob/354e9cb22017112a6f72fb20c36b133c9f346002/botocore/awsrequest.py#L364-L374

trivikr avatar Mar 08 '22 23:03 trivikr