Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added Rack::Static middleware.

darcs-hash:20070311134325-111e0-52498bf73acccbc91b95c79f91c3feaa8a8697a7.gz
  • Loading branch information...
commit 2143f5e1a315bd2fad61be641c351f25a406a3a0 1 parent 1d9ee63
Christian Neukirchen authored March 11, 2007
40  lib/rack/static.rb
... ...
@@ -0,0 +1,40 @@
  1
+module Rack
  2
+
  3
+  # The Rack::Static middleware intercepts requests for static files
  4
+  # (javascript files, images, stylesheets, etc) based on the url prefixes
  5
+  # passed in the options, and serve them using a Rack::File object. This
  6
+  # allows a Rack stack to serve both static and dynamic content.
  7
+  #
  8
+  # Examples:
  9
+  #     use Rack::Static, :urls => ["/media"]
  10
+  #     will serve all requests beginning with /media from the "media" folder
  11
+  #     located in the current directory (ie media/*).
  12
+  #
  13
+  #     use Rack::Static, :urls => ["/css", "/images"], :root => "public"
  14
+  #     will serve all requests beginning with /css or /images from the folder
  15
+  #     "public" in the current directory (ie public/css/* and public/images/*)
  16
+
  17
+  class Static
  18
+
  19
+    def  initialize(app, options={})
  20
+      @app = app
  21
+      @urls = options[:urls] || Array.new
  22
+      @urls << "/favicon.ico"
  23
+      root = options[:root] || Dir.pwd
  24
+      @file_server = Rack::File.new(root)
  25
+    end
  26
+
  27
+    def call(env)
  28
+      path = env["PATH_INFO"]
  29
+      can_serve = @urls.any? { |url| path.index(url) == 0 }
  30
+
  31
+      if can_serve
  32
+        @file_server.call(env)
  33
+      else
  34
+        @app.call(env)
  35
+      end
  36
+    end
  37
+
  38
+  end
  39
+
  40
+end
37  test/spec_rack_static.rb
... ...
@@ -0,0 +1,37 @@
  1
+require 'test/spec'
  2
+
  3
+require 'rack/static'
  4
+require 'rack/mock'
  5
+
  6
+class DummyApp
  7
+  def call(env)
  8
+    [200, {}, "Hello World"]
  9
+  end
  10
+end
  11
+
  12
+context "Rack::Static" do
  13
+  root = File.expand_path(File.dirname(__FILE__))
  14
+  OPTIONS = {:urls => ["/cgi"], :root => root}
  15
+
  16
+  setup do
  17
+    @request = Rack::MockRequest.new(Rack::Static.new(DummyApp.new, OPTIONS))
  18
+  end
  19
+
  20
+  specify "serves files" do
  21
+    res = @request.get("/cgi/test")
  22
+    res.should.be.ok
  23
+    res.body.should =~ /ruby/
  24
+  end
  25
+
  26
+  specify "404s if url root is known but it can't find the file" do
  27
+    res = @request.get("/cgi/foo")
  28
+    res.should.be.not_found
  29
+  end
  30
+
  31
+  specify "calls down the chain if url root is not known" do
  32
+    res = @request.get("/something/else")
  33
+    res.should.be.ok
  34
+    res.body.should == "Hello World"
  35
+  end
  36
+
  37
+end

0 notes on commit 2143f5e

Please sign in to comment.
Something went wrong with that request. Please try again.