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
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
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!
> 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
Right, that is before the cookie manager sets cookies on the channel.
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
> 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 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
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...