Repository: socketio/socket.io
File: lib/manager.js (JavaScript)

Code before:
 * Generates a session id.
 *
 * @api private
 */

Manager.prototype.generateId = function () {
  return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
    + Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
};

/**
 * Handles a handshake request.
 *
 * @api private
 */

Manager.prototype.handleHandshake = function (data, req, res) {
  var self = this;

  function writeErr (status, message) {
    if (data.query.jsonp) {
      res.writeHead(200, { 'Content-Type': 'application/javascript' });
      res.end('io.j[' + data.query.jsonp + '](new Error("' + message + '"));');
    } else {
      res.writeHead(status);
      res.end(message);
    }
  };

  function error (err) {
    writeErr(500, 'handshake error');
    self.log.warn('handshake error ' + err);
  };

  if (!this.verifyOrigin(req)) {
    writeErr(403, 'handshake bad origin');
    return;
  }

  var handshakeData = this.handshakeData(data);

  this.authorize(handshakeData, function (err, authorized, newData) {
    if (err) return error(err);

    if (authorized) {
      var id = self.generateId()
        , hs = [
              id
            , self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
            , self.get('close timeout') || ''
            , self.transports(data).join(',')

Review comment (suggestion):
The jsonp response originally had 200, so I kept it for the plaintext
response as well. This isn't as much an http error as a handshake
interpreted by script error, no?

On Aug 31, 2011 5:52 PM, "guille" <
reply@reply.github.com>
wrote:

> > @@ -775,6 +775,7 @@ Manager.prototype.handleHandshake = function (data,
> > req, res) {
> > res.writeHead(200, { 'Content-Type': 'application/javascript' });
> > res.end('io.j[' + data.query.jsonp + '](new Error%28"' + message +);
> > } else {
> > - res.writeHead(200, { 'Content-Type': 'text/plain' });
> >   res.writeHead(status);
> 
> Two writeHeads ?
> Why 200 in writeErr?
> 
> ## 
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/LearnBoost/socket.io/pull/501/files#r108484


Code after:
 * Generates a session id.
 *
 * @api private
 */

Manager.prototype.generateId = function () {
  return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
    + Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
};

/**
 * Handles a handshake request.
 *
 * @api private
 */

Manager.prototype.handleHandshake = function (data, req, res) {
  var self = this;

  function writeErr (status, message) {
    if (data.query.jsonp) {
      res.writeHead(200, { 'Content-Type': 'application/javascript' });
      res.end('io.j[' + data.query.jsonp + '](new Error("' + message + '"));');
    } else {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.writeHead(status);
      res.end(message);
    }
  };

  function error (err) {
    writeErr(500, 'handshake error');
    self.log.warn('handshake error ' + err);
  };

  if (!this.verifyOrigin(req)) {
    writeErr(403, 'handshake bad origin');
    return;
  }

  var handshakeData = this.handshakeData(data);

  this.authorize(handshakeData, function (err, authorized, newData) {
    if (err) return error(err);

    if (authorized) {
      var id = self.generateId()
        , hs = [
              id
            , self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
            , self.get('close timeout') || ''