Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message when request has incorrect content-length #11524

Closed
StevenWinde opened this issue Feb 23, 2017 · 5 comments
Closed

Better error message when request has incorrect content-length #11524

StevenWinde opened this issue Feb 23, 2017 · 5 comments
Labels
http Issues or PRs related to the http subsystem.

Comments

@StevenWinde
Copy link

Subsystem: http - ClientRequest

I was making a request that had a string with a special character. In a buffer the character's size is 3 instead of the regular 1. The request would fail with a socket hang up and it took awhile to figure out why the request was failing. If there could be a more detailed error message for when there is a mismatch between the content-length and the actual payload that would be appreciated.

Simplest Example to Reproduce

const http = require( 'http' );

const requestData = '–';

http.request( {
  headers: {
    accept:           'application/json',
    'content-type':   'application/json',
    'content-length': requestData.length
  },
  host:   'localhost',
  port:   3000,
  path:   '/foo',
  method: 'post'
}, ( res )=>{

  const accResponseBody = [];

  res
    .on( 'data', console.log )
    .on( 'error', console.log );

} )
  .on( 'error', console.log )
  .end( requestData );

Expected Behavior

{ Error: Incorrect content-length }

Current Behavior

{ Error: socket hang up
    at createHangUpError (_http_client.js:302:15)
    at Socket.socketOnEnd (_http_client.js:394:23)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:186:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9) code: 'ECONNRESET' }
@italoacasas italoacasas added the http Issues or PRs related to the http subsystem. label Feb 23, 2017
@ameliavoncat
Copy link
Contributor

I'd like to work on this.

@ameliavoncat
Copy link
Contributor

I'm getting a socket hang up with this example even when the content length is the correct byte length. The parser is receiving the body but the response callback isn't happening.

@bnoordhuis
Copy link
Member

The client's socket hang up happens because the server closes the connection, if I read the stack trace right.

To get the requested behavior of an 'Incorrect content-length' error, the client would need to keep track of how much bytes have been written and check that against the Content-Length header, but that's a bad idea because it adds considerable overhead.

@Trott
Copy link
Member

Trott commented Jul 26, 2017

Should this stay open or can it be closed?

@bnoordhuis
Copy link
Member

I think this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants