<commit_before>var api = require('./api');

module.exports = function(grunt) {
  'use strict';

  /* server.js */
  var express = require('express'),
      util = require('util');

  var startServer = function(config) {
    config = config || {};
    var requester = require(config.proxyProtocol);
    var server = express();
    var hourMs = config.hourMs || 0*60*60,
        vhost = config.vhost || 'localhost',
        base = config.base,
        port = config.port,
        host = config.host,
        apiPrefix = config.apiPrefix || '/api',
        basicAuth = config.basicAuth,
        additionalParams = config.additionalParams || {};

    function proxyRequest(request, response) {
      var postData = request.body;
      var options = {
        host: host,
        port: config.proxyPort,
        method: request.method,
        path: request.originalUrl.replace(new RegExp(apiPrefix), ''),
        headers: {}
      };
      var jsonData;
      options.headers.host = host;
      if (basicAuth) {
        options.headers.Authorization = "Basic " + new Buffer(basicAuth.username + ":" + basicAuth.password).toString("base64");
      }
      if ('POST' === request.method && typeof postData === 'object') {
        postData = JSON.stringify(postData);
      }
      for (var param in additionalParams) {
        options.path += options.path.indexOf('?') > -1 ? '&' : '?';
        if (additionalParams.hasOwnProperty(param)) {
          options.path += encodeURIComponent(param) + '=' + encodeURIComponent(additionalParams[param]);
        }
      }
      var req = requester.request(options, function(res) {
        var output = '';
        console.log(options.method + ' @ ' + options.host + options.path + ' Code: '+ res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          output += chunk;
        });
        res.on('end', function() {
          response
            .status(res.statusCode);
          try {
            jsonData = JSON.parse(output);
          } catch(e) {
            jsonData = null;
          }
          if (typeof jsonData === 'object') {
            response.json(jsonData);
          } else {
            response.send(output);
          }
        });
      });

      req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
      });

      if ('POST' === request.method) {
        req.write(postData);
      }

      req.end();
    }

    server.use(express['static'](base, {maxAge: hourMs}));
    server.use(express.directory(base, {icons: true}));
    server.use(express.bodyParser());
    server.use(express.errorHandler({dumpExceptions: true, showStack: true}));

    server.use(express.basicAuth('test', 'test'));

    // Custom API
    api.init(server);

    server.all(apiPrefix + '*', proxyRequest);

    server.get('/*', function(req, res) {<commit_msg>move basic auth challenge before all other express handlers<commit_after>var api = require('./api');

module.exports = function(grunt) {
  'use strict';

  /* server.js */
  var express = require('express'),
      util = require('util');

  var startServer = function(config) {
    config = config || {};
    var requester = require(config.proxyProtocol);
    var server = express();
    var hourMs = config.hourMs || 0*60*60,
        vhost = config.vhost || 'localhost',
        base = config.base,
        port = config.port,
        host = config.host,
        apiPrefix = config.apiPrefix || '/api',
        basicAuth = config.basicAuth,
        additionalParams = config.additionalParams || {};

    function proxyRequest(request, response) {
      var postData = request.body;
      var options = {
        host: host,
        port: config.proxyPort,
        method: request.method,
        path: request.originalUrl.replace(new RegExp(apiPrefix), ''),
        headers: {}
      };
      var jsonData;
      options.headers.host = host;
      if (basicAuth) {
        options.headers.Authorization = "Basic " + new Buffer(basicAuth.username + ":" + basicAuth.password).toString("base64");
      }
      if ('POST' === request.method && typeof postData === 'object') {
        postData = JSON.stringify(postData);
      }
      for (var param in additionalParams) {
        options.path += options.path.indexOf('?') > -1 ? '&' : '?';
        if (additionalParams.hasOwnProperty(param)) {
          options.path += encodeURIComponent(param) + '=' + encodeURIComponent(additionalParams[param]);
        }
      }
      var req = requester.request(options, function(res) {
        var output = '';
        console.log(options.method + ' @ ' + options.host + options.path + ' Code: '+ res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          output += chunk;
        });
        res.on('end', function() {
          response
            .status(res.statusCode);
          try {
            jsonData = JSON.parse(output);
          } catch(e) {
            jsonData = null;
          }
          if (typeof jsonData === 'object') {
            response.json(jsonData);
          } else {
            response.send(output);
          }
        });
      });

      req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
      });

      if ('POST' === request.method) {
        req.write(postData);
      }

      req.end();
    }

    server.use(express.basicAuth('test', 'test'));

    server.use(express['static'](base, {maxAge: hourMs}));
    server.use(express.directory(base, {icons: true}));
    server.use(express.bodyParser());
    server.use(express.errorHandler({dumpExceptions: true, showStack: true}));

    // Custom API
    api.init(server);

    server.all(apiPrefix + '*', proxyRequest);

    server.get('/*', function(req, res) {
{'fraction_non_alphanumeric': 0.15506547208821503, 'fraction_numerical': 0.0031013094417643005, 'mean_word_length': 2.2557487380818846, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7021615', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1654, 'n_words': 328}