<commit_before>/*
 * Copyright 2016 e-UCM (http://www.e-ucm.es/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * This project has received funding from the European Union’s Horizon
 * 2020 research and innovation programme under grant agreement No 644187.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0 (link is external)
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

var express = require('express'),
    router = express.Router(),
    httpProxy = require('http-proxy'),
    proxyOptions = {},
    pathToRe = require('path-to-regexp'),
    bodyParser = require('body-parser'),
    proxy = httpProxy.createProxyServer(proxyOptions);

var jsonParser;

proxy.on('proxyReq', function (proxyReq, req, res, options) {
    var forwardHeader = req.headers['x-forwarded-host'];
    var forwardProtocol = req.headers['x-forwarded-proto'];
    if (!forwardHeader) {
        var hostHeader = req.get('host') + '/api/proxy/' + req.params.prefix + '/';
        proxyReq.setHeader('x-forwarded-host', hostHeader);
    } else {
        proxyReq.setHeader('x-forwarded-host', forwardHeader);
    }
    if (!forwardProtocol) {
        var hostProtocol = req.protocol;
        proxyReq.setHeader('x-forwarded-proto', hostProtocol);
    } else {
        proxyReq.setHeader('x-forwarded-proto', forwardProtocol);
    }
    if (req.user) {
        var username = req.user.username;
        proxyReq.setHeader('X-Gleaner-User', username);
    }
    if (req.body) {
        var bodyData = JSON.stringify(req.body);
        // Incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
        proxyReq.setHeader('Content-Type', 'application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        // Stream the content
        proxyReq.write(bodyData);
    }
});

module.exports = function (jwtMiddleware) {

    var startsWith = function (source, str) {
        return source.slice(0, str.length) === str;
    };

    var endsWith = function (source, str) {
        return source.slice(-str.length) === str;
    };

    var proxyRequest = function (application, req, res, next, forward) {

        var host = application.host;
        if (host) {
            if (forward) {
                return forwardRequest(host, req, res, next);
            }

            var resource = application.prefix + req.params[0];<commit_msg>Fixes proxy request on failure<commit_after>/*
 * Copyright 2016 e-UCM (http://www.e-ucm.es/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * This project has received funding from the European Union’s Horizon
 * 2020 research and innovation programme under grant agreement No 644187.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0 (link is external)
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

var express = require('express'),
    router = express.Router(),
    httpProxy = require('http-proxy'),
    proxyOptions = {},
    pathToRe = require('path-to-regexp'),
    bodyParser = require('body-parser'),
    proxy = httpProxy.createProxyServer(proxyOptions);

var jsonParser;

proxy.on('proxyReq', function (proxyReq, req, res, options) {
    try {
        var forwardHeader = req.headers['x-forwarded-host'];
        var forwardProtocol = req.headers['x-forwarded-proto'];
        if (!forwardHeader) {
            var hostHeader = req.get('host') + '/api/proxy/' + req.params.prefix + '/';
            proxyReq.setHeader('x-forwarded-host', hostHeader);
        } else {
            proxyReq.setHeader('x-forwarded-host', forwardHeader);
        }
        if (!forwardProtocol) {
            var hostProtocol = req.protocol;
            proxyReq.setHeader('x-forwarded-proto', hostProtocol);
        } else {
            proxyReq.setHeader('x-forwarded-proto', forwardProtocol);
        }
        if (req.user) {
            var username = req.user.username;
            proxyReq.setHeader('X-Gleaner-User', username);
        }
        if (req.body) {
            var bodyData = JSON.stringify(req.body);
            // Incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
            proxyReq.setHeader('Content-Type', 'application/json');
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            // Stream the content
            proxyReq.write(bodyData);
        }
    } catch (err) {
        console.error(err);
    }
});

module.exports = function (jwtMiddleware) {

    var startsWith = function (source, str) {
        return source.slice(0, str.length) === str;
    };

    var endsWith = function (source, str) {
        return source.slice(-str.length) === str;
    };

    var proxyRequest = function (application, req, res, next, forward) {

        var host = application.host;
        if (host) {
            if (forward) {
                return forwardRequest(host, req, res, next);
            }

            var resource = application.prefix + req.params[0];
{'fraction_non_alphanumeric': 0.1207638052640633, 'fraction_numerical': 0.006881128505074832, 'mean_word_length': 2.755813953488372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '604549', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1584, 'n_words': 473}