<commit_before>var http = require('http')
var uuid = require('node-uuid')
var ncp = require('ncp').ncp
var fs = require('fs')
var crypto = require('crypto')
require('shelljs/global')

const PORT = 6000
const ARM9LOADER_SOURCE_PATH = './arm9loaderhax'

var deleteFolderRecursive = function(path) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}

var getShasums = function(path, installer){
  var algo = 'sha256'
  var shasum = crypto.createHash(algo)
  var installerSha = shasum.update(installer)
  return {installerSha: installerSha.digest('hex')}
}

function handleRequest(req, res){
  switch(req.url){
    case '/':
      var exitWithErr = function(){
        res.writeHead(500)
        res.end("something went wrong, come back later")
        return;
      }
      
      if(req.method == 'POST'){
			  var newUuid = uuid.v4()
        var newPath = './'+newUuid
				try{
          ncp(ARM9LOADER_SOURCE_PATH, newPath , function (err) {
            var f=fs.createWriteStream(newPath + '/data_input/otp.bin')
            if (err) {
              return exitWithErr()
            }else{
              req.on('data', function(chunk) {
				  	    f.write(chunk)
              })

              req.on('end', function() {
				  	    //TODO mudar esse content type
                //
                f.end()
                var oldDir = process.cwd()
                process.chdir(newPath)
                if (exec('make', {silent:true}).code !== 0) {
                  return exitWithErr()
                }else{
                  process.chdir(oldDir)
                  var installer = fs.readFileSync(newPath + '/data_output/arm9loaderhax.3dsx')
                  var shasums = getShasums(newPath, installer)
                  res.writeHead(200, "OK", {'Content-Type': 'binary', 'Installer-Sha256': shasums.installerSha, 'Presented-by': 'felipejfc'})
                  res.end(installer)
                  deleteFolderRecursive(newPath)
                  return
                }
              }) 
            }
          })
        }catch(e){
          return exitWithErr()
        }
      } else {
        res.writeHead(405, "Method not supported", {'Content-Type': 'text/plain'})
        res.end('only post is supported.')
        return 
      }
      break;
  } 
}

var server = http.createServer(handleRequest)

server.listen(PORT, function(){
  console.log("Server listening on: http://localhost:%s", PORT);
});<commit_msg>send otp sha256<commit_after>var http = require('http')
var uuid = require('node-uuid')
var ncp = require('ncp').ncp
var fs = require('fs')
var crypto = require('crypto')
require('shelljs/global')

const PORT = 6000
const ARM9LOADER_SOURCE_PATH = './arm9loaderhax'

var deleteFolderRecursive = function(path) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else {
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}

var getShasums = function(path, installer, otp){
  var algo = 'sha256'
  var shasum = crypto.createHash(algo)
  var installerSha = shasum.update(installer)
  shasum = crypto.createHash(algo)
  var otpSha = shasum.update(otp)
  return {installerSha: installerSha.digest('hex'), otpSha: otpSha.digest('hex')}
}

function handleRequest(req, res){
  switch(req.url){
    case '/':
      var exitWithErr = function(){
        res.writeHead(500)
        res.end("something went wrong, come back later")
        return;
      }

      if(req.method == 'POST'){
        var newUuid = uuid.v4()
        var newPath = './'+newUuid
	      try{
          ncp(ARM9LOADER_SOURCE_PATH, newPath , function (err) {
            var f=fs.createWriteStream(newPath + '/data_input/otp.bin')
            if (err) {
              return exitWithErr()
            }else{
              req.on('data', function(chunk) {
                 f.write(chunk)
              })

              req.on('end', function() {
                f.end()
                f.on('close', function() {
                  var oldDir = process.cwd()
                  process.chdir(newPath)
                  if (exec('make', {silent:true}).code !== 0) {
                    return exitWithErr()
                  }else{
                    process.chdir(oldDir)
                    var installer = fs.readFileSync(newPath + '/data_output/arm9loaderhax.3dsx')
                    var otp = fs.readFileSync(newPath + '/data_input/otp.bin')
                    var shasums = getShasums(newPath, installer, otp)
                    res.writeHead(200, "OK", {'Content-Type': 'binary', 'Installer-Sha256': shasums.installerSha, 'OTP-Sha256': shasums.otpSha, 'Presented-by': 'felipejfc'})
                    res.end(installer)
                    deleteFolderRecursive(newPath)
                    return
                  }
                })
              })
            }
          })
        }catch(e){
          return exitWithErr()
        }
      } else {
        res.writeHead(405, "Method not supported", {'Content-Type': 'text/plain'})
        res.end('only post is supported.')
        return
      }
      break;
    default:
      res.writeHead(404)
      res.end('not found')		
  }
}

var server = http.createServer(handleRequest)

server.listen(PORT, function(){
  console.log("Server listening on: http://localhost:%s", PORT);
});
{'fraction_non_alphanumeric': 0.13507853403141362, 'fraction_numerical': 0.010645724258289703, 'mean_word_length': 1.9526017516743945, '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': '4053057', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1767, 'n_words': 338}