XPCOM:嗅探 HTTP 响应

发布于 2024-10-04 08:37:19 字数 458 浏览 5 评论 0原文

我想用 XPCOM 编写一个小组件,可以嗅探浏览器收到的所有 HTTP 响应。现在,我能找到的唯一示例(如下面附加的示例)仅允许我检索我自己触发的请求的响应:

var req = new XMLHttpRequest();  
req.open('GET', 'http://www.mozilla.org/', true);  
req.onreadystatechange = function (aEvt) {  
if (req.readyState == 4) {  
    if(req.status == 200)  
        dump(req.responseText);  
        else  
        dump("Error loading page\n");  
    }  
};

我想要的是浏览器收到的任何 HTTP 响应都获取相应的 HTTP 标头要求。

谢谢

I would like to write a little component with XPCOM that can sniff all HTTP responses received by the browser. Right now the only examples that I can find (like the one appended below) only allow me to retrieve the response for a request that I fire myself:

var req = new XMLHttpRequest();  
req.open('GET', 'http://www.mozilla.org/', true);  
req.onreadystatechange = function (aEvt) {  
if (req.readyState == 4) {  
    if(req.status == 200)  
        dump(req.responseText);  
        else  
        dump("Error loading page\n");  
    }  
};

What I want is for any HTTP response that the browser receives get the HTTP headers of the corresponding request.

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-10-11 08:37:19

您还可以使用 http-on-modify-requesthttp-on-examine-response 通知。请参阅https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#HTTP_Observers

You can also use the http-on-modify-request and http-on-examine-response notifications. See https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#HTTP_Observers

迷鸟归林 2024-10-11 08:37:19

您可以通过 nsIHttpActivityObserver 嗅探所有 http 流量,这是从 Firefox Web 控制台抄写的示例:

const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "activityDistributor",
                                       "@mozilla.org/network/http-activity-distributor;1",
                                       "nsIHttpActivityDistributor");

    let httpTrafficObserver = {

      /**
       * Begin observing HTTP traffic that we care about,
       * namely traffic that originates inside any context that a Heads Up Display
       * is active for.
       */
      startHTTPObservation: function httpObserverFactory()
      {
        // creates an observer for http traffic
        var self = this;
        var httpObserver = {
          observeActivity :
          function observeActivity(aChannel,
                                   aActivityType,
                                   aActivitySubtype,
                                   aTimestamp,
                                   aExtraSizeData,
                                   aExtraStringData)
          {
            if (aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_HTTP_TRANSACTION ||
                aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_SOCKET_TRANSPORT) {

              aChannel = aChannel.QueryInterface(Ci.nsIHttpChannel);

              let transCodes = this.httpTransactionCodes;

              if (aActivitySubtype ==
                  activityDistributor.ACTIVITY_SUBTYPE_REQUEST_HEADER ) {

                let httpActivity = {
                  url: aChannel.URI.spec,
                  method: aChannel.requestMethod,
                  channel: aChannel
                };

              }
            }
          },

          httpTransactionCodes: {
            0x5001: "REQUEST_HEADER",
            0x5002: "REQUEST_BODY_SENT",
            0x5003: "RESPONSE_START",
            0x5004: "RESPONSE_HEADER",
            0x5005: "RESPONSE_COMPLETE",
            0x5006: "TRANSACTION_CLOSE",

            0x804b0003: "STATUS_RESOLVING",
            0x804b0007: "STATUS_CONNECTING_TO",
            0x804b0004: "STATUS_CONNECTED_TO",
            0x804b0005: "STATUS_SENDING_TO",
            0x804b000a: "STATUS_WAITING_FOR",
            0x804b0006: "STATUS_RECEIVING_FROM"
          }
        };

        this.httpObserver = httpObserver;

        activityDistributor.addObserver(httpObserver);
      }

    };

You can sniff all http traffic via the nsIHttpActivityObserver, an example cribbed from the Firefox web console:

const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "activityDistributor",
                                       "@mozilla.org/network/http-activity-distributor;1",
                                       "nsIHttpActivityDistributor");

    let httpTrafficObserver = {

      /**
       * Begin observing HTTP traffic that we care about,
       * namely traffic that originates inside any context that a Heads Up Display
       * is active for.
       */
      startHTTPObservation: function httpObserverFactory()
      {
        // creates an observer for http traffic
        var self = this;
        var httpObserver = {
          observeActivity :
          function observeActivity(aChannel,
                                   aActivityType,
                                   aActivitySubtype,
                                   aTimestamp,
                                   aExtraSizeData,
                                   aExtraStringData)
          {
            if (aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_HTTP_TRANSACTION ||
                aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_SOCKET_TRANSPORT) {

              aChannel = aChannel.QueryInterface(Ci.nsIHttpChannel);

              let transCodes = this.httpTransactionCodes;

              if (aActivitySubtype ==
                  activityDistributor.ACTIVITY_SUBTYPE_REQUEST_HEADER ) {

                let httpActivity = {
                  url: aChannel.URI.spec,
                  method: aChannel.requestMethod,
                  channel: aChannel
                };

              }
            }
          },

          httpTransactionCodes: {
            0x5001: "REQUEST_HEADER",
            0x5002: "REQUEST_BODY_SENT",
            0x5003: "RESPONSE_START",
            0x5004: "RESPONSE_HEADER",
            0x5005: "RESPONSE_COMPLETE",
            0x5006: "TRANSACTION_CLOSE",

            0x804b0003: "STATUS_RESOLVING",
            0x804b0007: "STATUS_CONNECTING_TO",
            0x804b0004: "STATUS_CONNECTED_TO",
            0x804b0005: "STATUS_SENDING_TO",
            0x804b000a: "STATUS_WAITING_FOR",
            0x804b0006: "STATUS_RECEIVING_FROM"
          }
        };

        this.httpObserver = httpObserver;

        activityDistributor.addObserver(httpObserver);
      }

    };

and http://mxr.mozilla.org/mozilla-central/source/netwerk/protocol/http/nsIHttpActivityObserver.idl

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