Got: `content-length: 0` on linux file with size `0` on `stat`

Created on 4 Oct 2020  Â·  8Comments  Â·  Source: sindresorhus/got

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

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
bug regression ✭ help wanted ✭

All 8 comments

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

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

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

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

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.

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.

No problem, I'll look into this ASAP.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pvdlg picture pvdlg  Â·  3Comments

sindresorhus picture sindresorhus  Â·  3Comments

khizarsonu picture khizarsonu  Â·  3Comments

carvallegro picture carvallegro  Â·  4Comments

lukehorvat picture lukehorvat  Â·  3Comments