Domanda

I am trying to implement a Firefox Extension which modify the POST request data.

Code follows, it fails where marked "Fails here!!!"

Any insight would be helpful.

    Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

    var newData = "test 123";
    function LOG(msg) {
        var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                     .getService(Components.interfaces.nsIConsoleService);
        consoleService.logStringMessage(msg);
    }

    function CMP() {
        this.registered = false;
        this.register();
    }

    CMP.prototype = {

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

        observe: function(subject, topic, data) 
        {
            LOG("Inside observe");
            if (topic == "http-on-modify-request") 
            {
                LOG("TOPIC is http-on-modify-request");
                var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);

                if(httpChannel.requestMethod == "POST"){
                    LOG("Inside POST")
                    var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
                    //var uploadChannelStream = uploadChannel.uploadStream; 

Modify the data here. Here for testing i am passing "test 123" as new data

                    var newStringInputStream = Components.classes['@mozilla.org/io/string-input-stream;1'].createInstance(Components.interfaces.nsIStringInputStream);
                    newStringInputStream.setData(newData,newData.length);
                    LOG("set data in newStringInputStream!!");

                    uploadChannel.setUploadStream(newStringInputStream, "text/plain", -1 );// Fails here!!!
                    httpChannel.requestMethod = "POST";
                    LOG("upload DONE!!")  
                }   
            }
        },

        QueryInterface : function(aIID) {
        if (aIID.equals(Components.interfaces.nsISupports) ||
            aIID.equals(Components.interfaces.nsIObserver))
          return this;
        throw Components.results.NS_NOINTERFACE;
        },

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

        classID: Components.ID('{F799F47E-ABA5-4AF1-B8F2-BD74E3E5BCC0}'),
        QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver])
    };

    if (XPCOMUtils.generateNSGetFactory)
    {
      var NSGetFactory = XPCOMUtils.generateNSGetFactory([CMP]);
    }
È stato utile?

Soluzione

Fixed it by changing by following in the above code. Main change was in setting modified data in httpChannel.uploadStream.

Hope this helps someone!

var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
            if(httpChannel.requestMethod == "POST")
            {
                LOG("Inside POST")
                var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel); 
                var newStringInputStream = Components.classes['@mozilla.org/io/string-input-stream;1'].createInstance(Components.interfaces.nsIStringInputStream);
                newStringInputStream.setData(newData,newData.length);
                var uploadChannelStream = uploadChannel.uploadStream;
                uploadChannelStream = uploadChannelStream.QueryInterface(Components.interfaces.nsISeekableStream).seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
                httpChannel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);
                httpChannel.uploadStream.setData(newStringInputStream);
                LOG("Done POST")
            }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top