|
26 | 26 | */ |
27 | 27 | package org.apache.hc.client5.http.impl; |
28 | 28 |
|
29 | | -import java.net.URISyntaxException; |
30 | 29 | import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.BitSet; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.LinkedList; |
31 | 33 | import java.util.List; |
32 | 34 |
|
33 | 35 | import org.apache.hc.core5.annotation.Internal; |
34 | 36 | import org.apache.hc.core5.http.HttpRequest; |
| 37 | +import org.apache.hc.core5.net.PathSegmentCodec; |
35 | 38 | import org.apache.hc.core5.net.PercentCodec; |
36 | | -import org.apache.hc.core5.net.URIBuilder; |
37 | 39 |
|
38 | 40 | /** |
39 | 41 | * Protocol support methods. For internal use only. |
|
43 | 45 | @Internal |
44 | 46 | public final class RequestSupport { |
45 | 47 |
|
46 | | - public static String extractPathPrefix(final HttpRequest request) { |
47 | | - final String path = request.getPath(); |
48 | | - try { |
49 | | - final URIBuilder uriBuilder = new URIBuilder(path); |
50 | | - uriBuilder.setFragment(null); |
51 | | - uriBuilder.clearParameters(); |
52 | | - uriBuilder.optimize(); |
53 | | - final List<String> pathSegments = uriBuilder.getPathSegments(); |
| 48 | + static final BitSet UNRESERVED = new BitSet(256); |
| 49 | + |
| 50 | + static { |
| 51 | + for (int i = 'a'; i <= 'z'; i++) { |
| 52 | + UNRESERVED.set(i); |
| 53 | + } |
| 54 | + for (int i = 'A'; i <= 'Z'; i++) { |
| 55 | + UNRESERVED.set(i); |
| 56 | + } |
| 57 | + // numeric characters |
| 58 | + for (int i = '0'; i <= '9'; i++) { |
| 59 | + UNRESERVED.set(i); |
| 60 | + } |
| 61 | + UNRESERVED.set('-'); |
| 62 | + UNRESERVED.set('.'); |
| 63 | + UNRESERVED.set('_'); |
| 64 | + UNRESERVED.set('~'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * TODO To be replaced by method from core |
| 69 | + */ |
| 70 | + static List<String> optimizePathSegments(final List<String> inputSegments) { |
| 71 | + if (!inputSegments.isEmpty()) { |
| 72 | + final LinkedList<String> outputSegments = new LinkedList<>(); |
| 73 | + for (final String inputSegment : inputSegments) { |
| 74 | + if (!inputSegment.isEmpty() && !".".equals(inputSegment)) { |
| 75 | + if ("..".equals(inputSegment)) { |
| 76 | + if (!outputSegments.isEmpty()) { |
| 77 | + outputSegments.removeLast(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + outputSegments.addLast(inputSegment); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + if (!inputSegments.isEmpty()) { |
| 85 | + final String lastSegment = inputSegments.get(inputSegments.size() - 1); |
| 86 | + if (lastSegment.isEmpty() || lastSegment.equals("..") || lastSegment.equals(".")) { |
| 87 | + outputSegments.addLast(""); |
| 88 | + } |
| 89 | + } |
| 90 | + return outputSegments; |
| 91 | + } else { |
| 92 | + return Collections.singletonList(""); |
| 93 | + } |
| 94 | + } |
54 | 95 |
|
55 | | - if (!pathSegments.isEmpty()) { |
56 | | - pathSegments.remove(pathSegments.size() - 1); |
| 96 | + public static String extractPathPrefix(final HttpRequest request) { |
| 97 | + String path = request.getPath(); |
| 98 | + boolean hasSegments = false; |
| 99 | + boolean requiresOptimization = false; |
| 100 | + char previous = 0; |
| 101 | + for (int i = 0; i < path.length(); i++) { |
| 102 | + final char ch = path.charAt(i); |
| 103 | + if (ch == '?' || ch == '#') { |
| 104 | + path = path.substring(0, i); |
| 105 | + break; |
| 106 | + } |
| 107 | + if (ch == '/') { |
| 108 | + hasSegments = true; |
| 109 | + if (previous == '/') { |
| 110 | + requiresOptimization = true; |
| 111 | + } |
| 112 | + } |
| 113 | + if (ch == '.' || ch != '/' && !UNRESERVED.get(ch)) { |
| 114 | + requiresOptimization = true; |
| 115 | + } |
| 116 | + previous = ch; |
| 117 | + } |
| 118 | + if (!requiresOptimization) { |
| 119 | + return hasSegments ? path.substring(0, path.lastIndexOf('/') + 1) : path; |
| 120 | + } else { |
| 121 | + final List<String> inputSegments = PathSegmentCodec.parsePathSegments(path, StandardCharsets.UTF_8); |
| 122 | + final List<String> outputSegments = optimizePathSegments(inputSegments); |
| 123 | + if (!outputSegments.isEmpty()) { |
| 124 | + outputSegments.remove(outputSegments.size() - 1); |
57 | 125 | } |
58 | | - if (pathSegments.isEmpty()) { |
| 126 | + if (outputSegments.isEmpty()) { |
59 | 127 | return "/"; |
60 | 128 | } |
61 | 129 | final StringBuilder buf = new StringBuilder(); |
62 | 130 | buf.append('/'); |
63 | | - for (final String pathSegment : pathSegments) { |
64 | | - PercentCodec.encode(buf, pathSegment, StandardCharsets.US_ASCII); |
| 131 | + for (final String pathSegment : outputSegments) { |
| 132 | + PercentCodec.encode(buf, pathSegment, StandardCharsets.UTF_8); |
65 | 133 | buf.append('/'); |
66 | 134 | } |
67 | 135 | return buf.toString(); |
68 | | - } catch (final URISyntaxException ex) { |
69 | | - return path; |
70 | 136 | } |
71 | 137 | } |
72 | 138 |
|
|
0 commit comments