/
ServletPathSpec.java
370 lines (332 loc) · 11.2 KB
/
ServletPathSpec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http.pathmap;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class ServletPathSpec extends AbstractPathSpec
{
private static final Logger LOG = Log.getLogger(ServletPathSpec.class);
private final String _declaration;
private final PathSpecGroup _group;
private final int _pathDepth;
private final int _specLength;
private final String _prefix;
private final String _suffix;
/**
* If a servlet or filter path mapping isn't a suffix mapping, ensure
* it starts with '/'
*
* @param pathSpec the servlet or filter mapping pattern
* @return the pathSpec prefixed by '/' if appropriate
*/
public static String normalize(String pathSpec)
{
if (StringUtil.isNotBlank(pathSpec) && !pathSpec.startsWith("/") && !pathSpec.startsWith("*"))
return "/" + pathSpec;
return pathSpec;
}
public ServletPathSpec(String servletPathSpec)
{
if (servletPathSpec == null)
servletPathSpec = "";
if (servletPathSpec.startsWith("servlet|"))
servletPathSpec = servletPathSpec.substring("servlet|".length());
assertValidServletPathSpec(servletPathSpec);
// The Root Path Spec
if (servletPathSpec.isEmpty())
{
_declaration = "";
_group = PathSpecGroup.ROOT;
_pathDepth = -1; // Set pathDepth to -1 to force this to be at the end of the sort order.
_specLength = 1;
_prefix = null;
_suffix = null;
return;
}
// The Default Path Spec
if ("/".equals(servletPathSpec))
{
_declaration = "/";
_group = PathSpecGroup.DEFAULT;
_pathDepth = -1; // Set pathDepth to -1 to force this to be at the end of the sort order.
_specLength = 1;
_prefix = null;
_suffix = null;
return;
}
int specLength = servletPathSpec.length();
PathSpecGroup group;
String prefix;
String suffix;
// prefix based
if (servletPathSpec.charAt(0) == '/' && servletPathSpec.endsWith("/*"))
{
group = PathSpecGroup.PREFIX_GLOB;
prefix = servletPathSpec.substring(0, specLength - 2);
suffix = null;
}
// suffix based
else if (servletPathSpec.charAt(0) == '*' && servletPathSpec.length() > 1)
{
group = PathSpecGroup.SUFFIX_GLOB;
prefix = null;
suffix = servletPathSpec.substring(2, specLength);
}
else
{
group = PathSpecGroup.EXACT;
prefix = servletPathSpec;
suffix = null;
if (servletPathSpec.endsWith("*"))
{
LOG.warn("Suspicious URL pattern: '{}'; see sections 12.1 and 12.2 of the Servlet specification",
servletPathSpec);
}
}
int pathDepth = 0;
for (int i = 0; i < specLength; i++)
{
int cp = servletPathSpec.codePointAt(i);
if (cp < 128)
{
char c = (char)cp;
if (c == '/')
pathDepth++;
}
}
_declaration = servletPathSpec;
_group = group;
_pathDepth = pathDepth;
_specLength = specLength;
_prefix = prefix;
_suffix = suffix;
if (LOG.isDebugEnabled())
{
LOG.debug("Creating ServletPathSpec[{}] (group: {}, prefix: \"{}\", suffix: \"{}\")",
_declaration, _group, _prefix, _suffix);
}
}
private static void assertValidServletPathSpec(String servletPathSpec)
{
if ((servletPathSpec == null) || servletPathSpec.equals(""))
{
return; // empty path spec
}
int len = servletPathSpec.length();
// path spec must either start with '/' or '*.'
if (servletPathSpec.charAt(0) == '/')
{
// Prefix Based
if (len == 1)
{
return; // simple '/' path spec
}
int idx = servletPathSpec.indexOf('*');
if (idx < 0)
{
return; // no hit on glob '*'
}
// only allowed to have '*' at the end of the path spec
if (idx != (len - 1))
{
throw new IllegalArgumentException("Servlet Spec 12.2 violation: glob '*' can only exist at end of prefix based matches: bad spec \"" + servletPathSpec + "\"");
}
}
else if (servletPathSpec.startsWith("*."))
{
// Suffix Based
int idx = servletPathSpec.indexOf('/');
// cannot have path separator
if (idx >= 0)
throw new IllegalArgumentException("Servlet Spec 12.2 violation: suffix based path spec cannot have path separators: bad spec \"" + servletPathSpec + "\"");
idx = servletPathSpec.indexOf('*', 2);
// only allowed to have 1 glob '*', at the start of the path spec
if (idx >= 1)
throw new IllegalArgumentException("Servlet Spec 12.2 violation: suffix based path spec cannot have multiple glob '*': bad spec \"" + servletPathSpec + "\"");
}
else
{
throw new IllegalArgumentException("Servlet Spec 12.2 violation: path spec must start with \"/\" or \"*.\": bad spec \"" + servletPathSpec + "\"");
}
}
@Override
public int getSpecLength()
{
return _specLength;
}
@Override
public PathSpecGroup getGroup()
{
return _group;
}
@Override
public int getPathDepth()
{
return _pathDepth;
}
/**
* @deprecated use {@link #matched(String)}#{@link MatchedPath#getPathInfo()} instead.
*/
@Deprecated
@Override
public String getPathInfo(String path)
{
switch (_group)
{
case ROOT:
return path;
case PREFIX_GLOB:
if (path.length() == (_specLength - 2))
return null;
return path.substring(_specLength - 2);
default:
return null;
}
}
/**
* @deprecated use {@link #matched(String)}#{@link MatchedPath#getPathMatch()} instead.
*/
@Deprecated
@Override
public String getPathMatch(String path)
{
switch (_group)
{
case ROOT:
return "";
case EXACT:
if (_declaration.equals(path))
return path;
return null;
case PREFIX_GLOB:
if (isWildcardMatch(path))
return path.substring(0, _specLength - 2);
return null;
case SUFFIX_GLOB:
if (path.regionMatches(path.length() - (_specLength - 1), _declaration, 1, _specLength - 1))
return path;
return null;
case DEFAULT:
return path;
default:
return null;
}
}
@Override
public String getDeclaration()
{
return _declaration;
}
@Override
public String getPrefix()
{
return _prefix;
}
@Override
public String getSuffix()
{
return _suffix;
}
private boolean isWildcardMatch(String path)
{
// For a spec of "/foo/*" match "/foo" , "/foo/..." but not "/foobar"
int cpl = _specLength - 2;
if ((_group == PathSpecGroup.PREFIX_GLOB) && (path.regionMatches(0, _declaration, 0, cpl)))
return (path.length() == cpl) || ('/' == path.charAt(cpl));
return false;
}
@Override
public MatchedPath matched(String path)
{
switch (_group)
{
case EXACT:
if (_declaration.equals(path))
return new ServletMatchedPath(path, null); // TODO: return final matchedpath
break;
case PREFIX_GLOB:
if (isWildcardMatch(path))
{
String pathMatch = path;
String pathInfo = null;
if (path.length() != (_specLength - 2))
{
pathMatch = path.substring(0, _specLength - 2);
pathInfo = path.substring(_specLength - 2);
}
return new ServletMatchedPath(pathMatch, pathInfo);
}
break;
case SUFFIX_GLOB:
if (path.regionMatches((path.length() - _specLength) + 1, _declaration, 1, _specLength - 1))
return new ServletMatchedPath(path, null);
break;
case ROOT:
// Only "/" matches
if ("/".equals(path))
return new ServletMatchedPath("", path); // TODO: review this
break;
case DEFAULT:
// If we reached this point, then everything matches
return new ServletMatchedPath(path, null);
}
return null;
}
@Override
public boolean matches(String path)
{
switch (_group)
{
case EXACT:
return _declaration.equals(path);
case PREFIX_GLOB:
return isWildcardMatch(path);
case SUFFIX_GLOB:
return path.regionMatches((path.length() - _specLength) + 1, _declaration, 1, _specLength - 1);
case ROOT:
// Only "/" matches
return ("/".equals(path));
case DEFAULT:
// If we reached this point, then everything matches
return true;
default:
return false;
}
}
public static class ServletMatchedPath implements MatchedPath
{
private final String servletName;
private final String pathInfo;
public ServletMatchedPath(String servletName, String pathInfo)
{
this.servletName = servletName;
this.pathInfo = pathInfo;
}
@Override
public String getPathMatch()
{
return this.servletName;
}
@Override
public String getPathInfo()
{
return this.pathInfo;
}
}
}