Exemplo n.º 1
0
    function doRouting(url, doDefault, isDefault) {
        anyRouteHandled = true;
        var urlParts = parseUri(url);
        var handlers = routing.recognize(urlParts.path);
        var handlerExecuted = false;
        var defaultRouteExecuted = isDefault || false;
        var promises = [];
        if (handlers) {
            for (var i = 0; i < handlers.length; ++i) {
                var $context = {
                    url: urlParts,
                    params: handlers[i].params,
                    query: queryToDict(urlParts.query)
                };

                promises.push(handlers[i].handler($context));
            }
            handlerExecuted = true;
        } else if ((doDefault !== false) && defaultRoute) {
            promises.push(doDefaultRoute(defaultRoute));
        }

        onRoutingHandlers.forEach(function(handler) {
            handler(url, urlParts, handlerExecuted, defaultRouteExecuted);
        });

        return $q.all(promises);
    }
Exemplo n.º 2
0
function getImgInfo(url, callback) {
  if (!(cache[url] && cache[url].desc && cache[url].title)) {

    var pu = parseuri(url);
    //console.log(pu);

    var opts = {
      host: pu.host,
      path: pu.relative || pu.path
    };

    var req = http.request(opts, function(res) {
      var str = '';

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

      res.on('end', function() {
        // check for description
        var descArr = descRegex.exec(str);
        var titleArr = titleRegex.exec(str);

        if (!cache[url]) {
          cache[url] = {
            url: url,
            hits: 0,
            created: new Date().getTime()
          }
        }

        if (descArr)
          cache[url].desc = descArr[1] || null;
        if (titleArr)
          cache[url].title = titleArr[1] || null;

        console.log(cache[url].desc);
        console.log(cache[url].title);

        // send the info
        callback(null, {
          desc: cache[url].desc || (pu.host + pu.path) || "No description found.",
          title: cache[url].title || pu.host || "No Title found."
        });
      })
    });

    req.on('error', function(err) {
      console.log('req problem: ' + err.message);
    });

    req.end();
  } else {
    // send the info
    callback(null, {
      desc: cache[url].desc,
      title: cache[url].title
    });
  }
}
Exemplo n.º 3
0
function _getHueBridgeHost(description) {
    var uri;

    if (_isHueBridge(description)) {
        uri = parseUri(description.url);
        return {
            "id": description.model.serial,
            "ipaddress": uri.host
        };
    }
    return null;
}
Exemplo n.º 4
0
function Socket(uri, opts){
  if (!(this instanceof Socket)) return new Socket(uri, opts);

  opts = opts || {};

  if (uri && 'object' == typeof uri) {
    opts = uri;
    uri = null;
  }

  if (uri) {
    uri = parseuri(uri);
    opts.host = uri.host;
    opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
    opts.port = uri.port;
    if (uri.query) opts.query = uri.query;
  }

  this.secure = null != opts.secure ? opts.secure :
    (global.location && 'https:' == location.protocol);

  if (opts.host) {
    var pieces = opts.host.split(':');
    opts.hostname = pieces.shift();
    if (pieces.length) opts.port = pieces.pop();
  }

  this.agent = opts.agent || false;
  this.hostname = opts.hostname ||
    (global.location ? location.hostname : 'localhost');
  this.port = opts.port || (global.location && location.port ?
       location.port :
       (this.secure ? 443 : 80));
  this.query = opts.query || {};
  if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
  this.upgrade = false !== opts.upgrade;
  this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  this.forceJSONP = !!opts.forceJSONP;
  this.jsonp = false !== opts.jsonp;
  this.forceBase64 = !!opts.forceBase64;
  this.enablesXDR = !!opts.enablesXDR;
  this.timestampParam = opts.timestampParam || 't';
  this.timestampRequests = opts.timestampRequests;
  this.transports = opts.transports || ['polling', 'websocket'];
  this.readyState = '';
  this.writeBuffer = [];
  this.callbackBuffer = [];
  this.policyPort = opts.policyPort || 843;
  this.rememberUpgrade = opts.rememberUpgrade || false;
  this.open();
  this.binaryType = null;
  this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
}
Exemplo n.º 5
0
function url(uri, loc){
  var obj = uri;

  // default to window.location
  var loc = loc || global.location;
  if (null == uri) uri = loc.protocol + '//' + loc.host;

  // relative path support
  if ('string' == typeof uri) {
    if ('/' == uri.charAt(0)) {
      if ('/' == uri.charAt(1)) {
        uri = loc.protocol + uri;
      } else {
        uri = loc.host + uri;
      }
    }

    if (!/^(https?|wss?):\/\//.test(uri)) {
      debug('protocol-less url %s', uri);
      if ('undefined' != typeof loc) {
        uri = loc.protocol + '//' + uri;
      } else {
        uri = 'https://' + uri;
      }
    }

    // parse
    debug('parse %s', uri);
    obj = parseuri(uri);
  }

  // make sure we treat `localhost:80` and `localhost` equally
  if (!obj.port) {
    if (/^(http|ws)$/.test(obj.protocol)) {
      obj.port = '80';
    }
    else if (/^(http|ws)s$/.test(obj.protocol)) {
      obj.port = '443';
    }
  }

  obj.path = obj.path || '/';

  var ipv6 = obj.host.indexOf(':') !== -1;
  var host = ipv6 ? '[' + obj.host + ']' : obj.host;

  // define unique id
  obj.id = obj.protocol + '://' + host + ':' + obj.port;
  // define href
  obj.href = obj.protocol + '://' + host + (loc && loc.port == obj.port ? '' : (':' + obj.port));

  return obj;
}
Exemplo n.º 6
0
/**
 * Identifies the bridges based on the response from possible devices from SSDP search
 * @param possibleBridges The results from the search to be processed.
 * @return A promise that will process all the results and return only the valid Hue Bridges.
 * @private
 */
