Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing an http channel's cookies header

82 views
Skip to first unread message

Tom

unread,
May 8, 2008, 10:39:20 PM5/8/08
to
Hi All,

I'm hitting some weird issues with setting a cookies for httpChannel. So
what I'm doing is:

httpChannel.setRequestHeader('cookie', 'foo=bar', false);

What happens though is that it appends foo=bar to the cookie list.
That's not what I want though. I want to make sure that if there is an
existing cookie foo then I want it's value to change to bar.

Thinking myself clever I tried httpChannel.setRequestHeader('cookie',
null, false); and httpChannel.setRequestHeader('cookie', '', false);
thinking that
http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#59
means that that should clear the cookies from that channel and then I
can set them to what I want. It doesn't seem to do that (possibly
related to an exception that's thrown by that channel if I try to get
that header).

Another option would be to use the cookie manager. I don't want to use
this as the cookie manager will not only affect the users's session but
stop me from doing parallel requests (since the cookie manager's cookies
are shared).

Any ideas on what I'm doing wrong?

Thanks,

Tom

Doug Turner

unread,
May 8, 2008, 11:43:38 PM5/8/08
to Tom, dev-pl...@lists.mozilla.org
have you just tried to enumerator over the headers to check for the
foo header using:

http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/public/nsIHttpChannel.idl#144

> _______________________________________________
> dev-platform mailing list
> dev-pl...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-platform

Doug Turner

unread,
May 9, 2008, 12:12:19 AM5/9/08
to Tom Aratyn, dev-pl...@lists.mozilla.org, Tom
>>>
> Hi Doug,
>
> Thanks for replying.
>
> I haven't tried that but just to clarify I'm not looking for a foo
> header. I'm looking for the foo cookie in the Cookie's header.
>
> Tom


Right, there isn't a straight forward way to get a specific value from
the cookie header from the http stream. Looking at the cookie manager
API might work, or you can also inspect the header for the value you
are looking for, then set it as required:

var httpChannel =
aRequest.QueryInterface(Components.interfaces.nsIHttpChannel)
cookieData = httpChannel.getRequestHeader("cookie");
// look for |foo = *| using regular expressions
// if match:
httpChannel.setRequestHeader('cookie', 'foo=<newvalue>', false);

hope this helps!

Doug Turner

unread,
May 9, 2008, 12:24:06 AM5/9/08
to Tom Aratyn, dev-pl...@lists.mozilla.org, Tom

On May 8, 2008, at 9:15 PM, Tom Aratyn wrote:

> Hi Doug,
>
> That's precisely what I'm doing. And what it does is instead of
> overwritting foo's value it just appends :-(. Maybe using the visitor
> will work better...
>
> Tom

I guess it depends on when in the network load process are you
attempting to change the headers. Are you doing this from the "http-
on-modify-request" event?

Doug

Doug Turner

unread,
May 9, 2008, 1:23:43 AM5/9/08
to Tom Aratyn, dev-pl...@lists.mozilla.org, Tom
>>
> No, I'm creating the channel using the IO service and this is all done
> before I do the async open.


Right, that is before the cookie manager sets cookies on the channel.

Mike Shaver

unread,
May 9, 2008, 9:12:32 AM5/9/08
to Tom, dev-pl...@lists.mozilla.org
On Thu, May 8, 2008 at 7:39 PM, Tom <Themys...@gmail.com> wrote:
> httpChannel.setRequestHeader('cookie', 'foo=bar', false);
>
> What happens though is that it appends foo=bar to the cookie list.
> That's not what I want though. I want to make sure that if there is an
> existing cookie foo then I want it's value to change to bar.
>
> Thinking myself clever I tried httpChannel.setRequestHeader('cookie',
> null, false); and httpChannel.setRequestHeader('cookie', '', false);
> thinking that
> http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#59
> means that that should clear the cookies from that channel and then I
> can set them to what I want. It doesn't seem to do that (possibly
> related to an exception that's thrown by that channel if I try to get
> that header).

If you've tried the approach in

http://developer.mozilla.org/en/docs/Creating_Sandboxed_HTTP_Connections

and it's not working, can you post sample code and details of the
exception you get?

Mike

Doug Turner

unread,
May 9, 2008, 10:19:51 AM5/9/08
to Mike Shaver, dev-pl...@lists.mozilla.org, Tom

On May 9, 2008, at 6:12 AM, Mike Shaver wrote:

> On Thu, May 8, 2008 at 7:39 PM, Tom <Themys...@gmail.com> wrote:

>> httpChannel.setRequestHeader('cookie', 'foo=bar', false);
>>
>> What happens though is that it appends foo=bar to the cookie list.
>> That's not what I want though. I want to make sure that if there is
>> an
>> existing cookie foo then I want it's value to change to bar.
>>
>> Thinking myself clever I tried httpChannel.setRequestHeader('cookie',
>> null, false); and httpChannel.setRequestHeader('cookie', '', false);
>> thinking that
>> http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#59
>> means that that should clear the cookies from that channel and then I
>> can set them to what I want. It doesn't seem to do that (possibly
>> related to an exception that's thrown by that channel if I try to get
>> that header).
>

