Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A few fixes/changes from papandreou's code, also added new semantics …

…for onResponse.
  • Loading branch information...
commit fee5f89159a8f36b25df509c55093bf7ebd1c993 1 parent b2af15f
@mikeal mikeal authored
Showing with 52 additions and 25 deletions.
  1. +52 −25 main.js
View
77 main.js
@@ -7,9 +7,10 @@ var toBase64 = function(str) {
return (new Buffer(str || "", "ascii")).toString("base64");
};
-function request (options, callback) {
- callback = callback || function () {};
+var isUrl = /^https?:/;
+function request (options, callback) {
+ if (!options.callback) options.callback = callback;
if (options.url) {
// People use this property instead all the time so why not just support it.
options.uri = options.url;
@@ -26,11 +27,9 @@ function request (options, callback) {
}
options._redirectsFollowed = options._redirectsFollowed || 0;
- options.maxRedirects = options.maxRedirects || 10;
+ options.maxRedirects = (options.maxRedirects !== undefined) ? options.maxRedirects : 10;
+ options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true;
- if (!('followRedirect' in options)) {
- options.followRedirect = true;
- }
options.method = options.method || 'GET';
options.headers = options.headers || {};
@@ -62,10 +61,16 @@ function request (options, callback) {
options.client = http.createClient(options.uri.port, options.uri.hostname, options.uri.protocol === 'https:');
}
}
+
+ if (options.onResponse === true) {
+ options.onResponse = options.callback;
+ delete options.callback;
+ }
var clientErrorHandler = function (error) {
if (setHost) delete options.headers.host;
- callback(error);
+ if (options.onResponse) options.onResponse(error);
+ if (options.callback) options.callback(error);
};
options.client.addListener('error', clientErrorHandler);
@@ -81,7 +86,7 @@ function request (options, callback) {
if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);
- if ('body' in options) {
+ if (options.body !== undefined) {
options.body = Buffer.isBuffer(options.body) ? options.body : new Buffer(options.body);
options.headers['content-length'] = options.body.length;
}
@@ -92,10 +97,15 @@ function request (options, callback) {
options.client.removeListener("error", clientErrorHandler);
});
- if (response.statusCode >= 300 && 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."));
+ if (response.statusCode >= 300 &&
+ 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 (!/^https?:/.test(response.headers.location)) {
+ if (!isUrl.test(response.headers.location)) {
response.headers.location = url.resolve(options.uri.href, response.headers.location);
}
options.uri = response.headers.location;
@@ -103,29 +113,28 @@ function request (options, callback) {
if (options.headers) {
delete options.headers.host;
}
- request(options, callback);
+ request(options, options.callback);
return; // Ignore the rest of the response
} else {
options._redirectsFollowed = 0;
if (options.encoding) response.setEncoding(options.encoding);
if (options.responseBodyStream) {
sys.pump(response, options.responseBodyStream);
- callback(null, response, options.responseBodyStream);
- } else if (options.noBuffer) {
- callback(null, response);
- } else {
+ if (options.onResponse) options.onResponse(null, response);
+ if (options.callback) options.callback(null, response, options.responseBodyStream);
+ } else if (options.onResponse) {
+ options.onResponse(null, response);
+ } else if (options.callback) {
var buffer = '';
- response.on("data", function (chunk) {
- buffer += chunk;
- }).on("end", function () {
- callback(null, response, buffer);
- });
+ response
+ .on("data", function (chunk) { buffer += chunk; })
+ .on("end", function () { options.callback(null, response, buffer); });
}
}
});
if (options.body) {
- options.request.write(options.body, 'binary');
+ options.request.write(options.body);
options.request.end();
} else if (options.requestBodyStream) {
sys.pump(options.requestBodyStream, options.request);
@@ -137,6 +146,24 @@ 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 (options, callback) {
+ options.method = 'POST';
+ if (!options.body && !options.requestBodyStream) {
+ sys.error("HTTP POST requests need a body or requestBodyStream");
+ }
+ request(options, callback);
+};
+request.put = function (options, callback) {
+ options.method = 'PUT';
+ if (!options.body && !options.requestBodyStream) {
+ sys.error("HTTP PUT requests need a body or requestBodyStream");
+ }
+ request(options, callback);
+};
+request.head = function (options, callback) {
+ options.method = 'HEAD';
+ if (options.body || options.requestBodyStream) {
+ throw new Error("HTTP HEAD requests MUST NOT include a request body.");
+ }
+ request(options, callback);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.