utopia 0.9.17 → 0.9.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,9 +23,9 @@ module Utopia
23
23
  def initialize(app, options = {})
24
24
  @app = app
25
25
  @root = options[:root] || Utopia::Middleware::default_root
26
-
26
+
27
27
  @files = ["index.html"]
28
-
28
+
29
29
  @default = "index"
30
30
  end
31
31
 
@@ -36,14 +36,12 @@ module Utopia
36
36
  # Check to see if one of the files exists in the requested directory
37
37
  @files.each do |file|
38
38
  if File.exist?(File.join(@root, path.components, file))
39
- env["PATH_INFO"] = (path + file).to_s
40
- return @app.call(env)
39
+ return [307, {"Location" => (path + file).to_s}, []]
41
40
  end
42
41
  end
43
42
 
44
43
  # Use the default path
45
- env["PATH_INFO"] = (path + @default).to_s
46
- return @app.call(env)
44
+ return [307, {"Location" => (path + @default).to_s}, []]
47
45
  else
48
46
  return @app.call(env)
49
47
  end
@@ -18,6 +18,7 @@ require 'utopia/path'
18
18
 
19
19
  require 'time'
20
20
 
21
+ require 'digest/sha1'
21
22
  require 'mime/types'
22
23
 
23
24
  module Utopia
@@ -36,9 +37,11 @@ module Utopia
36
37
  class FileReader
37
38
  def initialize(path)
38
39
  @path = path
40
+ @etag = Digest::SHA1.hexdigest("#{File.size(@path)}#{mtime_date}")
39
41
  end
40
42
 
41
43
  attr :path
44
+ attr :etag
42
45
 
43
46
  def to_path
44
47
  @path
@@ -59,6 +62,19 @@ module Utopia
59
62
  end
60
63
  end
61
64
  end
65
+
66
+ def modified?(env)
67
+ if modified_since = env['HTTP_IF_MODIFIED_SINCE']
68
+ return false if File.mtime(@path) <= Time.parse(modified_since)
69
+ end
70
+
71
+ if etags = env['HTTP_IF_NONE_MATCH']
72
+ etags = etags.split(/\s*,\s*/)
73
+ return false if etags.include?(etag) || etags.include?('*')
74
+ end
75
+
76
+ return true
77
+ end
62
78
  end
63
79
 
64
80
  def load_mime_types(types)
@@ -112,6 +128,8 @@ module Utopia
112
128
  @extensions = load_mime_types(DEFAULT_TYPES)
113
129
  end
114
130
 
131
+ @cache_control = options[:cache_control] || "public, max-age=3600"
132
+
115
133
  LOG.info "#{self.class.name}: Running in #{@root} with #{extensions.size} filetypes"
116
134
  end
117
135
 
@@ -168,10 +186,16 @@ module Utopia
168
186
  response_headers = {
169
187
  "Last-Modified" => file.mtime_date,
170
188
  "Content-Type" => @extensions[ext],
171
- "Content-Length" => file.size.to_s
189
+ "Cache-Control" => @cache_control,
190
+ "ETag" => file.etag
172
191
  }
173
192
 
174
- return [200, response_headers, file]
193
+ if file.modified?(env)
194
+ response_headers["Content-Length"] = file.size.to_s
195
+ return [200, response_headers, file]
196
+ else
197
+ return [304, response_headers, []]
198
+ end
175
199
  elsif redirect = lookup_relative_file(path)
176
200
  return [307, {"Location" => redirect}, []]
177
201
  end
@@ -17,7 +17,7 @@ module Utopia
17
17
  module VERSION #:nodoc:
18
18
  MAJOR = 0
19
19
  MINOR = 9
20
- TINY = 17
20
+ TINY = 18
21
21
 
22
22
  STRING = [MAJOR, MINOR, TINY].join('.')
23
23
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 17
9
- version: 0.9.17
8
+ - 18
9
+ version: 0.9.18
10
10
  platform: ruby
11
11
  authors:
12
12
  - Samuel Williams