53 import java.util.List; |
53 import java.util.List; |
54 import java.util.Set; |
54 import java.util.Set; |
55 import java.util.TreeSet; |
55 import java.util.TreeSet; |
56 import java.util.concurrent.CompletionException; |
56 import java.util.concurrent.CompletionException; |
57 import java.util.concurrent.ExecutionException; |
57 import java.util.concurrent.ExecutionException; |
|
58 import java.util.function.BiPredicate; |
58 import java.util.function.Predicate; |
59 import java.util.function.Predicate; |
59 import java.util.function.Supplier; |
60 import java.util.function.Supplier; |
|
61 import java.util.stream.Collectors; |
60 import java.util.stream.Stream; |
62 import java.util.stream.Stream; |
61 |
63 |
62 import static java.util.stream.Collectors.joining; |
64 import static java.util.stream.Collectors.joining; |
63 |
65 |
64 /** |
66 /** |
108 } |
110 } |
109 |
111 |
110 public static final Predicate<String> |
112 public static final Predicate<String> |
111 ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header); |
113 ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header); |
112 |
114 |
113 public static final Predicate<String> IS_PROXY_HEADER = (k) -> |
115 private static final Predicate<String> IS_PROXY_HEADER = (k) -> |
114 k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6)); |
116 k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6)); |
115 public static final Predicate<String> NO_PROXY_HEADER = |
117 private static final Predicate<String> NO_PROXY_HEADER = |
116 IS_PROXY_HEADER.negate(); |
118 IS_PROXY_HEADER.negate(); |
117 public static final Predicate<String> ALL_HEADERS = (s) -> true; |
119 private static final Predicate<String> ALL_HEADERS = (s) -> true; |
|
120 |
|
121 private static final Set<String> PROXY_AUTH_DISABLED_SCHEMES; |
|
122 private static final Set<String> PROXY_AUTH_TUNNEL_DISABLED_SCHEMES; |
|
123 static { |
|
124 String proxyAuthDisabled = |
|
125 getNetProperty("jdk.http.auth.proxying.disabledSchemes"); |
|
126 String proxyAuthTunnelDisabled = |
|
127 getNetProperty("jdk.http.auth.tunneling.disabledSchemes"); |
|
128 PROXY_AUTH_DISABLED_SCHEMES = |
|
129 proxyAuthDisabled == null ? Set.of() : |
|
130 Stream.of(proxyAuthDisabled.split(",")) |
|
131 .map(String::trim) |
|
132 .filter((s) -> !s.isEmpty()) |
|
133 .collect(Collectors.toUnmodifiableSet()); |
|
134 PROXY_AUTH_TUNNEL_DISABLED_SCHEMES = |
|
135 proxyAuthTunnelDisabled == null ? Set.of() : |
|
136 Stream.of(proxyAuthTunnelDisabled.split(",")) |
|
137 .map(String::trim) |
|
138 .filter((s) -> !s.isEmpty()) |
|
139 .collect(Collectors.toUnmodifiableSet()); |
|
140 } |
|
141 |
|
142 private static final String WSPACES = " \t\r\n"; |
|
143 private static final boolean isAllowedForProxy(String name, |
|
144 List<String> value, |
|
145 Set<String> disabledSchemes, |
|
146 Predicate<String> allowedKeys) { |
|
147 if (!allowedKeys.test(name)) return false; |
|
148 if (disabledSchemes.isEmpty()) return true; |
|
149 if (name.equalsIgnoreCase("proxy-authorization")) { |
|
150 if (value.isEmpty()) return false; |
|
151 for (String scheme : disabledSchemes) { |
|
152 int slen = scheme.length(); |
|
153 for (String v : value) { |
|
154 int vlen = v.length(); |
|
155 if (vlen == slen) { |
|
156 if (v.equalsIgnoreCase(scheme)) { |
|
157 return false; |
|
158 } |
|
159 } else if (vlen > slen) { |
|
160 if (v.substring(0,slen).equalsIgnoreCase(scheme)) { |
|
161 int c = v.codePointAt(slen); |
|
162 if (WSPACES.indexOf(c) > -1 |
|
163 || Character.isSpaceChar(c) |
|
164 || Character.isWhitespace(c)) { |
|
165 return false; |
|
166 } |
|
167 } |
|
168 } |
|
169 } |
|
170 } |
|
171 } |
|
172 return true; |
|
173 } |
|
174 |
|
175 public static final BiPredicate<String, List<String>> PROXY_TUNNEL_FILTER = |
|
176 (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES, |
|
177 IS_PROXY_HEADER); |
|
178 public static final BiPredicate<String, List<String>> PROXY_FILTER = |
|
179 (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES, |
|
180 ALL_HEADERS); |
|
181 public static final BiPredicate<String, List<String>> NO_PROXY_HEADERS_FILTER = |
|
182 (n,v) -> Utils.NO_PROXY_HEADER.test(n); |
|
183 |
|
184 |
|
185 public static boolean proxyHasDisabledSchemes(boolean tunnel) { |
|
186 return tunnel ? ! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES.isEmpty() |
|
187 : ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty(); |
|
188 } |
118 |
189 |
119 public static ByteBuffer getBuffer() { |
190 public static ByteBuffer getBuffer() { |
120 return ByteBuffer.allocate(BUFSIZE); |
191 return ByteBuffer.allocate(BUFSIZE); |
121 } |
192 } |
122 |
193 |