From d0f2d465aa8e057595e98af42685d843376488a8 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 19 Feb 2010 16:26:48 -0800 Subject: [PATCH] http.cat no longer uses Promise --- lib/http.js | 52 +++++++++++++++++++++---------- src/node.js | 8 +---- test/mjsunit/test-http-cat.js | 22 ++++++++----- test/mjsunit/test-http-chunked.js | 14 +++------ 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/http.js b/lib/http.js index 59f8112fe5..2012edbdec 100644 --- a/lib/http.js +++ b/lib/http.js @@ -552,22 +552,40 @@ process.http.Client.prototype.request = function (method, url, headers) { }; -exports.cat = function (url, encoding, headers) { - var promise = new events.Promise(); - - encoding = encoding || "utf8"; +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); - headers = headers || {}; var hasHost = false; - for (var i in headers) if (i.toLowerCase() === "host") { - hasHost = true; - break; - } - if (!hasHost) { - headers["Host"] = url.hostname; + for (var i in headers) { + if (i.toLowerCase() === "host") { + hasHost = true; + break; + } } + if (!hasHost) headers["Host"] = url.hostname; var content = ""; @@ -576,21 +594,21 @@ exports.cat = function (url, encoding, headers) { req.addListener('response', function (res) { if (res.statusCode < 200 || res.statusCode >= 300) { - promise.emitError(res.statusCode); + if (callback) callback(res.statusCode); + client.close(); return; } res.setBodyEncoding(encoding); res.addListener('data', function (chunk) { content += chunk; }); res.addListener('end', function () { - promise.emitSuccess(content); + if (callback) callback(null, content); }); }); - client.addListener("error", function () { - promise.emitError(); + client.addListener("error", function (err) { + // todo an error should actually be passed here... + if (callback) callback(new Error('Connection error')); }); req.close(); - - return promise; }; diff --git a/src/node.js b/src/node.js index 4f07c7dc66..0615a82cbb 100644 --- a/src/node.js +++ b/src/node.js @@ -976,13 +976,7 @@ function cat (id, callback) { if (err) { if (callback) callback(err); } else { - http.cat(id) - .addCallback(function(content) { - if (callback) callback(null, content); - }) - .addErrback(function(err) { - if (callback) callback(err); - }); + http.cat(id, callback); } }); } else { diff --git a/test/mjsunit/test-http-cat.js b/test/mjsunit/test-http-cat.js index f38bd7dd6d..eb7cd9e403 100644 --- a/test/mjsunit/test-http-cat.js +++ b/test/mjsunit/test-http-cat.js @@ -17,16 +17,22 @@ server.listen(PORT); var got_good_server_content = false; var bad_server_got_error = false; -http.cat("http://localhost:"+PORT+"/", "utf8").addCallback(function (content) { - puts("got response"); - got_good_server_content = true; - assert.equal(body, content); - server.close(); +http.cat("http://localhost:"+PORT+"/", "utf8", function (err, content) { + if (err) { + throw err; + } else { + puts("got response"); + got_good_server_content = true; + assert.equal(body, content); + server.close(); + } }); -http.cat("http://localhost:12312/", "utf8").addErrback(function () { - puts("got error (this should happen)"); - bad_server_got_error = true; +http.cat("http://localhost:12312/", "utf8", function (err, content) { + if (err) { + puts("got error (this should happen)"); + bad_server_got_error = true; + } }); process.addListener("exit", function () { diff --git a/test/mjsunit/test-http-chunked.js b/test/mjsunit/test-http-chunked.js index c15bd132d8..4a2dac37d3 100644 --- a/test/mjsunit/test-http-chunked.js +++ b/test/mjsunit/test-http-chunked.js @@ -11,12 +11,8 @@ var server = http.createServer(function(req, res) { }); server.listen(PORT); -http.cat("http://localhost:"+PORT+"/", "utf8") - .addCallback(function (data) { - assert.equal(UTF8_STRING, data); - server.close(); - }) - .addErrback(function() { - assert.ok(false, 'http.cat should succeed in < 1000ms'); - }) - .timeout(1000); +http.cat("http://localhost:"+PORT+"/", "utf8", function (err, data) { + if (err) throw err; + assert.equal(UTF8_STRING, data); + server.close(); +})