-1

Iam building an extension where i catch all the post requests. But in the httpChannel.originalURI.spec there aren't any attributes from the post. How can i get the post's atrtibutes?

myObserver.prototype = {

 observe: function(subject, topic, data) {

  if("http-on-modify-request"){

    var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);

    if(httpChannel.requestMethod=="POST")
      alert(httpChannel.originalURI.spec);

       }
  }

},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);

},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                        .getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
}
}

Any ideas?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tony
  • 489
  • 2
  • 5
  • 22

2 Answers2

0

nsIHttpChannel only provides access to HTTP headers. POST data is sent as part of the request body so you need to change your object interface to nsIUploadChannel and read the binary upload data into a string.

var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
var uploadStream = uploadChannel.uploadStream;
uploadStream.QueryInterface(Ci.nsISeekableStream).
             seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
var binStream = Cc["@mozilla.org/binaryinputstream;1"].
                createInstance(Ci.nsIBinaryInputStream);
binStream.setInputStream(uploadStream);
var postBytes = binStream.readByteArray(binStream.available());
var postString = String.fromCharCode.apply(null, postBytes);
Luckyrat
  • 1,455
  • 14
  • 16
  • You'll want to make sure that the uploadChannel.uploadStream in question is NOT a nsIMultiplexInputStream. Those multiplexed streams are quite buggy when it comes to seeking, which I learned the hard way when my code broke lots of other extensions, mainly picture uploaders. Also, please rewind the stream (seek to the beginning) after you're done it it. – nmaier Aug 02 '13 at 16:33
-1

The code from Luckyrat did not work properly for me. I had to deal with some requests timing out. Noticing nmaiers comment this code is working correctly (as far as I can tell):

function getPostString(httpChannel) {
    var postStr = "";
    try {
        var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
        var uploadChannelStream = uploadChannel.uploadStream;
        if (!(uploadChannelStream instanceof Ci.nsIMultiplexInputStream)) {
            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
            var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
            stream.setInputStream(uploadChannelStream);
            var postBytes = stream.readByteArray(stream.available());

            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(0, 0);

            postStr = String.fromCharCode.apply(null, postBytes);
        }
    }
    catch (e) {
        console.error("Error while reading post string from channel: ", e);
    }
    finally {
        return postStr;
    }
}
DWK
  • 102
  • 1
  • 4