nsIHttpHeaderVisitor 编辑
netwerk/protocol/http/nsIHttpHeaderVisitor.idl
Scriptable This interface is used to view HTTP request and response headers. Inherits from: nsISupports
Last changed in Gecko 1.7Method 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论