function _identifyBridges(possibleBridges) {
    var lookups = [],
        path,
        uri;

    for (path in possibleBridges) {
        if (possibleBridges.hasOwnProperty(path)) {
            uri = parseUri(path);
            lookups.push(module.exports.description(uri.host).then(_getHueBridgeHost));
        }
    }
    return Q.all(lookups);
}
Exemplo n.º 7
0
Arquivo: api.js Projeto: Kryil/huectl
/**
 * Identifies the bridges based on the response from possible devices from SSDP search
 * @param possibleBridges The results from the search to be processed.
 * @return A promise that will process all the results and return only the valid Hue Bridges.
 * @private
 */
function _identifyBridges(possibleBridges) {
    function getHueBridgeHost(description) {
        function isHueBridge(description) {
            var name;

            if (description && description.model && description.model.name) {
                name = description.model.name;
                if (name.toLowerCase().indexOf("philips hue bridge") >= 0) {
                    return true;
                }
            }
            return false;
        }

        var uri;

        if (isHueBridge(description)) {
            uri = parseUri(description.url);
            return {
                "host": uri.host,
                "port": uri.port
            };
        }
        return null;
    }

    var lookups = [],
        path,
        uri;

    for (path in possibleBridges) {
        if (possibleBridges.hasOwnProperty(path)) {
            uri = parseUri(path);
            lookups.push(_descriptionXml(uri.host).then(getHueBridgeHost));
        }
    }
    return Q.all(lookups);
}
Exemplo n.º 8
0
Arquivo: api.js Projeto: Kryil/huectl
    function getHueBridgeHost(description) {
        function isHueBridge(description) {
            var name;

            if (description && description.model && description.model.name) {
                name = description.model.name;
                if (name.toLowerCase().indexOf("philips hue bridge") >= 0) {
                    return true;
                }
            }
            return false;
        }

        var uri;

        if (isHueBridge(description)) {
            uri = parseUri(description.url);
            return {
                "host": uri.host,
                "port": uri.port
            };
        }
        return null;
    }
