<commit_before>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

/**
  * @proxy.api.js
  *     - Very basic forward proxy implementation for Contrail WebUI
  */
var url         = require('url');
var http        = require('http');
var https       = require('https');
var config      = process.mainModule.exports.config;
var logutils    = require('../utils/log.utils');
var appErrors   = require('../errors/app.errors');
var util        = require('util');
var messages    = require('./messages');
var async       = require('async');
var commonUtils = require('../utils/common.utils');
var jsonPath    = require('JSONPath').eval;
var _           = require('underscore');
var configServerApi = require('./configServer.api');
var proxyHelper = require('./proxy.helper');
var global      = require('./global');
var nodeList     = {'hosts': {}, 'ips': {}};
var timeOutId    = null;
var proxyURLStr  = 'proxyURL';
var protocolList = ['http:', 'https:'];
/* Default allowed proxy ports per node */
var defVirtualRouterPorts   = [
    '8085', /* HttpPortAgent */
    '8102', /* HttpPortVRouterNodemgr */
];
var defControlNodePorts     = [
    '8083', /* HttpPortControl */
    '8092', /* HttpPortDns */
    '8101', /* HttpPortControlNodemgr */
]
var defAnalyticsNodePorts   = [
    '8081', /* OpServerPort */
    '8089', /* HttpPortCollector */
    '8090', /* HttpPortOpserver */
    '8091', /* HttpPortQueryEngine */
    '8104', /* HttpPortAnalyticsNodemgr */
];
var defConfigNodePorts      = [
    '5998', /* DiscoveryServerPort */
    '8082', /* ApiServerPort */
    '8084', /* HttpPortApiServer */
    '8087', /* HttpPortSchemaTransformer */
    '8088', /* HttpPortSvcMonitor */
    '8096', /* HttpPortDeviceManager */
    '8100', /* HttpPortConfigNodemgr */
];

function getAllowedProxyPortListByNodeType (nodeType)
{
    switch (nodeType) {
    case global.label.VROUTER:
        nodePortsLabel = 'vrouter_node_ports';
        defPorts = defVirtualRouterPorts;
        break;
    case global.label.CONTROL_NODE:
        nodePortsLabel = 'control_node_ports';
        defPorts = defControlNodePorts;
        break;
    case global.label.OPS_API_SERVER:
    case global.label.OPSERVER:
        nodePortsLabel = 'analytics_node_ports';
        defPorts = defAnalyticsNodePorts;
        break;
    case global.label.API_SERVER:
    case global.label.VNCONFIG_API_SERVER:
        nodePortsLabel = 'config_node_ports';
        defPorts = defConfigNodePorts;
        break;
    }
    return ((null != config.proxy) &&
            (null != config.proxy[nodePortsLabel]) &&
            (config.proxy[nodePortsLabel].length > 0)) ?
            config.proxy[nodePortsLabel] : defPorts;
}

function sendProxyRequest (request, response, appData, options, userData)
{
    var reqParams = options.query;
    var proxyURL = reqParams.proxyURL;
    var addAuthInfo = false;
    var isIndexedPage = (null != reqParams['indexPage']) ? true : false;
    logutils.logger.debug("Proxy Forwarder: Original: " + request.url +
                          " Forwarded TO: " + JSON.stringify(options));
    var protocol = ((options.protocol == 'http:') ? http : https);
    if ((null != options.port) && (null != userData)) {
        addAuthInfo = userData.addAuthInfo;
    }
    if (true == addAuthInfo) {
        options.headers = configServerApi.configAppHeaders(options.headers,
                                                           appData);
    }
    var rqst = protocol.request(options, function(res) {
        var body = '';
        res.on('end', function() {
            if (true == isIndexedPage) {
                /* Before sending the response back, modify the href */
                body =
                    body.replace(/a href="/g, 'a href="proxy?proxyURL=' +
                                 reqParams[proxyURLStr] + '/');
            } else if (true == addAuthInfo) {
                /* Api/OpServerPort needs to be handled differently */
                body = body.replace(/"href": "/g, '"href":"' + request.protocol
                        +'://'+ request.headers.host +'/proxy?proxyURL=');
                body = body.replace(/"parent_href": "/g, '"parent_href":"' + request.protocol
                        +'://'+ request.headers.host +'/proxy?proxyURL=');
            }
            response.end(body);
        });
        res.on('data', function(chunk) {
            body += chunk;
        });
    }).on('error', function(err) {
          logutils.logger.error(err.stack);
          var error =
            new appErrors.ConnectionError(util.format(messages.error.api_conn,
                                                      options.hostname + ':' +
                                                      options.port));
          response.send(global.HTTP_STATUS_INTERNAL_ERROR, error.message);
    });
    rqst.end();
}

