nsIHttpHeaderVisitor 编辑

netwerk/protocol/http/nsIHttpHeaderVisitor.idlScriptable 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.removeObserver(this, "http-on-examine-cached-response");
};

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:39 次

字数:4707

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文