nsIHttpHeaderVisitor

This interface is used to view HTTP request and response headers.
Inherits from: nsISupports Last changed in Gecko 1.7

Method overview

void visitHeader(in ACString aHeader, in ACString aValue);

Methods

visitHeader()

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
);
Parameters
aHeader
A string containing the key for a header such as "Content-Type"
aValue
The header's value field such as "text/html". Multiple values are separated by a comma.

Example

This 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.addObserver(this, "http-on-examine-cached-response", false);
};

See also

Document Tags and Contributors

Contributors to this page: Sheppy, MKaply, trevorh, Smi, Btipling, teoli, EricYChu
Last updated by: Sheppy,