sample code for byte-streams module?

14 views
Skip to first unread message

sajal

unread,
Nov 14, 2010, 2:31:39 PM11/14/10
to mozilla-labs-jetpack
I am doing an extension, which processes the request and adds certain
headers for authentication to a custom web service.

If the request is a POST, it needs the data being posted.... I am
having problems accessing this.

Here is the code so far to collect this information : http://pastie.org/1297613

the line
console.log(post.uploadStream);
gives me
info: [xpconnect wrapped nsIInputStream]

but bs.ByteReader(post.uploadStream);
is undefined....

I am too new to all this.. maybe im missing something really simple...
so is there any sample code available which uses byte-streams in the
correct manner, please let me know...

Thanks in advance
Sajal

Drew Willcoxon

unread,
Nov 14, 2010, 4:07:59 PM11/14/10
to mozilla-la...@googlegroups.com
ByteReader is a "low-level" constructor, so unlike the high-level
constructors in addon-kit, you need to use the "new" operator, like:

new bs.ByteReader(post.uploadStream)

The low-level file module uses ByteReader, so you can look to it for an
example. GitHub is down ATM, so here it is on mxr:

http://mxr.mozilla.org/labs-central/source/jetpack-sdk/packages/jetpack-core/lib/file.js#170

Drew

Sajal Kayan

unread,
Nov 15, 2010, 1:15:13 AM11/15/10
to mozilla-la...@googlegroups.com
Hi Drew,

Thanks a lot for the help. Works like expected now.

-Sajal

sajal

unread,
Nov 18, 2010, 6:53:37 AM11/18/10
to mozilla-labs-jetpack
Hi all,

I am still struggling a little with the ByteReader.

http://pastie.org/1308140

The problem is with this code running, all POST requests fail.

It has no problems in displaying the POST data on the console, however
it doesn't seem to make a proper request to the destination webserver.
It keeps showing "waiting for ..... " until Firefox times out that
request.

If i uncomment the reader.close() (line 26) , then Firefox says "Done"
as soon as i click a submit button.

My feeling is that the statement
data = reader.read();
[line 25]

Is somehow modifying the stream. If this is the case, is there a
neater way to read the stream (or another API to get just the POST
data) from the stream without affecting the query?

Thanks in Advance
Sajal

On Nov 15, 1:15 pm, Sajal Kayan <saja...@gmail.com> wrote:
> Hi Drew,
>
> Thanks a lot for the help. Works like expected now.
>
> -Sajal
>
> On 11/15/2010 04:07 AM, Drew Willcoxon wrote:
>
> > ByteReader is a "low-level" constructor, so unlike the high-level
> > constructors in addon-kit, you need to use the "new" operator, like:
>
> >   new bs.ByteReader(post.uploadStream)
>
> > The low-level file module uses ByteReader, so you can look to it for an
> > example.  GitHub is down ATM, so here it is on mxr:
>
> >http://mxr.mozilla.org/labs-central/source/jetpack-sdk/packages/jetpa...

Drew Willcoxon

unread,
Nov 18, 2010, 11:12:50 PM11/18/10
to mozilla-la...@googlegroups.com
The problem is you're modifying -- by reading and closing -- the same
stream that Firefox uses to complete the request. You can read the
stream, but you'll need rewind it once you're done, and you shouldn't
close it. byte-streams doesn't provide a way to rewind, but you can use
XPCOM. See the last paragraph in this section for details:

https://developer.mozilla.org/en/Code_snippets/Miscellaneous#Getting_postData_of_a_request_before_the_request_is_sent

Drew

Sajal Kayan

unread,
Nov 19, 2010, 10:34:58 AM11/19/10
to mozilla-la...@googlegroups.com
Hi Drew,

Thanks again for your help. Works fine now.

I researched for a while before asking here... don't know how i missed that.

Is there any central repository of more user generated snippets from
were n00bs like me can learn by others code?


BTW, instead of the byte-streams module, ive ended up using this

function getPostData(subject){
subject.QueryInterface(components.interfaces.nsIUploadChannel);
var postData = subject.uploadStream;
var stream =
Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);

stream.setInputStream(postData);
var postBytes = stream.readByteArray(stream.available());
var poststr = String.fromCharCode.apply(null, postBytes);

postData.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET,
0);
return poststr;
}

-Sajal

Reply all
Reply to author
Forward
0 new messages