Tag: cookies

Rails, Cookies (ciasteczka) oraz przecinki

Kilka dni temu, potrzebowałem odczytywać na potrzeby analizy, zawartość ciastka odpowiedzialnego za GUI interfejsu do Susanoo. Jakież było moje zdziwienie gdy zamiast oczekiwanego:

0,1,3,4,2,5,7,6,8||||280||||1200

Railsy widziały samo 0, a dokładniej to wartości do pierwszego przecinka.

Po chwili googlania, znalazłem taką oto wiadomość:

According to RFC 2109, commas aren't allowed in the cookie value and should be encoded.

Jak się później okazało, Railsy po prostu zachowują się poprawnie ;) więc musiałem nie ładować do ciastka przecinków.

Okazało się to bardzo proste, ponieważ między ciastkiem a warstwą aplikacji mam jeszcze warstwę abstrakcji, w której dzięki niewielkiej zmianie, zamieniam wszystkie przecinki na pauzy w taki sposób:

value = (String(value)).replace(/\,/gi,'-');

Następnie przy odczycie, robię to samo tylko w drugą stronę:

ret = ret.replace(/\-/gi,',');

Dodam tylko, że w zawartości ciastka, nie powinno być także poniższych znaków:

()<>@;:\"/[]?={}

oraz spacji i tabulatorów.

Why I have decided to quit using Cookie Jar

Recently I've been developing some JS stuff using Cookie Jar (source). Everything went great and Cookie Jar got along even with IE :) However, cookie jar cookie happened to grow and grow to fast.

Cookie Jar stores parts of Cookie as a key-value pair [key][value], so if you have a lot of small stuff - the cookie will be two times bigger than the data stored in it. Example:

[m][5] - number of chars: 2 (m,5); total amount: 6 ; waste 4 chars.

Phi, nothing special you say. Only 4 characters. In such a simple example maybe but if you store a lot of stuff, waste will be much bigger.

There has been additional problem. Some characters where stored as HTML entities. I don't know why - I've stored in cookie jar only numbers ;)

So, my cookie grew and grew more rapidly. And along with this - something else happened. Performance started to be really bad. Everything worked really slow. Can't say whether or not it is only Cookie Jar fault, but after switching to my own cookie engine, performance got better.

When I've been using Cookie Jar - "setting up" GUI took some time, so users felt this delay. Currently all init stuff goes really fast and none of them feels this.

Whether or not use Cookie Jar?

Cookie jar is convenient and easy to use. If you don't store whole bunch of small stuff in it and you don't care so much about performance - definitely you should use it. However if you need something really fast and thin - you should write your own dedicated cookie handler.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