<commit_before>// Load modules

var Zlib = require('zlib');
var Qs = require('querystring');
var Formidable = require('formidable');
var Err = require('./error');


// Declare internals

var internals = {};


// Read and parse body

exports.read = function (request, next) {

    var req = request.raw.req;

    if (request.method === 'get' ||
        request.method === 'head') {

        req.resume();
        return next();
    }

    // Levels are: 'stream', 'raw', 'parse'

    var level = request._route.config.payload;
    if (level === 'stream') {
        return next();
    }

    // Set parser

    var parserFunc = internals.setParser(req.headers);
    if (parserFunc instanceof Error) {
        return next(parserFunc);
    }

    // Check content size

    var contentLength = req.headers['content-length'];
    if (contentLength &&
        parseInt(contentLength, 10) > request.server.settings.payload.maxBytes) {

        return next(Err.badRequest('Payload content length greater than maximum allowed: ' + request.server.settings.payload.maxBytes));
    }

    // Return only once from events

    var isBailed = false;
    var finish = function (err) {

        if (!isBailed) {
            isBailed = true;
            return next(err);
        }
    };

    // Read incoming body

    var isGZip = (req.headers['content-encoding'] === 'gzip');
    var encoding = isGZip ? 'base64' : 'utf8';
    req.setEncoding(encoding);

    req.on('close', function () {

        return finish(Err.internal('Request closed before finished reading'));
    });

    req.on('error', function (err) {

        return finish(Err.internal('Request error before finished reading: ' + err));
    });

    var payload = '';
    req.on('data', function (chunk) {

        if (payload.length + chunk.length > request.server.settings.payload.maxBytes) {
            return finish(Err.badRequest('Payload size greater than maximum allowed: ' + request.server.settings.payload.maxBytes));
        }

        payload += chunk.toString(encoding);
    });

    req.on('end', function () {

        if (isBailed) {
            return;                 // next() already called
        }

        var unzip = function () {

            Zlib.unzip(new Buffer(payload, encoding), function (err, buffer) {

                if (err) {
                    return finish(Err.badRequest('Invalid gzip: ' + err));
                }

                var unzipped = buffer.toString();
                request.rawBody = unzipped;
                return parse(unzipped);
            });
        };

        var parse = function (result) {

            if (level !== 'parse') {    // 'raw'
                return finish();
            }

            request.payload = {};

            if (!result) {
                return finish();
            }

            parserFunc(result, function (err, payload) {

                if (err) {
                    return finish(Err.badRequest('Invalid request payload format'));
                }

                request.payload = payload;
                return finish();
            });
        };

        if (isGZip) {
            return unzip();
        }

        request.rawBody = payload;
        parse(payload);
    });

    req.resume();
};


internals.setParser = function (headers) {

    // Check content type

    var contentType = (headers && headers['content-type']) || 'application/json';           //  Defaults to 'application/json'
    var contentTypeParts = contentType.split(';');
    var mime = contentTypeParts[0];

    // JSON

    if (mime === 'application/json') {
        var jsonParser = function (result, callback) {

            var obj = null;
            try {
                obj = JSON.parse(result);
            }
            catch (exp) {
                return callback(Err.badRequest('Invalid request payload format'));
            }

            return callback(null, obj);
        };

        return jsonParser;
    }

    // Form-encoded

    if (mime === 'application/x-www-form-urlencoded') {<commit_msg>Cleanup<commit_after>// Load modules

var Zlib = require('zlib');
var Qs = require('querystring');
var Formidable = require('formidable');
var Err = require('./error');


// Declare internals

var internals = {};


// Read and parse body

exports.read = function (request, next) {

    var req = request.raw.req;

    if (request.method === 'get' ||
        request.method === 'head') {

        req.resume();
        return next();
    }

    // Levels are: 'stream', 'raw', 'parse'

    var level = request._route.config.payload;
    if (level === 'stream') {
        return next();
    }

    // Set parser

    var parserFunc = internals.setParser(req.headers);
    if (parserFunc instanceof Error) {
        return next(parserFunc);
    }

    // Check content size

    var contentLength = req.headers['content-length'];
    if (contentLength &&
        parseInt(contentLength, 10) > request.server.settings.payload.maxBytes) {

        return next(Err.badRequest('Payload content length greater than maximum allowed: ' + request.server.settings.payload.maxBytes));
    }

    // Return only once from events

    var isBailed = false;
    var finish = function (err) {

        if (!isBailed) {
            isBailed = true;
            return next(err);
        }
    };

    // Read incoming body

    var isGZip = (req.headers['content-encoding'] === 'gzip');
    var encoding = isGZip ? 'base64' : 'utf8';
    req.setEncoding(encoding);

    req.on('close', function () {

        return finish(Err.internal('Request closed before finished reading'));
    });

    req.on('error', function (err) {

        return finish(Err.internal('Request error before finished reading: ' + err));
    });

    var payload = '';
    req.on('data', function (chunk) {

        if (payload.length + chunk.length > request.server.settings.payload.maxBytes) {
            return finish(Err.badRequest('Payload size greater than maximum allowed: ' + request.server.settings.payload.maxBytes));
        }

        payload += chunk.toString(encoding);
    });

    req.on('end', function () {

        if (isBailed) {
            return;                 // next() already called
        }

        var unzip = function () {

            Zlib.unzip(new Buffer(payload, encoding), function (err, buffer) {

                if (err) {
                    return finish(Err.badRequest('Invalid gzip: ' + err));
                }

                var unzipped = buffer.toString();
                request.rawBody = unzipped;
                return parse(unzipped);
            });
        };

        var parse = function (result) {

            if (level !== 'parse') {    // 'raw'
                return finish();
            }

            request.payload = {};

            if (!result) {
                return finish();
            }

            parserFunc(result, function (err, payload) {

                if (err) {
                    return finish(Err.badRequest('Invalid request payload format'));
                }

                request.payload = payload;
                return finish();
            });
        };

        if (isGZip) {
            return unzip();
        }

        request.rawBody = payload;
        parse(payload);
    });

    req.resume();
};


internals.setParser = function (headers) {

    // Check content type

    var contentType = (headers && headers['content-type']) || 'application/json';           //  Defaults to 'application/json'
    var mime = contentType.split(';')[0];

    // JSON

    if (mime === 'application/json') {
        var jsonParser = function (result, callback) {

            var obj = null;
            try {
                obj = JSON.parse(result);
            }
            catch (exp) {
                return callback(Err.badRequest('Invalid request payload format'));
            }

            return callback(null, obj);
        };

        return jsonParser;
    }

    // Form-encoded

    if (mime === 'application/x-www-form-urlencoded') {
{'fraction_non_alphanumeric': 0.13263914714268005, 'fraction_numerical': 0.0014875418371141688, 'mean_word_length': 1.790729851262539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2303746', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2101, 'n_words': 535}