Skip to content

the-cave/cookiejar-of-greed

Repository files navigation

Cookiejar of Greed

What is it?

Cookiejar of Greed is a practical cookiejar implementation for Ruby.
It accepts and tolerates more HTTP cookies than the RFCs, the goal is to solve real-world problems people are facing, and maintains high compatibility with major browsers.

Why another cookiejar?

To be exact, for me, I have an issue with the existing one.
sparklemotion/http-cookie#28
Apart from what I directly encountered, the code base seems unmaintained, hard to work with, and plague with thread-safety issues.

IMO The existing one is beyond fixing, so I created another tiny one for myself, implementing a subset of the former.

Installation

gem 'cookiejar_of_greed'

What problem does this gem solves?

  • Library loading are thread-safed, leveraging on ActiveSupport::Autoload.
  • More tolerance to non-standard cookies

Which features the gem does not currently supported?

  • Cookie limit, and storage limit. (Depends on your use cases, this can made your implementation insecure.)
  • Netscape's cookies.txt serialization

Usage examples

Creating a cookiejar

require 'cookiejar_of_greed'
cookie_jar = ::Greed::Cookie::Jar.new

Adding cookies to the jar

# Whenever you receive a "set-cookie" header, just feed it to the jar
# everything else will be taken care of
cookie_jar.parse_set_cookie(
  'http://localhost/', # the document location that sent the header
  'cookiename=value; domain=localhost; max-age=3600' # the header
)

Persisting or serializing the content of the jar

serializable_content = cookie_jar.dump
serialized_jar = ::YAML.dump(serializable_content)
# persist serialized_jar somewhere

# jar can be created with dumped states
rehydrated_jar = ::Greed::Cookie::Jar.new(serializable_content)

Retrieving cookies from the jar for a HTTP request

cookie_header = cookie_jar.cookie_header_for('http://localhost/dashboard')

Contributing

Even though I started the project for myself, you can modify it to suit your use cases; a pull request is always welcome and discussable.

License

Cookiejar of Greed is released under the BSD 3-Clause License. 🎉