Class: Utopia::Middleware::Static
- Inherits:
-
Object
- Object
- Utopia::Middleware::Static
- Defined in:
- lib/utopia/middleware/static.rb
Defined Under Namespace
Classes: FileReader
Constant Summary collapse
- DEFAULT_TYPES =
[ "html", "css", "js", "txt", "rtf", "xml", "pdf", "zip", "tar", "tgz", "tar.gz", "tar.bz2", "dmg", "mp3", "mp4", "wav", "aiff", ["aac", "audio/x-aac"], "mov", "avi", "wmv", /^image/ ]
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #fetch_file(path) ⇒ Object
-
#initialize(app, options = {}) ⇒ Static
constructor
A new instance of Static.
- #lookup_relative_file(path) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Static
Returns a new instance of Static.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/utopia/middleware/static.rb', line 121 def initialize(app, = {}) @app = app @root = [:root] || Utopia::Middleware::default_root if [:types] @extensions = load_mime_types([:types]) else @extensions = load_mime_types(DEFAULT_TYPES) end @cache_control = [:cache_control] || "public, max-age=3600" LOG.info "#{self.class.name}: Running in #{@root} with #{extensions.size} filetypes" end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
177 178 179 |
# File 'lib/utopia/middleware/static.rb', line 177 def extensions @extensions end |
Instance Method Details
#call(env) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/utopia/middleware/static.rb', line 179 def call(env) request = Rack::Request.new(env) ext = File.extname(request.path_info) if @extensions.key? ext path = Path.create(request.path_info).simplify if file = fetch_file(path) response_headers = { "Last-Modified" => file.mtime_date, "Content-Type" => @extensions[ext], "Cache-Control" => @cache_control, "ETag" => file.etag } if file.modified?(env) response_headers["Content-Length"] = file.size.to_s return [200, response_headers, file] else return [304, response_headers, []] end elsif redirect = lookup_relative_file(path) return [307, {"Location" => redirect}, []] end end # else if no file was found: return @app.call(env) end |
#fetch_file(path) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/utopia/middleware/static.rb', line 136 def fetch_file(path) file_path = File.join(@root, path.components) if File.exist?(file_path) return FileReader.new(file_path) else return nil end end |
#lookup_relative_file(path) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/utopia/middleware/static.rb', line 145 def lookup_relative_file(path) file = nil name = path.basename if split = path.split("@rel@") path = split[0] name = split[1].components # Fix a problem if the browser request has multiple @rel@ # This normally indicates a browser bug.. :( name.delete("@rel@") else path = path.dirname # Relative lookups are not done unless explicitly required by @rel@ # ... but they do work. This is a performance optimization. return nil end # LOG.debug("Searching for #{name.inspect} starting in #{path.components}") path.ascend do |parent_path| file_path = File.join(@root, parent_path.components, name) # LOG.debug("File path: #{file_path}") if File.exist?(file_path) return (parent_path + name).to_s end end return nil end |