Class: Utopia::Middleware::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/static.rb

Defined Under Namespace

Classes: LocalFile

Constant Summary collapse

MIME_TYPES =
{
	:xiph => {
		"ogx" => "application/ogg",
		"ogv" => "video/ogg",
		"oga" => "audio/ogg",
		"ogg" => "audio/ogg",
		"spx" => "audio/ogg",
		"flac" => "audio/flac",
		"anx" => "application/annodex",
		"axa" => "audio/annodex",
		"xspf" => "application/xspf+xml",
	},
	:media => [
		:xiph, "mp3", "mp4", "wav", "aiff", ["aac", "audio/x-aac"], "mov", "avi", "wmv", "mpg"
	],
	:text => [
		"html", "css", "js", "txt", "rtf", "xml", "pdf"
	],
	:fonts => [
		"otf", ["eot", "application/vnd.ms-fontobject"], "ttf", "woff"
	],
	:archive => [
		"zip", "tar", "tgz", "tar.gz", "tar.bz2", ["dmg", "application/x-apple-diskimage"],
		["torrent", "application/x-bittorrent"]
	],
	:images => [
		"png", "gif", "jpeg", "tiff", "svg"
	],
	:default => [
		:media, :text, :archive, :images, :fonts
	]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Static

Returns a new instance of Static.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/utopia/middleware/static.rb', line 187

def initialize(app, options = {})
	@app = app
	@root = options[:root] || Utopia::Middleware::default_root

	if options[:types]
		@extensions = load_mime_types(options[:types])
	else
		@extensions = load_mime_types(MIME_TYPES[:default])
	end

	@cache_control = options[:cache_control] || "public, max-age=3600"

	LOG.info "** #{self.class.name}: Running in #{@root} with #{extensions.size} filetypes"
	# LOG.info @extensions.inspect
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



247
248
249
# File 'lib/utopia/middleware/static.rb', line 247

def extensions
  @extensions
end

Instance Method Details

#call(env) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/utopia/middleware/static.rb', line 249

def call(env)
	request = Rack::Request.new(env)
	ext = File.extname(request.path_info)

	if @extensions.key? ext.downcase
		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,
				"Accept-Ranges" => "bytes"
			}

			if file.modified?(env)
				return file.serve(env, response_headers)
			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



203
204
205
206
207
208
209
210
211
212
# File 'lib/utopia/middleware/static.rb', line 203

def fetch_file(path)
	# We need file_path to be an absolute path for X-Sendfile to work correctly.
	file_path = File.join(@root, path.components)
	
	if File.exist?(file_path)
		return LocalFile.new(@root, path)
	else
		return nil
	end
end

#lookup_relative_file(path) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/utopia/middleware/static.rb', line 214

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 = name.dup
		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