|
13 | 13 | package com.ning.http.util;
|
14 | 14 |
|
15 | 15 | import static com.ning.http.util.DateUtil.millisTime;
|
| 16 | + |
16 | 17 | import java.io.ByteArrayInputStream;
|
17 | 18 | import java.io.FileNotFoundException;
|
18 | 19 | import java.io.IOException;
|
19 | 20 | import java.io.InputStream;
|
20 | 21 | import java.io.SequenceInputStream;
|
21 | 22 | import java.io.UnsupportedEncodingException;
|
22 | 23 | import java.net.URI;
|
| 24 | +import java.net.URISyntaxException; |
23 | 25 | import java.text.ParsePosition;
|
24 | 26 | import java.text.SimpleDateFormat;
|
25 | 27 | import java.util.ArrayList;
|
|
49 | 51 | * The cookies's handling code is from the Netty framework.
|
50 | 52 | */
|
51 | 53 | public class AsyncHttpProviderUtils {
|
| 54 | + |
52 | 55 | private final static byte[] NO_BYTES = new byte[0];
|
53 | 56 |
|
54 | 57 | public final static String DEFAULT_CHARSET = "ISO-8859-1";
|
@@ -280,19 +283,48 @@ public final static String getHost(URI uri) {
|
280 | 283 | public final static URI getRedirectUri(URI uri, String location) {
|
281 | 284 | if(location == null)
|
282 | 285 | throw new IllegalArgumentException("URI " + uri + " was redirected to null location");
|
283 |
| - URI newUri = uri.resolve(location); |
| 286 | + |
| 287 | + URI locationURI = null; |
| 288 | + try { |
| 289 | + locationURI = new URI(location); |
| 290 | + } catch (URISyntaxException e) { |
| 291 | + // rich, we have a badly encoded location, let's try to encode the query params |
| 292 | + String[] parts = location.split("\\?"); |
| 293 | + if (parts.length != 2) { |
| 294 | + throw new IllegalArgumentException("Don't know how to turn this location into a proper URI:" + location, e); |
| 295 | + } else { |
| 296 | + StringBuilder properUrl = new StringBuilder(location.length()).append(parts[0]).append("?"); |
| 297 | + |
| 298 | + String[] queryParams = parts[1].split("&"); |
| 299 | + for (int i = 0; i < queryParams.length; i++) { |
| 300 | + String queryParam = queryParams[i]; |
| 301 | + if (i != 0) |
| 302 | + properUrl.append("&"); |
| 303 | + String[] nameValue = queryParam.split("=", 2); |
| 304 | + UTF8UrlEncoder.appendEncoded(properUrl, nameValue[0]); |
| 305 | + if (nameValue.length == 2) { |
| 306 | + properUrl.append("="); |
| 307 | + UTF8UrlEncoder.appendEncoded(properUrl, nameValue[1]); |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + locationURI = URI.create(properUrl.toString()); |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + URI redirectUri = uri.resolve(locationURI); |
284 | 316 |
|
285 |
| - String scheme = newUri.getScheme(); |
| 317 | + String scheme = redirectUri.getScheme(); |
286 | 318 |
|
287 | 319 | if (scheme == null || !scheme.equalsIgnoreCase("http")
|
288 | 320 | && !scheme.equalsIgnoreCase("https")
|
289 | 321 | && !scheme.equals("ws")
|
290 | 322 | && !scheme.equals("wss")) {
|
291 |
| - throw new IllegalArgumentException("The URI scheme, of the URI " + newUri |
| 323 | + throw new IllegalArgumentException("The URI scheme, of the URI " + redirectUri |
292 | 324 | + ", must be equal (ignoring case) to 'ws, 'wss', 'http', or 'https'");
|
293 | 325 | }
|
294 | 326 |
|
295 |
| - return newUri; |
| 327 | + return redirectUri; |
296 | 328 | }
|
297 | 329 |
|
298 | 330 | public final static int getPort(URI uri) {
|
|
0 commit comments