简体   繁体   中英

Post header in extension firefox

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?

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

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

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