2010-11-26 108 views
2

我想用XPCOM編寫一個可以嗅探瀏覽器收到的所有HTTP響應的小組件。眼下唯一的例子,我可以找到(像一個附在下面)只讓我找回我火了自己的請求的響應: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標頭。

感謝

回答

0

您可以通過nsIHttpActivityObserver,從Firefox的Web控制檯那兒剽竊的例子嗅出所有的HTTP流量:

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); 
     } 

    }; 

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