【问题标题】:Get content type of an HTTP request获取 HTTP 请求的内容类型
【发布时间】:2012-08-25 03:09:34
【问题描述】:

我正在 Addon SDK (V1.9) 上开发一个 Firefox 扩展。

目标

我需要在加载特定内容类型的 HTTP 请求之前(在发送请求之前)检查它们。

问题

通过“observer-service”模块添加观察者时,我使用“http-on-modify-request”来检查所有 HTTP 请求。这提供了标题。但是,在检查内容类型时,它永远不会根据请求设置。

Content-type 确实是在响应中设置的,我使用“http-on-examine-response”对其进行检查。但这意味着请求已经加载,违背了我扩展的目的。

编辑

@Wladimir Palant 感谢您指出在发送请求标头时未设置 content-type。

我正在尝试在一堆与​​我相关的正则表达式上测试 HTTP 请求的 URL 并阻止它们。但是,我希望能够跳过某些内容类型(例如图像),因为它们与我的应用程序无关。

【问题讨论】:

  • 我不确定这是否是您的意思 - 我认为您的真正意思是您想要中断响应并检查标头?
  • 发送请求时不知道内容类型 - 它来自服务器。也许您想解释您正在尝试解决的问题,而不是您认为的解决方案。您是否试图将图像与脚本或类似的东西区分开来?
  • @WladimirPalant 感谢您指出这一点。我只是添加了一个更好的解释,说明为什么我想在发送 HTTP 请求之前知道内容类型。
  • @jossigna:显然您已经发现使用内容策略就是答案——与 HTTP 观察者不同,它们具有扩展的上下文信息。这意味着我不需要写扩展答案:)

标签: firefox firefox-addon firefox-addon-sdk


【解决方案1】:

所以,总结一下这个问题。完全控制加载到浏览器中的资源的最佳方式似乎是使用nsIContentPolicy

@WladimirPalant 写了一个很好的例子here。这就是他的代码适应 Addon SDK (v1.9) 的样子:

const { Cc, Ci, Cu, Cm, components } = require('chrome');
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm");


let policy =
{
    classDescription: "Test content policy",
    classID: components.ID("{12345678-1234-1234-1234-123456789abc}"),
    contractID: "@adblockplus.org/test-policy;1",
    xpcom_categories: ["content-policy"],

    init: function()
    {
        let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
        registrar.registerFactory(this.classID, this.classDescription, this.contractID, this);

        let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
        for each (let category in this.xpcom_categories)
            catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true);
},

    // nsIContentPolicy interface implementation
    shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
    {
        dump("shouldLoad: " + contentType + " " +
                      (contentLocation ? contentLocation.spec : "null") + " " +
                      (requestOrigin ? requestOrigin.spec : "null") + " " +
                      node + " " +
                      mimeTypeGuess + "\n");
        return Ci.nsIContentPolicy.ACCEPT;
    },

    shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
    {
        dump("shouldProcess: " + contentType + " " +
                        (contentLocation ? contentLocation.spec : "null") + " " +
                        (requestOrigin ? requestOrigin.spec : "null") + " " +
                        node + " " +
                        mimeTypeGuess + "\n");
        return Ci.nsIContentPolicy.ACCEPT;
    },

    // nsIFactory interface implementation
    createInstance: function(outer, iid)
    {
        if (outer)
            throw Cr.NS_ERROR_NO_AGGREGATION;
        return this.QueryInterface(iid);
    },

    // nsISupports interface implementation
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIFactory])
};

policy.init();

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2014-06-17
    相关资源
    最近更新 更多