mozilla

Revision 105595 of Monitoring HTTP activity

  • Revision slug: Monitoring_HTTP_activity
  • Revision title: Monitoring HTTP activity
  • Revision id: 105595
  • Created:
  • Creator: teoli
  • Is current revision? Yes
  • Comment one or more formatting changes
Tags: 

Revision Content

{{ gecko_minversion_header("1.9.2") }}

Gecko includes the {{ interface("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 {{ interface("nsIHttpActivityObserver") }} interface within your code. This is very simple, requiring you to implement a single method, {{ ifmethod("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 {{ ifmethod("nsIHttpActivityDistributor", "addObserver") }} method in {{ interface("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 {{ ifmethod("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

  • {{ interface("nsIHttpActivityDistributor") }}
  • {{ interface("nsIHttpActivityObserver") }}

Revision Source

<p>{{ gecko_minversion_header("1.9.2") }}</p>
<p>Gecko includes the {{ interface("nsIHttpActivityObserver") }} interface, which you can implement in your code to monitor <a href="/en/HTTP" title="en/HTTP">HTTP</a> transactions in real time, receiving a callback as the transactions take place.</p>
<h2 id="Creating_an_HTTP activity_observer">Creating an HTTP activity observer</h2>
<p>To observe HTTP activity, you need to implement the {{ interface("nsIHttpActivityObserver") }} interface within your code. This is very simple, requiring you to implement a single method, {{ ifmethod("nsIHttpActivityObserver", "observeActivity") }}, which gets called each time an action of interest takes place on the HTTP channel.</p>
<pre><span>// Define a reference to the interface<br>var nsIHttpActivityObserver = Components.interfaces.</span>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;
        }
      }
    }
};
</pre>
<p>Then you need to install your activity observer. This is done using the {{ ifmethod("nsIHttpActivityDistributor", "addObserver") }} method in {{ interface("nsIHttpActivityDistributor") }}:</p>
<pre>var activityDistributor = Components.classes["@<a class="linkification-ext" href="http://mozilla.org/network/http-activity-distributor;1" title="Linkification: http://mozilla.org/network/http-activity-distributor;1">mozilla.org/network/http-activity-distributor;1</a>"]
                                    .getService(Components.interfaces.nsIHttpActivityDistributor);
activityDistributor.addObserver(httpObserver);
</pre>
<h2 id="Observable_activities">Observable activities</h2>
<p>There are two classes of observable activities: those that occur at the socket level and those that occur at the HTTP transaction level.</p>
<h3 id="Observable_socket_activities">Observable socket activities</h3>
<p>When the activity type reported to your {{ ifmethod("nsIHttpActivityObserver", "observeActivity") }} method is <code>ACTIVITY_TYPE_SOCKET_TRANSPORT</code>, the activity subtype, which indicates the specific type of activity that occurred, will be a <a href="/en/XPCOM_Interface_Reference/nsISocketTransport#nsITransportEventSink.0a_status_codes" title="en/XPCOM Interface Reference/nsISocketTransport#nsITransportEventSink.0a status codes">socket transport status code</a>.</p>
<h3 id="Observable_HTTP_activities">Observable HTTP activities</h3>
<p>When the activity type is <code>ACTIVITY_TYPE_HTTP_TRANSACTION</code>, the activity subtype will be one of the <a href="/en/nsIHttpActivityObserver#Activity_subtype_constants" title="en/nsIHttpActivityObserver#Activity subtype constants">activity subtype constants</a>. These include the ability to monitor outgoing HTTP request headers and bodies as well as incoming HTTP headers and complete HTTP transactions.</p>
<h2 id="See_also">See also</h2>
<ul> <li>{{ interface("nsIHttpActivityDistributor") }}</li> <li>{{ interface("nsIHttpActivityObserver") }}</li>
</ul>
Revert to this revision