morito 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/morito/processor.rb +66 -28
- data/lib/morito/version.rb +1 -1
- data/spec/morito/client_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a412f513664688f5e6e21294420c1ddfb5e45912
|
4
|
+
data.tar.gz: 8e086dc2f01c5d77afd1510e032e617494a03593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e5032b8ce8aabed98cdd427fbf8e72439073a16afda6cafce49107176fbdbb5c682378a1524cab46a77fa31c8198651c4c2b07e146195ac93aad8f9d7d15778
|
7
|
+
data.tar.gz: ec019f3ffd0c72b9895180811770c65cad776b281cba50e5a0ee91b8d7bdedc837d5ba4dd9ede71260aef72ff6e1f6ea95e68c8fb999a552cd904094a3e828c1
|
data/README.md
CHANGED
data/lib/morito/processor.rb
CHANGED
@@ -5,63 +5,101 @@ module Morito
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def allowed?(user_agent, path)
|
8
|
-
|
9
|
-
|
10
|
-
if disallows.empty?
|
11
|
-
true
|
12
|
-
else
|
13
|
-
regexp = Regexp.new("\\A(?:#{disallows.join('|')})")
|
14
|
-
regexp !~ path
|
15
|
-
end
|
8
|
+
UserAgentPermission.new(user_agent, whole_permission).allowed?(path)
|
16
9
|
end
|
17
10
|
|
18
11
|
private
|
19
12
|
|
20
|
-
def
|
21
|
-
|
13
|
+
def whole_permission
|
14
|
+
return @whole_permission if @whole_permission
|
15
|
+
|
16
|
+
@whole_permission = Hash.new {|h, k| h[k] = Hash.new {|h2, k2| h2[k2] = [] } }
|
17
|
+
parser = LineParser.new
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
@body.split(/\n+/).each do |line|
|
20
|
+
parser.parse(line)
|
21
|
+
if parser.disallow?
|
22
|
+
@whole_permission[parser.user_agent][:disallow] << parser.disallow
|
23
|
+
elsif parser.allow?
|
24
|
+
@whole_permission[parser.user_agent][:allow] << parser.allow
|
25
|
+
end
|
27
26
|
end
|
27
|
+
|
28
|
+
@whole_permission
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
class UserAgentPermission
|
32
|
+
def initialize(user_agent, whole_permission)
|
33
|
+
@user_agent = user_agent
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
if whole_permission[user_agent].empty?
|
36
|
+
@permission = whole_permission['*']
|
37
|
+
else
|
38
|
+
@permission = whole_permission[user_agent]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def allowed?(path)
|
43
|
+
return true if disallow_unavailable?
|
44
|
+
|
45
|
+
if !allow_unavailable? && regexp(:allow) =~ path
|
46
|
+
true
|
47
|
+
else
|
48
|
+
regexp(:disallow) !~ path
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def allow_unavailable?
|
55
|
+
@permission[:allow].empty?
|
56
|
+
end
|
57
|
+
|
58
|
+
def disallow_unavailable?
|
59
|
+
@permission[:disallow].empty?
|
38
60
|
end
|
39
61
|
|
40
|
-
|
62
|
+
def regexp(type)
|
63
|
+
raise ArgumentError unless @permission.keys.include?(type)
|
64
|
+
|
65
|
+
raw_regexps = @permission[type].map do |permission|
|
66
|
+
Regexp.escape(permission).gsub('\*', '.*').gsub('\$', '$')
|
67
|
+
end
|
68
|
+
|
69
|
+
Regexp.new("\\A(?:#{raw_regexps.join('|')})")
|
70
|
+
end
|
41
71
|
end
|
42
72
|
|
43
73
|
class LineParser
|
44
|
-
attr_reader :user_agent
|
74
|
+
attr_reader :user_agent, :allow, :disallow
|
45
75
|
|
46
76
|
def parse(line)
|
77
|
+
clear_permissions
|
78
|
+
|
47
79
|
case line
|
48
80
|
when /\AUser-agent:\s+(.+?)\s*(?:#.+)?\z/i
|
49
81
|
@user_agent = $1
|
50
|
-
@disallow = nil
|
51
82
|
when /\ADisallow:\s+(.+?)\s*(?:#.+)?\z/i
|
52
83
|
@disallow = $1
|
53
|
-
|
54
|
-
@
|
84
|
+
when /\AAllow:\s+(.+?)\s*(?:#.+)?\z/i
|
85
|
+
@allow = $1
|
55
86
|
end
|
56
87
|
end
|
57
88
|
|
58
|
-
def
|
59
|
-
|
89
|
+
def allow?
|
90
|
+
@user_agent && @allow
|
60
91
|
end
|
61
92
|
|
62
93
|
def disallow?
|
63
94
|
@user_agent && @disallow
|
64
95
|
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def clear_permissions
|
100
|
+
@disallow = nil
|
101
|
+
@allow = nil
|
102
|
+
end
|
65
103
|
end
|
66
104
|
end
|
67
105
|
end
|
data/lib/morito/version.rb
CHANGED
data/spec/morito/client_spec.rb
CHANGED
@@ -14,6 +14,7 @@ describe Morito::Client do
|
|
14
14
|
User-agent: *
|
15
15
|
Disallow: /private
|
16
16
|
Disallow: /*?$
|
17
|
+
Allow: /private/photo
|
17
18
|
|
18
19
|
# Comment
|
19
20
|
User-agent: restricted agent # Comment
|
@@ -67,6 +68,15 @@ EOS
|
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
71
|
+
context 'with private but allowed path' do
|
72
|
+
let(:requesting_url) { 'http://example.com/private/photo/1' }
|
73
|
+
|
74
|
+
context 'with some agent' do
|
75
|
+
let(:user_agent) { 'some agent' }
|
76
|
+
it { should == true }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
70
80
|
context 'with patternized path' do
|
71
81
|
let(:user_agent) { 'some agent' }
|
72
82
|
|