uWebSockets.js icon indicating copy to clipboard operation
uWebSockets.js copied to clipboard

Method to indicate that request has body

Open zdm opened this issue 1 year ago • 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.

zdm avatar Aug 21 '24 08:08 zdm

Can check request.getHeader('content-length')

uasan avatar Aug 21 '24 08:08 uasan

Data can be chunked, without known content length.

zdm avatar Aug 21 '24 08:08 zdm

hasBody is a good addition

uNetworkingAB avatar Aug 21 '24 08:08 uNetworkingAB

const hasBody = req => Number(req.getHeader('content-length')) || req.getHeader('transfer-encoding').includes('chunked');

e3dio avatar Aug 21 '24 09:08 e3dio

yes, this will work please, close, this issue, if you don;t want to add such method to the uws.

zdm avatar Aug 21 '24 09:08 zdm

The client can send 0 bytes

!!+req.getHeader('content-length')

uasan avatar Aug 21 '24 09:08 uasan

@uasan yes '0' string needs to be properly accounted for, I fixed the function

e3dio avatar Aug 21 '24 09:08 e3dio

// Several values can be listed, separated by a comma Transfer-Encoding: gzip, chunked

req.getHeader('transfer-encoding')?.includes('chunked')

webcarrot avatar Aug 21 '24 11:08 webcarrot

req.getHeader('transfer-encoding')?.toLowerCase().includes('chunked')

zdm avatar Aug 21 '24 11:08 zdm

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()

uNetworkingAB avatar Aug 21 '24 11:08 uNetworkingAB

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');

e3dio avatar Aug 21 '24 16:08 e3dio

Fot me hasBody is enough. I already user methos, which is limit download size.

zdm avatar Aug 21 '24 16:08 zdm

bodyType() could return 0 for false, 1 for fix length, 2 for stream. The parser already knows all of this

uNetworkingAB avatar Aug 21 '24 16:08 uNetworkingAB

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

uasan avatar Aug 21 '24 20:08 uasan

Agree, knowing the fixed length can help decide buffer size

uNetworkingAB avatar Aug 21 '24 20:08 uNetworkingAB

Will you add this method to the uws or lets close this issue?

zdm avatar Oct 19 '24 10:10 zdm