Skip to content

Commit d7fc9e4

Browse files
stsypanovcl4es
authored andcommittedAug 5, 2021
8267840: Improve URLStreamHandler.parseURL()
Reviewed-by: dfuchs, redestad
1 parent 55bd52a commit d7fc9e4

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed
 

‎src/java.base/share/classes/java/net/URLStreamHandler.java

+17-22
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@
2626
package java.net;
2727

2828
import java.io.IOException;
29-
import java.io.InputStream;
30-
import java.io.File;
31-
import java.io.OutputStream;
32-
import java.util.Hashtable;
3329
import java.util.Objects;
3430
import sun.net.util.IPAddressUtil;
35-
import sun.net.www.ParseUtil;
3631

3732
/**
3833
* The abstract class {@code URLStreamHandler} is the common
@@ -158,13 +153,12 @@ protected void parseURL(URL u, String spec, int start, int limit) {
158153
queryOnly = queryStart == start;
159154
if ((queryStart != -1) && (queryStart < limit)) {
160155
query = spec.substring(queryStart+1, limit);
161-
if (limit > queryStart)
162-
limit = queryStart;
156+
limit = queryStart;
163157
spec = spec.substring(0, queryStart);
164158
}
165159
}
166160

167-
int i = 0;
161+
int i;
168162
// Parse the authority part if any
169163
boolean isUNCName = (start <= limit - 4) &&
170164
(spec.charAt(start) == '/') &&
@@ -249,7 +243,7 @@ protected void parseURL(URL u, String spec, int start, int limit) {
249243
start = i;
250244
// If the authority is defined then the path is defined by the
251245
// spec only; See RFC 2396 Section 5.2.4.
252-
if (authority != null && !authority.isEmpty())
246+
if (!authority.isEmpty())
253247
path = "";
254248
}
255249

@@ -259,26 +253,27 @@ protected void parseURL(URL u, String spec, int start, int limit) {
259253

260254
// Parse the file path if any
261255
if (start < limit) {
256+
String specStr = spec.substring(start, limit);
262257
if (spec.charAt(start) == '/') {
263-
path = spec.substring(start, limit);
258+
path = specStr;
264259
} else if (path != null && !path.isEmpty()) {
265260
isRelPath = true;
266261
int ind = path.lastIndexOf('/');
267-
String separator = "";
268-
if (ind == -1 && authority != null)
269-
separator = "/";
270-
path = path.substring(0, ind + 1) + separator +
271-
spec.substring(start, limit);
272-
262+
if (ind == -1 && authority != null) {
263+
path = "/".concat(specStr);
264+
} else {
265+
path = path.substring(0, ind + 1).concat(specStr);
266+
}
273267
} else {
274-
path = spec.substring(start, limit);
275-
path = (authority != null) ? "/" + path : path;
268+
path = (authority != null) ? "/".concat(specStr) : specStr;
276269
}
277270
} else if (queryOnly && path != null) {
278271
int ind = path.lastIndexOf('/');
279-
if (ind < 0)
280-
ind = 0;
281-
path = path.substring(0, ind) + "/";
272+
if (ind < 0) {
273+
path = "/";
274+
} else {
275+
path = path.substring(0, ind + 1);
276+
}
282277
}
283278
if (path == null)
284279
path = "";
@@ -299,7 +294,7 @@ protected void parseURL(URL u, String spec, int start, int limit) {
299294
*/
300295
if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
301296
(path.indexOf("/../", limit) != 0)) {
302-
path = path.substring(0, limit) + path.substring(i + 3);
297+
path = path.substring(0, limit).concat(path.substring(i + 3));
303298
i = 0;
304299
} else {
305300
i = i + 3;

0 commit comments

Comments
 (0)