Michael Gomringer
Michael Gomringer

Reputation: 31

Access to TCP-Connection via JavaScript

My goal is it to identify the TCP-Connections in a firefox-plugin.

For that I need to set a unique ID to every connection. My question is, if its possible and someone knows how to get access to the Object of the TCP-Connection from a HTTP-Request? Then I could set a unique ID to it and every request/response pair would be uniquely set to a connection.

Upvotes: 3

Views: 789

Answers (4)

skabbes
skabbes

Reputation: 900

I do something very similar in my own ff addon. I'm assuming that you want a reference to the nsiHttpChannel associated with the connection. However, I'm not sure you can just add properties to it (and have them persist), since its probably backed by native code, I'm not sure either way. But you can store the nsiHttpChannel elsewhere and keep an id on it that way.

Here's some simplified code that I use to monitor http traffic in my addon, which should solve your problem.

var Cc = Components.classes;
var Ci = Components.interfaces;

var MyHttpObserver = {
    // must be exposed so that the ObserverService can register itself
    observe: function(subject, topic, data) {
        subject.QueryInterface(Ci.nsIHttpChannel);
        if( topic === "http-on-modify-request" ){
            // store 'subject' somewhere
        } else if( topic === "http-on-examine-response" ){
            // look up 'subject' it will be the same reference as before
        }
    },

    register: function() {
        var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
        observerService.addObserver(this, "http-on-modify-request", false);
        observerService.addObserver(this, "http-on-examine-response", false);
    },

    unregister: function() {
        var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
        observerService.removeObserver(this, "http-on-modify-request");
        observerService.removeObserver(this, "http-on-examine-response");
    },

    QueryInterface: function (aIID) {
        if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports) ){
            return this;
        }
        throw Components.results.NS_NOINTERFACE;
    }
};

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92284

From MDN: How to monitor HTTP activity

Here's their sample code

// Define a reference to the interface
var nsIHttpActivityObserver = Components.interfaces.nsIHttpActivityObserver;

var httpObserver =
{
    observeActivity: function(aHttpChannel, aActivityType, aActivitySubtype, aTimestamp, aExtraSizeData, aExtraStringData)
    {
      if (aActivityType == nsIHttpActivityObserver.ACTIVITY_TYPE_HTTP_TRANSACTION) {
        switch(aActivitySubtype) {
          case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_HEADER:
            // received response header
            break;
          case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE:
            // received complete HTTP response
            break;
        }
      }
    }
};

var activityDistributor = 
    Components.classes["@mozilla.org/network/http-activity-distributor;1"]
    .getService(Components.interfaces.nsIHttpActivityDistributor);
activityDistributor.addObserver(httpObserver);

Upvotes: 0

Hasin Hayder
Hasin Hayder

Reputation: 818

Assuming that you are talking about client side javascript, there are some projects which will help you to achieve this functionality.

  1. http://code.google.com/p/jssockets/ [flash is required]
  2. http://socket.io/

Hope it helps.

Upvotes: 0

Chris Cherry
Chris Cherry

Reputation: 28554

While I don't know the direct answer to your question, I would suggest taking a look at the source code for Firebug, it seems to have access to at least the HTTP request level of the network stack, maybe even lower.

Hope this helps, good luck!

Upvotes: 1

Related Questions