express-http-proxy icon indicating copy to clipboard operation
express-http-proxy copied to clipboard

`content-length` request header is removed when `parseReqBody` is `false`

Open mdmower-csnw opened this issue 1 year ago • 3 comments

Problem When option {parseReqBody: false} is used, the original request content-length header is lost when the request is forwarded to the target origin.

Analysis It looks like the header is removed here: https://github.com/villadora/express-http-proxy/blob/b05cb04fe8e87e215c886462aae264dc70f9a3c5/lib/requestOptions.js#L52-L65

But then is only recalculated when parseReqBody is true, here: https://github.com/villadora/express-http-proxy/blob/b05cb04fe8e87e215c886462aae264dc70f9a3c5/app/steps/sendProxyRequest.js#L57

Potential solution When {parseReqBody: false}...

  1. If bodyContent is provided, then content-length could be determined from the size of bodyContent
  2. If bodyContent is not provided (i.e. proxyReq is piped), then the original content-length header could be restored (if defined)

Supposing number item 1 is handled (content-length determined from size of bodyContent), then it may not be necessary to remove content-length from the original request at all. It would be overwritten in sendProxyRequest when appropriate and left alone otherwise.

mdmower-csnw avatar Aug 13 '24 22:08 mdmower-csnw

@mdmower-csnw Thanks for this detailed report. I'll take a look.

monkpow avatar Aug 14 '24 13:08 monkpow

@mdmower-csnw Thanks for the report. I ended up following your parenthetical advice and not removing content-length at all.

monkpow avatar Aug 20 '24 00:08 monkpow

Thanks for the quick attention on this, @monkpow!

mdmower-csnw avatar Aug 20 '24 00:08 mdmower-csnw