I believe you have to make the stream seekable (so that you can rewind
it after you read it, in order for the POST request to be sent to the
server properly), and, if you are using JavaScript, scriptable (so that
we can access it from JavaScript).
So, assuming you intercept the request before it is sent to the server,
you would do something like:
channel.QueryInterface(Components.interfaces.nsIUploadChannel);
channel.uploadStream.QueryInterface(Components.interfaces.nsISeekableStream);
var stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
stream.init(channel.uploadStream);
channel.uploadStream.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET,
0);
...then read from stream: stream.read(...)
...and finally maybe set the POST body to something else, depending on
what you read in, and allow the request to proceed:
var stringStream =
Components.classes["@mozilla.org/io/string-input-stream;1"]
.createInstance(Components.interfaces.nsIStringInputStream);
var newbody = <some string>;
stringStream.setData(newbody, newbody.length);
// have to rewind the stream before setting the new POST body
channel.QueryInterface(Components.interfaces.nsIUploadChannel);
channel.uploadStream.QueryInterface(Components.interfaces.nsISeekableStream);
channel.uploadStream.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET,
0);
channel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);
channel.uploadStream.setData(stringStream);
Since you are using C++, you probably do not need to cast to
nsIScriptableInputStream at the beginning.
HTH
nikitas
[EMAIL PROTECTED] wrote:
> On Feb 20, 12:01 am, [EMAIL PROTECTED] wrote:
>> Hi,
>>
>> Can you please tell me how can I get the HTTP Post data from a http
>> observer?
>>
>> Thank you.
>
> I did some research, and come up the following code.
> But when i read the UploadStream, it always return 0. I have checked
> via etheral, there is post data, but my code just is not able to read
> it.
>
> Can you please tell me what did I do wrong?
>
> here is an overview of what I did:
> 1. cast the observer subject to be a nsIUploadChannel
> 2. get the Upload input Stream from the uploadChannel
> 3. read from the upload input stream.
>
> But i just get 0 when I read from the stream.
>
> nsCAutoString requestMethod;
> nsCOMPtr<nsIHttpChannel> channel =
> do_QueryInterface(aSubject);
>
> channel->GetRequestMethod(requestMethod);
>
> FILE* log = fopen( "http.log", "a+");
>
> if (requestMethod.Equals("POST") ) {
>
> nsCOMPtr<nsIUploadChannel> uploadChannel =
> do_QueryInterface(aSubject);
>
> if (uploadChannel) {
> nsCOMPtr<nsIInputStream> is;
> uploadChannel->GetUploadStream(getter_AddRefs(is));
>
> if(is) {
> nsresult rv2;
>
> PRUint32 numRead;
> char buf[512];
> fprintf (log, "%s POST ",uri);
> while (1)
> {
> rv = is->Read(buf, sizeof(buf), &numRead);
> if (NS_FAILED(rv))
> {
> printf("### error reading stream: %x\n", rv);
> fprintf (log, " error ");
> break;
> }
>
> if (numRead == 0) {
> fprintf (log, " nothing ");
> break;
> }
>
> buf[numRead -1]= '\0';
> // buf now contains numRead bytes of data
> fprintf (log, "%s",buf);
> }
>
> fprintf (log, "\n");
> }
>
> } else {
>
> fprintf (log, "%s POST \n",uri);
> }
>
> } else {
> fprintf (log, "%s\n",uri);
> }
>
> fclose(log);
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network