Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content-length request header is removed when parseReqBody is false #545

Open
mdmower-csnw opened this issue Aug 13, 2024 · 3 comments
Open
Assignees

Comments

@mdmower-csnw
Copy link

mdmower-csnw commented Aug 13, 2024

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:

function reqHeaders(req, options) {
var headers = options.headers || {};
var skipHdrs = [ 'connection', 'content-length' ];
if (!options.preserveHostHdr) {
skipHdrs.push('host');
}
var hds = extend(headers, req.headers, skipHdrs);
hds.connection = 'close';
return hds;
}

But then is only recalculated when parseReqBody is true, here:

proxyReq.setHeader('Content-Length', Buffer.byteLength(body));

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.

@monkpow
Copy link
Collaborator

monkpow commented Aug 14, 2024

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

@monkpow
Copy link
Collaborator

monkpow commented Aug 20, 2024

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

@monkpow monkpow self-assigned this Aug 20, 2024
@mdmower-csnw
Copy link
Author

Thanks for the quick attention on this, @monkpow!

monkpow added a commit that referenced this issue Aug 20, 2024
monkpow added a commit that referenced this issue Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants