0

In Google chrome there is a header called, Remote Address. I'm writing an add-on for Firefox and I need to decide something based on the remote host but it looks like there is no such header in Firefox. If you know how to access remote host from the observer object please tell me.

observe : function(aSubject, aTopic, aData) {
    //I need remote host here
}

her is the screen shot to the header in Google chrome enter image description here

Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33

1 Answers1

1

If the header is not there it throws excetption NS_ERROR_NOT_AVAILABLE

var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');

var httpRequestObserver =
{
    observe: function(subject, topic, data)
    {
        var httpChannel, requestURL;
        httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
        requestURL = httpChannel.URI.spec;

        if (topic == "http-on-modify-request") {

            //if (requestURL.indexOf('google.com') > -1) {
                //httpChannel.setRequestHeader('MyCustomRequestHeader', 'hiiii', false);
                try {
                   var Host = httpChannel.getRequestHeader('Host');
                } catch (ex) {
                    var Host = 'NULL';
                }
                console.log('REQUEST Header "Host" = ' + Host);
            //}
        } else if (topic == "http-on-examine-response") {
            try {
               var Host = httpChannel.getResponseHeader('Host');
            } catch (ex) {
                var Host = 'NULL';
            }
            console.log('RESPONSE Header "Host" = ' + Host);
        }
    }
};

Services.obs.addObserver(httpRequestObserver, "http-on-modify-request", false);
Services.obs.addObserver(httpRequestObserver, "http-on-examine-response", false);
//Services.obs.removeObserver(httpRequestObserver, "http-on-modify-request", false); //run this on shudown of your addon otherwise the observer stags registerd
//Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response", false); //run this on shudown of your addon otherwise the observer stags registerd

Useful articles used to make this snippet:

Noitidart
  • 35,443
  • 37
  • 154
  • 323