nsISupports
Last changed in Gecko 1.7 void visitHeader(in ACString aHeader, in ACString aValue); |
Called by the nsIHttpChannel implementation when visiting request and response headers. This method can throw an exception to terminate enumeration of the channel's headers.
void visitHeader( in ACString aHeader, in ACString aValue );
aHeaderaValueThis example shows how to detect Flash content. It implements the nsIHttpHeaderVisitor Interface in JavaScript and uses it to evaluate the mime type of a http response. This is done by subscribing to the http-on-examine-response and http-on-examine-cached-response observers. When the observer fires, the visitor interface is used to walk through the response headers and evaluate the MIME type.
myNsIHttpHeaderVisitor = function ( ) {
this._isFlash = false;
};
myNsIHttpHeaderVisitor.prototype.visitHeader = function ( aHeader, aValue ) {
if ( aHeader.indexOf( "Content-Type" ) !== -1 ) {
if ( aValue.indexOf( "application/x-shockwave-flash" ) !== -1 ) {
this._isFlash = true;
}
}
};
myNsIHttpHeaderVisitor.prototype.isFlash = function ( ) {
return this._isFlash;
};
myHttpRequestObserver = function ( ) {
this.register( );
this.aborted = Components.results.NS_BINDING_ABORTED;
this.nsIHttpChannel = Components.interfaces.nsIHttpChannel;
this.nsIChannel = Components.interfaces.nsIChannel;
this.nsIRequest = Components.interfaces.nsIRequest;
};
myHttpRequestObserver.prototype.observe = function ( subject, topic, data ) {
var uri, aVisitor;
if ( subject instanceof this.nsIHttpChannel ) {
aVisitor = new myNsIHttpHeaderVisitor( );
subject.visitResponseHeaders( aVisitor );
if ( aVisitor.isFlash( ) ) {
uri = subject.URI;
subject.cancel( this.aborted );
alert( "Found Flash!" );
//handle flash file here
}
}
};
myHttpRequestObserver.prototype.register = function ( ) {
var observerService = Components.classes[ "@mozilla.org/observer-service;1" ].getService( Components.interfaces.nsIObserverService );
observerService.addObserver(this, "http-on-examine-response", false);
observerService.addObserver(this, "http-on-examine-cached-response", false);
};
myHttpRequestObserver.prototype.unregister = function ( ) {
var observerService = Components.classes[ "@mozilla.org/observer-service;1" ].getService( Components.interfaces.nsIObserverService );
observerService.removeObserver( this, "http-on-examine-response" );
observerService.removeObserver(this, "http-on-examine-cached-response");
};