smart_cookie_store 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +41 -0
  3. data/Rakefile +30 -0
  4. data/lib/smart_cookie_store.rb +39 -0
  5. metadata +58 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Urban Hafner
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ The Problem
2
+ ===========
3
+
4
+ `SmartCookieStore` is a fix for the current (as of 2.3.5) Rails
5
+ `CookieStore`. The problem with the `CookieStore` as is, is that it
6
+ sends a `Set-Cookie` header whenever the `session` (including the `flash`)
7
+ is accessed. This means that whenever you have a call to `flash[:notice]`
8
+ in your layout file, a `Set-Cookie` will be sent to the client. Even if
9
+ the session/flash is completely empty! This is problematic when you want
10
+ to use something like Varnish. By default it doesn't cache pages that
11
+ send a header (and rightly so).
12
+
13
+ The Solution
14
+ ============
15
+
16
+ This plugin adds a new `CookieStore`, the `SmartCookieStore` that checks
17
+ if the session is empty and only sends the `Set-Cookie` header when the
18
+ session is not empty.
19
+
20
+ Usage
21
+ =====
22
+
23
+ Install as a plugin:
24
+
25
+ script/plugin install git://github.com/msales/smart_cookie_store.git
26
+
27
+ The configure your app to use the new `SmartCookieStore`:
28
+
29
+ ActionController::Base.session_store = :smart_cookie_store
30
+
31
+ Compatibility
32
+ =============
33
+
34
+ This is an initial release that was only tested on Rails 2.3.5!
35
+
36
+ To Do
37
+ =====
38
+
39
+ This plugin should also include a `SmartAbstractStore` that fixes the
40
+ Rails `AbstractStore`. This is the parent class of the `ActiveRecordStore`
41
+ which would fix its behaviour in turn. Some unit tests would be good, too ;)
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake/gempackagetask'
2
+
3
+ PKG_FILES = FileList[
4
+ '[a-zA-Z]*',
5
+ 'generators/**/*',
6
+ 'lib/**/*',
7
+ 'rails/**/*',
8
+ 'tasks/**/*',
9
+ 'test/**/*'
10
+ ]
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = "smart_cookie_store"
14
+ s.version = "0.0.1"
15
+ s.author = "Urban Hafner"
16
+ s.email = "urban@bettong.net"
17
+ s.homepage = "http://github.com/msales/smart_cookie_store"
18
+ s.platform = Gem::Platform::RUBY
19
+ s.summary = "Enhanced Rails CookieStore"
20
+ s.files = PKG_FILES.to_a
21
+ s.require_path = "lib"
22
+
23
+ s.has_rdoc = false
24
+ s.extra_rdoc_files = ["README.md"]
25
+ end
26
+
27
+ desc 'Turn this plugin into a gem.'
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
@@ -0,0 +1,39 @@
1
+ class SmartCookieStore < ActionController::Session::CookieStore
2
+
3
+ SH = ActionController::Session::AbstractStore::SessionHash
4
+ def call(env)
5
+ env[ENV_SESSION_KEY] = SH.new(self, env)
6
+ env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup
7
+
8
+ status, headers, body = @app.call(env)
9
+
10
+ session_data = env[ENV_SESSION_KEY]
11
+ options = env[ENV_SESSION_OPTIONS_KEY]
12
+
13
+ if !session_data.is_a?(SH) || session_data.send(:loaded?) || options[:expire_after]
14
+ session_data.send(:load!) if session_data.is_a?(SH) && !session_data.send(:loaded?)
15
+
16
+ unless session_data.keys.reject {|k| k == :session_id }.empty?
17
+ marshaled_session_data = marshal(session_data.to_hash)
18
+
19
+ raise CookieOverflow if marshaled_session_data.size > MAX
20
+
21
+ cookie = Hash.new
22
+ cookie[:value] = marshaled_session_data
23
+ unless options[:expire_after].nil?
24
+ cookie[:expires] = Time.now + options[:expire_after]
25
+ end
26
+
27
+ cookie = build_cookie(@key, cookie.merge(options))
28
+ unless headers[HTTP_SET_COOKIE].blank?
29
+ headers[HTTP_SET_COOKIE] << "\n#{cookie}"
30
+ else
31
+ headers[HTTP_SET_COOKIE] = cookie
32
+ end
33
+ end
34
+ end
35
+
36
+ [status, headers, body]
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_cookie_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Urban Hafner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-29 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: urban@bettong.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - LICENSE
26
+ - Rakefile
27
+ - README.md
28
+ - lib/smart_cookie_store.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/msales/smart_cookie_store
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Enhanced Rails CookieStore
57
+ test_files: []
58
+