<commit_before>/* jshint node: true */
/**
 * Wake Up Platform
 * (c) Telefonica Digital, 2013 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 */

var log = require('../shared_libs/logger'),
    querystring = require('querystring'),
    net = require('net');

module.exports.info = {
  name: 'wakeupRouter',
  type: 'router',
  virtualpath: 'wakeup/v1',
  description: 'The heart of the system: Used to wakeup devices (V1)'
};

function processWakeUpQuery(paramsString, request, response, cb) {
  response.setHeader('Content-Type', 'text/plain');

  // Check request ...
  if (!paramsString) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> No required data provided');
    response.statusCode = 400;
    response.write('Bad parameters. No required data provided');
    return;
  }
  var wakeup_data = querystring.parse(paramsString);

  // Check parameters
  if (!net.isIP(wakeup_data.ip) ||     // Is a valid IP address
      isNaN(wakeup_data.port) ||       // The port is a Number
      wakeup_data.port <= 0 || wakeup_data.port > 65535 // Port in a valid range
  ) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad IP/Port');
    response.statusCode = 400;
    response.write('Bad parameters. Bad IP/Port');
    return;
  }

  // Check protocol
  if ((!wakeup_data.netid || typeof(wakeup_data.netid) !== 'string') &&
       (!wakeup_data.mcc || !wakeup_data.mnc ||
        isNaN(wakeup_data.mcc) || isNaN(wakeup_data.mnc))) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad NetID OR MCC/MNC');
    response.statusCode = 400;
    response.write('Bad parameters. Bad NetID OR MCC/MNC');
    return;
  }

  // All Ok, we can wakeup the device !
  log.debug('WU_ListenerHTTP_WakeUpRouter --> WakeUp IP = ' + wakeup_data.ip +
    ':' + wakeup_data.port +
    ' network (' + wakeup_data.mcc + '-' + wakeup_data.mnc + ' | ' +
      wakeup_data.netid + ')');

  response.statusCode = 200;
  response.write('Accepted');
  process.nextTick(function() {
    cb(wakeup_data);
  });
}

module.exports.entrypoint =
  function router_wakeupV1(parsedURL, body, request, response, cb) {
    switch (request.method) {
    case 'GET':
      processWakeUpQuery(parsedURL.query, request, response, cb);
      break;
    case 'POST':
      processWakeUpQuery(body, request, response, cb);
      break;
    default:
      response.setHeader('Content-Type', 'text/plain');
      response.statusCode = 405;
      response.write('Bad method. Only GET and POST are allowed');
      log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad method - ' +
        request.method);
    }
  };<commit_msg>Removed GET method into global node<commit_after>/* jshint node: true */
/**
 * Wake Up Platform
 * (c) Telefonica Digital, 2013 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 */

var log = require('../shared_libs/logger'),
    querystring = require('querystring'),
    net = require('net');

module.exports.info = {
  name: 'wakeupRouter',
  type: 'router',
  virtualpath: 'wakeup/v1',
  description: 'The heart of the system: Used to wakeup devices (V1)'
};

function processWakeUpQuery(paramsString, request, response, cb) {
  response.setHeader('Content-Type', 'text/plain');

  // Check request ...
  if (!paramsString) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> No required data provided');
    response.statusCode = 400;
    response.write('Bad parameters. No required data provided');
    return;
  }
  var wakeup_data = querystring.parse(paramsString);

  // Check parameters
  if (!net.isIP(wakeup_data.ip) ||     // Is a valid IP address
      isNaN(wakeup_data.port) ||       // The port is a Number
      wakeup_data.port <= 0 || wakeup_data.port > 65535 // Port in a valid range
  ) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad IP/Port');
    response.statusCode = 400;
    response.write('Bad parameters. Bad IP/Port');
    return;
  }

  // Check protocol
  if ((!wakeup_data.netid || typeof(wakeup_data.netid) !== 'string') &&
       (!wakeup_data.mcc || !wakeup_data.mnc ||
        isNaN(wakeup_data.mcc) || isNaN(wakeup_data.mnc))) {
    log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad NetID OR MCC/MNC');
    response.statusCode = 400;
    response.write('Bad parameters. Bad NetID OR MCC/MNC');
    return;
  }

  // All Ok, we can wakeup the device !
  log.debug('WU_ListenerHTTP_WakeUpRouter --> WakeUp IP = ' + wakeup_data.ip +
    ':' + wakeup_data.port +
    ' network (' + wakeup_data.mcc + '-' + wakeup_data.mnc + ' | ' +
      wakeup_data.netid + ')');

  response.statusCode = 200;
  response.write('Accepted');
  process.nextTick(function() {
    cb(wakeup_data);
  });
}

module.exports.entrypoint =
  function router_wakeupV1(parsedURL, body, request, response, cb) {
    if (request.method !== 'POST') {
      response.setHeader('Content-Type', 'text/plain');
      response.statusCode = 405;
      response.write('Bad method. Only GET and POST are allowed');
      log.debug('WU_ListenerHTTP_WakeUpRouter --> Bad method - ' +
        request.method);
    } else {
      processWakeUpQuery(body, request, response, cb);
    }
  };
{'fraction_non_alphanumeric': 0.1465550797975866, 'fraction_numerical': 0.011288439081354613, 'mean_word_length': 3.236603462489695, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '4012444', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1710, 'n_words': 386}