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: 0 on linux file with size 0 on stat #1486

Closed
2 tasks done
Giotino opened this issue Oct 4, 2020 · 8 comments · Fixed by #1488
Closed
2 tasks done

content-length: 0 on linux file with size 0 on stat #1486

Giotino opened this issue Oct 4, 2020 · 8 comments · Fixed by #1488
Labels
bug Something does not work as it should ✭ help wanted ✭ regression Something does not work anymore

Comments

@Giotino
Copy link
Collaborator

Giotino commented Oct 4, 2020

Describe the bug

  • Node.js version: Irrelevant
  • OS & version: Linux and maybe MacOS

In linux where "everything is a file" some files have no size when calling stat (eg. /proc/cpuinfo).
In this case Got set content-length to 0, when it should use a chunked transfer encoding.

Actual behavior

Got set content-length to 0.

Expected behavior

Got uses a chunked transfer encoding.

Code to reproduce

const {createReadStream} = require('fs');
const got = require('got');

const stream = createReadStream('/proc/cpuinfo');

(async () => {
	const response = got('https://httpbin.org/post', {method: 'POST', body: stream});
	const json = await response.json();
	
	console.log(json)
})();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak szmarczak added bug Something does not work as it should ✭ help wanted ✭ labels Oct 4, 2020
@szmarczak
Copy link
Collaborator

It's working properly on 9.6.0 but it's failing on 10.0.0

@szmarczak szmarczak added the regression Something does not work anymore label Oct 4, 2020
@szmarczak
Copy link
Collaborator

szmarczak commented Oct 4, 2020

Works on [email protected] but doesn't on [email protected].

@szmarczak
Copy link
Collaborator

@Giotino
Copy link
Collaborator Author

Giotino commented Oct 5, 2020

I think the fix is quite simple: if it's a stream go with chunked, no content-length even if we know it.

@Giotino
Copy link
Collaborator Author

Giotino commented Oct 5, 2020

I think the fix is quite simple: if it's a stream go with chunked, no content-length even if we know it.

Ok, it's not that easy because the upload progress feature is gonna break, but at least we can go with:

if stat.size == 0
 	we don't know the size
else
 	stat.size

@szmarczak
Copy link
Collaborator

Ok, it's not that easy because the upload progress feature is gonna break, but at least we can go with:

That's not the right solution. The alpha version has the same getBodySize implementation and it works there.

@Giotino
Copy link
Collaborator Author

Giotino commented Oct 5, 2020

That's not the right solution. The alpha version has the same getBodySize implementation and it works there.

I'm sorry but currently I've no time to investigate this, but I can say this: if stat returned 0 and Got used that value on content-length there's no way it was working fine, except in the case of a Got bug that wasn't setting the content-length properly.

@szmarczak
Copy link
Collaborator

No problem, I'll look into this ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should ✭ help wanted ✭ regression Something does not work anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants