|
@ -552,22 +552,40 @@ process.http.Client.prototype.request = function (method, url, headers) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.cat = function (url, encoding, headers) { |
|
|
exports.cat = function (url, encoding_, headers_) { |
|
|
var promise = new events.Promise(); |
|
|
var encoding = 'utf8', |
|
|
|
|
|
headers = {}, |
|
|
encoding = encoding || "utf8"; |
|
|
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 url = require("url").parse(url); |
|
|
headers = headers || {}; |
|
|
|
|
|
|
|
|
|
|
|
var hasHost = false; |
|
|
var hasHost = false; |
|
|
for (var i in headers) if (i.toLowerCase() === "host") { |
|
|
for (var i in headers) { |
|
|
|
|
|
if (i.toLowerCase() === "host") { |
|
|
hasHost = true; |
|
|
hasHost = true; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
if (!hasHost) { |
|
|
|
|
|
headers["Host"] = url.hostname; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
if (!hasHost) headers["Host"] = url.hostname; |
|
|
|
|
|
|
|
|
var content = ""; |
|
|
var content = ""; |
|
|
|
|
|
|
|
@ -576,21 +594,21 @@ exports.cat = function (url, encoding, headers) { |
|
|
|
|
|
|
|
|
req.addListener('response', function (res) { |
|
|
req.addListener('response', function (res) { |
|
|
if (res.statusCode < 200 || res.statusCode >= 300) { |
|
|
if (res.statusCode < 200 || res.statusCode >= 300) { |
|
|
promise.emitError(res.statusCode); |
|
|
if (callback) callback(res.statusCode); |
|
|
|
|
|
client.close(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
res.setBodyEncoding(encoding); |
|
|
res.setBodyEncoding(encoding); |
|
|
res.addListener('data', function (chunk) { content += chunk; }); |
|
|
res.addListener('data', function (chunk) { content += chunk; }); |
|
|
res.addListener('end', function () { |
|
|
res.addListener('end', function () { |
|
|
promise.emitSuccess(content); |
|
|
if (callback) callback(null, content); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
client.addListener("error", function () { |
|
|
client.addListener("error", function (err) { |
|
|
promise.emitError(); |
|
|
// todo an error should actually be passed here...
|
|
|
|
|
|
if (callback) callback(new Error('Connection error')); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
req.close(); |
|
|
req.close(); |
|
|
|
|
|
|
|
|
return promise; |
|
|
|
|
|
}; |
|
|
}; |
|
|