/
UriParser.java
360 lines (319 loc) · 10.4 KB
/
UriParser.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
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.uri.internal;
import org.glassfish.jersey.internal.LocalizationMessages;
/**
* Parser for string URI with template parameters which produces {@link java.net.URI URIs} from Strings.
* Example of parsed uri: {@code "http://user@{host}:{port}/a/{path}?query=1#fragment"}.
* The parser is not thread safe.
*
* @author Paul Sandoz
* @author Marek Potociar
*/
class UriParser {
private static final String ERROR_STATE = LocalizationMessages.URI_PARSER_NOT_EXECUTED();
private final String input;
private CharacterIterator ci;
private String scheme;
private String userInfo;
private String host;
private String port;
private String query;
private String path;
private String fragment;
private String ssp;
private String authority;
private boolean opaque;
private boolean parserExecuted;
/**
* Creates new parser initialized with {@code uri}.
*
* @param uri String with URI to be parsed. May contain template parameters.
*/
UriParser(String uri) {
this.input = uri;
}
private String parseComponentWithIP(String delimiters, boolean mayEnd) {
return parseComponent(delimiters, mayEnd, true);
}
private String parseComponent(String delimiters, boolean mayEnd) {
return parseComponent(delimiters, mayEnd, false);
}
/**
* Parses the URI component. Parsing starts at position of the first character of
* component and ends with position of one of the delimiters. The string and current
* position is taken from the {@link org.glassfish.jersey.uri.internal.CharacterIterator}.
*
* @param delimiters String with delimiters which terminates the component.
* @param mayEnd True if component might be the last part of the URI.
* @param isIp True if the component might contain IPv6 address.
* @return Extracted component.
*/
private String parseComponent(String delimiters, boolean mayEnd, boolean isIp) {
int curlyBracketsCount = 0;
int squareBracketsCount = 0;
StringBuilder sb = new StringBuilder();
boolean endOfInput = false;
char c = ci.current();
while (!endOfInput) {
if (c == '{') {
curlyBracketsCount++;
sb.append(c);
} else if (c == '}') {
curlyBracketsCount--;
sb.append(c);
} else if (isIp && c == '[') {
squareBracketsCount++;
sb.append(c);
} else if (isIp && c == ']') {
squareBracketsCount--;
sb.append(c);
// test IPv6 or regular expressions in the template params
} else if ((!isIp || squareBracketsCount == 0) && (curlyBracketsCount == 0)
&& (delimiters != null && delimiters.indexOf(c) >= 0)) {
return sb.length() == 0 ? null : sb.toString();
} else {
sb.append(c);
}
endOfInput = !ci.hasNext();
if (!endOfInput) {
c = ci.next();
}
}
if (mayEnd) {
return sb.length() == 0 ? null : sb.toString();
}
throw new IllegalArgumentException(LocalizationMessages.URI_PARSER_COMPONENT_DELIMITER(delimiters, ci.pos()));
}
/**
* Parses the input string URI. After calling this method The result components can be retrieved by calling appropriate
* getter methods like {@link #getHost()}, {@link #getPort()}, etc.
*/
public void parse() {
this.parserExecuted = true;
this.ci = new CharacterIterator(input);
if (!ci.hasNext()) {
// empty string on input -> set both SSP and path to ""
this.path = "";
this.ssp = "";
return;
}
ci.next();
String comp = parseComponent(":/?#", true);
if (ci.hasNext()) {
this.ssp = ci.getInput().substring(ci.pos() + 1);
}
this.opaque = false;
if (ci.current() == ':') {
// absolute
if (comp == null) {
throw new IllegalArgumentException(LocalizationMessages.URI_PARSER_SCHEME_EXPECTED(ci.pos(), input));
}
scheme = comp;
if (!ci.hasNext()) {
// empty SSP/path -> set both SSP and path to ""
this.path = "";
this.ssp = "";
return;
}
char c = ci.next();
if (c == '/') {
// hierarchical
parseHierarchicalUri();
} else {
// opaque
this.opaque = true;
}
} else {
ci.setPosition(0);
// relative
if (ci.current() == '/') {
parseHierarchicalUri();
} else {
parsePath();
}
}
}
private void parseHierarchicalUri() {
if (ci.hasNext() && ci.peek() == '/') {
// authority
ci.next();
ci.next();
parseAuthority();
}
if (!ci.hasNext()) {
if (ci.current() == '/') {
path = "/";
}
return;
}
parsePath();
}
private void parseAuthority() {
int start = ci.pos();
String comp = parseComponentWithIP("@/?#", true);
if (ci.current() == '@') {
this.userInfo = comp;
if (!ci.hasNext()) {
return;
}
ci.next();
comp = parseComponentWithIP(":/?#", true);
} else {
ci.setPosition(start);
comp = parseComponentWithIP("@:/?#", true);
}
this.host = comp;
if (ci.current() == ':') {
if (!ci.hasNext()) {
return;
}
ci.next();
this.port = parseComponent("/?#", true);
}
this.authority = ci.getInput().substring(start, ci.pos());
if (this.authority.length() == 0) {
this.authority = null;
}
}
private void parsePath() {
this.path = parseComponent("?#", true);
if (ci.current() == '?') {
if (!ci.hasNext()) {
return;
}
ci.next(); // skip ?
this.query = parseComponent("#", true);
}
if (ci.current() == '#') {
if (!ci.hasNext()) {
return;
}
ci.next(); // skip #
this.fragment = parseComponent(null, true);
}
}
/**
* Returns parsed scheme specific part. The {@link #parse() method} must be called before executing this method.
*
* @return Scheme specific part.
*/
public String getSsp() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return ssp;
}
/**
* Returns parsed scheme component. The {@link #parse() method} must be called before executing this method.
*
* @return Scheme.
*/
public String getScheme() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return scheme;
}
/**
* Returns parsed user info component. The {@link #parse() method} must be called before executing this method.
*
* @return User info.
*/
public String getUserInfo() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return userInfo;
}
/**
* Returns parsed host component. The {@link #parse() method} must be called before executing this method.
*
* @return Host.
*/
public String getHost() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return host;
}
/**
* Returns parsed port component. The {@link #parse() method} must be called before executing this method.
*
* @return Port.
*/
public String getPort() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return port;
}
/**
* Returns parsed query component. The {@link #parse() method} must be called before executing this method.
*
* @return Query.
*/
public String getQuery() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return query;
}
/**
* Returns parsed path component. The {@link #parse() method} must be called before executing this method.
*
* @return Path.
*/
public String getPath() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return path;
}
/**
* Returns parsed fragment component. The {@link #parse() method} must be called before executing this method.
*
* @return Fragment.
*/
public String getFragment() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return fragment;
}
/**
* Returns parsed authority component. The {@link #parse() method} must be called before executing this method.
*
* @return Authority.
*/
public String getAuthority() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return authority;
}
/**
* Returns whether the input string URI is opaque. The {@link #parse() method} must be called before executing this method.
*
* @return True if the uri is opaque.
*/
public boolean isOpaque() {
if (!parserExecuted) {
throw new IllegalStateException(ERROR_STATE);
}
return opaque;
}
}