<commit_before>"use strict";

/*
 * RESTBase web service entry point
 *
 * Sets up a restbase instance by loading and setting up handlers and the
 * storage layer, and then dispatches requests to it.
 */

var fs = require('fs');
if (!global.Promise) {
    global.Promise = require('bluebird');
} else if (!Promise.promisify) {
    // Node 0.11+
    Promise.promisify = require('bluebird').promisify;
}
var util = require('./util');
var Restbase = require('./restbase');
var http = require('http');
var RouteSwitch = require('routeswitch');

var app = {
    // The global proxy object
    proxy: null
};

var opts = {
    // Log method
    log: util.log
};

// Handle a single request
function handleRequest (req, resp) {

    // Start off by parsing any POST data with BusBoy
    return util.parsePOST(req)

    // Then process the request
    .then(function() {
        // Create a new, clean request object
        var urlData = util.parseURL(req.url);
        var body = req.body;

        if (/^application\/json/i.test(req.headers['content-type'])) {
            try {
                body = JSON.parse(req.body.toString());
            } catch (e) {
                console.error(e);
            }
        }
        var newReq = {
            uri: urlData.pathname,
            query: urlData.query,
            method: req.method.toLowerCase(),
            headers: req.headers,
            body: body
        };
        return app.restbase.request(newReq);
    })

    // And finally handle the response
    .then(function(response) {
        //console.log('resp', response);
        if (response && response.status) {
            if (response.status >= 400) {
                if (!response.body) {
                    response.body = {};
                }
                var body = response.body;
                if (response.status === 404) {
                    if (!body.type) { body.type = 'not_found'; }
                    if (!body.title) { body.title = 'Not found.'; }
                }
                if (response.status >= 400) {
                    if (!body.uri) { body.uri = req.url; }
                    if (!body.method) { body.method = req.method.toLowerCase(); }
                }
                if (response.body.type) {
                    // Prefix error base URL
                    // XXX: make the prefix configurable
                    response.body.type = 'https://restbase.org/errors/'
                                            + response.body.type;
                }
            }
            if (response.body) {
                var body = response.body;
                // Convert to a buffer
                if (!Buffer.isBuffer(body)) {
                    if (typeof body === 'object') {<commit_msg>Set JSON content type if it's missing & we encode an object<commit_after>"use strict";

/*
 * RESTBase web service entry point
 *
 * Sets up a restbase instance by loading and setting up handlers and the
 * storage layer, and then dispatches requests to it.
 */

var fs = require('fs');
if (!global.Promise) {
    global.Promise = require('bluebird');
} else if (!Promise.promisify) {
    // Node 0.11+
    Promise.promisify = require('bluebird').promisify;
}
var util = require('./util');
var Restbase = require('./restbase');
var http = require('http');
var RouteSwitch = require('routeswitch');

var app = {
    // The global proxy object
    proxy: null
};

var opts = {
    // Log method
    log: util.log
};

// Handle a single request
function handleRequest (req, resp) {

    // Start off by parsing any POST data with BusBoy
    return util.parsePOST(req)

    // Then process the request
    .then(function() {
        // Create a new, clean request object
        var urlData = util.parseURL(req.url);
        var body = req.body;

        if (/^application\/json/i.test(req.headers['content-type'])) {
            try {
                body = JSON.parse(req.body.toString());
            } catch (e) {
                console.error(e);
            }
        }
        var newReq = {
            uri: urlData.pathname,
            query: urlData.query,
            method: req.method.toLowerCase(),
            headers: req.headers,
            body: body
        };
        return app.restbase.request(newReq);
    })

    // And finally handle the response
    .then(function(response) {
        //console.log('resp', response);
        if (response && response.status) {
            if (response.status >= 400) {
                if (!response.body) {
                    response.body = {};
                }
                var body = response.body;
                if (response.status === 404) {
                    if (!body.type) { body.type = 'not_found'; }
                    if (!body.title) { body.title = 'Not found.'; }
                }
                if (response.status >= 400) {
                    if (!body.uri) { body.uri = req.url; }
                    if (!body.method) { body.method = req.method.toLowerCase(); }
                }
                if (response.body.type) {
                    // Prefix error base URL
                    // XXX: make the prefix configurable
                    response.body.type = 'https://restbase.org/errors/'
                                            + response.body.type;
                }
            }
            if (response.body) {
                var body = response.body;
                // Convert to a buffer
                if (!Buffer.isBuffer(body)) {
                    if (typeof body === 'object') {
                        if (!response.headers['content-type']) {
                            response.headers['content-type'] = 'application/json';
                        }
{'fraction_non_alphanumeric': 0.12460623031151558, 'fraction_numerical': 0.004200210010500525, 'mean_word_length': 1.4633620689655173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894473', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1475, 'n_words': 410}