PDA

View Full Version : Cookies in SlimServer7



GoCubs
2007-09-07, 09:58
Recently I started having cookie issues with the SlimServer 7 version of my Sirius plugin.

Is SlimServer 7 handling cookies differently? In the past I've handled cookies myself be retrieving them in a http call, caching them, and passing them back manually in the HTTP headers in async calls. It seems like something recently changed and my cookies are being overridden/ignored.

I'm fairly certain something new is going on in SS7 as there is now a cookies.dat in the Cache directory with entries pertaining to the Sirius site.

Is there a way prevent the new caching logic and let me manually handle cookies?

Much thanks
-Greg

andyg
2007-09-07, 10:04
Yeah some automatic cookie handling code was recently added: http://svn.slimdevices.com/trunk/server/Slim/Networking/Async/HTTP.pm?rev=12535&view=rev

IMO, it makes sense to let the underlying HTTP code handle cookies.

What do you need to do that this won't allow?

GoCubs
2007-09-07, 10:31
Yeah some automatic cookie handling code was recently added: http://svn.slimdevices.com/trunk/server/Slim/Networking/Async/HTTP.pm?rev=12535&view=rev

IMO, it makes sense to let the underlying HTTP code handle cookies.

What do you need to do that this won't allow?
It wasn't a fun task figuring out exactly which cookies to pass throughout the Sirius login process and it would be nice to have something handle this automatically, but it doesn't appear to be working in my situation. There are also some additional required cookies that I manually set aren't currently set by any of the web pages the plugin visits.

Is there a way to have to automatic cookie handling unless a cookie string is passed manually in the async call the old way (in headers)?

-Greg

Triode
2007-09-07, 10:52
Well your plugin can access the cookie jar and add delete them.

I use this the following to set a cookie once prior to using async http to
carry on using it:

my $url = shift;

my $ua = new LWP::UserAgent;

$ua->cookie_jar(Slim::Networking::Async::HTTP::cookie_j ar());

my $res = $ua->post('<some url>',
Content_Type => 'form-data',
Content => [
'email_address' => $username,
'password' => $password,
'remember' => '1'
]
);

So essentially you can access a cookie jar and do anything you can with
HTTP::Cookies

Does this work for you?


----- Original Message -----
From: "GoCubs" <GoCubs.2wjwkb1189186501 (AT) no-mx (DOT) forums.slimdevices.com>
To: <developers (AT) lists (DOT) slimdevices.com>
Sent: Friday, September 07, 2007 6:31 PM
Subject: Re: [Developers] Cookies in SlimServer7


>
> andyg;225866 Wrote:
>> Yeah some automatic cookie handling code was recently added:
>> http://svn.slimdevices.com/trunk/server/Slim/Networking/Async/HTTP.pm?rev=12535&view=rev
>>
>> IMO, it makes sense to let the underlying HTTP code handle cookies.
>>
>> What do you need to do that this won't allow?
> It wasn't a fun task figuring out exactly which cookies to pass
> throughout the Sirius login process and it would be nice to have
> something handle this automatically, but it doesn't appear to be
> working in my situation. There are also some additional required
> cookies that I manually set aren't currently set by any of the web
> pages the plugin visits.
>
> Is there a way to have to automatic cookie handling unless a cookie
> string is passed manually in the async call the old way (in headers)?
>
> -Greg
>
>
> --
> GoCubs
> ------------------------------------------------------------------------
> GoCubs's Profile: http://forums.slimdevices.com/member.php?userid=312
> View this thread: http://forums.slimdevices.com/showthread.php?t=38238
>
>

GoCubs
2007-09-07, 11:15
Well your plugin can access the cookie jar and add delete them.

I use this the following to set a cookie once prior to using async http to
carry on using it:

my $url = shift;

my $ua = new LWP::UserAgent;

$ua->cookie_jar(Slim::Networking::Async::HTTP::cookie_j ar());

my $res = $ua->post('<some url>',
Content_Type => 'form-data',
Content => [
'email_address' => $username,
'password' => $password,
'remember' => '1'
]
);

So essentially you can access a cookie jar and do anything you can with
HTTP::Cookies

Does this work for you?

As always, thanks Andy. Plugins/Perl development is sort of a side hobby for me but for some reason I've chosen do develop more involved plugins...

I'll try setting the cookies using your method above. So if I understand it right, email_address, password, etc are the cookies you're setting even though the content-type is 'form-data'? Also, how long are the cookies 'cached' on SlimServer?

-Greg

Triode
2007-09-07, 11:47
> As always, thanks Andy. Plugins/Perl development is sort of a side
> hobby for me but for some reason I've chosen do develop more involved
> plugins...

Hey this is a hobby for me too...

I probably confused you with the code fragment. What I was saying is that
it uses a HTTP::Cookies cookie_jar which you have access too.

So in my code fragment I showed creating a new user agent and use it to
connect to a web site, but I set the cookie jar to be the one used by http
so this can later reuse the same cookies.

I don't know what your plugin does, but you should be able to create a
cookie and store it in the cookie jar used by httpasync by:

my $cookie_jar = Slim::Networking::Async::HTTP::cookie_jar()

this is now a HTTP::Cookies object so you can call any methods from
HTTP::Cookies on it
http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Cookies.pm

so $cookie_jar->set_cookie() should set a cookie etc

GoCubs
2007-09-07, 12:22
>
Hey this is a hobby for me too...

I probably confused you with the code fragment. What I was saying is that
it uses a HTTP::Cookies cookie_jar which you have access too.

So in my code fragment I showed creating a new user agent and use it to
connect to a web site, but I set the cookie jar to be the one used by http
so this can later reuse the same cookies.

I don't know what your plugin does, but you should be able to create a
cookie and store it in the cookie jar used by httpasync by:

my $cookie_jar = Slim::Networking::Async::HTTP::cookie_jar()

this is now a HTTP::Cookies object so you can call any methods from
HTTP::Cookies on it
http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Cookies.pm

so $cookie_jar->set_cookie() should set a cookie etc
Oops, thanks Triode. I thought it was Andy responding with the code fragment.

I'll give er a whirl. It's just frustrating having to go through the process again as it wasn't fun reverse engineering Sirius's login/cookie process initially and Sirius only gives you a handful of login attempts each day to play with.

I'm not sure how many other plugins manually set cookies, but until I found cookies.dat I was going nuts trying to figure out why my plugin was acting unpredictably. It'd probably be useful to output some sort of message if a cookie is manually passed in the header.

-Greg