<commit_before>var path = require('path');
var querystring = require("querystring");
var request = require("request");
var https = require("https");
var url = require('url');
var Promise = require("promise");
var config = require('./config')

module.exports = {
  home: function(req, res) {
    res.sendFile(path.join(__dirname, 'public/index.html'));
  },

  signup: function(req, res) {
    res.sendFile(path.join(__dirname, 'public/signup.html'));
  },

  newUser: function(req, res) {
    // Create User URL: hackingedu.parseapp.com/actions/n0354d89c28ec399c00d3cb2d094cf093
    //  Validation URL: hackingedu.parseapp.com/actions/v62110a75095ebf61417a51fff9af9c7f

    // This is now a proxy to Cloud Code: will probably be better for security
    // Also, all the validation is now handled by Parse servers (cloud code),
    // might be better for server load
    var post_data = querystring.stringify(req.body);

    // AJAX promise definitions
    var validate = new Promise(
      function(resolve, reject) {
        // Validate fields
        var val_request = https.request(
          {
              method: "POST",
            hostname: "hackingedu.parseapp.com",
                path: "/actions/v62110a75095ebf61417a51fff9af9c7f", // Validation AJAX
             headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "Content-Length": post_data.length
                      }
          }
        ).on("response", function httpsResponse(response) {
            response.setEncoding("utf8");
            response.on("data", function(chunk) {
              resolve(chunk);
            });
          }
        ).on("error", function httpsResponse(response) {
            console.log("POST error...?");
            reject(response);
          }
        );

        val_request.write(post_data);
        val_request.end();
      }
    );

    var createUser = new Promise(
      function(resolve, reject) {
        // Validate fields
        var new_request = https.request(
          {
              method: "POST",
            hostname: "hackingedu.parseapp.com",
                path: "/actions/n0354d89c28ec399c00d3cb2d094cf093", // New User AJAX
             headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "Content-Length": post_data.length
                      }
          }
        ).on("response", function httpsResponse(response) {
            response.setEncoding("utf8");
            response.on("data", function(chunk) {
              resolve(chunk);
            });
          }
        ).on("error", function httpsResponse(response) {
            console.log("POST error...?");
            reject(response);
          }
        );

        new_request.write(post_data);
        new_request.end();
      }
    );



    validate.then(
      function handleValidated(retval) {
        // retval = { code: ..., message: ..., fields: ...
        //  @code: status code
        //  @message: ...
        //  @fields: Array of invalid fields, if any
        retval = JSON.parse(retval);
        if(retval.fields.length > 0) return Promise.reject(retval);
        return createUser;
      }
    ).then(
      function handleCreated(retval) {
        // retval = { code: ..., message: ..., fields: ...
        //  @code: status code
        //  @message: ...
        //  @fields: Array of invalid fields, if any
        console.log(retval);
        res.end(retval);
      },
      function ajaxError(err) {
        console.log(err);
        res.end(err);
      }
    );

  },

  unsubscribe: function(req, res) {
    // User email unsubscribe page
    //    @q: User's registration hash
    //    @u: If set, unsubscribe this user from further emails
    if(req.query.u) {
      request.post("http://hackingedu.parseapp.com/webhooks/u63e1c9c825c067ac7475a85f606f6c72",
        { form: { q: req.query.q } },
        function(err, response, body) {
          if(err) { console.log(err); }
          else {
            console.log(body);
          }

        }<commit_msg>Changed request body post from querystring to full json body<commit_after>var path = require('path');
var request = require("request");
var Promise = require("promise");

module.exports = {
  home: function(req, res) {
    res.sendFile(path.join(__dirname, 'public/index.html'));
  },

  signup: function(req, res) {
    res.sendFile(path.join(__dirname, 'public/signup.html'));
  },

  newUser: function(req, res) {
    // Create User URL: hackingedu.parseapp.com/actions/n0354d89c28ec399c00d3cb2d094cf093
    //  Validation URL: hackingedu.parseapp.com/actions/v62110a75095ebf61417a51fff9af9c7f

    // This is now a proxy to Cloud Code: will probably be better for security
    // Also, all the validation is now handled by Parse servers (cloud code),
    // might be better for server load

    // AJAX promise definitions
    var validate = new Promise(
      function(resolve, reject) {
        // Request to validate fields
        request.post("http://hackingedu.parseapp.com/actions/v62110a75095ebf61417a51fff9af9c7f",
          { form: req.body },
          function(err, response, body) {
            if(err) { reject(err);   }
            else    { resolve(body); }
          }
        );
      }
    );

    var createUser = new Promise(
      function(resolve, reject) {
        // Request to create a new user
        request.post("http://hackingedu.parseapp.com/actions/n0354d89c28ec399c00d3cb2d094cf093",
          { form: req.body },
          function(err, response, body) {
            if(err) { reject(err);   }
            else    { resolve(body); }
          }
        );
      }
    );

    validate.then(
      function handleValidated(retval) {
        // retval = { code: ..., message: ..., fields: ...
        //  @code: status code
        //  @message: ...
        //  @fields: Array of invalid fields, if any
        retval = JSON.parse(retval);
        if(retval.fields.length > 0) return Promise.reject(retval);
        return createUser;
      }
    ).then(
      function handleCreated(retval) {
        // retval = { code: ..., message: ..., fields: ...
        //  @code: status code
        //  @message: ...
        console.log(retval);
        res.end(retval);
      },
      function ajaxError(err) {
        console.log(err);
        res.end(err);
      }
    );

  },

  unsubscribe: function(req, res) {
    // User email unsubscribe page
    //    @q: User's registration hash
    //    @u: If set, unsubscribe this user from further emails
    if(req.query.u) {
      request.post("http://hackingedu.parseapp.com/webhooks/u63e1c9c825c067ac7475a85f606f6c72",
        { form: { q: req.query.q } },
        function(err, response, body) {
          if(err) { console.log(err); }
          else {
            console.log(body);
          }

        }
{'fraction_non_alphanumeric': 0.13192229631777327, 'fraction_numerical': 0.030733545955349377, 'mean_word_length': 1.9036195286195285, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1907313', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2012, 'n_words': 432}