Monitoring HTTP activity

Gecko includes the nsIHttpActivityObserver interface, which you can implement in your code to monitor HTTP transactions in real time, receiving a callback as the transactions take place.

Creating an HTTP activity observer

To observe HTTP activity, you need to implement the nsIHttpActivityObserver interface within your code. This is very simple, requiring you to implement a single method, nsIHttpActivityObserver.observeActivity(), which gets called each time an action of interest takes place on the HTTP channel.

// Define a reference to the interface
var nsIHttpActivityObserver = Components.interfaces.
nsIHttpActivityObserver; var httpObserver = { observeActivity: function(aHttpChannel, aActivityType, aActivitySubtype, aTimestamp, aExtraSizeData, aExtraStringData) { if (aActivityType == nsIHttpActivityObserver.ACTIVITY_TYPE_HTTP_TRANSACTION) { switch(aActivitySubtype) { case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_HEADER: // received response header break; case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE: // received complete HTTP response break; } } } };

Then you need to install your activity observer. This is done using the nsIHttpActivityDistributor.addObserver() method in nsIHttpActivityDistributor:

var activityDistributor = Components.classes["@mozilla.org/network/http-activity-distributor;1"]
                                    .getService(Components.interfaces.nsIHttpActivityDistributor);
activityDistributor.addObserver(httpObserver);

Observable activities

There are two classes of observable activities: those that occur at the socket level and those that occur at the HTTP transaction level.

Observable socket activities

When the activity type reported to your nsIHttpActivityObserver.observeActivity() method is ACTIVITY_TYPE_SOCKET_TRANSPORT, the activity subtype, which indicates the specific type of activity that occurred, will be a socket transport status code.

Observable HTTP activities

When the activity type is ACTIVITY_TYPE_HTTP_TRANSACTION, the activity subtype will be one of the activity subtype constants. These include the ability to monitor outgoing HTTP request headers and bodies as well as incoming HTTP headers and complete HTTP transactions.

See also