<commit_before>var http = require('http'),
      fs = require('fs'),
    path = require('path'),
    util = require('util');

function serveStaticFile(res, path, contentType, responseCode) {
  if(!responseCode) responseCode = 200;
  var read_path = __dirname + '/' + path;
  util.log("Read path:" + read_path);
  fs.readFile(read_path,
    function(err,data) {
      if(err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('500 - Internal Error:' + err.message);
      } else {
        res.writeHead(responseCode,
          { 'Content-Type': contentType });
        res.end(data);
      }
    }
  );
}

function loadData(res,load_path) {
  pth = buildSafePath(res,load_path);
  if(!pth)
    return;
  fs.readFile(pth,
    function(err,data) {
      if(err) {
        response = '500 - Internal Error:' + err.message;
      } else {
        var isDirectory = false;
        try {
          isDirectory = fs.lstatSync(pth).isDirectory();
        } catch(e) {
          response = '500 - Error:'+e;
          respondPost(res,response);
          return;
        }
        if(isDirectory) {
          var darray = [];
          for(var i in data) {
            var p = path.join(pth, data[i]);
            //console.log(p);
            var t = "file";
            if(fs.lstatSync(p).isDirectory())
              t = "dir";
            var ft = {type:t,name:data[i]};
            darray.push(ft);
          }
          rtnval = {type:'dir', path: path_ext, dir:darray};
          response = JSON.stringify(rtnval);
        } else {
          //console.log("Response:" + response);
          console.log("File is of length:" + data.length);
          console.log("Type:" + (typeof data));
          rtnval = {type:'file',data:data.toString()};
          response = JSON.stringify(rtnval);
        }
        console.log("Response:" + response);
        respondPost(res,response);
      }
    }
  );
}

function buildSafePath(res,rel_path) {
  var path_ext = decodeURI(rel_path);
  console.log("Path Extension:"+path_ext);
  if(path_ext.indexOf("..") >= 0) {  //we don't want anyone hacking into a parent directory
    response = '500 - Error: Attempting to access parent directories';
    respondPost(res,response);
    return null;
  }
  return path.join(__dirname, path_ext);
}

function saveData(res,save_path,data,overwrite) {
  pth = buildSafePath(res,save_path);
  if(!path)
    return;
  console.log("Path:"+pth);
  isDirectory = false;
  try {
    if(fs.lstatSync(pth).isDirectory()) {
      response = '500 - Error: Attempting to write to directory';
      respondPost(res,response);
      return;
    } else if(!overwrite) {
      rtnval = {type:'ack',msg:'fileExists'};
      respondPost(res,rtnval);
      return;
    }
  } catch(e) {
    // file does not exist yet - could have also used if (fs.existsSync(path))
  }
  try {
    console.log("Attempting to write " + data.length + "bytes to path:"+pth);
    fs.writeFile(pth,data,
      function(err) {
        if(err) {
          response = '500 - Internal Error:' + err.message;
        } else {
          console.log("File written");
          rtnval = {type:'ack',msg:'data saved'};
          response = JSON.stringify(rtnval);
        }
        respondPost(res,response);
      }
    );
  } catch(err) {
    respondPost(res,'500 - Internal Error:' + err.message);
  }
}

function respondPost(res,resval) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(resval);
}