> If you've tried the approach in
>
> http://developer.mozilla.org/en/docs/Creating_Sandboxed_HTTP_Connections
>
> and it's not working, can you post sample code and details of the
> exception you get?
>

Tom is modifying the channel before AsyncOpen. It will work fine if
the check for the header happens in http-on-modify-request as
illustrated by the documentation.

Doug

Doug Turner

unread,
May 9, 2008, 1:06:10 PM5/9/08
to Tom, dev-pl...@lists.mozilla.org, Mike Shaver

On May 9, 2008, at 9:58 AM, Tom wrote:

> Doug Turner wrote:
>>
>> On May 9, 2008, at 6:12 AM, Mike Shaver wrote:
>>
>>> On Thu, May 8, 2008 at 7:39 PM, Tom <Themys...@gmail.com> wrote:

>>>> httpChannel.setRequestHeader('cookie', 'foo=bar', false);
>>>>
>>>> What happens though is that it appends foo=bar to the cookie list.
>>>> That's not what I want though. I want to make sure that if there
>>>> is an
>>>> existing cookie foo then I want it's value to change to bar.
>>>>
>>>> Thinking myself clever I tried
>>>> httpChannel.setRequestHeader('cookie',
>>>> null, false); and httpChannel.setRequestHeader('cookie', '',
>>>> false);
>>>> thinking that
>>>> http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#59
>>>>
>>>> means that that should clear the cookies from that channel and
>>>> then I
>>>> can set them to what I want. It doesn't seem to do that (possibly
>>>> related to an exception that's thrown by that channel if I try to
>>>> get
>>>> that header).
>>>

>>> If you've tried the approach in
>>>
>>> http://developer.mozilla.org/en/docs/Creating_Sandboxed_HTTP_Connections
>>>
>>> and it's not working, can you post sample code and details of the
>>> exception you get?
>>>
>>
>> Tom is modifying the channel before AsyncOpen. It will work fine if
>> the check for the header happens in http-on-modify-request as
>> illustrated by the documentation.
>>
>> Doug

> Out of curiosity, why does that happen? Why is it that I can't modify
> this header before I open the request?


When AsyncOpen is called on the http channel, cookies are added into
the request replacing what was there. Once the cookies are set in
AsyncOpen, the http-on-modify-request is called allowing you to modify
this header:

http://lxr.mozilla.org/mozilla/source/netwerk/protocol/http/src/nsHttpChannel.cpp#3683

If you want to set cookies on a channel before AsyncOpen is called,
using the cookie manager might be the way to go.

Regards,
Doug

themys...@gmail.com

unread,
May 12, 2008, 11:13:15 PM5/12/08
to
Thanks Doug and Mike, that did solve the issue.

On May 9, 1:06 pm, Doug Turner <doug.tur...@gmail.com> wrote:
> On May 9, 2008, at 9:58 AM, Tom wrote:
>
>
>
> > Doug Turner wrote:
>
> >> On May 9, 2008, at 6:12 AM, Mike Shaver wrote:
>

> >>> On Thu, May 8, 2008 at 7:39 PM, Tom <Themystic...@gmail.com> wrote:
> >>>> httpChannel.setRequestHeader('cookie', 'foo=bar', false);
>
> >>>> What happens though is that it appends foo=bar to the cookie list.
> >>>> That's not what I want though. I want to make sure that if there
> >>>> is an
> >>>> existing cookie foo then I want it's value to change to bar.
>
> >>>> Thinking myself clever I tried
> >>>> httpChannel.setRequestHeader('cookie',
> >>>> null, false); and httpChannel.setRequestHeader('cookie', '',
> >>>> false);
> >>>> thinking that

> >>>>http://mxr.mozilla.org/mozilla1.8/source/netwerk/protocol/http/src/ns...


>
> >>>> means that that should clear the cookies from that channel and
> >>>> then I
> >>>> can set them to what I want. It doesn't seem to do that (possibly
> >>>> related to an exception that's thrown by that channel if I try to
> >>>> get
> >>>> that header).
>
> >>> If you've tried the approach in
>
> >>>http://developer.mozilla.org/en/docs/Creating_Sandboxed_HTTP_Connections
>
> >>> and it's not working, can you post sample code and details of the
> >>> exception you get?
>
> >> Tom is modifying the channel before AsyncOpen. It will work fine if
> >> the check for the header happens in http-on-modify-request as
> >> illustrated by the documentation.
>
> >> Doug
> > Out of curiosity, why does that happen? Why is it that I can't modify
> > this header before I open the request?
>
> When AsyncOpen is called on the http channel, cookies are added into
> the request replacing what was there. Once the cookies are set in
> AsyncOpen, the http-on-modify-request is called allowing you to modify
> this header:
>

> http://lxr.mozilla.org/mozilla/source/netwerk/protocol/http/src/nsHtt...

0 new messages