ronin-web 0.2.1 → 0.3.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +4 -0
- data/.gemtest +0 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/COPYING.txt +674 -0
- data/{History.txt → ChangeLog.md} +39 -8
- data/Gemfile +49 -0
- data/{README.txt → README.md} +56 -40
- data/Rakefile +25 -24
- data/bin/ronin-web +18 -5
- data/gemspec.yml +30 -0
- data/lib/ronin/network/{helpers → mixins}/web.rb +74 -39
- data/lib/ronin/web.rb +7 -6
- data/lib/ronin/web/extensions.rb +7 -6
- data/lib/ronin/web/extensions/nokogiri.rb +22 -0
- data/lib/ronin/web/extensions/nokogiri/xml.rb +22 -0
- data/lib/ronin/web/extensions/nokogiri/xml/attr.rb +35 -1
- data/lib/ronin/web/extensions/nokogiri/xml/document.rb +34 -4
- data/lib/ronin/web/extensions/nokogiri/xml/element.rb +36 -2
- data/lib/ronin/web/extensions/nokogiri/xml/node.rb +59 -20
- data/lib/ronin/web/extensions/nokogiri/xml/text.rb +36 -2
- data/lib/ronin/web/middleware.rb +27 -0
- data/lib/ronin/web/middleware/base.rb +144 -0
- data/lib/ronin/web/middleware/directories.rb +177 -0
- data/lib/ronin/web/middleware/files.rb +142 -0
- data/lib/ronin/web/middleware/filters.rb +28 -0
- data/lib/ronin/web/middleware/filters/campaign_filter.rb +77 -0
- data/lib/ronin/web/middleware/filters/ip_filter.rb +73 -0
- data/lib/ronin/web/middleware/filters/path_filter.rb +73 -0
- data/lib/ronin/web/middleware/filters/referer_filter.rb +71 -0
- data/lib/ronin/web/middleware/filters/user_agent_filter.rb +71 -0
- data/lib/ronin/web/middleware/filters/vhost_filter.rb +71 -0
- data/lib/ronin/web/middleware/helpers.rb +145 -0
- data/lib/ronin/web/middleware/proxy.rb +265 -0
- data/lib/ronin/web/middleware/proxy_request.rb +262 -0
- data/lib/ronin/web/middleware/request.rb +58 -0
- data/lib/ronin/web/middleware/response.rb +33 -0
- data/lib/ronin/web/middleware/router.rb +167 -0
- data/lib/ronin/web/middleware/rule.rb +103 -0
- data/lib/ronin/web/proxy.rb +7 -6
- data/lib/ronin/web/proxy/app.rb +7 -6
- data/lib/ronin/web/proxy/base.rb +14 -9
- data/lib/ronin/web/proxy/web.rb +9 -6
- data/lib/ronin/web/server.rb +7 -6
- data/lib/ronin/web/server/app.rb +7 -6
- data/lib/ronin/web/server/base.rb +209 -82
- data/lib/ronin/web/server/web.rb +10 -6
- data/lib/ronin/web/spider.rb +38 -26
- data/lib/ronin/web/version.rb +8 -7
- data/lib/ronin/web/web.rb +122 -80
- data/ronin-web.gemspec +15 -0
- data/spec/helpers/output.rb +3 -0
- data/spec/spec_helper.rb +2 -3
- data/spec/web/extensions/nokogiri_spec.rb +7 -7
- data/spec/web/{server/helpers/server.rb → helpers/rack_app.rb} +3 -14
- data/spec/web/helpers/root.rb +15 -0
- data/spec/web/helpers/root/test1.txt +1 -0
- data/spec/web/helpers/root/test1/index.html +1 -0
- data/spec/web/helpers/root/test1/test1.txt +1 -0
- data/spec/web/helpers/root/test2.txt +1 -0
- data/spec/web/helpers/root/test2/test2.txt +1 -0
- data/spec/web/helpers/root/test3.txt +1 -0
- data/spec/web/helpers/root/test3/test3.txt +1 -0
- data/spec/web/middleware/directories_spec.rb +86 -0
- data/spec/web/middleware/files_spec.rb +57 -0
- data/spec/web/middleware/filters/campaign_filter_spec.rb +30 -0
- data/spec/web/middleware/filters/ip_filter_spec.rb +25 -0
- data/spec/web/middleware/filters/path_filter_spec.rb +29 -0
- data/spec/web/middleware/filters/referer_filter_spec.rb +25 -0
- data/spec/web/middleware/filters/user_agent_filter_spec.rb +25 -0
- data/spec/web/middleware/filters/vhost_filter_spec.rb +23 -0
- data/spec/web/middleware/proxy_spec.rb +67 -0
- data/spec/web/middleware/response_spec.rb +20 -0
- data/spec/web/middleware/router_spec.rb +65 -0
- data/spec/web/middleware/rule_spec.rb +37 -0
- data/spec/web/proxy/base_spec.rb +1 -2
- data/spec/web/server/base_spec.rb +4 -13
- data/spec/web/server/classes/sub_app.rb +2 -2
- data/spec/web/server/classes/test_app.rb +1 -1
- data/spec/web/web_spec.rb +57 -67
- metadata +171 -146
- data.tar.gz.sig +0 -0
- data/Manifest.txt +0 -64
- data/lib/ronin/scanners/web.rb +0 -52
- data/lib/ronin/web/server/files.rb +0 -92
- data/lib/ronin/web/server/helpers.rb +0 -25
- data/lib/ronin/web/server/helpers/files.rb +0 -126
- data/lib/ronin/web/server/helpers/hosts.rb +0 -72
- data/lib/ronin/web/server/helpers/proxy.rb +0 -153
- data/lib/ronin/web/server/helpers/rendering.rb +0 -36
- data/lib/ronin/web/server/hosts.rb +0 -86
- data/lib/ronin/web/server/proxy.rb +0 -116
- data/spec/scanners/web_spec.rb +0 -24
- data/spec/web/helpers/root/index.html +0 -1
- data/spec/web/helpers/root/test.txt +0 -1
- data/spec/web/helpers/server.rb +0 -10
- data/spec/web/server/classes/files/dir/file.txt +0 -1
- data/spec/web/server/classes/files/dir/index.html +0 -1
- data/spec/web/server/classes/files/dir2/file2.txt +0 -1
- data/spec/web/server/classes/files/dir3/page.xml +0 -4
- data/spec/web/server/classes/files/file.txt +0 -1
- data/spec/web/server/classes/files_app.rb +0 -27
- data/spec/web/server/classes/hosts_app.rb +0 -40
- data/spec/web/server/classes/proxy_app.rb +0 -45
- data/spec/web/server/files_spec.rb +0 -74
- data/spec/web/server/hosts_spec.rb +0 -55
- data/spec/web/server/proxy_spec.rb +0 -49
- data/tasks/spec.rb +0 -10
- data/tasks/yard.rb +0 -13
- metadata.gz.sig +0 -3
@@ -1,3 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# Ronin Web - A Ruby library for Ronin that provides support for web
|
3
|
+
# scraping and spidering functionality.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
|
6
|
+
#
|
7
|
+
# This file is part of Ronin Web.
|
8
|
+
#
|
9
|
+
# Ronin is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# Ronin is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU General Public License
|
20
|
+
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
#
|
22
|
+
|
1
23
|
require 'nokogiri'
|
2
24
|
|
3
25
|
module Nokogiri
|
@@ -5,38 +27,55 @@ module Nokogiri
|
|
5
27
|
class Node
|
6
28
|
|
7
29
|
#
|
8
|
-
#
|
30
|
+
# Calculates the sum of all children of the node.
|
31
|
+
#
|
32
|
+
# @return [Integer]
|
33
|
+
# The total number of children of the node.
|
34
|
+
#
|
35
|
+
# @api public
|
9
36
|
#
|
10
37
|
def total_children
|
11
38
|
count = 0
|
12
|
-
first = self.child
|
13
39
|
|
14
|
-
|
40
|
+
traverse { |node| count += 1 }
|
15
41
|
|
16
|
-
|
17
|
-
count += (1 + first.total_children)
|
18
|
-
|
19
|
-
first = first.next
|
20
|
-
end
|
21
|
-
|
22
|
-
count
|
42
|
+
return count - 1
|
23
43
|
end
|
24
44
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
45
|
+
#
|
46
|
+
# Traverses all text nodes which are children of the node.
|
47
|
+
#
|
48
|
+
# @yield [node]
|
49
|
+
# A block will be passed each text node.
|
50
|
+
#
|
51
|
+
# @yieldparam [Nokogiri::XML::Text] node
|
52
|
+
# A text node.
|
53
|
+
#
|
54
|
+
# @return [Enumerator]
|
55
|
+
# If no block is given, an Enumerator object will be returned.
|
56
|
+
#
|
57
|
+
# @api public
|
58
|
+
#
|
59
|
+
def traverse_text
|
60
|
+
return enum_for(:traverse_text) unless block_given?
|
29
61
|
|
30
|
-
|
31
|
-
first.traverse_text(&block)
|
62
|
+
yield self if text?
|
32
63
|
|
33
|
-
|
64
|
+
traverse do |node|
|
65
|
+
yield node if node.text?
|
34
66
|
end
|
35
|
-
|
36
|
-
self
|
37
67
|
end
|
38
68
|
|
39
|
-
|
69
|
+
#
|
70
|
+
# Determines if the node is similar to another node.
|
71
|
+
#
|
72
|
+
# @return [Boolean]
|
73
|
+
# Specifies whether the node is equal, in identity or value, to
|
74
|
+
# another node.
|
75
|
+
#
|
76
|
+
# @api public
|
77
|
+
#
|
78
|
+
def ==(other)
|
40
79
|
return false unless other
|
41
80
|
|
42
81
|
(self.type == other.type) && (self.name == other.name)
|
@@ -1,10 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# Ronin Web - A Ruby library for Ronin that provides support for web
|
3
|
+
# scraping and spidering functionality.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
|
6
|
+
#
|
7
|
+
# This file is part of Ronin Web.
|
8
|
+
#
|
9
|
+
# Ronin is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# Ronin is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU General Public License
|
20
|
+
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
#
|
22
|
+
|
1
23
|
require 'nokogiri'
|
2
24
|
|
3
25
|
module Nokogiri
|
4
26
|
module XML
|
5
|
-
class Text <
|
27
|
+
class Text < CharacterData
|
6
28
|
|
7
|
-
|
29
|
+
#
|
30
|
+
# Determines if the text node is similar to another text node.
|
31
|
+
#
|
32
|
+
# @param [Nokogiri::XML::Text] other
|
33
|
+
# The other text node.
|
34
|
+
#
|
35
|
+
# @return [Boolean]
|
36
|
+
# Specifies if the text node is similar, in indentity or value,
|
37
|
+
# to the other text node.
|
38
|
+
#
|
39
|
+
# @api public
|
40
|
+
#
|
41
|
+
def ==(other)
|
8
42
|
super(other) && (self.content == other.content)
|
9
43
|
end
|
10
44
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
# Ronin Web - A Ruby library for Ronin that provides support for web
|
3
|
+
# scraping and spidering functionality.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
|
6
|
+
#
|
7
|
+
# This file is part of Ronin Web.
|
8
|
+
#
|
9
|
+
# Ronin is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# Ronin is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU General Public License
|
20
|
+
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
#
|
22
|
+
|
23
|
+
require 'ronin/web/middleware/base'
|
24
|
+
require 'ronin/web/middleware/files'
|
25
|
+
require 'ronin/web/middleware/directories'
|
26
|
+
require 'ronin/web/middleware/router'
|
27
|
+
require 'ronin/web/middleware/proxy'
|
@@ -0,0 +1,144 @@
|
|
1
|
+
#
|
2
|
+
# Ronin Web - A Ruby library for Ronin that provides support for web
|
3
|
+
# scraping and spidering functionality.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
|
6
|
+
#
|
7
|
+
# This file is part of Ronin Web.
|
8
|
+
#
|
9
|
+
# Ronin is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# Ronin is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU General Public License
|
20
|
+
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
#
|
22
|
+
|
23
|
+
require 'ronin/web/middleware/helpers'
|
24
|
+
require 'ronin/ui/output/helpers'
|
25
|
+
|
26
|
+
module Ronin
|
27
|
+
module Web
|
28
|
+
module Middleware
|
29
|
+
#
|
30
|
+
# Baase-class for all Ronin Web Middleware.
|
31
|
+
#
|
32
|
+
class Base
|
33
|
+
|
34
|
+
include Helpers
|
35
|
+
include UI::Output::Helpers
|
36
|
+
|
37
|
+
# The default status code to return
|
38
|
+
DEFAULT_STATUS = 200
|
39
|
+
|
40
|
+
# The status code to return
|
41
|
+
attr_accessor :default_status
|
42
|
+
|
43
|
+
# The default headers to return
|
44
|
+
attr_reader :default_headers
|
45
|
+
|
46
|
+
#
|
47
|
+
# Creates a new middleware object.
|
48
|
+
#
|
49
|
+
# @param [#call] app
|
50
|
+
# The application the middleware will sit in front of.
|
51
|
+
#
|
52
|
+
# @param [Hash] options
|
53
|
+
# Additional options.
|
54
|
+
#
|
55
|
+
# @option options [Integer] :default_status (DEFAULT_STATUS)
|
56
|
+
# The status code to return.
|
57
|
+
#
|
58
|
+
# @option options [Hash] :default_headers
|
59
|
+
# The headers to return.
|
60
|
+
#
|
61
|
+
# @yield [middleware]
|
62
|
+
# If a block is given, it will be passed the new middleware.
|
63
|
+
#
|
64
|
+
# @yieldparam [Base] middleware
|
65
|
+
# The new middleware object.
|
66
|
+
#
|
67
|
+
# @since 0.3.0
|
68
|
+
#
|
69
|
+
# @api semipublic
|
70
|
+
#
|
71
|
+
def initialize(app,options={})
|
72
|
+
@app = app
|
73
|
+
|
74
|
+
@default_status = (options[:default_status] || DEFAULT_STATUS)
|
75
|
+
@default_headers = {}
|
76
|
+
|
77
|
+
if options.has_key?(:default_headers)
|
78
|
+
@default_headers.merge!(options[:default_headers])
|
79
|
+
end
|
80
|
+
|
81
|
+
yield self if block_given?
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Passes the request to the application.
|
86
|
+
#
|
87
|
+
# @param [Hash, Rack::Request] env
|
88
|
+
# The request.
|
89
|
+
#
|
90
|
+
# @return [Rack::Response]
|
91
|
+
# The response.
|
92
|
+
#
|
93
|
+
# @since 0.3.0
|
94
|
+
#
|
95
|
+
# @api semipublic
|
96
|
+
#
|
97
|
+
def call(env)
|
98
|
+
@app.call(env)
|
99
|
+
end
|
100
|
+
|
101
|
+
protected
|
102
|
+
|
103
|
+
#
|
104
|
+
# Creates a new response.
|
105
|
+
#
|
106
|
+
# @param [String, Array, IO] body
|
107
|
+
# The body for the response.
|
108
|
+
#
|
109
|
+
# @param [Hash] headers
|
110
|
+
# Additional headers for the response.
|
111
|
+
#
|
112
|
+
# @param [Integer] status
|
113
|
+
# The HTTP Status Code for the response.
|
114
|
+
#
|
115
|
+
# @yield [response]
|
116
|
+
# If a block is given, it will be passed the new response.
|
117
|
+
#
|
118
|
+
# @yieldparam [Response] response
|
119
|
+
# The new response.
|
120
|
+
#
|
121
|
+
# @return [Array]
|
122
|
+
# The new response.
|
123
|
+
#
|
124
|
+
# @example Create a response.
|
125
|
+
# response ['Hello'], {'Content-Type' => 'text/txt'}, 200
|
126
|
+
#
|
127
|
+
# @example Create a response with just a String.
|
128
|
+
# response 'Hello'
|
129
|
+
#
|
130
|
+
# @since 0.3.0
|
131
|
+
#
|
132
|
+
# @api semipublic
|
133
|
+
#
|
134
|
+
def response(body=[],headers={},status=nil)
|
135
|
+
status ||= @default_status
|
136
|
+
headers = @default_headers.merge(headers)
|
137
|
+
|
138
|
+
return super(body,headers,status)
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
#
|
2
|
+
# Ronin Web - A Ruby library for Ronin that provides support for web
|
3
|
+
# scraping and spidering functionality.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
|
6
|
+
#
|
7
|
+
# This file is part of Ronin Web.
|
8
|
+
#
|
9
|
+
# Ronin is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# Ronin is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU General Public License
|
20
|
+
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
#
|
22
|
+
|
23
|
+
require 'ronin/web/middleware/base'
|
24
|
+
|
25
|
+
require 'set'
|
26
|
+
|
27
|
+
module Ronin
|
28
|
+
module Web
|
29
|
+
module Middleware
|
30
|
+
#
|
31
|
+
# A Rack middleware to host local directories at specific remote
|
32
|
+
# paths.
|
33
|
+
#
|
34
|
+
# use Ronin::Web::Middleware::Directories do |dirs|
|
35
|
+
# dirs.map '/downloads', '/tmp/ronin_downloads'
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
class Directories < Base
|
39
|
+
|
40
|
+
# The predefined index file names
|
41
|
+
INDEX_NAMES = %w[index.html index.xhtml index.htm]
|
42
|
+
|
43
|
+
# The mapping of remote paths to local directories
|
44
|
+
attr_reader :paths
|
45
|
+
|
46
|
+
#
|
47
|
+
# Creates a new {Directories} middleware.
|
48
|
+
#
|
49
|
+
# @param [#call] app
|
50
|
+
# The application the middleware sits in front of.
|
51
|
+
#
|
52
|
+
# @param [Hash] options
|
53
|
+
# Additional options.
|
54
|
+
#
|
55
|
+
# @option options [Hash{String => String}] :paths
|
56
|
+
# The mapping of remote paths to local directories.
|
57
|
+
#
|
58
|
+
# @yield [directories]
|
59
|
+
# If a block is given, it will be passed the new directories
|
60
|
+
# middleware.
|
61
|
+
#
|
62
|
+
# @yieldparam [Directories] directories
|
63
|
+
# The new directories middleware object.
|
64
|
+
#
|
65
|
+
# @since 0.3.0
|
66
|
+
#
|
67
|
+
# @api public
|
68
|
+
#
|
69
|
+
def initialize(app,options={},&block)
|
70
|
+
@paths = {}
|
71
|
+
@paths_order = []
|
72
|
+
|
73
|
+
if options.has_key?(:paths)
|
74
|
+
options[:paths].each do |remote_path,local_dir|
|
75
|
+
map(remote_path,local_dir)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
super(app,&block)
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# The names of index files.
|
84
|
+
#
|
85
|
+
# @return [Set]
|
86
|
+
# The set of index file names.
|
87
|
+
#
|
88
|
+
# @since 0.3.0
|
89
|
+
#
|
90
|
+
# @api public
|
91
|
+
#
|
92
|
+
def Directories.index_names
|
93
|
+
@@directories_index_names ||= Set.new(INDEX_NAMES)
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Maps a remote path to a local directory.
|
98
|
+
#
|
99
|
+
# @param [String] remote_path
|
100
|
+
# The remote path to map.
|
101
|
+
#
|
102
|
+
# @param [String] local_dir
|
103
|
+
# The local directory that the remote path will map to.
|
104
|
+
#
|
105
|
+
# @return [true]
|
106
|
+
#
|
107
|
+
# @since 0.3.0
|
108
|
+
#
|
109
|
+
# @api public
|
110
|
+
#
|
111
|
+
def map(remote_path,local_dir)
|
112
|
+
@paths[remote_path] = local_dir
|
113
|
+
|
114
|
+
# sort paths by number of sub-directories
|
115
|
+
@paths_order = @paths.keys.sort_by do |path|
|
116
|
+
-(path.split('/').length)
|
117
|
+
end
|
118
|
+
|
119
|
+
return true
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Returns a file from a local directory, if the directory
|
124
|
+
# was mapped to a remote path.
|
125
|
+
#
|
126
|
+
# @param [Hash, Rack::Request] env
|
127
|
+
# The request.
|
128
|
+
#
|
129
|
+
# @return [Array, Response]
|
130
|
+
# The response.
|
131
|
+
#
|
132
|
+
# @since 0.3.0
|
133
|
+
#
|
134
|
+
# @api public
|
135
|
+
#
|
136
|
+
def call(env)
|
137
|
+
path = sanitize_path(env['PATH_INFO'])
|
138
|
+
|
139
|
+
# finds the remote directory that the paths starts with or is
|
140
|
+
# equal to.
|
141
|
+
remote_path = @paths_order.find do |remote_path|
|
142
|
+
if remote_path == '/'
|
143
|
+
true
|
144
|
+
elsif path[0,remote_path.length] == remote_path
|
145
|
+
(path[remote_path.length] == '/') ||
|
146
|
+
(path.length == remote_path.length)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
if remote_path
|
151
|
+
local_dir = @paths[remote_path]
|
152
|
+
sub_path = path[remote_path.length..-1]
|
153
|
+
|
154
|
+
return_file = proc { |local_path|
|
155
|
+
if File.file?(local_path)
|
156
|
+
print_info "Returning file #{local_path.dump}"
|
157
|
+
return response_for(local_path)
|
158
|
+
end
|
159
|
+
}
|
160
|
+
|
161
|
+
if sub_path.empty?
|
162
|
+
# attempt to find an index file in the directory
|
163
|
+
Directories.index_names.each do |index|
|
164
|
+
return_file.call(File.join(local_dir,index))
|
165
|
+
end
|
166
|
+
else
|
167
|
+
return_file.call(File.join(local_dir,sub_path))
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
super(env)
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|