annotate src/org/eclipse/jetty/util/URIUtil.java @ 1069:7dd6ec499000

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