class Tansu::Static

Constants

ACCEPT_METHODS

Public Class Methods

new(app, root) click to toggle source
# File lib/tansu/static.rb, line 5
def initialize(app, root)
  @app         = app
  @root        = root
  @file_server = Rack::File.new(root)
  @parser      = URI::Parser.new
end

Public Instance Methods

call(env) click to toggle source
# File lib/tansu/static.rb, line 20
def call(env)
  method = env['REQUEST_METHOD']
  path   = env['PATH_INFO']

  if ACCEPT_METHODS.include?(method) && path = match(path)
    env['PATH_INFO'] = path

    @file_server.(env)
  else
    @app.(env)
  end
end
match(path) click to toggle source
# File lib/tansu/static.rb, line 12
def match(path)
  return false unless path = verify(path)

  path = Rack::Utils.clean_path_info(path)

  match?(path) && path
end

Private Instance Methods

match?(path) click to toggle source
# File lib/tansu/static.rb, line 42
def match?(path)
  path = File.join(@root, path)

  File.file?(path) && File.readable?(path)
rescue SystemCallError
  false
end
verify(path) click to toggle source
# File lib/tansu/static.rb, line 35
def verify(path)
  encode = Encoding::UTF_8 if (encode = path.encoding) == Encoding::US_ASCII
  path   = @parser.unescape(path).force_encoding(encode)

  path.valid_encoding? ? path : false
end