Skip to content

Commit

Permalink
Don't rely on automatic semicolon insertion (pretty please :)
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 22, 2011
1 parent 051fa97 commit 5e59fb5
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions request/main.js
Expand Up @@ -13,71 +13,71 @@ function request (options, callback) {
options.uri = options.url;
delete options.url;
}

if (!options.uri) {
throw new Error("options.uri is a required argument")
throw new Error("options.uri is a required argument");
} else {
if (typeof options.uri == "string") options.uri = url.parse(options.uri);
}
if (options.proxy) {
if (typeof options.proxy == 'string') options.proxy = url.parse(options.proxy);
}

options._redirectsFollowed = options._redirectsFollowed ? options._redirectsFollowed : 0;
options.maxRedirects = options.maxRedirects ? options.maxRedirects : 10;

options.followRedirect = (options.followRedirect === undefined) ? true : options.followRedirect;
options.method = options.method ? options.method : 'GET';

options.headers = options.headers ? options.headers : {};
if (!options.headers.host) {
options.headers.host = options.uri.hostname;
if (options.uri.port) {
if ( !(options.uri.port === 80 && options.uri.protocol === 'http:') &&
if ( !(options.uri.port === 80 && options.uri.protocol === 'http:') &&
!(options.uri.port === 443 && options.uri.protocol === 'https:') )
options.headers.host += (':'+options.uri.port)
options.headers.host += (':'+options.uri.port);
}
var setHost = true;
} else {
var setHost = false;
}
if (!options.uri.pathname) {options.uri.pathname = '/'}

if (!options.uri.pathname) {options.uri.pathname = '/';}
if (!options.uri.port) {
if (options.uri.protocol == 'http:') {options.uri.port = 80}
else if (options.uri.protocol == 'https:') {options.uri.port = 443}
if (options.uri.protocol == 'http:') {options.uri.port = 80;}
else if (options.uri.protocol == 'https:') {options.uri.port = 443;}
}

if (options.bodyStream) {
sys.error('options.bodyStream is deprecated. use options.reponseBodyStream instead.');
options.responseBodyStream = options.bodyStream;
}
if (options.proxy) {
var secure = (options.proxy.protocol == 'https:') ? true : false
var secure = (options.proxy.protocol == 'https:') ? true : false;
options.client = options.client ? options.client : http.createClient(options.proxy.port, options.proxy.hostname, secure);
} else {
var secure = (options.uri.protocol == 'https:') ? true : false
var secure = (options.uri.protocol == 'https:') ? true : false;
options.client = options.client ? options.client : http.createClient(options.uri.port, options.uri.hostname, secure);
}

var clientErrorHandler = function (error) {
if (setHost) delete options.headers.host;
if (callback) callback(error);
}
};
options.client.addListener('error', clientErrorHandler);

if (options.uri.auth && !options.headers.authorization) {
options.headers.authorization = "Basic " + toBase64(options.uri.auth);
}
if (options.proxy && options.proxy.auth && !options.headers['proxy-authorization']) {
options.headers['proxy-authorization'] = "Basic " + toBase64(options.proxy.auth);
}

options.fullpath = options.uri.href.replace(options.uri.protocol + '//' + options.uri.host, '');
if (options.fullpath.length === 0) options.fullpath = '/'
if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath)
if (options.fullpath.length === 0) options.fullpath = '/';

if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);

if (options.body) {
options.body = Buffer.isBuffer(options.body) ? options.body : new Buffer(options.body);
options.headers['content-length'] = options.body.length;
Expand All @@ -92,34 +92,34 @@ function request (options, callback) {
}
else {
buffer = '';
response.addListener("data", function (chunk) { buffer += chunk; } )
response.addListener("data", function (chunk) { buffer += chunk; } );
}

response.addListener("end", function () {
options.client.removeListener("error", clientErrorHandler);

if (response.statusCode > 299 && response.statusCode < 400 && options.followRedirect && response.headers.location) {
if (options._redirectsFollowed >= options.maxRedirects) client.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."))
options._redirectsFollowed += 1
if (options._redirectsFollowed >= options.maxRedirects) client.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."));
options._redirectsFollowed += 1;
if (response.headers.location.slice(0, 'http:'.length) !== 'http:' &&
response.headers.location.slice(0, 'https:'.length) !== 'https:'
) {
response.headers.location = url.resolve(options.uri.href, response.headers.location);
}
options.uri = response.headers.location;
delete options.client;
delete options.client;
if (options.headers) {
delete options.headers.host;
}
request(options, callback);
return;
} else {options._redirectsFollowed = 0}
} else {options._redirectsFollowed = 0;}

if (setHost) delete options.headers.host;
if (callback) callback(null, response, buffer);
})
})
});
});

if (options.body) {
options.request.write(options.body, 'binary');
options.request.end();
Expand All @@ -133,6 +133,6 @@ function request (options, callback) {
module.exports = request;

request.get = request;
request.post = function () {arguments[0].method = 'POST', request.apply(request, arguments)};
request.put = function () {arguments[0].method = 'PUT', request.apply(request, arguments)};
request.head = function () {arguments[0].method = 'HEAD', request.apply(request, arguments)};
request.post = function () {arguments[0].method = 'POST', request.apply(request, arguments);};
request.put = function () {arguments[0].method = 'PUT', request.apply(request, arguments);};
request.head = function () {arguments[0].method = 'HEAD', request.apply(request, arguments);};

0 comments on commit 5e59fb5

Please sign in to comment.