<commit_before>var http = require('http'),
  url = require('url'),
  request = require('request'),
  u = require('underscore');

var apiPort = process.argv[2] || 8000;

http.createServer(function(req, res) {
  var params = url.parse(req.url, true).query,
    apiUrl = params.url || params.src;

  if (!apiUrl){
    res.end('welcome');
  } else {
    request({
      uri: apiUrl,
      strictSSL: false,
      headers: {
        Accept: 'application/json'
      }
    }, function(error, response, body){
      var status, json;
      if (error){
        status = 502; // bad gateway
        json = JSON.stringify({ error: error.message || body });
      } else {
        status = response.statusCode;
        json = body;
      }

      // copy headers from the external request
      var finalHeaders = u.extend({}, response.headers, {
        'access-control-allow-origin': '*' // allow cross-domain AJAX (CORS)
      });
      // remove headers that node should generate
      delete finalHeaders['content-length'];
      delete finalHeaders.connection;
      delete finalHeaders.server;

      var callbackName = params.callback || params.jsonp,
        finalBody;

      if (callbackName) {
        finalHeaders['content-type'] = 'text/javascript';
        finalBody = callbackName + '(' + json + ');';
      } else {
        // treat as an AJAX request
        finalHeaders['content-type'] = 'application/json';
        finalBody = json;
      }

      res.writeHead(status, finalHeaders);
      res.end(finalBody);
    });
  }

}).listen(apiPort);

console.log('Server running at http://127.0.0.1:' + apiPort);<commit_msg>pass request headers to external request<commit_after>var http = require('http'),
  url = require('url'),
  request = require('request'),
  u = require('underscore');

var apiPort = process.argv[2] || 8000;

function except(obj /*, properties */){
  var result = u.extend({}, obj),
    properties = u.rest(arguments);

  properties.forEach(function(prop){
    delete result[prop];
  });

  return result;
}


http.createServer(function(req, res) {
  var params = url.parse(req.url, true).query,
    apiUrl = params.url || params.src;

  if (!apiUrl){
    res.end('welcome');
  } else {
    var externalReqHeaders = except(req.headers, 'accept-encoding', 'connection', 'cookie', 'host', 'user-agent');
    externalReqHeaders.accept = 'application/json';

    request({
      uri: apiUrl,
      strictSSL: false,
      headers: externalReqHeaders
    }, function(error, response, body){
      // copy headers from the external request, but remove those that node should generate
      var finalHeaders = except(response.headers, 'content-length', 'connection', 'server'),
        callbackName = params.callback || params.jsonp,
        status, json, finalBody;

      finalHeaders['access-control-allow-origin'] = '*'; // allow cross-domain AJAX (CORS)

      if (error){
        status = 502; // bad gateway
        json = JSON.stringify({ error: error.message || body });
      } else {
        status = response.statusCode;
        json = body;
      }

      if (callbackName) {
        finalHeaders['content-type'] = 'text/javascript';
        finalBody = callbackName + '(' + json + ');';
      } else {
        // treat as an AJAX request
        finalHeaders['content-type'] = 'application/json';
        finalBody = json;
      }

      res.writeHead(status, finalHeaders);
      res.end(finalBody);
    });
  }

}).listen(apiPort);

console.log('Server running at http://127.0.0.1:' + apiPort);
{'fraction_non_alphanumeric': 0.1481586402266289, 'fraction_numerical': 0.00793201133144476, 'mean_word_length': 2.6742976066597293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5464964', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1016, 'n_words': 250}