gadicc / meteor-headers

Access HTTP headers on both server and client. Client IP with proxy support.
https://atmospherejs.com/gadicohen/headers
GNU Lesser General Public License v3.0
61 stars 21 forks source link

Header information not available yet in `Meteor.onConnection()` callback #14

Closed gadicc closed 10 years ago

gadicc commented 10 years ago

headers is only available inside Publish and Method functions. However, a Method can potentially be called from anywhere, including an onConnection callback, but this occurs before we've had a chance to reassociate the headers with the DDP connection (see README). Currently, this results in the following incorrect and misleading error being displayed, simply because the header data isn't available yet.

Exception in onConnection callback: Error: Call headers.getClientIP(this) only
from within a method or publish function. With callbacks / anonymous
functions, use: var self=this; and call headers.getClientIP(self);

Current status: low priority until it's use is justified.

Short term fix: better error message

Long term fix: We could potentially get access to the headers sooner if we send the headers token when opening the DDP connection using cookies, or we could wait for Meteor core to provide a better way.

method headers are still available, and you can get to them like this:

  Meteor.onConnection(function(connection) {
    console.log(headers.methodGet({connection: connection}));
  }); 
DiyahM commented 10 years ago

My current need is that I'm calling a rest api on connection, the api takes the visitor's ip as a param. The results of the api are then displayed in the browser. Any suggestion for a workaround?

gadicc commented 10 years ago

If I'm not mistaken, headers.methodClientIP(this) should still work. Just rushing out now but I'll check back again a bit later. If it doesn't work, it won't take me long to make a new release where it will work... this data (as opposed to the original header and IP info) will be available already at connect time, although it's possible that my code doesn't realize this :)

DiyahM commented 10 years ago

Changed to headers.methodClientIP(this) and receiving the same error.

gadicc commented 10 years ago

Ooh, it looks like the onConnection callback is called before Meteor has actually attached the session. But we can work around it quite easily. Hope following code will finally solve your problem :)

  Meteor.onConnection(function(connection) {
    var ip = headers.methodClientIP({connection: connection});
    console.log(ip);
  }); 
gadicc commented 10 years ago

Hi Diyah, did this help you? I think this is an acceptable workaround and would like to close the issue if you're happy.

DiyahM commented 10 years ago

Sorry, for the delay in response. Yes, that works great!

gadicc commented 10 years ago

Happy to hear it :)

Futurama56 commented 10 years ago

How would you handle a proxy with this code?

gadicc commented 10 years ago

at the proxyCount as a 2nd argument to methodClientIP(). in the workaround above, this would be:

var ip = headers.methodClientIP({connection: connection}, proxyCount);

even better, call headers.setProxyCount(proxyCount) somewhere appropriate (in a lib/config.js file or your main site js or something). In the future, this will use HTTP_FORWARDED_COUNT environment variable, which was introduced in Meteor 0.7.1+ (see README).