http.createServer(function(req,res){
  util.log("Request method:" + req.method);
  if (req.method == 'GET') {
    // normalize url by removing querystring, optional
    // trailing slash, and making lowercase
    var url_path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
    util.log("Request:" + url_path);
    util.log("Request length:" + url_path.length);
    util.log("Res:" + res);
    var content_type = 'text/html';
    var ext = path.extname(url_path);
    if( ext == ".js")
      content_type = 'text/javascript';
    else if(ext == "png")
        content_type = 'image/png';
    else if(ext == "jpg")<commit_msg>Forgot to commit from copy(47) - only added log<commit_after>var http = require('http'),
      fs = require('fs'),
    path = require('path'),
    util = require('util');

function serveStaticFile(res, path, contentType, responseCode) {
  if(!responseCode) responseCode = 200;
  var read_path = __dirname + '/' + path;
  util.log("Read path:" + read_path);
  fs.readFile(read_path,
    function(err,data) {
      if(err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('500 - Internal Error:' + err.message);
      } else {
        res.writeHead(responseCode,
          { 'Content-Type': contentType });
        res.end(data);
      }
    }
  );
}

function loadData(res,load_path) {
  pth = buildSafePath(res,load_path);
  if(!pth)
    return;
  fs.readFile(pth,
    function(err,data) {
      if(err) {
        response = '500 - Internal Error:' + err.message;
      } else {
        var isDirectory = false;
        try {
          isDirectory = fs.lstatSync(pth).isDirectory();
        } catch(e) {
          response = '500 - Error:'+e;
          respondPost(res,response);
          return;
        }
        if(isDirectory) {
          var darray = [];
          for(var i in data) {
            var p = path.join(pth, data[i]);
            //console.log(p);
            var t = "file";
            if(fs.lstatSync(p).isDirectory())
              t = "dir";
            var ft = {type:t,name:data[i]};
            darray.push(ft);
          }
          rtnval = {type:'dir', path: path_ext, dir:darray};
          response = JSON.stringify(rtnval);
        } else {
          //console.log("Response:" + response);
          console.log("File is of length:" + data.length);
          console.log("Type:" + (typeof data));
          rtnval = {type:'file',data:data.toString()};
          response = JSON.stringify(rtnval);
        }
        console.log("Response:" + response);
        respondPost(res,response);
      }
    }
  );
}

function buildSafePath(res,rel_path) {
  var path_ext = decodeURI(rel_path);
  console.log("Path Extension:"+path_ext);
  if(path_ext.indexOf("..") >= 0) {  //we don't want anyone hacking into a parent directory
    response = '500 - Error: Attempting to access parent directories';
    respondPost(res,response);
    return null;
  }
  return path.join(__dirname, path_ext);
}

function saveData(res,save_path,data,overwrite) {
  pth = buildSafePath(res,save_path);
  if(!path)
    return;
  console.log("Path:"+pth);
  isDirectory = false;
  try {
    if(fs.lstatSync(pth).isDirectory()) {
      response = '500 - Error: Attempting to write to directory';
      respondPost(res,response);
      return;
    } else if(!overwrite) {
      rtnval = {type:'ack',msg:'fileExists'};
      respondPost(res,rtnval);
      return;
    }
  } catch(e) {
    // file does not exist yet - could have also used if (fs.existsSync(path))
  }
  try {
    console.log("Attempting to write " + data.length + "bytes to path:"+pth);
    fs.writeFile(pth,data,
      function(err) {
        if(err) {
          response = '500 - Internal Error:' + err.message;
        } else {
          console.log("File written");
          rtnval = {type:'ack',msg:'data saved'};
          response = JSON.stringify(rtnval);
        }
        respondPost(res,response);
      }
    );
  } catch(err) {
    respondPost(res,'500 - Internal Error:' + err.message);
  }
}

function respondPost(res,resval) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(resval);
}

http.createServer(function(req,res){
  util.log("Request method:" + req.method);
  if (req.method == 'GET') {
    // normalize url by removing querystring, optional
    // trailing slash, and making lowercase
    util.log("Request:" + req.url);
    var url_path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
    util.log("Request:" + url_path);
    util.log("Request length:" + url_path.length);
    util.log("Res:" + res);
    var content_type = 'text/html';
    var ext = path.extname(url_path);
    if( ext == ".js")
      content_type = 'text/javascript';
    else if(ext == "png")
        content_type = 'image/png';
    else if(ext == "jpg")
{'fraction_non_alphanumeric': 0.1606280193236715, 'fraction_numerical': 0.007729468599033816, 'mean_word_length': 2.4403822185292894, 'pattern_counts': {'":': 0, '<': 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': '2827163', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2677, 'n_words': 544}