cookieless 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +1 -1
  2. data/VERSION +1 -1
  3. data/lib/cookieless.rb +24 -15
  4. metadata +18 -18
data/README.rdoc CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  2, Add it as middleware to your application.rb
13
13
 
14
- config.middleware.use Rack::Cookieless
14
+ config.middleware.insert_before ActionDispatch::Cookies, Rack::Cookieless, :session_id => :si
15
15
 
16
16
  3, Enjoy it!
17
17
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/cookieless.rb CHANGED
@@ -9,22 +9,23 @@ module Rack
9
9
 
10
10
  def call(env)
11
11
  # have cookies or not
12
- support_cookie = env["action_dispatch.cookies"].present?
12
+ support_cookie = env["HTTP_COOKIE"].present?
13
13
 
14
14
  if support_cookie
15
15
  @app.call(env)
16
16
  else
17
- session_id, session = get_session_by_query(env["QUERY_STRING"], env) || get_session_by_query((URI.parse(env['HTTP_REFERER']).query rescue nil), env)
18
- env["rack.session"].update(session) if session
17
+ session_id, cookies = get_cookies_by_query(env["QUERY_STRING"], env) || get_cookies_by_query((URI.parse(env['HTTP_REFERER']).query rescue nil), env)
18
+ env["HTTP_COOKIE"] = cookies if cookies
19
19
 
20
20
  status, header, response = @app.call(env)
21
21
 
22
- session_id = save_session_by_id(session_id || env["rack.session"]["session_id"], env)
23
-
24
- ## fix 3xx redirect
25
- header["Location"] = convert_url(header["Location"], session_id) if header["Location"]
26
- ## only process html page
27
- response.body = process_body(response.body, session_id) if env['action_dispatch.request.path_parameters'][:format].to_s =~ /\A(html)?\Z/ && response.respond_to?(:body)
22
+ if %w(css js).exclude?(env['action_dispatch.request.path_parameters'][:format].to_s)
23
+ session_id = save_cookies_by_session_id(session_id || env["rack.session"]["session_id"], env, header["Set-Cookie"])
24
+ ## fix 3xx redirect
25
+ header["Location"] = convert_url(header["Location"], session_id) if header["Location"]
26
+ ## only process html page
27
+ response.body = process_body(response.body, session_id) if response.respond_to?(:body)
28
+ end
28
29
 
29
30
  [status, header, response]
30
31
  end
@@ -39,25 +40,33 @@ module Rack
39
40
  (@options[:session_id] || :session_id).to_s
40
41
  end
41
42
 
42
- def get_session_by_query(query, env)
43
+ def get_cookies_by_query(query, env)
43
44
  session_id = Rack::Utils.parse_query(query, "&")[session_key].to_s
44
45
  return nil if session_id.blank?
45
46
 
46
- cache_id = Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"] + env["REMOTE_ADDR"])
47
+ cache_id = generate_cookie_id(session_id, env)
47
48
  return nil unless Rails.cache.exist?(cache_id)
48
49
  return [session_id, cache_store.read(cache_id)]
49
50
  end
50
51
 
51
- def save_session_by_id(session_id, env)
52
- cache_id = Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"] + env["REMOTE_ADDR"])
53
- cache_store.write(cache_id, env["rack.session"].to_hash)
52
+ def save_cookies_by_session_id(session_id, env, cookie)
53
+ cache_store.write(generate_cookie_id(session_id, env), cookie)
54
54
  session_id
55
55
  end
56
56
 
57
+ def generate_cookie_id(session_id, env)
58
+ Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"].to_s + env["REMOTE_ADDR"].to_s)
59
+ end
60
+
57
61
  def process_body(body, session_id)
58
62
  body_doc = Nokogiri::HTML(body)
59
63
  body_doc.css("a").map { |a| a["href"] = convert_url(a['href'], session_id) if a["href"] }
60
- body_doc.css("form").map { |form| form["action"] = convert_url(form["action"], session_id) if form["action"] }
64
+ body_doc.css("form").map do |form|
65
+ if form["action"]
66
+ form["action"] = convert_url(form["action"], session_id)
67
+ form.add_child("<input type='hidden' name='#{session_key}' value='#{session_id}'>")
68
+ end
69
+ end
61
70
  body_doc.to_html
62
71
  end
63
72
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cookieless
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jinzhu
@@ -15,12 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-28 00:00:00 +08:00
18
+ date: 2011-09-04 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  type: :runtime
23
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
26
  - - ">="
@@ -29,12 +29,12 @@ dependencies:
29
29
  segments:
30
30
  - 0
31
31
  version: "0"
32
- name: nokogiri
33
- version_requirements: *id001
32
+ requirement: *id001
34
33
  prerelease: false
34
+ name: nokogiri
35
35
  - !ruby/object:Gem::Dependency
36
36
  type: :development
37
- requirement: &id002 !ruby/object:Gem::Requirement
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
39
39
  requirements:
40
40
  - - ~>
@@ -45,12 +45,12 @@ dependencies:
45
45
  - 0
46
46
  - 0
47
47
  version: 1.0.0
48
- name: bundler
49
- version_requirements: *id002
48
+ requirement: *id002
50
49
  prerelease: false
50
+ name: bundler
51
51
  - !ruby/object:Gem::Dependency
52
52
  type: :development
53
- requirement: &id003 !ruby/object:Gem::Requirement
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
56
  - - ~>
@@ -61,12 +61,12 @@ dependencies:
61
61
  - 6
62
62
  - 4
63
63
  version: 1.6.4
64
- name: jeweler
65
- version_requirements: *id003
64
+ requirement: *id003
66
65
  prerelease: false
66
+ name: jeweler
67
67
  - !ruby/object:Gem::Dependency
68
68
  type: :development
69
- requirement: &id004 !ruby/object:Gem::Requirement
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
70
  none: false
71
71
  requirements:
72
72
  - - ">="
@@ -75,9 +75,9 @@ dependencies:
75
75
  segments:
76
76
  - 0
77
77
  version: "0"
78
- name: rcov
79
- version_requirements: *id004
78
+ requirement: *id004
80
79
  prerelease: false
80
+ name: rcov
81
81
  description: Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application
82
82
  email: wosmvp@gmail.com
83
83
  executables: []
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  requirements: []
129
129
 
130
130
  rubyforge_project:
131
- rubygems_version: 1.6.2
131
+ rubygems_version: 1.4.2
132
132
  signing_key:
133
133
  specification_version: 3
134
134
  summary: Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application