Sha256: 3a37d2eb21f6d01dda0467e5d208a837ed8683378b22c8b6db470dd460384d78

Contents?: true

Size: 1.69 KB

Versions: 17

Compression:

Stored size: 1.69 KB

Contents

module Rack

  # The Rack::Static middleware intercepts requests for static files
  # (javascript files, images, stylesheets, etc) based on the url prefixes or
  # route mappings passed in the options, and serves them using a Rack::File
  # object. This allows a Rack stack to serve both static and dynamic content.
  #
  # Examples:
  #
  # Serve all requests beginning with /media from the "media" folder located
  # in the current directory (ie media/*):
  #
  #     use Rack::Static, :urls => ["/media"]
  #
  # Serve all requests beginning with /css or /images from the folder "public"
  # in the current directory (ie public/css/* and public/images/*):
  #
  #     use Rack::Static, :urls => ["/css", "/images"], :root => "public"
  #
  # Serve all requests to / with "index.html" from the folder "public" in the
  # current directory (ie public/index.html):
  #
  #     use Rack::Static, :urls => {"/" => 'index.html'}, :root => 'public'
  #
  # Set a fixed Cache-Control header for all served files:
  #
  #     use Rack::Static, :root => 'public', :cache_control => 'public'
  #

  class Static

    def initialize(app, options={})
      @app = app
      @urls = options[:urls] || ["/favicon.ico"]
      root = options[:root] || Dir.pwd
      cache_control = options[:cache_control]
      @file_server = Rack::File.new(root, cache_control)
    end

    def call(env)
      path = env["PATH_INFO"]

      unless @urls.kind_of? Hash
        can_serve = @urls.any? { |url| path.index(url) == 0 }
      else
        can_serve = @urls.key? path
      end

      if can_serve
        env["PATH_INFO"] = @urls[path] if @urls.kind_of? Hash
        @file_server.call(env)
      else
        @app.call(env)
      end
    end

  end
end

Version data entries

17 entries across 15 versions & 3 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/1.8/gems/rack-1.3.5/lib/rack/static.rb
candlepin-api-0.4.0 bundle/ruby/gems/rack-1.3.5/lib/rack/static.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/rack-1.3.5/lib/rack/static.rb
rack-1.3.10 lib/rack/static.rb
rack-1.3.9 lib/rack/static.rb
rack-1.3.8 lib/rack/static.rb
rack-1.3.7 lib/rack/static.rb
rack-1.3.6 lib/rack/static.rb
rack-1.3.5 lib/rack/static.rb
rack-1.3.4 lib/rack/static.rb
rack-1.3.3 lib/rack/static.rb
rack-1.3.2 lib/rack/static.rb
rack-1.3.1 lib/rack/static.rb
search_biomodel-1.0.0 search_biomodel/ruby/1.8/gems/rack-1.3.0/lib/rack/static.rb
rack-1.3.0 lib/rack/static.rb
rack-1.3.0.beta2 lib/rack/static.rb
rack-1.3.0.beta lib/rack/static.rb