<commit_before>#!/usr/bin/env node

var util = require('util'),
    http = require('http'),
    fs = require('fs'),
    url = require('url'),
    events = require('events'),
    httpProxy = require('http-proxy');

var DEFAULT_PORT = process.env.PORT || 5000;

//config reverse proxy to make transparent requests to back-end services
var proxy = httpProxy.createProxyServer({});

console.log('env configuration: ' + process.env.PORT);

function main(argv) {
  new HttpServer({
    'GET': createServlet(StaticServlet),
    'HEAD': createServlet(StaticServlet)
  }).start(DEFAULT_PORT);
}

function escapeHtml(value) {
  return value.toString().
    replace('<', '&lt;').
    replace('>', '&gt;').
    replace('"', '&quot;');
}

function createServlet(Class) {
  var servlet = new Class();
  return servlet.handleRequest.bind(servlet);
}

/**
 * An Http server implementation that uses a map of methods to decide
 * action routing.
 *
 * @param {Object} Map of method => Handler function
 */
function HttpServer(handlers) {
  this.handlers = handlers;
  this.server = http.createServer(this.handleRequest_.bind(this));
}

HttpServer.prototype.start = function(port) {
  this.port = port;
  this.server.listen(port);
  util.puts('Http Server running at http://localhost:' + port + '/');
};

HttpServer.prototype.parseUrl_ = function(urlString) {
  var parsed = url.parse(urlString);
  parsed.pathname = url.resolve('/', parsed.pathname);
  return url.parse(url.format(parsed), true);
};

HttpServer.prototype.handleRequest_ = function(req, res) {
  /*
   * handle requests to be proxied to back-end services:
   * proxy jsonrpc (api) and vfs (images) requests to backend xbmc instance
   */
  var BACKEND_XBMC_SERVICE_HOST = process.env.XBMC_HOST || 'raspbmc.mmv.ie:3128';
  console.log('BACKEND_XBMC_SERVICE_HOST ['+BACKEND_XBMC_SERVICE_HOST+']');

  if((req.url.indexOf('jsonrpc') != -1 )|| (req.url.indexOf('vfs') != -1 ))
  {
    console.log('attempting proxy to xbmc - manually setting remote header for transparent proxied request');
    proxy.web(req, res, { target: 'http://' + BACKEND_XBMC_SERVICE_HOST });
  }
  //need to look at little closer at how this is operating - this may be a bit too hacky
  else
  {
      var logEntry = req.method + ' ' + req.url;
      if (req.headers['user-agent']) {
          logEntry += ' ' + req.headers['user-agent'];
      }
      util.puts(logEntry);
      req.url = this.parseUrl_(req.url);

      var handler = this.handlers[req.method];
      if (!handler) {
          res.writeHead(501);
          res.end();<commit_msg>pass configuration for backEnd service URL during startup - (was trying to access it within wrong scope)<commit_after>#!/usr/bin/env node

var util = require('util'),
    http = require('http'),
    fs = require('fs'),
    url = require('url'),
    events = require('events'),
    httpProxy = require('http-proxy');

var DEFAULT_PORT = process.env.PORT || 5000;
var BACKEND_XBMC_SERVICE_HOST = process.env.XBMC_HOST || 'raspbmc.mmv.ie:3128';

//config reverse proxy to make transparent requests to back-end services
var proxy = httpProxy.createProxyServer({});

console.log('env configuration: ' + process.env);

function main(argv) {
  new HttpServer({
    'GET': createServlet(StaticServlet),
    'HEAD': createServlet(StaticServlet)
  }).start(DEFAULT_PORT, BACKEND_XBMC_SERVICE_HOST);
}

function escapeHtml(value) {
  return value.toString().
    replace('<', '&lt;').
    replace('>', '&gt;').
    replace('"', '&quot;');
}

function createServlet(Class) {
  var servlet = new Class();
  return servlet.handleRequest.bind(servlet);
}

/**
 * An Http server implementation that uses a map of methods to decide
 * action routing.
 *
 * @param {Object} Map of method => Handler function
 */
function HttpServer(handlers) {
  this.handlers = handlers;
  this.server = http.createServer(this.handleRequest_.bind(this));
}

HttpServer.prototype.start = function(port, backEndXbmcServiceHost) {
  this.port = port;
  this.server.listen(port);
  this.backEndXbmcServiceHost = backEndXbmcServiceHost;
  util.puts('Http Server running at http://localhost:' + port + '/');
};

HttpServer.prototype.parseUrl_ = function(urlString) {
  var parsed = url.parse(urlString);
  parsed.pathname = url.resolve('/', parsed.pathname);
  return url.parse(url.format(parsed), true);
};

HttpServer.prototype.handleRequest_ = function(req, res) {
  /*
   * handle requests to be proxied to back-end services:
   * proxy jsonrpc (api) and vfs (images) requests to backend xbmc instance
   */
  console.log('BACKEND_XBMC_SERVICE_HOST ['+this.backEndXbmcServiceHost+']');

  if((req.url.indexOf('jsonrpc') != -1 )|| (req.url.indexOf('vfs') != -1 ))
  {
    console.log('attempting proxy to xbmc - manually setting remote header for transparent proxied request');
    proxy.web(req, res, { target: 'http://' + this.backEndXbmcServiceHost });
  }
  //need to look at little closer at how this is operating - this may be a bit too hacky
  else
  {
      var logEntry = req.method + ' ' + req.url;
      if (req.headers['user-agent']) {
          logEntry += ' ' + req.headers['user-agent'];
      }
      util.puts(logEntry);
      req.url = this.parseUrl_(req.url);

      var handler = this.handlers[req.method];
      if (!handler) {
          res.writeHead(501);
          res.end();
{'fraction_non_alphanumeric': 0.15106023644210922, 'fraction_numerical': 0.004878964158378683, 'mean_word_length': 3.7717099373321394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4473340', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1689, 'n_words': 412}