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();
-};
+++ /dev/null
-// 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 common = require('../common');
-var assert = require('assert');
-var http = require('http');
-
-var body = 'exports.A = function() { return "A";}';
-var server = http.createServer(function(req, res) {
- console.log('got request');
- res.writeHead(200, [
- ['Content-Length', body.length],
- ['Content-Type', 'text/plain']
- ]);
- res.end(body);
-});
-
-var got_good_server_content = false;
-var bad_server_got_error = false;
-
-server.listen(common.PORT, function() {
- http.cat('http://localhost:' + common.PORT + '/', 'utf8',
- function(err, content) {
- if (err) {
- throw err;
- } else {
- console.log('got response');
- got_good_server_content = true;
- assert.equal(body, content);
- server.close();
- }
- });
-
- http.cat('http://localhost:12312/', 'utf8', function(err, content) {
- if (err) {
- console.log('got error (this should happen)');
- bad_server_got_error = true;
- }
- });
-});
-
-process.addListener('exit', function() {
- console.log('exit');
- assert.equal(true, got_good_server_content);
- assert.equal(true, bad_server_got_error);
-});
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();
- });
});
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();
+
});