2011-03-10 16:54:52 +08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-10-12 05:04:09 +08:00
|
|
|
var util = require('util');
|
2012-05-16 22:27:34 +08:00
|
|
|
var url = require('url');
|
2011-01-20 18:41:16 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-03-20 12:49:00 +08:00
|
|
|
|
|
|
|
|
2013-04-12 06:00:45 +08:00
|
|
|
var incoming = require('_http_incoming');
|
|
|
|
var IncomingMessage = exports.IncomingMessage = incoming.IncomingMessage;
|
2013-01-08 12:33:56 +08:00
|
|
|
|
2010-03-20 10:22:04 +08:00
|
|
|
|
2013-04-12 06:15:41 +08:00
|
|
|
var common = require('_http_common');
|
|
|
|
var parsers = exports.parsers = common.parsers;
|
2009-05-12 01:08:29 +08:00
|
|
|
|
2010-11-29 23:41:08 +08:00
|
|
|
|
2013-04-12 06:37:14 +08:00
|
|
|
var outgoing = require('_http_outgoing');
|
|
|
|
var OutgoingMessage = exports.OutgoingMessage = outgoing.OutgoingMessage;
|
2011-01-20 18:41:16 +08:00
|
|
|
|
|
|
|
|
2013-04-12 07:00:19 +08:00
|
|
|
var server = require('_http_server');
|
|
|
|
exports.ServerResponse = server.ServerResponse;
|
|
|
|
exports.STATUS_CODES = server.STATUS_CODES;
|
2009-07-15 00:31:50 +08:00
|
|
|
|
2010-11-29 23:41:08 +08:00
|
|
|
|
2013-04-12 05:47:15 +08:00
|
|
|
var agent = require('_http_agent');
|
2011-10-05 02:51:34 +08:00
|
|
|
|
2013-04-12 05:47:15 +08:00
|
|
|
var Agent = exports.Agent = agent.Agent;
|
|
|
|
var globalAgent = exports.globalAgent = agent.globalAgent;
|
2011-07-26 06:15:15 +08:00
|
|
|
|
2013-04-12 07:11:12 +08:00
|
|
|
var client = require('_http_client');
|
|
|
|
var ClientRequest = exports.ClientRequest = client.ClientRequest;
|
2011-10-05 02:51:34 +08:00
|
|
|
|
|
|
|
exports.request = function(options, cb) {
|
2012-05-16 22:27:34 +08:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = url.parse(options);
|
2013-05-16 04:25:45 +08:00
|
|
|
} else if (options && options.path && / /.test(options.path)) {
|
|
|
|
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
|
|
|
// with an additional rule for ignoring percentage-escaped characters
|
|
|
|
// but that's a) hard to capture in a regular expression that performs
|
|
|
|
// well, and b) possibly too restrictive for real-world usage. That's
|
|
|
|
// why it only scans for spaces because those are guaranteed to create
|
|
|
|
// an invalid request.
|
|
|
|
throw new TypeError('Request path contains unescaped characters.');
|
2012-05-16 22:27:34 +08:00
|
|
|
}
|
|
|
|
|
2011-10-15 08:10:49 +08:00
|
|
|
if (options.protocol && options.protocol !== 'http:') {
|
|
|
|
throw new Error('Protocol:' + options.protocol + ' not supported.');
|
|
|
|
}
|
|
|
|
|
2011-10-05 02:51:34 +08:00
|
|
|
return new ClientRequest(options, cb);
|
2011-02-05 07:14:58 +08:00
|
|
|
};
|
|
|
|
|
2011-10-05 02:51:34 +08:00
|
|
|
exports.get = function(options, cb) {
|
|
|
|
var req = exports.request(options, cb);
|
|
|
|
req.end();
|
|
|
|
return req;
|
|
|
|
};
|
2011-02-05 07:14:58 +08:00
|
|
|
|
2012-01-24 22:49:32 +08:00
|
|
|
|
2013-04-12 07:00:19 +08:00
|
|
|
var httpSocketSetup = common.httpSocketSetup;
|
2010-03-20 10:22:04 +08:00
|
|
|
|
2013-04-12 07:00:19 +08:00
|
|
|
exports._connectionListener = server._connectionListener;
|
|
|
|
var Server = exports.Server = server.Server;
|
2010-11-29 23:41:08 +08:00
|
|
|
|
2010-12-02 10:07:20 +08:00
|
|
|
exports.createServer = function(requestListener) {
|
2010-03-20 10:22:04 +08:00
|
|
|
return new Server(requestListener);
|
2009-07-15 00:31:50 +08:00
|
|
|
};
|
2009-05-19 01:33:05 +08:00
|
|
|
|
2010-11-29 23:41:08 +08:00
|
|
|
|
2011-10-05 02:51:34 +08:00
|
|
|
// Legacy Interface
|
2009-06-27 00:30:55 +08:00
|
|
|
|
2011-10-05 02:51:34 +08:00
|
|
|
function Client(port, host) {
|
2012-01-29 12:13:42 +08:00
|
|
|
if (!(this instanceof Client)) return new Client(port, host);
|
2012-06-13 03:53:08 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2011-10-05 02:51:34 +08: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 18:41:16 +08:00
|
|
|
}
|
2011-10-05 02:51:34 +08:00
|
|
|
util.inherits(Client, EventEmitter);
|
|
|
|
Client.prototype.request = function(method, path, headers) {
|
2011-01-20 18:41:16 +08:00
|
|
|
var self = this;
|
2011-10-05 02:51:34 +08: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 06:21:52 +08:00
|
|
|
});
|
2012-02-19 07:01:35 +08:00
|
|
|
// The old Client interface emitted 'end' on socket end.
|
2011-10-05 02:51:34 +08: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 06:53:11 +08:00
|
|
|
if (self._decoder) {
|
|
|
|
var ret = self._decoder.end();
|
|
|
|
if (ret)
|
|
|
|
self.emit('data', ret);
|
|
|
|
}
|
2011-10-05 02:51:34 +08:00
|
|
|
self.emit('end');
|
2011-07-26 06:21:52 +08:00
|
|
|
});
|
2011-10-05 02:51:34 +08:00
|
|
|
});
|
2010-03-20 10:22:04 +08:00
|
|
|
return c;
|
2010-05-27 08:59:55 +08:00
|
|
|
};
|
|
|
|
|
2012-06-22 02:42:33 +08:00
|
|
|
exports.Client = util.deprecate(Client,
|
|
|
|
'http.Client will be removed soon. Do not use it.');
|
2012-01-29 12:13:42 +08:00
|
|
|
|
2012-06-22 02:42:33 +08:00
|
|
|
exports.createClient = util.deprecate(function(port, host) {
|
2011-10-05 02:51:34 +08:00
|
|
|
return new Client(port, host);
|
2012-06-22 02:42:33 +08:00
|
|
|
}, 'http.createClient is deprecated. Use `http.request` instead.');
|