Hi,
I am developing a firefox extension to block certain http requests
from leaving the client. I tried to block requests using
nsIRequest.cancel(Components.results.NS_BINDING_ABORTED) and also
tried with nsiHttpChannel.loadGroup.removeRequest(nsIRequest, null,
NS_BINDING_ABORTED) method. However, the request is still sent to the
server. When the response comes, it displays a blank page for the
request made.
Is there something I need to do prevent the request from being sent to
the server?
My code is(as taken from various tutorials):
<code>
var listener, gChannel;
function sameDomain(fromUri, toUri) {
/* check if both uris come under same domain */
}
function StreamListener() {
mCallBackFunction(null);
}
StreamListener.prototype = {
mData: "",
// nsIStreamListener
onStartRequest: function (aRequest, aContext) {
this.mData = "";
},
onDataAvailable: function (aRequest, aContext, aStream,
aSourceOffset, aLength) {
var scriptableInputStream =
Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
scriptableInputStream.init(aStream);
this.mData += scriptableInputStream.read(aLength);
},
onStopRequest: function (aRequest, aContext, aStatus) {
if (Components.isSuccessCode(aStatus)) {
// request was successfull
this.mCallbackFunc(this.mData);
} else {
// request failed
this.mCallbackFunc(null);
}
gChannel = null;
},
// nsIChannelEventSink
onChannelRedirect: function (aOldChannel, aNewChannel, aFlags) {
// if redirecting, store the new channel
gChannel = aNewChannel;
},
// nsIInterfaceRequestor
getInterface: function (aIID) {
try {
return this.QueryInterface(aIID);
} catch (e) {
throw Components.results.NS_NOINTERFACE;
}
},
// nsIProgressEventSink (not implementing will cause annoying
exceptions)
onProgress : function (aRequest, aContext, aProgress, aProgressMax)
{ },
onStatus : function (aRequest, aContext, aStatus, aStatusArg) { },
// nsIHttpEventSink (not implementing will cause annoying
exceptions)
onRedirect : function (aOldChannel, aNewChannel) { },
observe : function(subject, topic, data) {
if(topic=='http-on-modify-request') {
subject.QueryInterface(Components.interfaces.nsIHttpChannel);
this.fModifyRequest(subject, data);
}
},
fModifyRequest : function(subject, data) {
var uri = subject.URI.asciiSpec;
/** block a request if it matches criteria; but the
request is still sent **/
if( !sameDomain(subject.URI.host,
subject.referrer.host)) {
if(subject instanceof
Components.interfaces.nsIRequest) {
subject.cancel(Components.results.NS_BINDING_ABORTED);
if(subject.loadGroup) {
try {
var lgroup =
subject.loadGroup;
if(lgroup instanceof
Components.interfaces.nsILoadGroup) {
lgroup.removeRequest(subject, null, NS_BINDING_ABORTED);
}
} catch(e) {
}
}
}
}
},
// we are faking an XPCOM interface, so we need to implement QI
QueryInterface : function(aIID) {
if (aIID.equals(Components.interfaces.nsISupports) ||
aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
aIID.equals(Components.interfaces.nsIChannelEventSink) ||
aIID.equals(Components.interfaces.nsIProgressEventSink) ||
aIID.equals(Components.interfaces.nsIHttpEventSink) ||
aIID.equals(Components.interfaces.nsIStreamListener)) {
return this;
}
throw Components.results.NS_NOINTERFACE;
},
};
function fInitialize(event) {
var targetBrowser = null;
var uri_one;
if (gBrowser) {
if (gBrowser.mTabbedMode) {
var targetBrowserIndex =
gBrowser.getBrowserIndexForDocument(event.originalTarget);
if (targetBrowserIndex != -1) {
targetBrowser =
gBrowser.getBrowserAtIndex(targetBrowserIndex);
}
} else {
targetBrowser = gBrowser.mCurrentBrowser;
}
}
if (targetBrowser && targetBrowser.currentURI.spec) {
uri_one = targetBrowser.currentURI.spec;
}
var io= Components.classes["@mozilla.org/network/io-service;
1"].getService(Components.interfaces.nsIIOService);
var uri= io.newURI(uri_one,null,null);
gChannel= io.newChannelFromURI(uri);
// get a listener
listener = new StreamListener();
gChannel.notificationCallbacks = listener;
gChannel.asyncOpen(listener, null);
var observerService = Components.classes["@mozilla.org/
observer-service;
1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(listener,"http-on-modify-
request",false);
observerService.addObserver(listener,"http-on-examine-
response",false);
}
function fFinalize(event) {
// window.removeEventListener("load", fInitialize, true);
// window.removeEventListener("unload", fFinalize, true);
}
window.addEventListener("load", fInitialize, true);
window.addEventListener("unload", fFinalize, true);
</code>
-Thanks
Neelofer
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network