[英]Trying to get HTTP POST request in Firefox extension
I'm building an extension to get the POST request in Firefox. 我正在构建一个扩展来获取Firefox中的POST请求。 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). 我阅读了文档以了解拦截页面加载和HTTP观察者,但仍无法在页面加载时获取特定的POST数据(例如:data1 = 50&sdata2 = 0&data3 = 50)。
I looked into TamperData's code, and found that they used stream.available() and stream.read(1). 我查看了TamperData的代码,发现他们使用了stream.available()和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. 首先,根本不需要"http-on-examine-response"
和TracingListener
。 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. 如果您想对响应执行某些操作,那么这些内容将具有优点,但您在请求中的数据之后,因此topic == 'http-on-modify-request'
就是这样。
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
. 假定从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... 根据您的使用情况,您可能需要检查us instanceof
例如nsIMIMEInputStream
或nsIStringInputStream
是否用于特殊处理或快速路径...
You'd call it from your observer like: 你可以从你的观察者那里称它为:
observe: function(subject, topic, data) {
if (topic == 'http-on-modify-request') {
observeRequest(subject, topic, data);
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.