1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.util;
20
21 import org.apache.shiro.lang.util.StringUtils;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class AntPathMatcher implements PatternMatcher {
63
64
65
66
67
68
69 public static final String DEFAULT_PATH_SEPARATOR = "/";
70
71 private String pathSeparator = DEFAULT_PATH_SEPARATOR;
72
73
74
75
76
77
78 public void setPathSeparator(String pathSeparator) {
79 this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
80 }
81
82
83
84
85
86
87
88
89 public boolean isPattern(String path) {
90 if (path == null) {
91 return false;
92 }
93 return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
94 }
95
96 public boolean matches(String pattern, String source) {
97 return match(pattern, source);
98 }
99
100 public boolean match(String pattern, String path) {
101 return doMatch(pattern, path, true);
102 }
103
104 public boolean matchStart(String pattern, String path) {
105 return doMatch(pattern, path, false);
106 }
107
108
109
110
111
112
113
114
115
116
117
118 @SuppressWarnings({"checkstyle:ReturnCount", "checkstyle:CyclomaticComplexity",
119 "checkstyle:NPathComplexity", "checkstyle:MethodLength"})
120 protected boolean doMatch(String pattern, String path, boolean fullMatch) {
121 if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
122 return false;
123 }
124
125 String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
126 String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);
127
128 int pattIdxStart = 0;
129 int pattIdxEnd = pattDirs.length - 1;
130 int pathIdxStart = 0;
131 int pathIdxEnd = pathDirs.length - 1;
132
133
134 while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
135 String patDir = pattDirs[pattIdxStart];
136 if ("**".equals(patDir)) {
137 break;
138 }
139 if (!matchStrings(patDir, pathDirs[pathIdxStart])) {
140 return false;
141 }
142 pattIdxStart++;
143 pathIdxStart++;
144 }
145
146 if (pathIdxStart > pathIdxEnd) {
147
148 if (pattIdxStart > pattIdxEnd) {
149 return (pattern.endsWith(this.pathSeparator)
150 ? path.endsWith(this.pathSeparator) : !path.endsWith(this.pathSeparator));
151 }
152 if (!fullMatch) {
153 return true;
154 }
155 if (pattIdxStart == pattIdxEnd && "*".equals(pattDirs[pattIdxStart])
156 && path.endsWith(this.pathSeparator)) {
157 return true;
158 }
159 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
160 if (!"**".equals(pattDirs[i])) {
161 return false;
162 }
163 }
164 return true;
165 } else if (pattIdxStart > pattIdxEnd) {
166
167 return false;
168 } else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) {
169
170 return true;
171 }
172
173
174 while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
175 String patDir = pattDirs[pattIdxEnd];
176 if (patDir.equals("**")) {
177 break;
178 }
179 if (!matchStrings(patDir, pathDirs[pathIdxEnd])) {
180 return false;
181 }
182 pattIdxEnd--;
183 pathIdxEnd--;
184 }
185 if (pathIdxStart > pathIdxEnd) {
186
187 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
188 if (!pattDirs[i].equals("**")) {
189 return false;
190 }
191 }
192 return true;
193 }
194
195 while (pattIdxStart != pattIdxEnd && pathIdxStart <= pathIdxEnd) {
196 int patIdxTmp = -1;
197 for (int i = pattIdxStart + 1; i <= pattIdxEnd; i++) {
198 if (pattDirs[i].equals("**")) {
199 patIdxTmp = i;
200 break;
201 }
202 }
203 if (patIdxTmp == pattIdxStart + 1) {
204
205 pattIdxStart++;
206 continue;
207 }
208
209
210 int patLength = (patIdxTmp - pattIdxStart - 1);
211 int strLength = (pathIdxEnd - pathIdxStart + 1);
212 int foundIdx = -1;
213
214 strLoop:
215 for (int i = 0; i <= strLength - patLength; i++) {
216 for (int j = 0; j < patLength; j++) {
217 String subPat = (String) pattDirs[pattIdxStart + j + 1];
218 String subStr = (String) pathDirs[pathIdxStart + i + j];
219 if (!matchStrings(subPat, subStr)) {
220 continue strLoop;
221 }
222 }
223 foundIdx = pathIdxStart + i;
224 break;
225 }
226
227 if (foundIdx == -1) {
228 return false;
229 }
230
231 pattIdxStart = patIdxTmp;
232 pathIdxStart = foundIdx + patLength;
233 }
234
235 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
236 if (!pattDirs[i].equals("**")) {
237 return false;
238 }
239 }
240
241 return true;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 @SuppressWarnings({"checkstyle:ReturnCount", "checkstyle:CyclomaticComplexity",
258 "checkstyle:NPathComplexity", "checkstyle:MethodLength"})
259 private boolean matchStrings(String pattern, String str) {
260 char[] patArr = pattern.toCharArray();
261 char[] strArr = str.toCharArray();
262 int patIdxStart = 0;
263 int patIdxEnd = patArr.length - 1;
264 int strIdxStart = 0;
265 int strIdxEnd = strArr.length - 1;
266 char ch;
267
268 boolean containsStar = false;
269 for (char aPatArr : patArr) {
270 if (aPatArr == '*') {
271 containsStar = true;
272 break;
273 }
274 }
275
276 if (!containsStar) {
277
278 if (patIdxEnd != strIdxEnd) {
279
280 return false;
281 }
282 for (int i = 0; i <= patIdxEnd; i++) {
283 ch = patArr[i];
284 if (ch != '?') {
285 if (ch != strArr[i]) {
286
287 return false;
288 }
289 }
290 }
291
292 return true;
293 }
294
295
296 if (patIdxEnd == 0) {
297
298 return true;
299 }
300
301
302 while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {
303 if (ch != '?') {
304 if (ch != strArr[strIdxStart]) {
305
306 return false;
307 }
308 }
309 patIdxStart++;
310 strIdxStart++;
311 }
312 if (strIdxStart > strIdxEnd) {
313
314
315 for (int i = patIdxStart; i <= patIdxEnd; i++) {
316 if (patArr[i] != '*') {
317 return false;
318 }
319 }
320 return true;
321 }
322
323
324 while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) {
325 if (ch != '?') {
326 if (ch != strArr[strIdxEnd]) {
327
328 return false;
329 }
330 }
331 patIdxEnd--;
332 strIdxEnd--;
333 }
334 if (strIdxStart > strIdxEnd) {
335
336
337 for (int i = patIdxStart; i <= patIdxEnd; i++) {
338 if (patArr[i] != '*') {
339 return false;
340 }
341 }
342 return true;
343 }
344
345
346
347 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
348 int patIdxTmp = -1;
349 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {
350 if (patArr[i] == '*') {
351 patIdxTmp = i;
352 break;
353 }
354 }
355 if (patIdxTmp == patIdxStart + 1) {
356
357 patIdxStart++;
358 continue;
359 }
360
361
362 int patLength = (patIdxTmp - patIdxStart - 1);
363 int strLength = (strIdxEnd - strIdxStart + 1);
364 int foundIdx = -1;
365 strLoop:
366 for (int i = 0; i <= strLength - patLength; i++) {
367 for (int j = 0; j < patLength; j++) {
368 ch = patArr[patIdxStart + j + 1];
369 if (ch != '?') {
370 if (ch != strArr[strIdxStart + i + j]) {
371 continue strLoop;
372 }
373 }
374 }
375
376 foundIdx = strIdxStart + i;
377 break;
378 }
379
380 if (foundIdx == -1) {
381 return false;
382 }
383
384 patIdxStart = patIdxTmp;
385 strIdxStart = foundIdx + patLength;
386 }
387
388
389
390 for (int i = patIdxStart; i <= patIdxEnd; i++) {
391 if (patArr[i] != '*') {
392 return false;
393 }
394 }
395
396 return true;
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415 public String extractPathWithinPattern(String pattern, String path) {
416 String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
417 String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);
418 StringBuilder builder = new StringBuilder();
419 boolean pathStarted = false;
420
421 for (int segment = 0; segment < patternParts.length; segment++) {
422 String patternPart = patternParts[segment];
423 if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) {
424 for (; segment < pathParts.length; segment++) {
425 if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) {
426 builder.append(this.pathSeparator);
427 }
428 builder.append(pathParts[segment]);
429 pathStarted = true;
430 }
431 }
432 }
433
434 return builder.toString();
435 }
436
437
438 }