Trevor
Trevor

Reputation: 1385

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?

Upvotes: 6

Views: 1719

Answers (2)

Jan Jongboom
Jan Jongboom

Reputation: 27313

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.

Upvotes: 2

alessioalex
alessioalex

Reputation: 63663

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

Upvotes: 1

Related Questions