Sha256: 2bf3092b724957bdbf227c243ec1865d369861391932f29a939f0bd26b3654da

Contents?: true

Size: 1.11 KB

Versions: 16

Compression:

Stored size: 1.11 KB

Contents

module Pancake
  module Middlewares
    class Static
      attr_accessor :app, :stack
      def initialize(app, stack)
        @app, @stack = app, stack
        unless Pancake::Paths === stack
          raise "#{self.class} needs to be initialized with a stack (or something including Pancake::Paths)"
        end
      end

      def call(env)
        static_file = nil
        root = stack.dirs_for(:public).detect do |root|
          root = File.expand_path(root)
          file_name = Rack::Utils.unescape(File.expand_path(File.join(root, env['PATH_INFO'].chomp("/"))))

          # If the client is asking for files outside the public directory, return missing
          # i.e. get "/../../foo.secret"
          if file_name !~ /^#{root}/
            return Rack::Response.new("Not Found", 404).finish
          end

          # If we get to here and the file exists serve it
          if File.file?(file_name)
            static_file = file_name
          end
        end

        if static_file
          Rack::Response.new(File.open(static_file)).finish
        else
          app.call(env)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pancake-0.1.28 lib/pancake/middlewares/static.rb
pancake-0.1.27 lib/pancake/middlewares/static.rb
pancake-0.1.26 lib/pancake/middlewares/static.rb
pancake-0.1.25 lib/pancake/middlewares/static.rb
pancake-0.1.24 lib/pancake/middlewares/static.rb
pancake-0.1.22 lib/pancake/middlewares/static.rb
pancake-0.1.20 lib/pancake/middlewares/static.rb
pancake-0.1.19 lib/pancake/middlewares/static.rb
pancake-0.1.18 lib/pancake/middlewares/static.rb
pancake-0.1.17 lib/pancake/middlewares/static.rb
pancake-0.1.16 lib/pancake/middlewares/static.rb
pancake-0.1.15 lib/pancake/middlewares/static.rb
pancake-0.1.13 lib/pancake/middlewares/static.rb
pancake-0.1.12 lib/pancake/middlewares/static.rb
pancake-0.1.10 lib/pancake/middlewares/static.rb
pancake-0.1.8 lib/pancake/middlewares/static.rb