Method to indicate thar request has body
Opened this issue ยท 16 comments
Is it possible to add method hasBody()
, which will return true
if request has body, in order to understand that we need to read it using onData
.
Can check request.getHeader('content-length')
Data can be chunked, without known content length.
hasBody is a good addition
const hasBody = req => Number(req.getHeader('content-length')) || req.getHeader('transfer-encoding').includes('chunked');
yes, this will work
please, close, this issue, if you don;t want to add such method to the uws.
The client can send 0 bytes
!!+req.getHeader('content-length')
// Several values can be listed, separated by a comma
Transfer-Encoding: gzip, chunked
req.getHeader('transfer-encoding')?.includes('chunked')
req.getHeader('transfer-encoding')?.toLowerCase().includes('chunked')
Obviously this should be hasBody() as we already do standards compliant check for this internally. So it's just setting a boolean and returning it via hasBody()
Usually you want to differentiate between standard request uploads with a 'content-length' and potentially unlimited stream of data with 'transfer-encoding: chunked', a simple hasBody would not help with that, encourages bad practice I think, maybe some people need it for something ?
const contentLength = Number(req.getHeader('content-length'));
const transferEncoding = req.getHeader('transfer-encoding');
if (contentLength) {
const body = await getBody(res);
} else if (transferEncoding.includes('chunked')) {
await processStream(res);
} else {
console.log('no body');
}
res.end('done');
Fot me hasBody
is enough. I already user methos, which is limit download size.
bodyType() could return 0 for false, 1 for fix length, 2 for stream. The parser already knows all of this
bodyType() could return 0 for false, 1 for fix length, 2 for stream. The parser already knows all of this
With a fixed body length, it is very useful to know it in advance, then it will be better like this:
req.getBodyLength(): 0 | number | Infinity
Agree, knowing the fixed length can help decide buffer size
Will you add this method to the uws or lets close this issue?