cookieless 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+ gem 'nokogiri'
3
+
4
+ group :development do
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.4"
7
+ gem "rcov", ">= 0"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ nokogiri (1.5.0)
10
+ rake (0.9.2)
11
+ rcov (0.9.9)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ nokogiri
20
+ rcov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jinzhu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = Cookieless
2
+
3
+ Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application
4
+
5
+
6
+ == Setup Cookieless
7
+
8
+ 1, Add cookieless to your Gemfile
9
+
10
+ gem :cookieless
11
+
12
+ 2, Add it as middleware to your application.rb
13
+
14
+ config.middleware.use Rack::Cookieless
15
+
16
+ 3, Enjoy it!
17
+
18
+
19
+ == Contributing to cookieless
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
23
+ * Fork the project
24
+ * Start a feature/bugfix branch
25
+ * Commit and push until you are happy with your contribution
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2011 Jinzhu. See LICENSE.txt for
32
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cookieless"
18
+ gem.homepage = "http://github.com/jinzhu/cookieless"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application}
21
+ gem.description = %Q{Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application}
22
+ gem.email = "wosmvp@gmail.com"
23
+ gem.authors = ["Jinzhu"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "cookieless #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/cookieless.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'digest/sha1'
2
+ require 'uri'
3
+
4
+ module Rack
5
+ class Cookieless
6
+ def initialize(app, options={})
7
+ @app, @options = app, options
8
+ end
9
+
10
+ def call(env)
11
+ # have cookies or not
12
+ support_cookie = env["action_dispatch.cookies"].present?
13
+
14
+ if support_cookie
15
+ @app.call(env)
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
19
+
20
+ status, header, response = @app.call(env)
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)
28
+
29
+ [status, header, response]
30
+ end
31
+ end
32
+
33
+ private
34
+ def cache_store
35
+ @options[:cache_store] || Rails.cache
36
+ end
37
+
38
+ def session_key
39
+ (@options[:session_id] || :session_id).to_s
40
+ end
41
+
42
+ def get_session_by_query(query, env)
43
+ session_id = Rack::Utils.parse_query(query, "&")[session_key].to_s
44
+ return nil if session_id.blank?
45
+
46
+ cache_id = Digest::SHA1.hexdigest(session_id + env["HTTP_USER_AGENT"] + env["REMOTE_ADDR"])
47
+ return nil unless Rails.cache.exist?(cache_id)
48
+ return [session_id, cache_store.read(cache_id)]
49
+ end
50
+
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)
54
+ session_id
55
+ end
56
+
57
+ def process_body(body, session_id)
58
+ body_doc = Nokogiri::HTML(body)
59
+ 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"] }
61
+ body_doc.to_html
62
+ end
63
+
64
+ def convert_url(u, session_id)
65
+ u = URI.parse(u)
66
+ u.query = Rack::Utils.build_query(Rack::Utils.parse_query(u.query).merge({session_key => session_id})) unless u.scheme =~ /javascript/
67
+ u.to_s
68
+ end
69
+ end
70
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'cookieless'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCookieless < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookieless
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jinzhu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-28 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: nokogiri
33
+ version_requirements: *id001
34
+ prerelease: false
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ name: bundler
49
+ version_requirements: *id002
50
+ prerelease: false
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 6
62
+ - 4
63
+ version: 1.6.4
64
+ name: jeweler
65
+ version_requirements: *id003
66
+ prerelease: false
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: rcov
79
+ version_requirements: *id004
80
+ prerelease: false
81
+ description: Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application
82
+ email: wosmvp@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - lib/cookieless.rb
99
+ - test/helper.rb
100
+ - test/test_cookieless.rb
101
+ has_rdoc: true
102
+ homepage: http://github.com/jinzhu/cookieless
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.6.2
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application
135
+ test_files: []
136
+