Socket
Socket
Sign inDemoInstall

request

Package Overview
Dependencies
0
Maintainers
0
Versions
126
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

6

._package.json

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR����"�"com.macromates.caret{
column = 18;
line = 3;
Mac OS X  2��ATTR 2��#�#com.macromates.caret{
column = 30;
line = 11;
}

@@ -7,6 +7,9 @@ var http = require('http')

var toBase64 = function(str) {
return (new Buffer(str || "", "ascii")).toString("base64");
return (new Buffer(str || "", "ascii")).toString("base64");
};
var isUrl = /^https?:/;
function request (options, callback) {
if (!options.callback) options.callback = callback;
if (options.url) {

@@ -17,5 +20,5 @@ // People use this property instead all the time so why not just support it.

}
if (!options.uri) {
throw new Error("options.uri is a required argument")
throw new Error("options.uri is a required argument");
} else {

@@ -27,28 +30,27 @@ if (typeof options.uri == "string") options.uri = url.parse(options.uri);

}
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 : {};
options._redirectsFollowed = options._redirectsFollowed || 0;
options.maxRedirects = (options.maxRedirects !== undefined) ? options.maxRedirects : 10;
options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true;
options.method = options.method || 'GET';
options.headers = options.headers || {};
var setHost = false;
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;
setHost = true;
}
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) {

@@ -58,16 +60,22 @@ sys.error('options.bodyStream is deprecated. use options.reponseBodyStream instead.');

}
if (options.proxy) {
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
options.client = options.client ? options.client : http.createClient(options.uri.port, options.uri.hostname, secure);
if (!options.client) {
if (options.proxy) {
options.client = http.createClient(options.proxy.port, options.proxy.hostname, options.proxy.protocol === 'https:');
} else {
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;
if (callback) callback(error);
}
if (options.onResponse) options.onResponse(error);
if (options.callback) options.callback(error);
};
options.client.addListener('error', clientErrorHandler);
if (options.uri.auth && !options.headers.authorization) {

@@ -79,49 +87,57 @@ options.headers.authorization = "Basic " + toBase64(options.uri.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.body) {options.headers['content-length'] = options.body.length}
if (options.fullpath.length === 0) options.fullpath = '/';
if (options.proxy) options.fullpath = (options.uri.protocol + '//' + options.uri.host + options.fullpath);
if (options.body !== undefined) {
options.body = Buffer.isBuffer(options.body) ? options.body : new Buffer(options.body);
options.headers['content-length'] = options.body.length;
}
options.request = options.client.request(options.method, options.fullpath, options.headers);
options.request.addListener("response", function (response) {
var buffer;
if (options.encoding) response.setEncoding(options.encoding);
if (options.responseBodyStream) {
buffer = options.responseBodyStream;
sys.pump(response, options.responseBodyStream);
if (setHost) delete options.headers.host;
response.on("end", function () {
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."));
}
options._redirectsFollowed += 1;
if (!isUrl.test(response.headers.location)) {
response.headers.location = url.resolve(options.uri.href, response.headers.location);
}
options.uri = response.headers.location;
delete options.client;
if (options.headers) {
delete options.headers.host;
}
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);
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 () { options.callback(null, response, buffer); });
}
}
else {
buffer = '';
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 (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;
if (options.headers) {
delete options.headers.host;
}
request(options, callback);
return;
} 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.write(options.body);
options.request.end();

@@ -138,4 +154,22 @@ } else if (options.requestBodyStream) {

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);
};
{ "name" : "request"
, "description" : "Simplified HTTP request method."
, "tags" : ["http", "simple", "util", "utility"]
, "version" : "1.0.0"
, "version" : "1.1.0"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
, "directories" :
{ "lib" : "lib" }
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/mikeal/node-utils.git"
, "url" : "http://github.com/mikeal/request.git"
}
, "bugs" :
{ "web" : "http://github.com/mikeal/node-utils/issues" }
, "engines" : ["node >=0.1.90"]
{ "web" : "http://github.com/mikeal/request/issues" }
, "engines" : ["node < 0.3.6"]
, "main" : "./main"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc