<commit_before>/*jslint node: true */
'use strict';
var async     = require("async")
    , _       = require("lodash")
    , request = require("request")
    , utils   = require("./../utils")
    , commons = require("./../commons")
    , config
    , log;

/**
 * The raw headers as they were received are pass in the headers parameter, which is an array of [key, value, key2, value2, ...]
 * @param headers: array
 * @private
 */
function _getRawOriginalHeaders (rawHeaders) {
    rawHeaders = rawHeaders || [];
    var rawHeadersStr = "";
    for (var i=0, j=1; i< rawHeaders.length; i+=2, j+=2) {
        rawHeadersStr += rawHeaders[i] + ": " + rawHeaders[j];
        // for the last headers don't add '\n'
        rawHeadersStr += (i < rawHeaders.length-2) ? "\n" : "";
    }
    return rawHeadersStr;
}

/**
 * callback method to be called once request return from call
 * @param req
 * @param error
 * @param response
 * @param body
 * @param callback
 * @private
 */
function _process(req, error, response, body, callback) {
    log.debug("[processor] _process called");
    var _result = {};

    if (!response) {
        log.error("[processor] _process no response: " + response);
        _result = utils.builder.buildResponse(error.code || commons.CONST.RESPONSE_TYPE.ERROR_API_URL);
    }
    else {
        if (error) {
            log.error(commons.CONST.BODY_RESPONSE.ERROR_API_URL + " " + req.url +  " Error Code: " + error.code + ", IP: " + req.ip);
            _result = utils.builder.buildResponse(commons.CONST.RESPONSE_TYPE.ERROR_API_URL);
        } else {
            try {
                log.debug("[processor] _process body: " + body);
                _result.body = JSON.parse(body);
            } catch (err) {
                _result.body = body;
                log.debug(commons.CONST.BODY_RESPONSE.NO_JSON_OBJECT + ", IP: " + req.ip);
            }
        }

        // set the headers/status in the _result object
        _result.headers = _result.headers || response.headers;
        _result.statusCode = _result.statusCode || response.statusCode;
        _result.HTTPStatus = _result.HTTPStatus || response.statusCode;

        // if the flag originalHeader is true, add originalHeader to the response
        if (config.originalHeader) {
            _result.originalHeader = _getRawOriginalHeaders(response.rawHeaders);
        }

    }

    // callback of the async.parallel method (async.parallel(reqs, function (err, _results) {...))
    // in the callback expects two parameters - error and _results
    return callback(null, _result);
}

/**
 * method to be called in parallel from async.parallelLimit
 * @param req - request to threat
 * @returns {Function}
 * @private
 */
function _getReq(req) {
    return function (cb) {
        var body = req.body || req.data;

        try {
            // body - entity body for PATCH, POST and PUT requests. Must be a Buffer or String, unless json is true. If json is true, then body must be a JSON-serializable object.
            body = (body) ? JSON.stringify(body) : null;
            req = req || {};<commit_msg>add runner test for eterator + fix useImmediate implementation add caseSensitiveHeaders per request and return the headers with the first letter with Capital letter<commit_after>/*jslint node: true */
'use strict';
var async     = require("async")
    , _       = require("lodash")
    , request = require("request")
    , utils   = require("./../utils")
    , commons = require("./../commons")
    , config
    , log;

/**
 * The raw headers as they were received are pass in the headers parameter, which is an array of [key, value, key2, value2, ...]
 * @param headers: array
 * @private
 */
function _getRawOriginalHeaders (rawHeaders) {
    rawHeaders = rawHeaders || [];
    var rawHeadersStr = "";
    var rawHeadersObj = {};
    for (var i=0, j=1; i< rawHeaders.length; i+=2, j+=2) {
        rawHeadersStr += rawHeaders[i] + ": " + rawHeaders[j];
        rawHeadersObj[rawHeaders[i]] = rawHeaders[j];
        // for the last headers don't add '\n'
        rawHeadersStr += (i < rawHeaders.length-2) ? "\n" : "";
    }
    return {
        originalHeadersStr: rawHeadersStr,
        originalHeadersObj: rawHeadersObj
    };
}

/**
 * callback method to be called once request return from call
 * @param req
 * @param error
 * @param response
 * @param body
 * @param callback
 * @private
 */
function _process(req, error, response, body, callback) {
    log.debug("[processor] _process called");
    var _result = {};
    var rawHeaders;

    if (!response) {
        log.error("[processor] _process no response: " + response);
        _result = utils.builder.buildResponse(error.code || commons.CONST.RESPONSE_TYPE.ERROR_API_URL);
    }
    else {
        if (error) {
            log.error(commons.CONST.BODY_RESPONSE.ERROR_API_URL + " " + req.url +  " Error Code: " + error.code + ", IP: " + req.ip);
            _result = utils.builder.buildResponse(commons.CONST.RESPONSE_TYPE.ERROR_API_URL);
        } else {
            try {
                log.debug("[processor] _process body: " + body);
                _result.body = JSON.parse(body);
            } catch (err) {
                _result.body = body;
                log.debug(commons.CONST.BODY_RESPONSE.NO_JSON_OBJECT + ", IP: " + req.ip);
            }
        }

        // set the headers/status in the _result object
        _result.headers = _result.headers || response.headers;
        _result.statusCode = _result.statusCode || response.statusCode;
        _result.HTTPStatus = _result.HTTPStatus || response.statusCode;

        // if the flag originalHeader is true, add originalHeader to the response
        if (config.originalHeader || req.caseSensitiveHeaders) {

            // call to the method in case one of the options is true
            rawHeaders = _getRawOriginalHeaders(response.rawHeaders);

            if (config.originalHeader) {
                _result.originalHeader = rawHeaders.originalHeadersStr;
            }

            _result.headers = (req.caseSensitiveHeaders) ? rawHeaders.originalHeadersObj : _result.headers;

        }

    }

    // callback of the async.parallel method (async.parallel(reqs, function (err, _results) {...))
    // in the callback expects two parameters - error and _results
    return callback(null, _result);
}

/**
 * method to be called in parallel from async.parallelLimit
 * @param req - request to threat
 * @returns {Function}
 * @private
 */
function _getReq(req) {
    return function (cb) {
        var body = req.body || req.data;

        try {
            // body - entity body for PATCH, POST and PUT requests. Must be a Buffer or String, unless json is true. If json is true, then body must be a JSON-serializable object.
            body = (body) ? JSON.stringify(body) : null;
            req = req || {};
{'fraction_non_alphanumeric': 0.12255617157864021, 'fraction_numerical': 0.0020426028596440037, 'mean_word_length': 2.437813440320963, 'pattern_counts': {'":': 2, '<': 7, '<?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': '2831981', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1965, 'n_words': 566}