Browse Source

http: bubble up parser errors to ClientRequest

Make parser errors bubble up to the ClientRequest instead of the underlying
net.Socket object.

This is a back-port of commit c78678b from the master branch.

Fixes #3776.
v0.8.17-release
Brian White 13 years ago
committed by Ben Noordhuis
parent
commit
827b2a9b0b
  1. 4
      lib/http.js
  2. 54
      test/simple/test-http-client-parse-error.js

4
lib/http.js

@ -1368,7 +1368,9 @@ function socketOnData(d, start, end) {
if (ret instanceof Error) { if (ret instanceof Error) {
debug('parse error'); debug('parse error');
freeParser(parser, req); freeParser(parser, req);
socket.destroy(ret); socket.destroy();
req.emit('error', ret);
req._hadError = true;
} else if (parser.incoming && parser.incoming.upgrade) { } else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade or CONNECT // Upgrade or CONNECT
var bytesParsed = ret; var bytesParsed = ret;

54
test/simple/test-http-client-parse-error.js

@ -25,37 +25,35 @@ var assert = require('assert');
var http = require('http'); var http = require('http');
var net = require('net'); var net = require('net');
// Create a TCP server var connects = 0;
var srv = net.createServer(function(c) { var parseErrors = 0;
c.write('bad http - should trigger parse error\r\n');
// Create a TCP server
net.createServer(function(c) {
console.log('connection'); console.log('connection');
if (++connects === 1) {
c.on('end', function() { c.end(); }); c.end('HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world');
}); } else {
c.end('bad http - should trigger parse error\r\n');
var parseError = false; this.close();
}
srv.listen(common.PORT, '127.0.0.1', function() { }).listen(common.PORT, '127.0.0.1', function() {
var req = http.request({ for (var i = 0; i < 2; i++) {
host: '127.0.0.1', http.request({
port: common.PORT, host: '127.0.0.1',
method: 'GET', port: common.PORT,
path: '/'}); method: 'GET',
req.end(); path: '/'
}).on('error', function(e) {
req.on('error', function(e) { console.log('got error from client');
console.log('got error from client'); assert.ok(e.message.indexOf('Parse Error') >= 0);
srv.close(); assert.equal(e.code, 'HPE_INVALID_CONSTANT');
assert.ok(e.message.indexOf('Parse Error') >= 0); parseErrors++;
assert.equal(e.code, 'HPE_INVALID_CONSTANT'); }).end();
parseError = true; }
});
}); });
process.on('exit', function() { process.on('exit', function() {
assert.ok(parseError); assert.equal(connects, 2);
assert.equal(parseErrors, 2);
}); });

Loading…
Cancel
Save