Sha256: 052ab62fe5f0e58a1e9c1ed01a91f734956697869d2b2fa5b396cdbb919de5cd

Contents?: true

Size: 1010 Bytes

Versions: 16

Compression:

Stored size: 1010 Bytes

Contents

class Rack::App::FileServer

  def initialize(root_folder)
    require 'rack/file'
    @root_folder = root_folder
    @relative_file_paths = Dir.glob(File.join(@root_folder, '**', '*')).map { |file_path| file_path.sub(@root_folder, '') }.sort_by { |str| str.length }.reverse
    @rack_file_server = ::Rack::File.new(@root_folder, {})
  end

  def call(env)
    path_info = clean_path_info(env)

    @relative_file_paths.each do |relative_file_path|
      if path_info =~ /#{Regexp.escape(relative_file_path)}$/
        env[::Rack::PATH_INFO]= relative_file_path
        break
      end
    end

    @rack_file_server.call(env)
  end

  def self.serve_file(env, file_path)
    file_server = self.new(File.dirname(file_path))
    env = env.dup
    env[::Rack::REQUEST_METHOD]= 'GET'
    env[::Rack::PATH_INFO]= file_path
    file_server.call(env)
  end

  protected

  def clean_path_info(env)
    path_info = ::Rack::Utils.unescape(env[::Rack::PATH_INFO])
    ::Rack::Utils.clean_path_info(path_info)
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rack-app-5.2.0 lib/rack/app/file_server.rb
rack-app-5.1.0 lib/rack/app/file_server.rb
rack-app-5.0.0 lib/rack/app/file_server.rb
rack-app-5.0.0.rc2 lib/rack/app/file_server.rb
rack-app-5.0.0.rc1 lib/rack/app/file_server.rb
rack-app-4.0.1 lib/rack/app/file_server.rb
rack-app-3.6.0 lib/rack/app/file_server.rb
rack-app-3.5.2 lib/rack/app/file_server.rb
rack-app-3.5.1 lib/rack/app/file_server.rb
rack-app-3.5.0 lib/rack/app/file_server.rb
rack-app-3.4.0 lib/rack/app/file_server.rb
rack-app-3.3.0 lib/rack/app/file_server.rb
rack-app-3.2.1 lib/rack/app/file_server.rb
rack-app-3.2.0 lib/rack/app/file_server.rb
rack-app-3.1.0 lib/rack/app/file_server.rb
rack-app-3.0.0 lib/rack/app/file_server.rb