diff options
-rw-r--r-- | actionpack/lib/action_dispatch.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 44 | ||||
-rw-r--r-- | actionpack/test/dispatch/static_test.rb (renamed from railties/test/rack_static_test.rb) | 15 | ||||
-rw-r--r-- | actionpack/test/fixtures/public/foo/bar.html (renamed from railties/test/fixtures/public/foo/bar.html) | 0 | ||||
-rw-r--r-- | actionpack/test/fixtures/public/foo/index.html (renamed from railties/test/fixtures/public/foo/index.html) | 0 | ||||
-rw-r--r-- | actionpack/test/fixtures/public/index.html (renamed from railties/test/fixtures/public/index.html) | 0 | ||||
-rw-r--r-- | railties/guides/source/rails_on_rack.textile | 11 | ||||
-rw-r--r-- | railties/lib/rails/initializer.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/rack/static.rb | 47 |
9 files changed, 56 insertions, 66 deletions
diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index ff365dac68..38aaa6146e 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -34,10 +34,11 @@ module ActionDispatch autoload :Utils, 'action_dispatch/http/utils' autoload :Callbacks, 'action_dispatch/middleware/callbacks' + autoload :MiddlewareStack, 'action_dispatch/middleware/stack' autoload :ParamsParser, 'action_dispatch/middleware/params_parser' autoload :Rescue, 'action_dispatch/middleware/rescue' autoload :ShowExceptions, 'action_dispatch/middleware/show_exceptions' - autoload :MiddlewareStack, 'action_dispatch/middleware/stack' + autoload :Static, 'action_dispatch/middleware/static' autoload :Assertions, 'action_dispatch/testing/assertions' autoload :Integration, 'action_dispatch/testing/integration' diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb new file mode 100644 index 0000000000..d7e88a54e4 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -0,0 +1,44 @@ +require 'rack/utils' + +module ActionDispatch + class Static + FILE_METHODS = %w(GET HEAD).freeze + + def initialize(app, root) + @app = app + @file_server = ::Rack::File.new(root) + end + + def call(env) + path = env['PATH_INFO'].chomp('/') + method = env['REQUEST_METHOD'] + + if FILE_METHODS.include?(method) + if file_exist?(path) + return @file_server.call(env) + else + cached_path = directory_exist?(path) ? "#{path}/index" : path + cached_path += ::ActionController::Base.page_cache_extension + + if file_exist?(cached_path) + env['PATH_INFO'] = cached_path + return @file_server.call(env) + end + end + end + + @app.call(env) + end + + private + def file_exist?(path) + full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path)) + File.file?(full_path) && File.readable?(full_path) + end + + def directory_exist?(path) + full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path)) + File.directory?(full_path) && File.readable?(full_path) + end + end +end diff --git a/railties/test/rack_static_test.rb b/actionpack/test/dispatch/static_test.rb index 695b011d03..e6957bb0ea 100644 --- a/railties/test/rack_static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -1,21 +1,10 @@ require 'abstract_unit' -require 'action_controller' -require 'rails/rack' - -class RackStaticTest < ActiveSupport::TestCase - def setup - FileUtils.cp_r "#{RAILS_ROOT}/fixtures/public", "#{RAILS_ROOT}/public" - end - - def teardown - FileUtils.rm_rf "#{RAILS_ROOT}/public" - end - +class StaticTest < ActiveSupport::TestCase DummyApp = lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]] } - App = Rails::Rack::Static.new(DummyApp, "#{RAILS_ROOT}/public") + App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public") test "serves dynamic content" do assert_equal "Hello, World!", get("/nofile") diff --git a/railties/test/fixtures/public/foo/bar.html b/actionpack/test/fixtures/public/foo/bar.html index 9a35646205..9a35646205 100644 --- a/railties/test/fixtures/public/foo/bar.html +++ b/actionpack/test/fixtures/public/foo/bar.html diff --git a/railties/test/fixtures/public/foo/index.html b/actionpack/test/fixtures/public/foo/index.html index 497a2e898f..497a2e898f 100644 --- a/railties/test/fixtures/public/foo/index.html +++ b/actionpack/test/fixtures/public/foo/index.html diff --git a/railties/test/fixtures/public/index.html b/actionpack/test/fixtures/public/index.html index 525950ba6b..525950ba6b 100644 --- a/railties/test/fixtures/public/index.html +++ b/actionpack/test/fixtures/public/index.html diff --git a/railties/guides/source/rails_on_rack.textile b/railties/guides/source/rails_on_rack.textile index 545aaa18e0..1ad45f1cca 100644 --- a/railties/guides/source/rails_on_rack.textile +++ b/railties/guides/source/rails_on_rack.textile @@ -40,11 +40,8 @@ Here's how +script/server+ creates an instance of +Rack::Builder+ app = Rack::Builder.new { use Rails::Rack::LogTailer unless options[:detach] use Rails::Rack::Debugger if options[:debugger] - - map "/" do - use Rails::Rack::Static - run ActionController::Dispatcher.new - end + use ActionDispatch::Static + run ActionController::Dispatcher.new }.to_app </ruby> @@ -52,7 +49,7 @@ Middlewares used in the code above are primarily useful only in the development |_.Middleware|_.Purpose| |+Rails::Rack::LogTailer+|Appends log file output to console| -|+Rails::Rack::Static+|Serves static files inside +RAILS_ROOT/public+ directory| +|+ActionDispatch::Static+|Serves static files inside +RAILS_ROOT/public+ directory| |+Rails::Rack::Debugger+|Starts Debugger| h4. +rackup+ @@ -64,7 +61,7 @@ To use +rackup+ instead of Rails' +script/server+, you can put the following ins require "config/environment" use Rails::Rack::LogTailer -use Rails::Rack::Static +use ActionDispatch::Static run ActionController::Dispatcher.new </ruby> diff --git a/railties/lib/rails/initializer.rb b/railties/lib/rails/initializer.rb index e6f42f643c..72d23f2642 100644 --- a/railties/lib/rails/initializer.rb +++ b/railties/lib/rails/initializer.rb @@ -272,7 +272,7 @@ module Rails # Include middleware to serve up static assets Initializer.default.add :initialize_static_server do if configuration.frameworks.include?(:action_controller) && configuration.serve_static_assets - configuration.middleware.insert(0, Rails::Rack::Static, Rails.public_path) + configuration.middleware.insert(0, ActionDispatch::Static, Rails.public_path) end end diff --git a/railties/lib/rails/rack/static.rb b/railties/lib/rails/rack/static.rb index 25b6cadbfc..d6b8face27 100644 --- a/railties/lib/rails/rack/static.rb +++ b/railties/lib/rails/rack/static.rb @@ -1,46 +1,5 @@ -require 'rack/utils' +require 'action_dispatch' -module Rails - module Rack - class Static - FILE_METHODS = %w(GET HEAD).freeze - - def initialize(app, root) - @app = app - @file_server = ::Rack::File.new(root) - end - - def call(env) - path = env['PATH_INFO'].chomp('/') - method = env['REQUEST_METHOD'] - - if FILE_METHODS.include?(method) - if file_exist?(path) - return @file_server.call(env) - else - cached_path = directory_exist?(path) ? "#{path}/index" : path - cached_path += ::ActionController::Base.page_cache_extension - - if file_exist?(cached_path) - env['PATH_INFO'] = cached_path - return @file_server.call(env) - end - end - end - - @app.call(env) - end - - private - def file_exist?(path) - full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path)) - File.file?(full_path) && File.readable?(full_path) - end - - def directory_exist?(path) - full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path)) - File.directory?(full_path) && File.readable?(full_path) - end - end - end +module Rails::Rack + Static = Deprecation::DeprecatedConstantProxy.new('Rails::Rack::Static', ActionDispatch::Static) end |