Browse Source

http: remove deprecated Client interface

PR-URL: https://github.com/nodejs/node/pull/8104
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v7.x
Brian White 8 years ago
parent
commit
2cc7fa5e7d
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 11
      doc/api/http.md
  2. 86
      lib/http.js
  3. 14
      test/parallel/test-http-client-race-2.js
  4. 70
      test/parallel/test-http-legacy.js

11
doc/api/http.md

@ -1336,17 +1336,6 @@ A collection of all the standard HTTP response status codes, and the
short description of each. For example, `http.STATUS_CODES[404] === 'Not
Found'`.
## http.createClient([port][, host])
<!-- YAML
added: v0.1.13
deprecated: v0.3.6
-->
> Stability: 0 - Deprecated: Use [`http.request()`][] instead.
Constructs a new HTTP client. `port` and `host` refer to the server to be
connected to.
## http.createServer([requestListener])
<!-- YAML
added: v0.1.13

86
lib/http.js

@ -1,28 +1,23 @@
'use strict';
const util = require('util');
const internalUtil = require('internal/util');
const EventEmitter = require('events');
exports.IncomingMessage = require('_http_incoming').IncomingMessage;
const common = require('_http_common');
exports.METHODS = common.methods.slice().sort();
exports.OutgoingMessage = require('_http_outgoing').OutgoingMessage;
exports.METHODS = require('_http_common').methods.slice().sort();
const agent = require('_http_agent');
exports.Agent = agent.Agent;
exports.globalAgent = agent.globalAgent;
const server = require('_http_server');
exports.ServerResponse = server.ServerResponse;
exports.STATUS_CODES = server.STATUS_CODES;
exports._connectionListener = server._connectionListener;
const Server = exports.Server = server.Server;
const agent = require('_http_agent');
const Agent = exports.Agent = agent.Agent;
exports.globalAgent = agent.globalAgent;
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
const client = require('_http_client');
const ClientRequest = exports.ClientRequest = client.ClientRequest;
@ -36,64 +31,3 @@ exports.get = function(options, cb) {
req.end();
return req;
};
exports._connectionListener = server._connectionListener;
const Server = exports.Server = server.Server;
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
// Legacy Interface
function Client(port, host) {
if (!(this instanceof Client)) return new Client(port, host);
EventEmitter.call(this);
host = host || 'localhost';
port = port || 80;
this.host = host;
this.port = port;
this.agent = new Agent({ host: host, port: port, maxSockets: 1 });
}
util.inherits(Client, EventEmitter);
Client.prototype.request = function(method, path, headers) {
var self = this;
var options = {};
options.host = self.host;
options.port = self.port;
if (method[0] === '/') {
headers = path;
path = method;
method = 'GET';
}
options.method = method;
options.path = path;
options.headers = headers;
options.agent = self.agent;
var c = new ClientRequest(options);
c.on('error', function(e) {
self.emit('error', e);
});
// The old Client interface emitted 'end' on socket end.
// This doesn't map to how we want things to operate in the future
// but it will get removed when we remove this legacy interface.
c.on('socket', function(s) {
s.on('end', function() {
if (self._decoder) {
var ret = self._decoder.end();
if (ret)
self.emit('data', ret);
}
self.emit('end');
});
});
return c;
};
exports.Client = internalUtil.deprecate(Client, 'http.Client is deprecated.');
exports.createClient = internalUtil.deprecate(function(port, host) {
return new Client(port, host);
}, 'http.createClient is deprecated. Use http.request instead.');

14
test/parallel/test-http-client-race-2.js

@ -34,13 +34,10 @@ var body2 = '';
var body3 = '';
server.on('listening', function() {
var client = http.createClient(this.address().port);
//
// Client #1 is assigned Parser #1
//
var req1 = client.request('/1');
req1.end();
var req1 = http.get({ port: this.address().port, path: '/1' });
req1.on('response', function(res1) {
res1.setEncoding('utf8');
@ -58,15 +55,11 @@ server.on('listening', function() {
// The bug would introduce itself here: Client #2 would be allocated the
// parser that previously belonged to Client #1. But we're not finished
// with Client #1 yet!
//
var client2 = http.createClient(server.address().port);
//
// At this point, the bug would manifest itself and crash because the
// internal state of the parser was no longer valid for use by Client #1
//
var req2 = client.request('/2');
req2.end();
var req2 = http.get({ port: server.address().port, path: '/2' });
req2.on('response', function(res2) {
res2.setEncoding('utf8');
res2.on('data', function(chunk) { body2 += chunk; });
@ -76,8 +69,7 @@ server.on('listening', function() {
// Just to be really sure we've covered all our bases, execute a
// request using client2.
//
var req3 = client2.request('/3');
req3.end();
var req3 = http.get({ port: server.address().port, path: '/3' });
req3.on('response', function(res3) {
res3.setEncoding('utf8');
res3.on('data', function(chunk) { body3 += chunk; });

70
test/parallel/test-http-legacy.js

@ -1,70 +0,0 @@
'use strict';
const common = require('../common');
var assert = require('assert');
var http = require('http');
var url = require('url');
var responses_sent = 0;
var body0 = '';
var body1 = '';
var server = http.createServer(function(req, res) {
if (responses_sent === 0) {
assert.equal('GET', req.method);
assert.equal('/hello', url.parse(req.url).pathname);
console.dir(req.headers);
assert.equal(true, 'accept' in req.headers);
assert.equal('*/*', req.headers['accept']);
assert.equal(true, 'foo' in req.headers);
assert.equal('bar', req.headers['foo']);
}
if (responses_sent === 1) {
assert.equal('POST', req.method);
assert.equal('/world', url.parse(req.url).pathname);
this.close();
}
req.on('end', function() {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('The path was ' + url.parse(req.url).pathname);
res.end();
responses_sent += 1;
});
req.resume();
});
server.listen(0, common.mustCall(function() {
var client = http.createClient(this.address().port);
var req = client.request('/hello', {'Accept': '*/*', 'Foo': 'bar'});
setTimeout(function() {
req.end();
}, 100);
req.on('response', common.mustCall(function(res) {
assert.equal(200, res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) { body0 += chunk; });
console.error('Got /hello response');
}));
setTimeout(common.mustCall(function() {
var req = client.request('POST', '/world');
req.end();
req.on('response', common.mustCall(function(res) {
assert.equal(200, res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) { body1 += chunk; });
console.error('Got /world response');
}));
}), 1);
}));
process.on('exit', function() {
console.error('responses_sent: ' + responses_sent);
assert.equal(2, responses_sent);
assert.equal('The path was /hello', body0);
assert.equal('The path was /world', body1);
});
Loading…
Cancel
Save