Skip to content

Commit

Permalink
Remove http.cat. fixes nodejs#1447
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal authored and bnoordhuis committed Aug 15, 2011
1 parent 721f265 commit 584ae7b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 161 deletions.
82 changes: 0 additions & 82 deletions lib/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,85 +1491,3 @@ exports.Client = Client;
exports.createClient = function(port, host) {
return new Client(port, host);
};

exports.cat = function(url, encoding_, headers_) {
var encoding = 'utf8';
var headers = {};
var callback = null;

console.error("http.cat will be removed in the near future. use http.get");

// 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 path = (url.pathname || '/') + (url.search || '') + (url.hash || '');
var callbackSent = false;
var req = exports.request({port: url.port || 80, host: url.hostname, path: path}, 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;
}
});
});


req.addListener('error', function(err) {
if (callback && !callbackSent) {
callback(err);
callbackSent = true;
}
});

req.end();
};
64 changes: 0 additions & 64 deletions test/simple/test-http-cat.js

This file was deleted.

29 changes: 18 additions & 11 deletions test/simple/test-http-chunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf8'});
res.end(UTF8_STRING, 'utf8');
});
server.listen(common.PORT);
server.listen(common.PORT, function() {
var data = '';
var get = http.get({path:'/', host:'localhost', port:common.PORT}, function (x) {
x.setEncoding('utf8')
x.on('data', function (c) {data += c});
x.on('error', function (e) {
throw e;
})
x.on('end', function () {
assert.equal('string', typeof data);
console.log('here is the response:');
assert.equal(UTF8_STRING, data);
console.log(data);
server.close();
})
})
get.on('error', function (e) {throw e});
get.end();

server.addListener('listening', function() {
http.cat('http://127.0.0.1:' + common.PORT + '/', 'utf8',
function(err, data) {
if (err) throw err;
assert.equal('string', typeof data);
console.log('here is the response:');
assert.equal(UTF8_STRING, data);
console.log(data);
server.close();
});
});
9 changes: 5 additions & 4 deletions test/simple/test-http-set-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ server.listen(common.PORT, function() {
throw new Error('Timeout was not sucessful');
}, 2000);

var url = 'http://localhost:' + common.PORT + '/';

http.cat(url, 'utf8', function(err, content) {
var x = http.get({port:common.PORT, path:'/'});
x.on('error', function () {
clearTimeout(errorTimer);
console.log('HTTP REQUEST COMPLETE (this is good)');
});
})
x.end();

});

0 comments on commit 584ae7b

Please sign in to comment.