function Socket(uri, opts){
  if (!(this instanceof Socket)) return new Socket(uri, opts);

  opts = opts || {};

  if (uri && 'object' == typeof uri) {
    opts = uri;
    uri = null;
  }

  if (uri) {
    uri = parseuri(uri);
    opts.hostname = uri.host;
    opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
    opts.port = uri.port;
    if (uri.query) opts.query = uri.query;
  } else if (opts.host) {
    opts.hostname = parseuri(opts.host).host;
  }

  this.secure = null != opts.secure ? opts.secure :
    (global.location && 'https:' == location.protocol);

  if (opts.hostname && !opts.port) {
    // if no port is specified manually, use the protocol default
    opts.port = this.secure ? '443' : '80';
  }

  this.agent = opts.agent || false;
  this.hostname = opts.hostname ||
    (global.location ? location.hostname : 'localhost');
  this.port = opts.port || (global.location && location.port ?
       location.port :
       (this.secure ? 443 : 80));
  this.query = opts.query || {};
  if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
  this.upgrade = false !== opts.upgrade;
  this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  this.forceJSONP = !!opts.forceJSONP;
  this.jsonp = false !== opts.jsonp;
  this.forceBase64 = !!opts.forceBase64;
  this.enablesXDR = !!opts.enablesXDR;
  this.timestampParam = opts.timestampParam || 't';
  this.timestampRequests = opts.timestampRequests;
  this.transports = opts.transports || ['polling', 'websocket'];
  this.readyState = '';
  this.writeBuffer = [];
  this.policyPort = opts.policyPort || 843;
  this.rememberUpgrade = opts.rememberUpgrade || false;
  this.binaryType = null;
  this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
  this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || {}) : false;

  if (true === this.perMessageDeflate) this.perMessageDeflate = {};
  if (this.perMessageDeflate && null == this.perMessageDeflate.threshold) {
    this.perMessageDeflate.threshold = 1024;
  }

  // SSL options for Node.js client
  this.pfx = opts.pfx || null;
  this.key = opts.key || null;
  this.passphrase = opts.passphrase || null;
  this.cert = opts.cert || null;
  this.ca = opts.ca || null;
  this.ciphers = opts.ciphers || null;
  this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? null : opts.rejectUnauthorized;

  // other options for Node.js client
  var freeGlobal = typeof global == 'object' && global;
  if (freeGlobal.global === freeGlobal) {
    if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
      this.extraHeaders = opts.extraHeaders;
    }
  }

  this.open();
}
Exemplo n.º 10
0
function Socket(uri, opts){
  if (!(this instanceof Socket)) return new Socket(uri, opts);

  opts = opts || {};

  if (uri && 'object' == typeof uri) {
    opts = uri;
    uri = null;
  }

  if (uri) {
    uri = parseuri(uri);
    opts.host = uri.host;
    opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
    opts.port = uri.port;
    if (uri.query) opts.query = uri.query;
  }

  this.secure = null != opts.secure ? opts.secure :
    (global.location && 'https:' == location.protocol);

  if (opts.host) {
    var match = opts.host.match(/(\[.+\])(.+)?/)
      , pieces;

    if (match) {
      opts.hostname = match[1];
      if (match[2]) opts.port = match[2].slice(1);
    } else {
      pieces = opts.host.split(':');
      opts.hostname = pieces.shift();
      if (pieces.length) opts.port = pieces.pop();
    }

    // if `host` does not include a port and one is not specified manually,
    // use the protocol default
    if (!opts.port) opts.port = this.secure ? '443' : '80';
  }

  this.agent = opts.agent || false;
  this.hostname = opts.hostname ||
    (global.location ? location.hostname : 'localhost');
  this.port = opts.port || (global.location && location.port ?
       location.port :
       (this.secure ? 443 : 80));
  this.query = opts.query || {};
  if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
  this.upgrade = false !== opts.upgrade;
  this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  this.forceJSONP = !!opts.forceJSONP;
  this.jsonp = false !== opts.jsonp;
  this.forceBase64 = !!opts.forceBase64;
  this.enablesXDR = !!opts.enablesXDR;
  this.timestampParam = opts.timestampParam || 't';
  this.timestampRequests = opts.timestampRequests;
  this.transports = opts.transports || ['polling', 'websocket'];
  this.readyState = '';
  this.writeBuffer = [];
  this.policyPort = opts.policyPort || 843;
  this.rememberUpgrade = opts.rememberUpgrade || false;
  this.binaryType = null;
  this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
  this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false;

  // SSL options for Node.js client
  this.pfx = opts.pfx || null;
  this.key = opts.key || null;
  this.passphrase = opts.passphrase || null;
  this.cert = opts.cert || null;
  this.ca = opts.ca || null;
  this.ciphers = opts.ciphers || null;
  this.rejectUnauthorized = opts.rejectUnauthorized || null;

  // other options for Node.js client
  var freeGlobal = typeof global == 'object' && global;
  if (freeGlobal.global === freeGlobal) {
    if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
      this.extraHeaders = opts.extraHeaders;
    }
  }

  this.open();
}