简体   繁体   中英

Trying to get HTTP POST request in Firefox extension

I'm building an extension to get the POST request in Firefox. I read through the documentation for intercepting page loads and HTTP observers, but still couldn't manage to get the specific POST data on a page load (ex: data1=50&sdata2=0&data3=50).

I looked into TamperData's code, and found that they used stream.available() and stream.read(1). However, I couldn't get these commands to work with my code.

Currently my code looks like this:

var ObserverTest = {

observe: function(subject, topic, data) { if (topic == 'http-on-modify-request') { var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel); } if (topic == "http-on-examine-response") { var newListener = new TracingListener(); subject.QueryInterface(Ci.nsITraceableChannel); newListener.originalListener = subject.setNewListener(newListener); } }, register: function() { var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); observerService.addObserver(ObserverTest, "http-on-modify-request", false); observerService.addObserver(ObserverTest, "http-on-examine-response", false); }, unregister: function() { var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); observerService.removeObserver(ObserverTest, "http-on-modify-request"); observerService.removeObserver(ObserverTest,"http-on-examine-response"); } } window.addEventListener("load", ObserverTest.register, false); window.addEventListener("unload", ObserverTest.unregister, false); //Helper function for XPCOM instanciation (from Firebug) function CCIN(cName, ifaceName) { return Cc[cName].createInstance(Ci[ifaceName]); } // Copy response listener implementation. function TracingListener() { this.originalListener = null; this.receivedData = []; // array for incoming data. } TracingListener.prototype = { onDataAvailable: function(request, context, inputStream, offset, count) { var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream"); var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream"); var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream"); var stream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); stream.init(binaryInputStream); binaryInputStream.setInputStream(inputStream); storageStream.init(8192, count, null); binaryOutputStream.setOutputStream(storageStream.getOutputStream(0)); // Copy received data as they come. var data = binaryInputStream.readBytes(count); this.receivedData.push(data); binaryOutputStream.writeBytes(data, count); this.originalListener.onDataAvailable(request, context, storageStream.newInputStream(0), offset, count); }, onStartRequest: function(request, context) { this.originalListener.onStartRequest(request, context); }, onStopRequest: function(request, context, statusCode) { // Get entire response var responseSource = this.receivedData.join(); this.originalListener.onStopRequest(request, context, statusCode); }, QueryInterface: function (aIID) { if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) { return this; } throw Components.results.NS_NOINTERFACE; } }

First of all, the "http-on-examine-response" and TracingListener isn't required at all. This stuff would have merit if you wanted to do something with the response, but you're after data in the request, so topic == 'http-on-modify-request' it is.

The following function (untested, but copied from one of my extensions and cleaned up a bit) demonstrates how to get post data. The function is assumed to be called from http-on-modify-request .

const ScriptableInputStream = Components.Constructor(
  "@mozilla.org/scriptableinputstream;1",
  "nsIScriptableInputStream",
  "init");

function observeRequest(channel, topic, data) {
  let post = null;

  if (!(channel instanceof Ci.nsIHttpChannel) ||
    !(channel instanceof Ci.nsIUploadChannel)) {
    return post;
  }
  if (channel.requestMethod !== 'POST') {
    return post;
  }

  try {
    let us = channel.uploadStream;
    if (!us) {
      return post;
    }
    if (us instanceof Ci.nsIMultiplexInputStream) {
      // Seeking in a nsIMultiplexInputStream effectively breaks the stream.
      return post;
    }
    if (!(us instanceof Ci.nsISeekableStream)) {
      // Cannot seek within the stream :(
      return post;
    }

    let oldpos = us.tell();
    us.seek(0, 0);

    try {
      let is = new ScriptableInputStream(us);

      // we'll read max 64k
      let available = Math.min(is.available(), 1 << 16);
      if (available) {
        post = is.read(available);
      }
    }
    finally {
      // Always restore the stream position!
      us.seek(0, oldpos);
    }
  }
  catch (ex) {
    Cu.reportError(ex);
  }
  return post;
}

Depending on your use case, you might want to check if the us instanceof eg nsIMIMEInputStream or nsIStringInputStream for special handling or fast-paths...

You'd call it from your observer like:

  observe: function(subject, topic, data) {
    if (topic == 'http-on-modify-request') {
      observeRequest(subject, topic, data);
    }
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM