<commit_before>/*
TODO :
	* implement is_port_acceptable function
	* make each function can be call separately
*/

/*
dependencies
*/
var checkremote = exports,
	urllib = require('url'),
	dns = require('dns'),
	ip = require('ip'),
	Q = require('q');

function all_addrs(data) {
	/*
	Return via promise array of IP addresses linked to an hostname.
	*/
	var deferred = Q.defer();
	dns.resolve(data.host, function(err, addrs) {
		if (err) deferred.resolve(err);
		data['addrs'] = addrs;
		deferred.resolve(data);
	});
	return deferred.promise;
}

function is_addr_local(addr) {
	/*
	Return true if the given ip addresse (addr) is local, else false.
	*/
	if (ip.isPrivate(addr) == true) {
		return true;
	} else {
		return false;
	}
}

function is_host_local(data) {
	/*
	Test if a hostname has local IP addresses.

    This function checks every IP address given, which are associated to an hostname.

    Returns true if local addresse detected
    or false if no local addresse detected.
    */
	data['is_host_local'] = false;
	for (var index in data.addrs) {
		if (is_addr_local(data.addrs[index]) == true) {
			data['is_host_local'] = true;
		}
	}
	return data;
}

function is_host_blacklisted(data) {
	/*
	Test if the hostname is not blacklisted
	
	Return true if blacklisted hostname
	or false if not blacklisted.
	*/
	if (data.blacklist.indexOf(data.host) == -1) {
		data['is_host_blacklisted'] = false;
	} else {
		data['is_host_blacklisted'] = true;
	}
	return data;
}

function is_protocol_acceptable(data) {
	/*
	Test if the protocol asked is acceptable
	
	Return true if acceptable protocol else return false.

	Acceptable protocol list can be define in options arg.
	By default acceptables protocols are http & https.
	*/
	if (data.protocol_scheme.indexOf(data.protocol) == -1) {
		data['is_protocol_acceptable'] = false;
	} else {
		data['is_protocol_acceptable'] = true;
	}
	return data;
}

checkremote.check_url_safety = function(url, cb, options) {
	/*
	Run a check suite to control safety of an url given.
	
	Options are available :
		* protocol_scheme : list of protocols accepted. By default : http, https
		* blacklist : list of blacklisted hostnames. By default : none

	Result of the check suite is recover via callback given.

	Return true if all checks passed else return a report with value (true or false) for each check.
	*/
	if(!urllib.parse(url).host) {
		cb(false);
		return;
	}
	if(!urllib.parse(url).protocol) {
		cb(false);
		return;
	}
	if(!urllib.parse(url).hostname) {
		cb(false);
		return;
	}
	if (!options) {
		options = {};
		options.protocol_scheme = ['http', 'https'];
		options.blacklist = [];<commit_msg>add comments for documentation with jsdoc (checkremote.js)<commit_after>/**
 * @file
 */
/**
 * @module checkremote
 * @requires url
 * @requires dns
 * @requires ip
 * @requires q
 * @todo implement is_port_acceptable function
 * @todo make each function can be call separately
 * @todo docs
 */
var checkremote = exports,
	urllib = require('url'),
	dns = require('dns'),
	ip = require('ip'),
	Q = require('q');


/**
 * get all IP addresses linked to the given host
 * @augments checkremote
 * @param {Object} data data must contain at min host element for use in this function
 * @return {Promise} data promise which contain all addrs
 */
function all_addrs(data) {
	var deferred = Q.defer();
	dns.resolve(data.host, function(err, addrs) {
		if (err) deferred.resolve(err);
		data['addrs'] = addrs;
		deferred.resolve(data);
	});
	return deferred.promise;
}

/**
 * check if an IP addresses is private
 * @augments checkremote
 * @param {String} addr
 * @return {boolean} return true if the given addresse (addr) is lcoal, else false
 */
function is_addr_local(addr) {
	if (ip.isPrivate(addr) == true) {
		return true;
	} else {
		return false;
	}
}
/**
 * Test if a hostname has local IP addresses.
 * This function check every IP address given, which are associated to an hostname.
 * @augments checkremote
 * @param {Object} data data must contain at min addrs (IP addresses tab) element for use in this function
 * @return {Object} return data Object which contain is_host_local boolean. His value will be true if local addresse detected, else false
 */
function is_host_local(data) {
	data['is_host_local'] = false;
	for (var index in data.addrs) {
		if (is_addr_local(data.addrs[index]) == true) {
			data['is_host_local'] = true;
		}
	}
	return data;
}
/**
 * Test if the hostname is not blacklisted
 * @augments checkremote
 * @param {Object} data data must contain at min blacklist array for use in this function
 * @return {Object} return data Object which contain is_blacklisted boolean. His value will be true if blacklisted hostname, else false
 */
function is_host_blacklisted(data) {
	if (data.blacklist.indexOf(data.host) == -1) {
		data['is_host_blacklisted'] = false;
	} else {
		data['is_host_blacklisted'] = true;
	}
	return data;
}

function is_protocol_acceptable(data) {
	/*
	Test if the protocol asked is acceptable
	
	Return true if acceptable protocol else return false.

	Acceptable protocol list can be define in options arg.
	By default acceptables protocols are http & https.
	*/
	if (data.protocol_scheme.indexOf(data.protocol) == -1) {
		data['is_protocol_acceptable'] = false;
	} else {
		data['is_protocol_acceptable'] = true;
	}
	return data;
}

checkremote.check_url_safety = function(url, cb, options) {
	/*
	Run a check suite to control safety of an url given.
	
	Options are available :
		* protocol_scheme : list of protocols accepted. By default : http, https
		* blacklist : list of blacklisted hostnames. By default : none

	Result of the check suite is recover via callback given.

	Return true if all checks passed else return a report with value (true or false) for each check.
	*/
	if (!urllib.parse(url).host) {
		cb(false);
		return;
	}
	if (!urllib.parse(url).protocol) {
		cb(false);
		return;
	}
	if (!urllib.parse(url).hostname) {
		cb(false);
		return;
	}
	if (!options) {
		options = {};
		options.protocol_scheme = ['http', 'https'];
		options.blacklist = [];
{'fraction_non_alphanumeric': 0.1146108179419525, 'fraction_numerical': 0.0006596306068601583, 'mean_word_length': 3.91889699918897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '721841', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1938, 'n_words': 659}