Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to emulate browser-cookie implementation?

I'm trying to a create a fully functional web proxy using node.js which essentially downloads the webpage and displays it to the client. I'm having a problem implementing cookies though as it's harder than I thought since they have so many rules.

Are there any libraries that have been already made to emulate how a browser handles cookies?

like image 652
Trevor Avatar asked Nov 04 '11 08:11

Trevor


2 Answers

Use request, it already handles storing the cookies for consecutive requests in a cookie jar.

Or if you don't want to reinvent the wheel use node-http-proxy from Nodejitsu for a full blown proxy.

like image 58
Jan Jongboom Avatar answered Nov 10 '22 17:11

Jan Jongboom


You should really have a look at Tobi's implementation of cookie, here are some useful links:

Cookie and cookie jar:
https://github.com/LearnBoost/tobi/blob/master/lib/cookie/index.js https://github.com/LearnBoost/tobi/blob/master/lib/cookie/jar.js

Tests for cookie and cookie jar:
https://github.com/LearnBoost/tobi/blob/master/test/cookie.test.js https://github.com/LearnBoost/tobi/blob/master/test/cookie.jar.test.js

Last but not least, look at the browser file, to see how they are implemented on requests: (look for every occurence of the word "cookie" in that file)
https://github.com/LearnBoost/tobi/blob/master/lib/browser.js

like image 41
alessioalex Avatar answered Nov 10 '22 17:11

alessioalex