socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

If using match origin protocol, occasionally origin is undefined

Open crickeys opened this issue 14 years ago • 3 comments

I get a bunch of these errors showing up if I use "match origin protocol", which I need to use or I get errors in safari. I guess for some browsers origin ends up being undefined (might be IE, not sure).

I'm also not familiar enough with the "math origin protocol" to put in a correct check here for origin == undefined.

16 Mar 09:35:49 - Uncaught exception: TypeError: Cannot call method 'match' of undefined data: TypeError: Cannot call method 'match' of undefined data: at WebSocket.onSocketConnect (/home/node/node_modules/socket.io/lib/transports/websocket/hybi-07-12.js:103:30) data: at WebSocket.handleRequest (/home/node/node_modules/socket.io/lib/transport.js:71:10) data: at WebSocket.Transport (/home/node/node_modules/socket.io/lib/transport.js:31:8) data: at new WebSocket (/home/node/node_modules/socket.io/lib/transports/websocket/hybi-07-12.js:60:13) data: at new WebSocket (/home/node/node_modules/socket.io/lib/transports/websocket.js:31:17) data: at Manager.handleClient (/home/node/node_modules/socket.io/lib/manager.js:634:19) data: at Manager.handleUpgrade (/home/node/node_modules/socket.io/lib/manager.js:594:8) data: at HTTPServer. (/home/node/node_modules/socket.io/lib/manager.js:119:10) data: at HTTPServer.emit (events.js:81:20) data: at Socket. (http.js:1045:14)

crickeys avatar Mar 16 '12 18:03 crickeys

I also get this error if I use 'match origin protocol', this time with socket.io-client (not from a browser). I get this error for version 0.9.0 (of both soket.io and socket.io-client) and for version 0.9.5 (both).

Here is a sample code that illustrates the issue. Try uncommenting match origin protocol. I also tried experimenting with transports (because the error seems to be related to websockets) but if I specify xhr-polling in both the client and the server it wont work (it doesn't crash, but it doesn't connect either).

socketIo = require 'socket.io'
socketIoClient = require 'socket.io-client'

serverOptions =
  'log level': 2
  # 'match origin protocol': yes
  # transports: ['xhr-polling']

clientOptions =
  transports: ['xhr-polling']
  'try multiple transports': no

serverSocket = socketIo.listen 8009, serverOptions

serverSocket.on 'connection', (socket) ->
  console.log 'connected (server)'

clientSocket = socketIoClient.connect 'http://127.0.0.1:8009', clientOptions
clientSocket.on 'connect', ->
  console.log 'connected (client)'

ichernev avatar May 11 '12 10:05 ichernev

Hi there, i have the same issue here. I'm using socket.io 0.9.10, and when I'm trying to connect a client in the same network of the server, the origin sent during the handshake is undefined. I found that putting loggers in verifyOrigin of manager.js of the server

Manager.prototype.verifyOrigin = function (request) {
  var origin = request.headers.origin || request.headers.referer
    , origins = this.get('origins');

  if (origin === 'null') origin = '*';

  if (origins.indexOf('*:*') !== -1) {
    return true;
  }

  if (origin) {
    try {
      var parts = url.parse(origin);
      var ok =
        ~origins.indexOf(parts.hostname + ':' + parts.port) ||
        ~origins.indexOf(parts.hostname + ':*') ||
        ~origins.indexOf('*:' + parts.port);
      if (!ok) this.log.warn('illegal origin: ' + origin);
      return ok;
    } catch (ex) {
      this.log.warn('error parsing origin');
    }
  }
  else {
    this.log.warn('origin missing from handshake, yet required by config');        
  }
  return false;
};

Paradoxically, I've tryed to downgrade socket.io to 0.9.6, and all works great... any suggestion?

Modder avatar Oct 23 '12 17:10 Modder

I just ran into this issue while using ZombieJS. My fix was to simply set the "origin" header in the request to the one matching my socket.io configuration.

JohnPostlethwait avatar Mar 07 '14 07:03 JohnPostlethwait