question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No control over response when Content-Length is larger than body length & ECONNABORTED for lower Content-Length than body length

See original GitHub issue

Found 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Aug 20, 2018

Both cases can be replicated without Express / body-parser, making this likely a Node.js issue:

const http = require('http');

const server = http.createServer((req, res) => {
    let chunks = [];
    req.on('data', (c) => chunks.push(c));
    req.on('end', () => {
        res.statusCode = 500;
        res.end();
    });
});

server.listen(3000);
server.setTimeout(200);

process.once('SIGINT', () => server.close());

Let me know if I’m mistaken and I can re-open 👍

0reactions
aPoCoMiLogincommented, Aug 20, 2018

Thanks for the fast reply, I appreciate it!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found