No control over response when Content-Length is larger than body length & ECONNABORTED for lower Content-Length than body length
See original GitHub issueFound 2 issues with Content-Length
with body-parser & express
- if body length is 2 and
Content-Length
is 1, there will always be 400 http status, even if you override it - if body length is 1 and
Content-Length
is 2, there will be ECONNABORTED (for max httpServer.timeout), and no control over response at all
example code:
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
app.use((err, req, res, next) => {
console.error(err); // it will always log error
res.sendStatus(500); // it will never override response code
});
const server = app.listen(3000);
server.setTimeout(200);
process.once('SIGINT', () => server.close());
curl for first case:
curl -X POST \
http://127.0.0.1:3000 \
-H 'Content-Length: 2' \
-H 'Content-Type: application/json' \
-d '{'
curl for second case:
curl -X POST \
http://127.0.0.1:3000 \
-H 'Content-Length: 1' \
-H 'Content-Type: application/json' \
-d '{}'
Don’t know why I have no control over response in both cases. Timeout is what is worrying me, since it will just hang.
Tested it with:
- node: 10, 9, 8, 7, 6
- body-parser: 1.18.3, 1.18.0, 1.17.0, 1.16.0
- express: 4.16.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
What happens if more data than the Content-Length is ...
As HTTP is transferred over TCP it is possible that the client stops receiving data. BTW, currently, it's possible that the connection remains ......
Read more >Content-Length is always zero - MSDN - Microsoft
I need to build a RESTful service that acts as a proxy service to a third part RESTful service (in particular the BlobStorage...
Read more >Changes - Apache
[Ian Holsman / Rob Cromwell <apachechangelog@robcromwell.com>] *) The content-length filter no longer tries to buffer up the entire output of a long-running ...
Read more >listing. *) Change - Nginx.org
Changes with nginx 1.0.7 30 Sep 2011 *) Change: now if total size of all ranges is greater than source response size, then...
Read more >in trunk/tools/build/third_party/cherrypy: . lib process scaffold ...
+ response.headers['Content-Length'] = str(len(content)) ... + more control over object instantiation than is available in the various
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Both cases can be replicated without Express / body-parser, making this likely a Node.js issue:
Let me know if I’m mistaken and I can re-open 👍
Thanks for the fast reply, I appreciate it!