Skip to content

http/server: request.socket.bytesRead always 0 for IncomingMessage #3021

Closed
@skenqbx

Description

@skenqbx

Tested with node.js v4.1.0 & v4.1.1 (v0.10.x does not have this issue)

See the following test (parallel/test-http-bytesread.js) to reproduce the issue;

'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');

var body = 'hello world\n';
var sawEnd = false;
var bytesReadServer = 0;
var bytesReadClient = 0;

process.on('exit', function() {
  assert(sawEnd);
  console.log('ok');
});

var httpServer = http.createServer(function(req, res) {
  httpServer.close();

  req.on('readable', function() {
    var data = req.read();
    if (data !== null) {
      bytesReadServer += data.length;
    }
  });

  req.on('end', function() {
    assert(bytesReadServer > 0);
    assert(req.socket.bytesRead > 0);

    sawEnd = true;
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(body);
  });
});

httpServer.listen(common.PORT, function() {
  var req = http.request({
    method: 'PUT',
    port: common.PORT
  }, function(res) {
    res.on('readable', function() {
      var data = res.read();
      if (data !== null) {
        bytesReadClient += data.length;
      }
    });

    res.on('end', function() {
      assert(bytesReadClient > 0);
      assert(res.socket.bytesRead > 0);
    });
  });

  var chunk = new Array(1024 + 1).join('7');
  var bchunk = new Buffer(chunk);

  for (var i = 0; i < 1024; i++) {
    req.write(chunk);
    req.write(bchunk);
    req.write(chunk, 'hex');
  }

  req.end();
});

I could not yet figure out what causes this, any help is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.httpIssues or PRs related to the http subsystem.netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions