2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
2015-06-13 18:44:39 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2015-09-17 00:45:29 +02:00
|
|
|
const EventEmitter = require('events');
|
2010-03-20 05:49:00 +01:00
|
|
|
|
|
|
|
|
2014-06-11 21:16:48 +02:00
|
|
|
exports.IncomingMessage = require('_http_incoming').IncomingMessage;
|
2013-01-08 05:33:56 +01:00
|
|
|
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const common = require('_http_common');
|
2015-01-28 05:08:43 +01:00
|
|
|
exports.METHODS = common.methods.slice().sort();
|
2009-05-11 19:08:29 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2014-06-11 21:16:48 +02:00
|
|
|
exports.OutgoingMessage = require('_http_outgoing').OutgoingMessage;
|
2011-01-20 11:41:16 +01:00
|
|
|
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const server = require('_http_server');
|
2013-04-12 01:00:19 +02:00
|
|
|
exports.ServerResponse = server.ServerResponse;
|
|
|
|
exports.STATUS_CODES = server.STATUS_CODES;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const agent = require('_http_agent');
|
|
|
|
const Agent = exports.Agent = agent.Agent;
|
2014-06-11 21:16:48 +02:00
|
|
|
exports.globalAgent = agent.globalAgent;
|
2011-07-26 00:15:15 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const client = require('_http_client');
|
|
|
|
const ClientRequest = exports.ClientRequest = client.ClientRequest;
|
2011-10-04 20:51:34 +02:00
|
|
|
|
|
|
|
exports.request = function(options, cb) {
|
2014-02-25 23:15:02 +01:00
|
|
|
return new ClientRequest(options, cb);
|
2011-02-05 00:14:58 +01:00
|
|
|
};
|
|
|
|
|
2011-10-04 20:51:34 +02:00
|
|
|
exports.get = function(options, cb) {
|
2014-02-25 23:15:02 +01:00
|
|
|
var req = exports.request(options, cb);
|
|
|
|
req.end();
|
|
|
|
return req;
|
2011-10-04 20:51:34 +02:00
|
|
|
};
|
2011-02-05 00:14:58 +01:00
|
|
|
|
2013-04-12 01:00:19 +02:00
|
|
|
exports._connectionListener = server._connectionListener;
|
2015-01-21 17:36:59 +01:00
|
|
|
const Server = exports.Server = server.Server;
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
exports.createServer = function(requestListener) {
|
2010-03-20 03:22:04 +01:00
|
|
|
return new Server(requestListener);
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-10-04 20:51:34 +02:00
|
|
|
// Legacy Interface
|
2009-06-26 18:30:55 +02:00
|
|
|
|
2011-10-04 20:51:34 +02:00
|
|
|
function Client(port, host) {
|
2012-01-29 05:13:42 +01:00
|
|
|
if (!(this instanceof Client)) return new Client(port, host);
|
2012-06-12 21:53:08 +02:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2011-10-04 20:51:34 +02:00
|
|
|
host = host || 'localhost';
|
|
|
|
port = port || 80;
|
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
this.agent = new Agent({ host: host, port: port, maxSockets: 1 });
|
2011-01-20 11:41:16 +01:00
|
|
|
}
|
2011-10-04 20:51:34 +02:00
|
|
|
util.inherits(Client, EventEmitter);
|
|
|
|
Client.prototype.request = function(method, path, headers) {
|
2011-01-20 11:41:16 +01:00
|
|
|
var self = this;
|
2011-10-04 20:51:34 +02:00
|
|
|
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);
|
2011-07-26 00:21:52 +02:00
|
|
|
});
|
2012-02-19 00:01:35 +01:00
|
|
|
// The old Client interface emitted 'end' on socket end.
|
2011-10-04 20:51:34 +02:00
|
|
|
// 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() {
|
2012-10-12 00:53:11 +02:00
|
|
|
if (self._decoder) {
|
|
|
|
var ret = self._decoder.end();
|
|
|
|
if (ret)
|
|
|
|
self.emit('data', ret);
|
|
|
|
}
|
2011-10-04 20:51:34 +02:00
|
|
|
self.emit('end');
|
2011-07-26 00:21:52 +02:00
|
|
|
});
|
2011-10-04 20:51:34 +02:00
|
|
|
});
|
2010-03-20 03:22:04 +01:00
|
|
|
return c;
|
2010-05-27 02:59:55 +02:00
|
|
|
};
|
|
|
|
|
2015-06-13 18:44:39 +02:00
|
|
|
exports.Client = internalUtil.deprecate(Client, 'http.Client is deprecated.');
|
2012-01-29 05:13:42 +01:00
|
|
|
|
2015-06-13 18:44:39 +02:00
|
|
|
exports.createClient = internalUtil.deprecate(function(port, host) {
|
2011-10-04 20:51:34 +02:00
|
|
|
return new Client(port, host);
|
2015-06-13 18:44:39 +02:00
|
|
|
}, 'http.createClient is deprecated. Use http.request instead.');
|