Mercurial Hosting > luan
annotate src/org/eclipse/jetty/util/URIUtil.java @ 1052:4a2489f1d5fe
remove JBuffer.peek()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 00:27:16 -0700 |
parents | 6939226e0ac4 |
children |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.util; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.UnsupportedEncodingException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.net.URI; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.net.URLEncoder; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 /** URI Holder. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 * This class assists with the decoding and encoding or HTTP URI's. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * It differs from the java.net.URL class as it does not provide |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 * communications ability, but it does assist with query string |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 * formatting. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 * <P>UTF-8 encoding is used by default for % encoded characters. This |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 * may be overridden with the org.eclipse.jetty.util.URI.charset system property. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 * @see UrlEncoded |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 */ |
1013 | 38 public final class URIUtil { |
39 // Use UTF-8 as per http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars | |
40 public static final String __CHARSET = StringUtil.__UTF8; | |
41 | |
42 private URIUtil() | |
43 {} | |
44 | |
45 /* ------------------------------------------------------------ */ | |
46 /** Encode a URI path. | |
47 * This is the same encoding offered by URLEncoder, except that | |
48 * the '/' character is not encoded. | |
49 * @param path The path the encode | |
50 * @return The encoded path | |
51 */ | |
52 public static String encodePath(String path) | |
53 { | |
54 if (path==null || path.length()==0) | |
55 return path; | |
56 | |
57 StringBuilder buf = encodePath(null,path); | |
58 return buf==null?path:buf.toString(); | |
59 } | |
60 | |
61 /* ------------------------------------------------------------ */ | |
62 /** Encode a URI path. | |
63 * @param path The path the encode | |
64 * @param buf StringBuilder to encode path into (or null) | |
65 * @return The StringBuilder or null if no substitutions required. | |
66 */ | |
67 public static StringBuilder encodePath(StringBuilder buf, String path) | |
68 { | |
69 byte[] bytes=null; | |
70 if (buf==null) | |
71 { | |
72 loop: | |
73 for (int i=0;i<path.length();i++) | |
74 { | |
75 char c=path.charAt(i); | |
76 switch(c) | |
77 { | |
78 case '%': | |
79 case '?': | |
80 case ';': | |
81 case '#': | |
82 case '\'': | |
83 case '"': | |
84 case '<': | |
85 case '>': | |
86 case ' ': | |
87 buf=new StringBuilder(path.length()*2); | |
88 break loop; | |
89 default: | |
90 if (c>127) | |
91 { | |
92 try | |
93 { | |
94 bytes=path.getBytes(URIUtil.__CHARSET); | |
95 } | |
96 catch (UnsupportedEncodingException e) | |
97 { | |
98 throw new IllegalStateException(e); | |
99 } | |
100 buf=new StringBuilder(path.length()*2); | |
101 break loop; | |
102 } | |
103 | |
104 } | |
105 } | |
106 if (buf==null) | |
107 return null; | |
108 } | |
109 | |
110 synchronized(buf) | |
111 { | |
112 if (bytes!=null) | |
113 { | |
114 for (int i=0;i<bytes.length;i++) | |
115 { | |
116 byte c=bytes[i]; | |
117 switch(c) | |
118 { | |
119 case '%': | |
120 buf.append("%25"); | |
121 continue; | |
122 case '?': | |
123 buf.append("%3F"); | |
124 continue; | |
125 case ';': | |
126 buf.append("%3B"); | |
127 continue; | |
128 case '#': | |
129 buf.append("%23"); | |
130 continue; | |
131 case '"': | |
132 buf.append("%22"); | |
133 continue; | |
134 case '\'': | |
135 buf.append("%27"); | |
136 continue; | |
137 case '<': | |
138 buf.append("%3C"); | |
139 continue; | |
140 case '>': | |
141 buf.append("%3E"); | |
142 continue; | |
143 case ' ': | |
144 buf.append("%20"); | |
145 continue; | |
146 default: | |
147 if (c<0) | |
148 { | |
149 buf.append('%'); | |
150 TypeUtil.toHex(c,buf); | |
151 } | |
152 else | |
153 buf.append((char)c); | |
154 continue; | |
155 } | |
156 } | |
157 | |
158 } | |
159 else | |
160 { | |
161 for (int i=0;i<path.length();i++) | |
162 { | |
163 char c=path.charAt(i); | |
164 switch(c) | |
165 { | |
166 case '%': | |
167 buf.append("%25"); | |
168 continue; | |
169 case '?': | |
170 buf.append("%3F"); | |
171 continue; | |
172 case ';': | |
173 buf.append("%3B"); | |
174 continue; | |
175 case '#': | |
176 buf.append("%23"); | |
177 continue; | |
178 case '"': | |
179 buf.append("%22"); | |
180 continue; | |
181 case '\'': | |
182 buf.append("%27"); | |
183 continue; | |
184 case '<': | |
185 buf.append("%3C"); | |
186 continue; | |
187 case '>': | |
188 buf.append("%3E"); | |
189 continue; | |
190 case ' ': | |
191 buf.append("%20"); | |
192 continue; | |
193 default: | |
194 buf.append(c); | |
195 continue; | |
196 } | |
197 } | |
198 } | |
199 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
200 |
1013 | 201 return buf; |
202 } | |
203 | |
204 /* ------------------------------------------------------------ */ | |
205 /* Decode a URI path and strip parameters | |
206 * @param path The path the encode | |
207 * @param buf StringBuilder to encode path into | |
208 */ | |
209 public static String decodePath(String path) | |
210 { | |
211 if (path==null) | |
212 return null; | |
213 // Array to hold all converted characters | |
214 char[] chars=null; | |
215 int n=0; | |
216 // Array to hold a sequence of %encodings | |
217 byte[] bytes=null; | |
218 int b=0; | |
219 | |
220 int len=path.length(); | |
221 | |
222 for (int i=0;i<len;i++) | |
223 { | |
224 char c = path.charAt(i); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
225 |
1013 | 226 if (c=='%' && (i+2)<len) |
227 { | |
228 if (chars==null) | |
229 { | |
230 chars=new char[len]; | |
231 bytes=new byte[len]; | |
232 path.getChars(0,i,chars,0); | |
233 } | |
234 bytes[b++]=(byte)(0xff&TypeUtil.parseInt(path,i+1,2,16)); | |
235 i+=2; | |
236 continue; | |
237 } | |
238 else if (c==';') | |
239 { | |
240 if (chars==null) | |
241 { | |
242 chars=new char[len]; | |
243 path.getChars(0,i,chars,0); | |
244 n=i; | |
245 } | |
246 break; | |
247 } | |
248 else if (bytes==null) | |
249 { | |
250 n++; | |
251 continue; | |
252 } | |
253 | |
254 // Do we have some bytes to convert? | |
255 if (b>0) | |
256 { | |
257 // convert series of bytes and add to chars | |
258 String s; | |
259 try | |
260 { | |
261 s=new String(bytes,0,b,__CHARSET); | |
262 } | |
263 catch (UnsupportedEncodingException e) | |
264 { | |
265 s=new String(bytes,0,b); | |
266 } | |
267 s.getChars(0,s.length(),chars,n); | |
268 n+=s.length(); | |
269 b=0; | |
270 } | |
271 | |
272 chars[n++]=c; | |
273 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
274 |
1013 | 275 if (chars==null) |
276 return path; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
277 |
1013 | 278 // if we have a remaining sequence of bytes |
279 if (b>0) | |
280 { | |
281 // convert series of bytes and add to chars | |
282 String s; | |
283 try | |
284 { | |
285 s=new String(bytes,0,b,__CHARSET); | |
286 } | |
287 catch (UnsupportedEncodingException e) | |
288 { | |
289 s=new String(bytes,0,b); | |
290 } | |
291 s.getChars(0,s.length(),chars,n); | |
292 n+=s.length(); | |
293 } | |
294 | |
295 return new String(chars,0,n); | |
296 } | |
297 | |
298 /* ------------------------------------------------------------ */ | |
299 /* Decode a URI path and strip parameters. | |
300 * @param path The path the encode | |
301 * @param buf StringBuilder to encode path into | |
302 */ | |
303 public static String decodePath(byte[] buf, int offset, int length) | |
304 { | |
305 byte[] bytes=null; | |
306 int n=0; | |
307 | |
308 for (int i=0;i<length;i++) | |
309 { | |
310 byte b = buf[i + offset]; | |
311 | |
312 if (b=='%' && (i+2)<length) | |
313 { | |
314 b=(byte)(0xff&TypeUtil.parseInt(buf,i+offset+1,2,16)); | |
315 i+=2; | |
316 } | |
317 else if (b==';') | |
318 { | |
319 length=i; | |
320 break; | |
321 } | |
322 else if (bytes==null) | |
323 { | |
324 n++; | |
325 continue; | |
326 } | |
327 | |
328 if (bytes==null) | |
329 { | |
330 bytes=new byte[length]; | |
331 for (int j=0;j<n;j++) | |
332 bytes[j]=buf[j + offset]; | |
333 } | |
334 | |
335 bytes[n++]=b; | |
336 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
1013 | 338 if (bytes==null) |
339 return StringUtil.toString(buf,offset,length,__CHARSET); | |
340 return StringUtil.toString(bytes,0,n,__CHARSET); | |
341 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
342 |
1013 | 343 |
344 /* ------------------------------------------------------------ */ | |
345 /** Add two URI path segments. | |
346 * Handles null and empty paths, path and query params (eg ?a=b or | |
347 * ;JSESSIONID=xxx) and avoids duplicate '/' | |
348 * @param p1 URI path segment (should be encoded) | |
349 * @param p2 URI path segment (should be encoded) | |
350 * @return Legally combined path segments. | |
351 */ | |
352 public static String addPaths(String p1, String p2) | |
353 { | |
354 if (p1==null || p1.length()==0) | |
355 { | |
356 if (p1!=null && p2==null) | |
357 return p1; | |
358 return p2; | |
359 } | |
360 if (p2==null || p2.length()==0) | |
361 return p1; | |
362 | |
363 int split=p1.indexOf(';'); | |
364 if (split<0) | |
365 split=p1.indexOf('?'); | |
366 if (split==0) | |
367 return p2+p1; | |
368 if (split<0) | |
369 split=p1.length(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
370 |
1013 | 371 StringBuilder buf = new StringBuilder(p1.length()+p2.length()+2); |
372 buf.append(p1); | |
373 | |
374 if (buf.charAt(split-1)=='/') | |
375 { | |
376 if (p2.startsWith("/")) | |
377 { | |
378 buf.deleteCharAt(split-1); | |
379 buf.insert(split-1,p2); | |
380 } | |
381 else | |
382 buf.insert(split,p2); | |
383 } | |
384 else | |
385 { | |
386 if (p2.startsWith("/")) | |
387 buf.insert(split,p2); | |
388 else | |
389 { | |
390 buf.insert(split,'/'); | |
391 buf.insert(split+1,p2); | |
392 } | |
393 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
394 |
1013 | 395 return buf.toString(); |
396 } | |
397 | |
398 /* ------------------------------------------------------------ */ | |
399 /** Return the parent Path. | |
400 * Treat a URI like a directory path and return the parent directory. | |
401 */ | |
402 public static String parentPath(String p) | |
403 { | |
404 if (p==null || "/".equals(p)) | |
405 return null; | |
406 int slash=p.lastIndexOf('/',p.length()-2); | |
407 if (slash>=0) | |
408 return p.substring(0,slash+1); | |
409 return null; | |
410 } | |
411 | |
412 /* ------------------------------------------------------------ */ | |
413 /** Convert a path to a cananonical form. | |
414 * All instances of "." and ".." are factored out. Null is returned | |
415 * if the path tries to .. above its root. | |
416 * @param path | |
417 * @return path or null. | |
418 */ | |
419 public static String canonicalPath(String path) | |
420 { | |
421 if (path==null || path.length()==0) | |
422 return path; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
423 |
1013 | 424 int end=path.length(); |
425 int start = path.lastIndexOf('/', end); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
426 |
1013 | 427 search: |
428 while (end>0) | |
429 { | |
430 switch(end-start) | |
431 { | |
432 case 2: // possible single dot | |
433 if (path.charAt(start+1)!='.') | |
434 break; | |
435 break search; | |
436 case 3: // possible double dot | |
437 if (path.charAt(start+1)!='.' || path.charAt(start+2)!='.') | |
438 break; | |
439 break search; | |
440 } | |
441 | |
442 end=start; | |
443 start=path.lastIndexOf('/',end-1); | |
444 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
445 |
1013 | 446 // If we have checked the entire string |
447 if (start>=end) | |
448 return path; | |
449 | |
450 StringBuilder buf = new StringBuilder(path); | |
451 int delStart=-1; | |
452 int delEnd=-1; | |
453 int skip=0; | |
454 | |
455 while (end>0) | |
456 { | |
457 switch(end-start) | |
458 { | |
459 case 2: // possible single dot | |
460 if (buf.charAt(start+1)!='.') | |
461 { | |
462 if (skip>0 && --skip==0) | |
463 { | |
464 delStart=start>=0?start:0; | |
465 if(delStart>0 && delEnd==buf.length() && buf.charAt(delEnd-1)=='.') | |
466 delStart++; | |
467 } | |
468 break; | |
469 } | |
470 | |
471 if(start<0 && buf.length()>2 && buf.charAt(1)=='/' && buf.charAt(2)=='/') | |
472 break; | |
473 | |
474 if(delEnd<0) | |
475 delEnd=end; | |
476 delStart=start; | |
477 if (delStart<0 || delStart==0&&buf.charAt(delStart)=='/') | |
478 { | |
479 delStart++; | |
480 if (delEnd<buf.length() && buf.charAt(delEnd)=='/') | |
481 delEnd++; | |
482 break; | |
483 } | |
484 if (end==buf.length()) | |
485 delStart++; | |
486 | |
487 end=start--; | |
488 while (start>=0 && buf.charAt(start)!='/') | |
489 start--; | |
490 continue; | |
491 | |
492 case 3: // possible double dot | |
493 if (buf.charAt(start+1)!='.' || buf.charAt(start+2)!='.') | |
494 { | |
495 if (skip>0 && --skip==0) | |
496 { delStart=start>=0?start:0; | |
497 if(delStart>0 && delEnd==buf.length() && buf.charAt(delEnd-1)=='.') | |
498 delStart++; | |
499 } | |
500 break; | |
501 } | |
502 | |
503 delStart=start; | |
504 if (delEnd<0) | |
505 delEnd=end; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
506 |
1013 | 507 skip++; |
508 end=start--; | |
509 while (start>=0 && buf.charAt(start)!='/') | |
510 start--; | |
511 continue; | |
512 | |
513 default: | |
514 if (skip>0 && --skip==0) | |
515 { | |
516 delStart=start>=0?start:0; | |
517 if(delEnd==buf.length() && buf.charAt(delEnd-1)=='.') | |
518 delStart++; | |
519 } | |
520 } | |
521 | |
522 // Do the delete | |
523 if (skip<=0 && delStart>=0 && delEnd>=delStart) | |
524 { | |
525 buf.delete(delStart,delEnd); | |
526 delStart=delEnd=-1; | |
527 if (skip>0) | |
528 delEnd=end; | |
529 } | |
530 | |
531 end=start--; | |
532 while (start>=0 && buf.charAt(start)!='/') | |
533 start--; | |
534 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
535 |
1013 | 536 // Too many .. |
537 if (skip>0) | |
538 return null; | |
539 | |
540 // Do the delete | |
541 if (delEnd>=0) | |
542 buf.delete(delStart,delEnd); | |
543 | |
544 return buf.toString(); | |
545 } | |
546 | |
547 /* ------------------------------------------------------------ */ | |
548 /** | |
549 * @param uri URI | |
550 * @return True if the uri has a scheme | |
551 */ | |
552 public static boolean hasScheme(String uri) | |
553 { | |
554 for (int i=0;i<uri.length();i++) | |
555 { | |
556 char c=uri.charAt(i); | |
557 if (c==':') | |
558 return true; | |
559 if (!(c>='a'&&c<='z' || | |
560 c>='A'&&c<='Z' || | |
561 (i>0 &&(c>='0'&&c<='9' || | |
562 c=='.' || | |
563 c=='+' || | |
564 c=='-')) | |
565 )) | |
566 break; | |
567 } | |
568 return false; | |
569 } | |
570 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
571 } |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
572 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
573 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
574 |