rack_page_cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ gem "rack"
3
+ gem "mime-types"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "bundler"
9
+ gem "jeweler"
10
+ gem 'simplecov', :require => false
11
+ gem "ruby-debug19"
12
+ end
@@ -0,0 +1,45 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ archive-tar-minitar (0.5.2)
5
+ columnize (0.3.6)
6
+ git (1.2.5)
7
+ jeweler (1.8.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.7.0)
13
+ linecache19 (0.5.12)
14
+ ruby_core_source (>= 0.1.4)
15
+ mime-types (1.18)
16
+ multi_json (1.3.4)
17
+ rack (1.4.1)
18
+ rake (0.9.2.2)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ ruby-debug-base19 (0.11.25)
22
+ columnize (>= 0.3.1)
23
+ linecache19 (>= 0.5.11)
24
+ ruby_core_source (>= 0.1.4)
25
+ ruby-debug19 (0.11.6)
26
+ columnize (>= 0.3.1)
27
+ linecache19 (>= 0.5.11)
28
+ ruby-debug-base19 (>= 0.11.19)
29
+ ruby_core_source (0.1.5)
30
+ archive-tar-minitar (>= 0.5.2)
31
+ simplecov (0.6.2)
32
+ multi_json (~> 1.3)
33
+ simplecov-html (~> 0.5.3)
34
+ simplecov-html (0.5.3)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler
41
+ jeweler
42
+ mime-types
43
+ rack
44
+ ruby-debug19
45
+ simplecov
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 radiospiel
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.
@@ -0,0 +1,19 @@
1
+ = simple_cache
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to simple_cache
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * 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.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 radiospiel. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,35 @@
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 = "rack_page_cache"
18
+ gem.homepage = "http://github.com/radiospiel/rack_page_cache"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A rack middleware to cache requests in static files, a la Rails' page_cache}
21
+ gem.description = %Q{A rack middleware to cache requests in static files, a la Rails' page_cache}
22
+ gem.email = "eno@open-lab.org"
23
+ gem.authors = ["radiospiel"]
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
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
1
+ require 'rack'
2
+ require 'mime/types'
3
+ require 'fileutils'
4
+
5
+ # PageCache is a Rack middleware that caches responses for successful GET requests
6
+ # to disk. This works similar to Rails' page caching, allowing you to cache dynamic
7
+ # pages to static files that can be served directly by a front end webserver.
8
+ class Rack::PageCache
9
+ # Initialize a new ReponseCache object with the given arguments. Arguments:
10
+ # * app : The next middleware in the chain. This is always called.
11
+ # * cache : all cached files will use this directory as the root directory.
12
+ def initialize(app, cache, &block)
13
+ @app = app
14
+ @cache = cache.sub(/\/*$/, "/") # normalize path to have a single trailing slash.
15
+ end
16
+
17
+ def call(env)
18
+ res = @app.call(env)
19
+ page_cache(env, res)
20
+ res
21
+ end
22
+
23
+ def page_cache(env, res)
24
+ # If the request was successful (response status 200), was a GET request, and
25
+ # had an empty query string, this request is probably cacheable.
26
+ return res unless env['REQUEST_METHOD'] == 'GET' and res[0] == 200 and env['QUERY_STRING'] == ""
27
+
28
+ # But the content_type, that can be derived from the request path - and is what
29
+ # the web server would use in the next request - must match the content type
30
+ # in the response.
31
+ return unless matching_content_type?(env, res)
32
+
33
+ # Build path for cache file. The path must not contain '..', to prevent directory
34
+ # traversal attacks.
35
+ path = env['PATH_INFO']
36
+ return if path.include?('..')
37
+
38
+ # We cannot use File.join here, because this would eat the double slashes in the path_info.
39
+ # The @cache instance variable, however, is normalized to end in a single slash.
40
+ path = "#{@cache}/#{path}"
41
+
42
+ # If the cache file exists already, then we run inside rackup.
43
+ # The cached file will be delivered by Rack::Static, and we don't need
44
+ # (and don't want to: this zeroes the file, for some reason) to rewrite the path
45
+ return if File.exists?(path)
46
+
47
+ FileUtils.mkdir_p(File.dirname(path))
48
+ File.open(path, 'wb') do |f|
49
+ res[2].each do |c|
50
+ f.write(c)
51
+ end
52
+ end
53
+ end
54
+
55
+ def matching_content_type?(env, res)
56
+ return unless content_type = res[1]['Content-Type']
57
+
58
+ path_info = env['PATH_INFO']
59
+ mime_types = MIME::Types.type_for(path_info)
60
+ mime_types = [ "text/html" ] if mime_types.empty?
61
+
62
+ mime_types.any? do |mime_type|
63
+ content_type.index(mime_type)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'test_helper'
2
+
3
+ class PageCacheTest < Test::Unit::TestCase
4
+ def test_page_cache
5
+ Rack::PageCache
6
+ assert false, "Implement me"
7
+ end
8
+ end
@@ -0,0 +1,21 @@
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
+
11
+ require 'ruby-debug'
12
+ require 'simplecov'
13
+ require 'test/unit'
14
+ SimpleCov.start do
15
+ add_filter "test/*.rb"
16
+ end
17
+
18
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+
21
+ require 'rack/page_cache'
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_page_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &70256037206540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70256037206540
25
+ - !ruby/object:Gem::Dependency
26
+ name: mime-types
27
+ requirement: &70256037205700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70256037205700
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70256037204860 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70256037204860
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &70256037203940 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70256037203940
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70256037202780 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70256037202780
69
+ - !ruby/object:Gem::Dependency
70
+ name: ruby-debug19
71
+ requirement: &70256037202000 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70256037202000
80
+ description: A rack middleware to cache requests in static files, a la Rails' page_cache
81
+ email: eno@open-lab.org
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files:
85
+ - LICENSE.txt
86
+ - README.rdoc
87
+ files:
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ - Rakefile
93
+ - VERSION
94
+ - lib/rack/page_cache.rb
95
+ - test/page_cache_test.rb
96
+ - test/test_helper.rb
97
+ homepage: http://github.com/radiospiel/rack_page_cache
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 1645301882133738243
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.10
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: A rack middleware to cache requests in static files, a la Rails' page_cache
125
+ test_files: []