Sha256: e68d02f70cbf7eb5d9942bfb60f7c90c49d88281f5726a50a41b7ed4c1505f8b

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

#	This file is part of the "Utopia Framework" project, and is released under the MIT license.
#	Copyright 2010 Samuel Williams. All rights reserved.
#	See <utopia.rb> for licensing details.

require 'utopia/middleware'
require 'utopia/path'

module Utopia
	module Middleware

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

				@files = ["index.html"]

				@default = "index"
			end

			def call(env)
				path = Path.create(env["PATH_INFO"])
				
				if path.directory?
					# Check to see if one of the files exists in the requested directory
					@files.each do |file|
						if File.exist?(File.join(@root, path.components, file))
							return [307, {"Location" => (path + file).to_s}, []]
						end
					end
				
					# Use the default path
					return [307, {"Location" => (path + @default).to_s}, []]
				else
					return @app.call(env)
				end
			end
		end

	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
utopia-0.10.0 lib/utopia/middleware/directory_index.rb
utopia-0.9.61 lib/utopia/middleware/directory_index.rb
utopia-0.9.60 lib/utopia/middleware/directory_index.rb
utopia-0.9.59 lib/utopia/middleware/directory_index.rb