<commit_before>/*jslint node: true*/
"use strict";

// Third-party dependencies
var express = require('express');
var app = express();
var getRawBody = require('raw-body');
var expressWs = require('express-ws');
var fs = require('fs');
var responseTime = require('response-time'); // Add X-Response-Time headers
var path = require('path');
var session = require('express-session');
var http = require('http');
var https = require('https');
var request = require('request');
var uuid = require('node-uuid');
var cors = require('cors');

// ldnode dependencies
var acl = require('./lib/acl');
var metadata = require('./lib/metadata');
var header = require('./lib/header');
var LDP = require('./lib/ldp');
var login = require('./lib/login');
var parse = require('./lib/parse');
var debug = require('./lib/logging');

// Request handlers
var getHandler = require('./lib/handlers/get.js');
var postHandler = require('./lib/handlers/post.js');
var putHandler = require('./lib/handlers/put.js');
var deleteHandler = require('./lib/handlers/delete.js');
var patchHandler = require('./lib/handlers/patch.js');

// Error page handler
var errorHandler = require('./lib/handlers/error.js');

// Setting up cors
var corsSettings = cors({
    methods: [
        'OPTIONS', 'HEAD', 'GET',
        'PATCH', 'POST', 'PUT', 'DELETE'
    ],
    exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
    credentials: true,
    maxAge: 1728000,
    origin: true
});

function ldnode (argv) {
    var ldp = new LDP(argv);
    var app = express();

    // Setting options as local variable
    app.locals.ldp = ldp;

    // Session
    app.use(session({
        secret: ldp.secret || uuid.v1(),
        saveUninitialized: false,
        resave: false
    }));

    // Adding proxy
    if (ldp.proxy) {
        proxy(app, ldp.proxy);
    }

    // Setting up routes
    app.use('/', routes());

    // Setup Express app
    if (ldp.live) {
        ws(app);
    }

    debug.server("Router attached to " + ldp.mount);

    return app;
}

function createServer(argv) {
    var app = express();
    var ldpApp = ldnode(argv);
    var ldp = ldpApp.locals.ldp;
    app.use(ldp.mount, ldpApp);

    if (ldp && (ldp.webid || ldp.key || ldp.cert) ) {
        debug.settings("SSL Private Key path: " + ldp.key);
        debug.settings("SSL Certificate path: " + ldp.cert);

        if (!ldp.cert && !ldp.key) {
            throw new Error("Missing SSL cert and SSL key to enable WebID");
        }

        if (!ldp.key && ldp.cert) {
            throw new Error("Missing path for SSL key");
        }

        if (!ldp.cert && ldp.key) {
            throw new Error("Missing path for SSL cert");
        }

        var key;
        try {
            key = fs.readFileSync(ldp.key);
        } catch(e) {
            throw new Error("Can't find SSL key in " + ldp.key);
        }

        var cert;
        try {
            cert = fs.readFileSync(ldp.cert);
        } catch(e) {
            throw new Error("Can't find SSL cert in " + ldp.cert);
        }

        var credentials = {
                key: key,
                cert: cert,
                requestCert: true
            };

        debug.settings("Private Key: " + credentials.key);
        debug.settings("Certificate: " + credentials.cert);

        return https.createServer(credentials, app);
    }

    return app;
}

function proxy (app, path) {
    debug.settings('XSS Proxy listening to ' + path);
    app.get(path, corsSettings, function (req, res) {
        debug.settings('originalUrl: ' + req.originalUrl);
        var uri = req.query.uri;
        if (!uri) {
            return res
                .status(400)
                .send("Proxy has no uri param ");
        }

        debug.settings('Proxy destination URI: ' + uri);
        request.get(uri).pipe(res);
    });
}

