1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var ut = require('./utils');
|
8 |
|
9 | exports.Rule = Rule;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function Rule (path, allowance) {
|
22 |
|
23 | this.path = ut.quote(ut.unquote(path));
|
24 | this.allowance = allowance;
|
25 |
|
26 |
|
27 | if( path === '' && !allowance) {
|
28 | this.allowance = true;
|
29 | }
|
30 | }
|
31 |
|
32 | var end_char = ut.quote('$');
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | Rule.prototype.appliesTo = function(url) {
|
40 | var url = ut.quote(ut.unquote(url));
|
41 | ut.d(' * Rule.appliesTo, url: '+url+', path: '+this.path);
|
42 |
|
43 | if (this.path === '*' || url.indexOf(this.path) === 0)
|
44 | return true;
|
45 | else if (this.path.indexOf('*') === -1)
|
46 | return false;
|
47 | else {
|
48 |
|
49 | var have_strict_end = this.path.indexOf(end_char) + end_char.length === this.path.length;
|
50 | var parts_to_match = this.path.split('*');
|
51 | url = url + end_char;
|
52 |
|
53 | var last_matched_index = 0;
|
54 | for (var i = 0; i < parts_to_match.length; i++) {
|
55 | last_matched_index = url.indexOf(parts_to_match[i], last_matched_index);
|
56 | if (last_matched_index === -1)
|
57 | break;
|
58 | }
|
59 |
|
60 | if (have_strict_end && last_matched_index === (this.path.length + end_char.length)) {
|
61 | return true;
|
62 | }
|
63 | else {
|
64 | return last_matched_index !== -1;
|
65 | }
|
66 | }
|
67 | };
|
68 |
|
69 | Rule.prototype.toString = function() {
|
70 | return (this.allowance ? 'Allow' : 'Disallow') + ": " + this.path
|
71 | };
|