<commit_before>const config = require("../config/local.json");
const database = require("./database.js");
const logger = require("./logging.js");

exports.handleError = function(err, req, res, next) {
	res.status(500).send({"success": false, "message": "Internal Server Error"});
};

exports.handleGetHomeRequest = function(req, res) {
	res.send({"putr": "https://github.com/MarvinMenzerath/putr"});
};

exports.handleGetRequest = function(req, res) {
	res.send({"TODO": "TODO"});
};

exports.handlePutRequest = function(req, res) {
	// check if given endpoint exists
	let endpointConfig = {};
	config.storage.forEach(function(item) {
		if (item.endpoint === req.params.endpoint) {
			endpointConfig = item;
		}
	});

	// endpoint does not exist
	if (endpointConfig === undefined) {
		res.status(404).send({"success": false, "message": "Unknown endpoint"});
		logger.warn("Unknown endpoint '%s'", req.params.endpoint);
		return;
	} else {
		logger.info("Handling endpoint '%s'", req.params.endpoint);
	}

	// validate authentication token
	let data = req.body;
	if (!data.auth || endpointConfig.auth !== data.auth) {
		res.status(403).send({"success": false, "message": "Invalid authentication"});
		logger.warn("Invalid authentication on '%s'", req.params.endpoint);
		return;
	}

	// only insert whitelisted keys into database
	for (let key in data) {
		if (data.hasOwnProperty(key)) {
			if (endpointConfig.save.indexOf(key) === -1) {
				// remove non-whitelisted entry
				delete data[key];
			}
		}
	}

	// insert data into database
	database.getConnection((err, connection) => {
		if (err) {
			logger.error("No database connection (%s)", err);
			return;
		}

		connection.query("INSERT INTO " + endpointConfig.tablename + " SET ?", data, function(err, rows) {
			if (!err && rows.affectedRows === 1) {
				res.send({"success": true, "message": "Data saved"});
			} else {
				logger.error("Unable to save new data (%s)", err);
			}
			connection.release();
		});
	});
};<commit_msg>Added check for empty insert-objects<commit_after>const config = require("../config/local.json");
const database = require("./database.js");
const logger = require("./logging.js");

exports.handleError = function(err, req, res, next) {
	res.status(500).send({"success": false, "message": "Internal Server Error"});
};

exports.handleGetHomeRequest = function(req, res) {
	res.send({"putr": "https://github.com/MarvinMenzerath/putr"});
};

exports.handleGetRequest = function(req, res) {
	res.send({"TODO": "TODO"});
};

exports.handlePutRequest = function(req, res) {
	// check if given endpoint exists
	let endpointConfig = {};
	config.storage.forEach(function(item) {
		if (item.endpoint === req.params.endpoint) {
			endpointConfig = item;
		}
	});

	// endpoint does not exist
	if (endpointConfig === undefined) {
		res.status(404).send({"success": false, "message": "Unknown endpoint"});
		logger.warn("Unknown endpoint '%s'", req.params.endpoint);
		return;
	} else {
		logger.info("Handling endpoint '%s'", req.params.endpoint);
	}

	// validate authentication token
	let data = req.body;
	if (!data.auth || endpointConfig.auth !== data.auth) {
		res.status(403).send({"success": false, "message": "Invalid authentication"});
		logger.warn("Invalid authentication on '%s'", req.params.endpoint);
		return;
	}

	// only insert whitelisted keys into database
	for (let key in data) {
		if (data.hasOwnProperty(key)) {
			if (endpointConfig.save.indexOf(key) === -1) {
				// remove non-whitelisted entry
				delete data[key];
			}
		}
	}

	// check if data is empty
	if (Object.keys(data).length === 0) {
		res.status(400).send({"success": false, "message": "No insertable data given"});
		logger.warn("No insertable data given on '%s'", req.params.endpoint);
		return;
	}

	// insert data into database
	database.getConnection((err, connection) => {
		if (err) {
			logger.error("No database connection (%s)", err);
			return;
		}

		connection.query("INSERT INTO " + endpointConfig.tablename + " SET ?", data, function(err, rows) {
			if (!err && rows.affectedRows === 1) {
				res.send({"success": true, "message": "Data saved"});
			} else {
				logger.error("Unable to save new data (%s)", err);
			}
			connection.release();
		});
	});
};
{'fraction_non_alphanumeric': 0.1813679245283019, 'fraction_numerical': 0.006132075471698114, 'mean_word_length': 4.146844660194175, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1587239', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1335, 'n_words': 325}