You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
4.5 KiB

// 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.
var util = require('util');
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var incoming = require('_http_incoming');
var IncomingMessage = exports.IncomingMessage = incoming.IncomingMessage;
var common = require('_http_common');
var parsers = exports.parsers = common.parsers;
var outgoing = require('_http_outgoing');
var OutgoingMessage = exports.OutgoingMessage = outgoing.OutgoingMessage;
var server = require('_http_server');
exports.ServerResponse = server.ServerResponse;
exports.STATUS_CODES = server.STATUS_CODES;
var agent = require('_http_agent');
var Agent = exports.Agent = agent.Agent;
var globalAgent = exports.globalAgent = agent.globalAgent;
var client = require('_http_client');
var ClientRequest = exports.ClientRequest = client.ClientRequest;
exports.request = function(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
} 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.');
}
if (options.protocol && options.protocol !== 'http:') {
throw new Error('Protocol:' + options.protocol + ' not supported.');
}
return new ClientRequest(options, cb);
};
exports.get = function(options, cb) {
var req = exports.request(options, cb);
req.end();
return req;
};
var httpSocketSetup = common.httpSocketSetup;
exports._connectionListener = server._connectionListener;
var 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 = util.deprecate(Client,
'http.Client will be removed soon. Do not use it.');
exports.createClient = util.deprecate(function(port, host) {
return new Client(port, host);
}, 'http.createClient is deprecated. Use `http.request` instead.');