"use strict";

var https = require('https');
var fs = require('fs');
var qs = require('querystring');
var pem = require('pem');
var url = require('url');
var path = require('path');
var requirements = require('..\\requirements' + process.platform + '.json')

var PORT = 443;

function handleRequest(req, res){

  // Send file in response
  //http://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http
  //
  //Process Post Request
  if(req.method === "POST"){

    var data = '';

    req.on('data', function(chunk){
      data += chunk;
    });

    req.on('end', function(){
      var parseData = qs.parse(data);
      var prettyData = JSON.stringify(parseData, null, 2);
      console.log("Post request with:\n" + prettyData);
      res.end(prettyData);
    });

  } else if(req.method === "GET") {
    let url = req.url;
    console.log(`Url='${url}'`);
    if (url.endsWith("/download-manager/rest/tc-accepted?downloadURL=/file/cdk-2.1.0.zip")) {
      res.end('true');
    } else if(url.endsWith("/favicon.ico")) {
    } else {
      console.log(`Request to download manager `);
      for (let prop in requirements) {
        let requirement = requirements[prop];
        if(requirement['dmUrl'] && requirement['dmUrl'].endsWith(url)
          || requirement['url'] && requirement['url'].endsWith(url) ) {
          console.log('Issuing redirect ' + "https://" + req.headers['host'] + '/' + prop);
          res.writeHead(302, { "Location": "https://" + req.headers['host'] + '/requirements-cache/' + prop });
          res.end();
          return;
        }
      }
      url = url.substring(1);
      console.log(`Sending file ${url}`);
      let filePath = path.join(__dirname,"..",url);
      let fileData = fs.readFileSync(filePath);

      res.writeHead(200, {
        'Content-Type': 'application/x-msdownload',
        'Content-length': fs.statSync(filePath).size,
        'Content-Disposition': `inline; filename=${url}`});
      res.write(fileData, 'binary');
      res.end();
    }
  }
}

pem.createCertificate({days:5, selfSigned:true}, function(err, keys){
  if(err) {
    console.log(err);
    return;
  }
  var options = {
    key: keys.serviceKey,
    cert: keys.certificate
  };

  //Create a server
  var server = https.createServer(options, handleRequest);

  //Start server
  server.listen(PORT, function(){
    console.log("Server listening on: https://localhost:" + PORT);
  });
});

@@ -219,16 +219,17 @@
 irements
+-
 ' + proc

Fix fake download-manager https server start error