function forwardProxyRequest (request, response, appData)
{
    var index = 0;
    var options = url.parse(request.url, true);<commit_msg>Closes-Bug: #1609591 Fixing the documentation links for ApiServer/OpServer through proxy link<commit_after>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

/**
  * @proxy.api.js
  *     - Very basic forward proxy implementation for Contrail WebUI
  */
var url         = require('url');
var http        = require('http');
var https       = require('https');
var config      = process.mainModule.exports.config;
var logutils    = require('../utils/log.utils');
var appErrors   = require('../errors/app.errors');
var util        = require('util');
var messages    = require('./messages');
var async       = require('async');
var commonUtils = require('../utils/common.utils');
var jsonPath    = require('JSONPath').eval;
var _           = require('underscore');
var configServerApi = require('./configServer.api');
var proxyHelper = require('./proxy.helper');
var global      = require('./global');
var nodeList     = {'hosts': {}, 'ips': {}};
var timeOutId    = null;
var proxyURLStr  = 'proxyURL';
var protocolList = ['http:', 'https:'];
/* Default allowed proxy ports per node */
var defVirtualRouterPorts   = [
    '8085', /* HttpPortAgent */
    '8102', /* HttpPortVRouterNodemgr */
];
var defControlNodePorts     = [
    '8083', /* HttpPortControl */
    '8092', /* HttpPortDns */
    '8101', /* HttpPortControlNodemgr */
]
var defAnalyticsNodePorts   = [
    '8081', /* OpServerPort */
    '8089', /* HttpPortCollector */
    '8090', /* HttpPortOpserver */
    '8091', /* HttpPortQueryEngine */
    '8104', /* HttpPortAnalyticsNodemgr */
];
var defConfigNodePorts      = [
    '5998', /* DiscoveryServerPort */
    '8082', /* ApiServerPort */
    '8084', /* HttpPortApiServer */
    '8087', /* HttpPortSchemaTransformer */
    '8088', /* HttpPortSvcMonitor */
    '8096', /* HttpPortDeviceManager */
    '8100', /* HttpPortConfigNodemgr */
];

function getAllowedProxyPortListByNodeType (nodeType)
{
    switch (nodeType) {
    case global.label.VROUTER:
        nodePortsLabel = 'vrouter_node_ports';
        defPorts = defVirtualRouterPorts;
        break;
    case global.label.CONTROL_NODE:
        nodePortsLabel = 'control_node_ports';
        defPorts = defControlNodePorts;
        break;
    case global.label.OPS_API_SERVER:
    case global.label.OPSERVER:
        nodePortsLabel = 'analytics_node_ports';
        defPorts = defAnalyticsNodePorts;
        break;
    case global.label.API_SERVER:
    case global.label.VNCONFIG_API_SERVER:
        nodePortsLabel = 'config_node_ports';
        defPorts = defConfigNodePorts;
        break;
    }
    return ((null != config.proxy) &&
            (null != config.proxy[nodePortsLabel]) &&
            (config.proxy[nodePortsLabel].length > 0)) ?
            config.proxy[nodePortsLabel] : defPorts;
}

function sendProxyRequest (request, response, appData, options, userData)
{
    var reqParams = options.query;
    var proxyURL = reqParams.proxyURL;
    var addAuthInfo = false;
    var isIndexedPage = (null != reqParams['indexPage']) ? true : false;
    logutils.logger.debug("Proxy Forwarder: Original: " + request.url +
                          " Forwarded TO: " + JSON.stringify(options));
    var protocol = ((options.protocol == 'http:') ? http : https);
    if ((null != options.port) && (null != userData)) {
        addAuthInfo = userData.addAuthInfo;
    }
    if (true == addAuthInfo) {
        options.headers = configServerApi.configAppHeaders(options.headers,
                                                           appData);
    }
    var reqParamStr = reqParams[proxyURLStr];
    if (-1 != reqParamStr.indexOf('/documentation/')) {
        var lastSlashIdx = reqParamStr.lastIndexOf('/');
        reqParamStr = reqParamStr.substr(0, lastSlashIdx);
        var lastHtmlIdx = reqParams[proxyURLStr].lastIndexOf('.html');
        if (-1 != lastHtmlIdx) {
            var htmlFile = reqParams[proxyURLStr].substr(lastSlashIdx + 1, lastHtmlIdx);
        }
    }
    var rqst = protocol.request(options, function(res) {
        var body = '';
        res.on('end', function() {
            if (true == isIndexedPage) {
                /* Before sending the response back, modify the href */
                body =
                    body.replace(/a href="/g, 'a href="proxy?proxyURL=' +
                                 reqParams[proxyURLStr] + '/');
            } else if (-1 != request.url.indexOf('/documentation/messages')) {
                body = body.replace(/(a href=")|(a href=)/g, function($0) {
                    var index = {
                        'a href="': 'a href="proxy?proxyURL=' +
                                        reqParamStr + '/',
                        'a href=': 'a href=proxy?proxyURL=' +
                                        reqParamStr + '/'
                    }
                    return index[$0] != undefined ? index[$0] : $0;
                });
            } else if (-1 != request.url.indexOf('/documentation')) {
                /* Remove the last / and then replace it */
                body =
                    body.replace(/href="/g, 'href="proxy?proxyURL=' +
                                 reqParamStr + '/');
                body =
                    body.replace(/documentation\/#/g, 'documentation/' +
                                 htmlFile + '#');
                var importDir = "";
                var lastSlashIdx = reqParams[proxyURLStr].lastIndexOf('/');
                importDir = reqParams[proxyURLStr].substr(0, lastSlashIdx);
                body =
                    body.replace(/import url\("/g, 'import url\("' +
                                              'proxy?proxyURL=' + importDir +
                                              '/')
                body =
                    body.replace(/src="/g, 'src="proxy?proxyURL=' +
                                 reqParamStr + '/');
            } else if (true == addAuthInfo) {
                /* Api/OpServerPort needs to be handled differently */
                body = body.replace(/"href": "/g, '"href":"' + request.protocol
                        +'://'+ request.headers.host +'/proxy?proxyURL=');
                body = body.replace(/"parent_href": "/g, '"parent_href":"' + request.protocol
                        +'://'+ request.headers.host +'/proxy?proxyURL=');
            }
            response.end(body);
        });
        res.on('data', function(chunk) {
            body += chunk;
        });
    }).on('error', function(err) {
          logutils.logger.error(err.stack);
          var error =
            new appErrors.ConnectionError(util.format(messages.error.api_conn,
                                                      options.hostname + ':' +
                                                      options.port));
          response.send(global.HTTP_STATUS_INTERNAL_ERROR, error.message);
    });
    rqst.end();
}

function forwardProxyRequest (request, response, appData)
{
    var index = 0;
    var options = url.parse(request.url, true);
{'fraction_non_alphanumeric': 0.1399633211070357, 'fraction_numerical': 0.013837945981993997, 'mean_word_length': 1.9985003749062735, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5610061', 'n_tokens_mistral': 3456, 'n_tokens_neox': 3306, 'n_words': 690}