function routes () {
    var router = express.Router('/');

    // Add Link headers
    router.use(header.linksHandler);

    // Setting CORS
    router.use(corsSettings);

    router.use('/*', function(req, res, next) {
        getRawBody(req,
                   {
                       length: req.headers['content-length'],
                       limit: '1mb',
                       encoding: 'utf-8' // typer.parse(req.headers['content-type']).parameters.charset
                   },
                   function(err, string) {
                       if (err) {
                           return next(err);
                       }
                       req.text = string;
                       next();
                   });
    });

    router.use('/*', login.loginHandler);

    //ACL handlers
    router.get("/*", acl.allowReadHandler);<commit_msg>Removed the 1mb limit on raw GET<commit_after>/*jslint node: true*/
"use strict";

// Third-party dependencies
var express = require('express');
var app = express();
var getRawBody = require('raw-body');
var expressWs = require('express-ws');
var fs = require('fs');
var responseTime = require('response-time'); // Add X-Response-Time headers
var path = require('path');
var session = require('express-session');
var http = require('http');
var https = require('https');
var request = require('request');
var uuid = require('node-uuid');
var cors = require('cors');

// ldnode dependencies
var acl = require('./lib/acl');
var metadata = require('./lib/metadata');
var header = require('./lib/header');
var LDP = require('./lib/ldp');
var login = require('./lib/login');
var parse = require('./lib/parse');
var debug = require('./lib/logging');

// Request handlers
var getHandler = require('./lib/handlers/get.js');
var postHandler = require('./lib/handlers/post.js');
var putHandler = require('./lib/handlers/put.js');
var deleteHandler = require('./lib/handlers/delete.js');
var patchHandler = require('./lib/handlers/patch.js');

// Error page handler
var errorHandler = require('./lib/handlers/error.js');

// Setting up cors
var corsSettings = cors({
    methods: [
        'OPTIONS', 'HEAD', 'GET',
        'PATCH', 'POST', 'PUT', 'DELETE'
    ],
    exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
    credentials: true,
    maxAge: 1728000,
    origin: true
});

function ldnode (argv) {
    var ldp = new LDP(argv);
    var app = express();

    // Setting options as local variable
    app.locals.ldp = ldp;

    // Session
    app.use(session({
        secret: ldp.secret || uuid.v1(),
        saveUninitialized: false,
        resave: false
    }));

    // Adding proxy
    if (ldp.proxy) {
        proxy(app, ldp.proxy);
    }

    // Setting up routes
    app.use('/', routes());

    // Setup Express app
    if (ldp.live) {
        ws(app);
    }

    debug.server("Router attached to " + ldp.mount);

    return app;
}

function createServer(argv) {
    var app = express();
    var ldpApp = ldnode(argv);
    var ldp = ldpApp.locals.ldp;
    app.use(ldp.mount, ldpApp);

    if (ldp && (ldp.webid || ldp.key || ldp.cert) ) {
        debug.settings("SSL Private Key path: " + ldp.key);
        debug.settings("SSL Certificate path: " + ldp.cert);

        if (!ldp.cert && !ldp.key) {
            throw new Error("Missing SSL cert and SSL key to enable WebID");
        }

        if (!ldp.key && ldp.cert) {
            throw new Error("Missing path for SSL key");
        }

        if (!ldp.cert && ldp.key) {
            throw new Error("Missing path for SSL cert");
        }

        var key;
        try {
            key = fs.readFileSync(ldp.key);
        } catch(e) {
            throw new Error("Can't find SSL key in " + ldp.key);
        }

        var cert;
        try {
            cert = fs.readFileSync(ldp.cert);
        } catch(e) {
            throw new Error("Can't find SSL cert in " + ldp.cert);
        }

        var credentials = {
                key: key,
                cert: cert,
                requestCert: true
            };

        debug.settings("Private Key: " + credentials.key);
        debug.settings("Certificate: " + credentials.cert);

        return https.createServer(credentials, app);
    }

    return app;
}

function proxy (app, path) {
    debug.settings('XSS Proxy listening to ' + path);
    app.get(path, corsSettings, function (req, res) {
        debug.settings('originalUrl: ' + req.originalUrl);
        var uri = req.query.uri;
        if (!uri) {
            return res
                .status(400)
                .send("Proxy has no uri param ");
        }

        debug.settings('Proxy destination URI: ' + uri);
        request.get(uri).pipe(res);
    });
}

function routes () {
    var router = express.Router('/');

    // Add Link headers
    router.use(header.linksHandler);

    // Setting CORS
    router.use(corsSettings);

    router.use('/*', function(req, res, next) {
        getRawBody(req,
                   {
                       length: req.headers['content-length'],
                       encoding: 'utf-8' // typer.parse(req.headers['content-type']).parameters.charset
                   },
                   function(err, string) {
                       if (err) {
                           return next(err);
                       }
                       req.text = string;
                       next();
                   });
    });

    router.use('/*', login.loginHandler);

    //ACL handlers
    router.get("/*", acl.allowReadHandler);
{'fraction_non_alphanumeric': 0.15228966986155484, 'fraction_numerical': 0.0027689030883919063, 'mean_word_length': 2.0041586692258475, 'pattern_counts': {'":': 0, '<': 3, '<?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': '429476', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2738, 'n_words': 705}