<commit_before>var express = require('express');
var router = express.Router();
var superAgent = require('superagent');
var environment = require('./environment.js');

var firstLetterToLowerCase = (world) => {
	return world.charAt(0).toLowerCase() + world.slice(1);
};

var parseCookieString = (cookieString) => {
	var cookiePairs = cookieString.split('; ');

	var cookieName = null;
	var cookieValue = null;
	var cookieOptions = {
		path: '/',
		secure: false,
		expires: new Date('Fri, 31 Dec 9999 23:59:59 UTC'),
		httpOnly: false
	};

	cookiePairs.forEach((pair) => {
		var separatorIndexEqual =  pair.indexOf('=');
		var name = decodeURIComponent(firstLetterToLowerCase(pair.substr(0, separatorIndexEqual)));
		var value = decodeURIComponent(pair.substr(separatorIndexEqual + 1));

		if (cookieOptions[name]) {
			var date = new Date(value);

			if (isNaN(date.getTime())) {
				cookieOptions[name] = value;
			} else {
				cookieOptions[name] = date;
			}

		} else {
			cookieName = name;
			cookieValue = value;
		}
	});

	return {
		name: cookieName,
		value: cookieValue,
		options: cookieOptions
	};
};


var callRemoteServer = (req, res) => {
	var url = req.url;
	var httpRequest = null;

	if ((req.url.length > 1) && (req.url[req.url.length-1] === '/')) {
		url = url.substr(0, url.length - 1);
	}

	var proxyUrl = environment.$Proxy.server + url;

	console.log(`API: ${req.method} ${proxyUrl} query:`, req.query);

	switch(req.method) {
		case 'POST':
			httpRequest = superAgent
				.post(proxyUrl)
				.send(req.body);
			break;
		case 'PUT':
			httpRequest = superAgent
				.put(proxyUrl)
				.send(req.body);
			break;
		case 'PATCH':
			httpRequest = superAgent
				.patch(proxyUrl)
				.send(req.body);
			break;
		case 'DELETE':
			httpRequest = superAgent
				.delete(proxyUrl)
				.send(req.body);
			break;
		case 'GET':
			httpRequest = superAgent
				.get(proxyUrl)
				.query(req.query);
			break;
	}

	if (req.get('Authorization')) {
		httpRequest = httpRequest.set('Authorization', req.get('Authorization'));
	}

	if (req.get('Cookie') && req.get('Cookie') !== '') {
		httpRequest = httpRequest.set('Cookie', req.get('Cookie'));
	}

	httpRequest
		.end((error, respond) => {
			if (error) {
				console.error('API ERROR', error);
				res.status(error.status || 500).json({Error: 'API error', message: error.message});
			} else if (respond) {
				var settedCookies = respond.header['set-cookie'];

				if (settedCookies) {
					settedCookies.forEach((cookieString) => {
						var cookie = parseCookieString(cookieString);
						res.cookie(cookie.name, cookie.value, cookie.options);
					});
				}
				res.status(respond.status).json(respond.body);
			}
		});
};

router.all('*', function(req, res) {
	callRemoteServer(req, res);
});

module.exports = router;<commit_msg>#16 proxy pass all headers to request<commit_after>var express = require('express');
var router = express.Router();
var superAgent = require('superagent');
var environment = require('./environment.js');

var firstLetterToLowerCase = (world) => {
	return world.charAt(0).toLowerCase() + world.slice(1);
};

var firstLetterToUpperCase = (world) => {
	return world.charAt(0).toUpperCase() + world.slice(1);
};

var parseCookieString = (cookieString) => {
	var cookiePairs = cookieString.split('; ');

	var cookieName = null;
	var cookieValue = null;
	var cookieOptions = {
		path: '/',
		secure: false,
		expires: new Date('Fri, 31 Dec 9999 23:59:59 UTC'),
		httpOnly: false
	};

	cookiePairs.forEach((pair) => {
		var separatorIndexEqual =  pair.indexOf('=');
		var name = decodeURIComponent(firstLetterToLowerCase(pair.substr(0, separatorIndexEqual)));
		var value = decodeURIComponent(pair.substr(separatorIndexEqual + 1));

		if (cookieOptions[name]) {
			var date = new Date(value);

			if (isNaN(date.getTime())) {
				cookieOptions[name] = value;
			} else {
				cookieOptions[name] = date;
			}

		} else {
			cookieName = name;
			cookieValue = value;
		}
	});

	return {
		name: cookieName,
		value: cookieValue,
		options: cookieOptions
	};
};


var callRemoteServer = (req, res) => {
	var url = req.url;
	var httpRequest = null;

	if ((req.url.length > 1) && (req.url[req.url.length-1] === '/')) {
		url = url.substr(0, url.length - 1);
	}

	var proxyUrl = environment.$Proxy.server + url;

	console.log(`API: ${req.method} ${proxyUrl} query:`, req.query);

	switch(req.method) {
		case 'POST':
			httpRequest = superAgent
				.post(proxyUrl)
				.send(req.body);
			break;
		case 'PUT':
			httpRequest = superAgent
				.put(proxyUrl)
				.send(req.body);
			break;
		case 'PATCH':
			httpRequest = superAgent
				.patch(proxyUrl)
				.send(req.body);
			break;
		case 'DELETE':
			httpRequest = superAgent
				.delete(proxyUrl)
				.send(req.body);
			break;
		case 'GET':
			httpRequest = superAgent
				.get(proxyUrl)
				.query(req.query);
			break;
	}

	Object
		.keys(req.headers)
		.filter((key) => {
			return ['host', 'Cookie'].indexOf(key) === -1;
		})
		.forEach((key) => {
			httpRequest.set(key, req.headers[key]);
		});

	if (req.get('Cookie') && req.get('Cookie') !== '') {
		httpRequest = httpRequest.set('Cookie', req.get('Cookie'));
	}

	httpRequest
		.end((error, response) => {
			if (error) {
				console.error('API ERROR', error);
				res.status(error.status || 500).json({Error: 'API error', message: error.message});
			} else if (response) {
				var settedCookies = response.header['set-cookie'];

				if (settedCookies) {
					settedCookies.forEach((cookieString) => {
						var cookie = parseCookieString(cookieString);
						res.cookie(cookie.name, cookie.value, cookie.options);
					});
				}

				res.status(response.status).json(response.body);
			}
		});
};

router.all('*', function(req, res) {
	callRemoteServer(req, res);
});

module.exports = router;
{'fraction_non_alphanumeric': 0.1704623878536922, 'fraction_numerical': 0.008799171842650104, 'mean_word_length': 3.641313050440352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '1496057', 'n_tokens_mistral': 2298, 'n_tokens_neox': 1960, 'n_words': 402}