ninenines / gun

HTTP/1.1, HTTP/2, Websocket client (and more) for Erlang/OTP.
ISC License
906 stars 230 forks source link

Cookie support #35

Closed essen closed 4 years ago

essen commented 9 years ago

It could be interesting to support cookies. Thinking it should be per connection though, with cookies being forwarded to the owner process so that it can keep them elsewhere if needed.

marianoguerra commented 9 years ago

maybe implemented as I've seen in some other places where you have an instance of a "cookie jar" where you create a new instance, then pass responses (or headers or just the set-cookie headers) and it stores them, then you can ask it to format the Cookie request header for you.

something like:

Jar = cow_cookie_jar:new().
Jar1 = cow_cookie_jar:update(Jar, Response).
Jar2 = cow_cookie_jar:update(Jar1, Response1).
ReqCookieHeader = cow_cookie_har:format(Jar2).

what do you think?

essen commented 9 years ago

I want something per connection. I want 2 concurrent Gun connections to be able to use separate cookies. So Gun will keep track of cookies and send them back automatically.

Gun will also provide an interface to interact with cookies allowing for some kind of "cookie jar" pattern, but that pattern is not what I would like to implement.

Having separate cookies per connection allow having 2 concurrent connections querying a single website's API using different cookie values, which could be different users.

marianoguerra commented 9 years ago

yep, that makes it easier for the library user, I thought you wanted to provide cookie handling logic but leave it to the user to provide the cookies to the request.

essen commented 9 years ago

The only use case I am considering is cookies containing session tokens, and for that use case it's better to automatically handle it. Other use cases are more rare and generally not recommended so it's OK if the user needs to write a little extra code for those.

Thinking about it a bit more I think the user should be able to provide cookies for a request through the normal "cookie" header too, overriding any cookie Gun was already handling.

Anyway I probably will take a look only after I release Gun 1.0.

essen commented 5 years ago

I'll think about a callback interface for implementing cookie jar backends. It should be made to be ready for the current cookie draft at https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html

essen commented 5 years ago

https://docs.oracle.com/javase/7/docs/api/java/net/CookieStore.html is probably a good inspiration.

One important thing we're missing is parsing of the setcookie header and building of the cookie header.

essen commented 4 years ago

This part is basically what I started to implement, but it'll take a while. https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html#storage-model

essen commented 4 years ago

A copy of the work done so far is available at https://gist.github.com/essen/5e0908a2ff230ed72ad7124dd3ff554c

I will create a proper branch and PR once it gets further, perhaps once there are some tests. Currently it only compiles.

Tests should probably be based on https://github.com/web-platform-tests/wpt/tree/master/cookies

essen commented 4 years ago

Cookie store is roughly done. GC remains, as well as using it in Gun, and minor details. All Web platform tests pass.

essen commented 4 years ago

I've added the public suffix check that was missing.

essen commented 4 years ago

I've just pushed a WIP commit where Gun is actually using the cookie store, and some of the WPT tests pass (all http-state ones pass for HTTP, most pass for HTTPS, none pass for HTTP/2).

essen commented 4 years ago

The code in https://github.com/ninenines/gun/pull/219 is basically complete. I will merge once the documentation is written.

essen commented 4 years ago

Documentation has been added to https://github.com/ninenines/gun/pull/219 and it will be merged during the week.

essen commented 4 years ago

This has been merged. I will prepare a new pre-release on Wednesday. Closing, thanks!