http.Client shims

This commit is contained in:
Ryan Dahl 2011-01-20 11:21:42 -08:00
parent e576d4ec79
commit 105c35b9fd

View File

@ -737,7 +737,7 @@ function ClientRequest(options) {
var method = this.method = (options.method || 'GET').toUpperCase();
var path = options.path || '/';
var headers = options.headers;
var headers = options.headers || {};
// Host header set by default.
if (options.host && !(headers.host || headers.Host || headers.HOST)) {
@ -1110,3 +1110,137 @@ exports.request = function(options, cb) {
};
exports.get = function(options, cb) {
options.method = 'GET';
var req = exports.request(options, cb);
req.end();
};
// Shims to old interface.
function Client(port, host) {
this.port = port;
this.host = host;
}
Client.prototype.request = function(method, path, headers) {
if (typeof(path) != 'string') {
// assume method was omitted, shift arguments
headers = path;
path = method;
method = 'GET';
}
var options = {
method: method,
path: path,
headers: headers,
port: this.port,
host: this.host
};
return exports.request(options);
};
exports.createClient = function(port, host) {
return new Client(port, host);
};
exports.cat = function(url, encoding_, headers_) {
var encoding = 'utf8',
headers = {},
callback = null;
// parse the arguments for the various options... very ugly
if (typeof(arguments[1]) == 'string') {
encoding = arguments[1];
if (typeof(arguments[2]) == 'object') {
headers = arguments[2];
if (typeof(arguments[3]) == 'function') callback = arguments[3];
} else {
if (typeof(arguments[2]) == 'function') callback = arguments[2];
}
} else {
// didn't specify encoding
if (typeof(arguments[1]) == 'object') {
headers = arguments[1];
callback = arguments[2];
} else {
callback = arguments[1];
}
}
var url = require('url').parse(url);
var hasHost = false;
if (Array.isArray(headers)) {
for (var i = 0, l = headers.length; i < l; i++) {
if (headers[i][0].toLowerCase() === 'host') {
hasHost = true;
break;
}
}
} else if (typeof headers === 'Object') {
var keys = Object.keys(headers);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (key.toLowerCase() == 'host') {
hasHost = true;
break;
}
}
}
if (!hasHost) headers['Host'] = url.hostname;
var content = '';
var client = exports.createClient(url.port || 80, url.hostname);
var req = client.request((url.pathname || '/') +
(url.search || '') +
(url.hash || ''),
headers);
if (url.protocol == 'https:') {
client.https = true;
}
var callbackSent = false;
req.addListener('response', function(res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
if (callback && !callbackSent) {
callback(res.statusCode);
callbackSent = true;
}
client.end();
return;
}
res.setEncoding(encoding);
res.addListener('data', function(chunk) { content += chunk; });
res.addListener('end', function() {
if (callback && !callbackSent) {
callback(null, content);
callbackSent = true;
}
});
});
client.addListener('error', function(err) {
if (callback && !callbackSent) {
callback(err);
callbackSent = true;
}
});
client.addListener('close', function() {
if (callback && !callbackSent) {
callback(new Error('Connection closed unexpectedly'));
callbackSent = true;
}
});
req.end();
};