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
@@ -0,0 +1,262 @@
|
|
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/request'
|
24
|
+
|
25
|
+
module Ronin
|
26
|
+
module Web
|
27
|
+
module Middleware
|
28
|
+
class ProxyRequest < Request
|
29
|
+
|
30
|
+
#
|
31
|
+
# Changes the HTTP Host header of the request.
|
32
|
+
#
|
33
|
+
# @param [String] new_host
|
34
|
+
# The new value of the HTTP Host header.
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
# The new HTTP Host header.
|
38
|
+
#
|
39
|
+
# @since 0.3.0
|
40
|
+
#
|
41
|
+
# @api public
|
42
|
+
#
|
43
|
+
def host=(new_host)
|
44
|
+
@env['HTTP_HOST'] = new_host
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Changes the port the request is being sent to.
|
49
|
+
#
|
50
|
+
# @param [Integer] new_port
|
51
|
+
# The new port the request will be sent to.
|
52
|
+
#
|
53
|
+
# @return [Integer]
|
54
|
+
# The new port the request will be sent to.
|
55
|
+
#
|
56
|
+
# @since 0.3.0
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
#
|
60
|
+
def port=(new_port)
|
61
|
+
@env['SERVER_PORT'] = new_port
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Changes the URI scheme of the request.
|
66
|
+
#
|
67
|
+
# @param [String] new_port
|
68
|
+
# The new URI scheme for the request.
|
69
|
+
#
|
70
|
+
# @return [String]
|
71
|
+
# The new URI scheme of the request.
|
72
|
+
#
|
73
|
+
# @since 0.3.0
|
74
|
+
#
|
75
|
+
# @api public
|
76
|
+
#
|
77
|
+
def scheme=(new_scheme)
|
78
|
+
@env['rack.url_scheme'] = new_scheme
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Causes the request to be proxied over SSL.
|
83
|
+
#
|
84
|
+
# @return [ProxyRequest]
|
85
|
+
# The proxy request.
|
86
|
+
#
|
87
|
+
# @since 0.3.0
|
88
|
+
#
|
89
|
+
# @api public
|
90
|
+
#
|
91
|
+
def ssl!
|
92
|
+
self.port = 443
|
93
|
+
self.scheme = 'https'
|
94
|
+
|
95
|
+
return self
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Changes the HTTP Request Method of the request.
|
100
|
+
#
|
101
|
+
# @param [String] new_method
|
102
|
+
# The new HTTP Request Method.
|
103
|
+
#
|
104
|
+
# @return [String]
|
105
|
+
# The new HTTP Request Method.
|
106
|
+
#
|
107
|
+
# @since 0.3.0
|
108
|
+
#
|
109
|
+
# @api public
|
110
|
+
#
|
111
|
+
def request_method=(new_method)
|
112
|
+
@env['REQUEST_METHOD'] = new_method
|
113
|
+
end
|
114
|
+
|
115
|
+
alias path= path_info=
|
116
|
+
|
117
|
+
#
|
118
|
+
# Changes the HTTP Query String of the request.
|
119
|
+
#
|
120
|
+
# @param [String] new_query
|
121
|
+
# The new HTTP Query String for the request.
|
122
|
+
#
|
123
|
+
# @return [String]
|
124
|
+
# The new HTTP Query String of the request.
|
125
|
+
#
|
126
|
+
# @since 0.3.0
|
127
|
+
#
|
128
|
+
# @api public
|
129
|
+
#
|
130
|
+
def query_string=(new_query)
|
131
|
+
@env['QUERY_STRING'] = new_query
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Determines if the request is an XML-HTTP Request.
|
136
|
+
#
|
137
|
+
# @return [Boolean]
|
138
|
+
# Specifies whether the request is XML-HTTP.
|
139
|
+
#
|
140
|
+
# @since 0.3.0
|
141
|
+
#
|
142
|
+
# @api public
|
143
|
+
#
|
144
|
+
def xhr?
|
145
|
+
@env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Specifies that the request is a XML HTTP Request.
|
150
|
+
#
|
151
|
+
# @yield [request]
|
152
|
+
# If a block is given, it will be passed the request
|
153
|
+
# for further modification.
|
154
|
+
#
|
155
|
+
# @yieldparam [ProxyRequest] request
|
156
|
+
# The proxy request.
|
157
|
+
#
|
158
|
+
# @return [ProxyRequest]
|
159
|
+
# The request.
|
160
|
+
#
|
161
|
+
# @since 0.3.0
|
162
|
+
#
|
163
|
+
# @api public
|
164
|
+
#
|
165
|
+
def xhr!
|
166
|
+
@env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
167
|
+
|
168
|
+
yield self if block_given?
|
169
|
+
return self
|
170
|
+
end
|
171
|
+
|
172
|
+
#
|
173
|
+
# Changes the HTTP Content-Type header of the request.
|
174
|
+
#
|
175
|
+
# @param [String] new_content_type
|
176
|
+
# The new HTTP Content-Type for the request.
|
177
|
+
#
|
178
|
+
# @return [String]
|
179
|
+
# The new HTTP Content-Type of the request.
|
180
|
+
#
|
181
|
+
# @since 0.3.0
|
182
|
+
#
|
183
|
+
# @api public
|
184
|
+
#
|
185
|
+
def content_type=(new_content_type)
|
186
|
+
@env['CONTENT_TYPE'] = new_content_type
|
187
|
+
end
|
188
|
+
|
189
|
+
#
|
190
|
+
# Changes the HTTP Accept-Encoding header of the request.
|
191
|
+
#
|
192
|
+
# @param [String] new_encoding
|
193
|
+
# The new HTTP Accept-Encoding for the request.
|
194
|
+
#
|
195
|
+
# @return [String]
|
196
|
+
# The new HTTP Accept-Encoding of the request.
|
197
|
+
#
|
198
|
+
# @since 0.3.0
|
199
|
+
#
|
200
|
+
# @api public
|
201
|
+
#
|
202
|
+
def accept_encoding=(new_encoding)
|
203
|
+
@env['HTTP_ACCEPT_ENCODING'] = new_encoding
|
204
|
+
end
|
205
|
+
|
206
|
+
#
|
207
|
+
# Sets the HTTP User-Agent header of the request.
|
208
|
+
#
|
209
|
+
# @param [String] new_user_agent
|
210
|
+
# The new User-Agent header to use.
|
211
|
+
#
|
212
|
+
# @return [String]
|
213
|
+
# The new User-Agent header.
|
214
|
+
#
|
215
|
+
# @since 0.3.0
|
216
|
+
#
|
217
|
+
# @api public
|
218
|
+
#
|
219
|
+
def user_agent=(new_user_agent)
|
220
|
+
@env['HTTP_USER_AGENT'] = new_user_agent
|
221
|
+
end
|
222
|
+
|
223
|
+
#
|
224
|
+
# Changes the HTTP Referer header of the request.
|
225
|
+
#
|
226
|
+
# @param [String] new_referer
|
227
|
+
# The new HTTP Referer for the request.
|
228
|
+
#
|
229
|
+
# @return [String]
|
230
|
+
# The new HTTP Referer of the request.
|
231
|
+
#
|
232
|
+
# @since 0.3.0
|
233
|
+
#
|
234
|
+
# @api public
|
235
|
+
#
|
236
|
+
def referer=(new_referer)
|
237
|
+
@env['HTTP_REFERER'] = new_referer
|
238
|
+
end
|
239
|
+
|
240
|
+
alias referrer= referer=
|
241
|
+
|
242
|
+
#
|
243
|
+
# Changes the body of the request.
|
244
|
+
#
|
245
|
+
# @param [String] new_body
|
246
|
+
# The new body for the request.
|
247
|
+
#
|
248
|
+
# @return [String]
|
249
|
+
# The new body of the request.
|
250
|
+
#
|
251
|
+
# @since 0.3.0
|
252
|
+
#
|
253
|
+
# @api public
|
254
|
+
#
|
255
|
+
def body=(new_body)
|
256
|
+
@env['rack.input'] = new_body
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
@@ -0,0 +1,58 @@
|
|
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/network/http/http'
|
24
|
+
|
25
|
+
require 'rack'
|
26
|
+
|
27
|
+
module Ronin
|
28
|
+
module Web
|
29
|
+
module Middleware
|
30
|
+
class Request < Rack::Request
|
31
|
+
|
32
|
+
#
|
33
|
+
# The HTTP Headers for the request.
|
34
|
+
#
|
35
|
+
# @return [Hash{String => String}]
|
36
|
+
# The HTTP Headers of the request.
|
37
|
+
#
|
38
|
+
# @since 0.3.0
|
39
|
+
#
|
40
|
+
# @api public
|
41
|
+
#
|
42
|
+
def headers
|
43
|
+
headers = {}
|
44
|
+
|
45
|
+
self.env.each do |name,value|
|
46
|
+
if name =~ /^HTTP_/
|
47
|
+
header_name = Network::HTTP.header_name(name.sub('HTTP_',''))
|
48
|
+
headers[header_name] = value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
return headers
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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 'rack'
|
24
|
+
|
25
|
+
module Ronin
|
26
|
+
module Web
|
27
|
+
module Middleware
|
28
|
+
class Response < Rack::Response
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,167 @@
|
|
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/rule'
|
24
|
+
require 'ronin/web/middleware/base'
|
25
|
+
|
26
|
+
module Ronin
|
27
|
+
module Web
|
28
|
+
module Middleware
|
29
|
+
#
|
30
|
+
# A Rack middleware for routing requests based on predefined rules.
|
31
|
+
#
|
32
|
+
# use Ronin::Web::Middleware::Router do |router|
|
33
|
+
# # route requests by source IP address
|
34
|
+
# router.draw :ip => '212.18.45.0/24', :to => BannedApp
|
35
|
+
# router.draw :ip => '192.168.0.0/16' do |request|
|
36
|
+
# response ['Nothing here'], 404
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # route requests by Referer URL
|
40
|
+
# router.draw :referer => 'http://www.sexy.com/', :to => TrapApp
|
41
|
+
# router.draw :referer => /\.google\./ do |request|
|
42
|
+
# response ['Nothing to see here.'], 404
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# # route requests by User-Agent
|
46
|
+
# router.draw :user_agent => /Microsoft/, :to => IEApp
|
47
|
+
#
|
48
|
+
# # mix route options together
|
49
|
+
# router.draw :ip => '212.18.45.0/24',
|
50
|
+
# :user_agent => /Microsoft/, :to => PwnApp
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
class Router < Base
|
54
|
+
|
55
|
+
# The routes of the router
|
56
|
+
attr_reader :routes
|
57
|
+
|
58
|
+
#
|
59
|
+
# Creates a new Router middleware.
|
60
|
+
#
|
61
|
+
# @param [#call] app
|
62
|
+
# The application that the router sits in front of.
|
63
|
+
#
|
64
|
+
# @param [Hash] options
|
65
|
+
# Additional options.
|
66
|
+
#
|
67
|
+
# @yield [router]
|
68
|
+
# If a block is given, it will be passed the newly created
|
69
|
+
# router middleware.
|
70
|
+
#
|
71
|
+
# @yieldparam [Router] router
|
72
|
+
# The new router middleware object.
|
73
|
+
#
|
74
|
+
# @since 0.3.0
|
75
|
+
#
|
76
|
+
# @api public
|
77
|
+
#
|
78
|
+
def initialize(app,options={},&block)
|
79
|
+
@routes = {}
|
80
|
+
|
81
|
+
super(app,options,&block)
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Defines a rule to route requests by.
|
86
|
+
#
|
87
|
+
# @param [Hash] options
|
88
|
+
# Filter options.
|
89
|
+
#
|
90
|
+
# @option options [String] :campaign
|
91
|
+
# The name of the campaign who's targetted hosts will be routed.
|
92
|
+
#
|
93
|
+
# @option options [String, Regexp] :vhost
|
94
|
+
# The Virtual-Host to route.
|
95
|
+
#
|
96
|
+
# @option options [String, IPAddr] :ip
|
97
|
+
# The IP address or IP range to route.
|
98
|
+
#
|
99
|
+
# @option options [String, Regexp] :referer
|
100
|
+
# The Referer URL or pattern to route.
|
101
|
+
#
|
102
|
+
# @option options [String, Regexp] :user_agent
|
103
|
+
# The User-Agent string to route.
|
104
|
+
#
|
105
|
+
# @option options [Proc] :when
|
106
|
+
# Custom logic to route requests by.
|
107
|
+
#
|
108
|
+
# @option options [#call] :to
|
109
|
+
# The application that will receive routed requests.
|
110
|
+
#
|
111
|
+
# @yield [request]
|
112
|
+
# If a block is given, it will receive routed requests.
|
113
|
+
#
|
114
|
+
# @yieldparam [Rack::Request] request
|
115
|
+
# A routed request.
|
116
|
+
#
|
117
|
+
# @return [#call]
|
118
|
+
# The application that is being routed.
|
119
|
+
#
|
120
|
+
# @example Route requests going to an application.
|
121
|
+
# router.draw :ip => '210.18.0.0/16', :to => BannedApp
|
122
|
+
#
|
123
|
+
# @example Accept routed requests using a block.
|
124
|
+
# router.draw :ip => '210.18.0.0/16' do |request|
|
125
|
+
# response ['Banned!']
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# @since 0.3.0
|
129
|
+
#
|
130
|
+
# @api public
|
131
|
+
#
|
132
|
+
def draw(options={},&block)
|
133
|
+
app = (options.delete(:to) || block)
|
134
|
+
|
135
|
+
return @routes[Rule.new(options)] = app
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Filters requests based on the defined routes.
|
140
|
+
#
|
141
|
+
# @param [Hash, Rack::Request] env
|
142
|
+
# An incoming request.
|
143
|
+
#
|
144
|
+
# @return [Array, Response]
|
145
|
+
# A response.
|
146
|
+
#
|
147
|
+
# @since 0.3.0
|
148
|
+
#
|
149
|
+
# @api public
|
150
|
+
#
|
151
|
+
def call(env)
|
152
|
+
request = Request.new(env)
|
153
|
+
|
154
|
+
@routes.each do |rule,app|
|
155
|
+
if rule.match?(request)
|
156
|
+
print_info "Routing #{request.url}"
|
157
|
+
return app.call(env)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
super(env)
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|