innate 2012.03 → 2012.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,66 +0,0 @@
1
- module Innate
2
- class MiddlewareCompiler
3
- COMPILED = {}
4
-
5
- def self.build(name, &block)
6
- COMPILED[name] ||= new(name, &block)
7
- end
8
-
9
- def self.build!(name, &block)
10
- COMPILED[name] = new(name, &block)
11
- end
12
-
13
- attr_reader :middlewares, :name
14
-
15
- def initialize(name)
16
- @name = name.to_sym
17
- @middlewares = []
18
- @compiled = nil
19
- yield(self) if block_given?
20
- end
21
-
22
- def use(middleware, *args, &block)
23
- @middlewares << [middleware, args, block]
24
- end
25
-
26
- def apps(*middlewares)
27
- @middlewares.concat(middlewares.map{|mw| [mw, [], nil]})
28
- end
29
-
30
- def run(app)
31
- @app = app
32
- end
33
-
34
- def cascade(*apps)
35
- @app = Rack::Cascade.new(apps, [404, 405])
36
- end
37
-
38
- # Default application for Innate
39
- def innate(app = Innate::DynaMap, options = Innate.options)
40
- roots, publics = options[:roots], options[:publics]
41
-
42
- joined = roots.map{|root| publics.map{|public| ::File.join(root, public)}}
43
-
44
- apps = joined.flatten.map{|public_root| RackFileWrapper.new(public_root) }
45
- apps << Current.new(Route.new(app), Rewrite.new(app))
46
-
47
- cascade(*apps)
48
- end
49
-
50
- def call(env)
51
- compile
52
- @compiled.call(env)
53
- end
54
-
55
- def compile
56
- @compiled ? self : compile!
57
- end
58
-
59
- def compile!
60
- @compiled = @middlewares.reverse.
61
- inject(@app){|app, (middleware, args, block)|
62
- middleware.new(app, *args, &block) }
63
- self
64
- end
65
- end
66
- end
@@ -1,24 +0,0 @@
1
- module Innate
2
- class RackFileWrapper
3
- def initialize(root, cache_control = nil)
4
- @file = Rack::File.new(root, cache_control)
5
- end
6
-
7
- def call(env)
8
- status, header, body = @file.call(env)
9
-
10
- if status == 403
11
- unless Rack::File::ALLOWED_VERBS.include?(env['REQUEST_METHOD'])
12
- body = "File not Found: #{Rack::Utils.unescape(env['PATH_INFO'])}\n"
13
- return 404, {
14
- 'Content-Type' => 'text/plain',
15
- 'Content-Length' => body.size,
16
- 'X-Cascade' => 'pass',
17
- }, [body]
18
- end
19
- end
20
-
21
- return status, header, body
22
- end
23
- end
24
- end