How to cancel http requests?
I'm tring to cancel HTTP request using aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
but it dont work. The code is given below. Can anyone please correct it?
function(aSubject, aTopic, aData)
{
if ("http-on-modify-request" == aTopic)
{
//get the http request url
var url = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).originalURI.spec;
//get the referer of http request (this error gives an error; dont know why :(
var a = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).getRequestHeader("referer");
//Cancel http request from other domains
if (url.split('//')[1].split('/')[0] != a.split('//')[1].split('/')[0])
{
//the code below don't work! Someone please correct it.
//code to cancel http r开发者_开发知识库equest
aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
}
}
}
That's probably because NS_BINDING_SUCCEEDED isn't an error code (yes, the documentation seems to be wrong). What you actually want to use is Components.results.NS_BINDING_ABORTED
.
By the way, you seem to be comparing the host name in the URL. Given that you already have an nsIURI object this can be done easier:
var requestHost = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).originalURI.host;
var referrerHost = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel).referrer.host;
if (requestHost != referrerHost)
{
aSubject.cancel(Components.results.NS_BINDING_ABORTED);
}
精彩评论