|
20 | 20 | import java.io.InputStream;
|
21 | 21 | import java.io.UnsupportedEncodingException;
|
22 | 22 | import java.net.URI;
|
| 23 | +import java.net.URISyntaxException; |
23 | 24 | import java.text.ParsePosition;
|
24 | 25 | import java.text.SimpleDateFormat;
|
25 | 26 | import java.util.Collection;
|
26 | 27 | import java.util.Date;
|
27 | 28 | import java.util.List;
|
28 | 29 | import java.util.Locale;
|
29 | 30 |
|
30 |
| -import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder; |
31 | 31 | import com.ning.http.client.AsyncHttpClientConfig;
|
32 | 32 | import com.ning.http.client.AsyncHttpProvider;
|
33 | 33 | import com.ning.http.client.ByteArrayPart;
|
|
41 | 41 | import com.ning.http.multipart.ByteArrayPartSource;
|
42 | 42 | import com.ning.http.multipart.MultipartRequestEntity;
|
43 | 43 | import com.ning.http.multipart.PartSource;
|
| 44 | +import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder; |
44 | 45 |
|
45 | 46 | /**
|
46 | 47 | * {@link com.ning.http.client.AsyncHttpProvider} common utilities.
|
@@ -231,22 +232,51 @@ public final static String getHost(URI uri) {
|
231 | 232 | }
|
232 | 233 |
|
233 | 234 | public final static URI getRedirectUri(URI uri, String location) {
|
234 |
| - if(location == null) |
235 |
| - throw new IllegalArgumentException("URI " + uri + " was redirected to null location"); |
236 |
| - URI newUri = uri.resolve(location); |
237 |
| - |
238 |
| - String scheme = newUri.getScheme(); |
239 |
| - |
240 |
| - if (scheme == null || !scheme.equalsIgnoreCase("http") |
241 |
| - && !scheme.equalsIgnoreCase("https") |
242 |
| - && !scheme.equals("ws") |
243 |
| - && !scheme.equals("wss")) { |
244 |
| - throw new IllegalArgumentException("The URI scheme, of the URI " + newUri |
245 |
| - + ", must be equal (ignoring case) to 'ws, 'wss', 'http', or 'https'"); |
246 |
| - } |
| 235 | + if(location == null) |
| 236 | + throw new IllegalArgumentException("URI " + uri + " was redirected to null location"); |
| 237 | + |
| 238 | + URI locationURI = null; |
| 239 | + try { |
| 240 | + locationURI = new URI(location); |
| 241 | + } catch (URISyntaxException e) { |
| 242 | + // rich, we have a badly encoded location, let's try to encode the query params |
| 243 | + String[] parts = location.split("\\?"); |
| 244 | + if (parts.length != 2) { |
| 245 | + throw new IllegalArgumentException("Don't know how to turn this location into a proper URI:" + location, e); |
| 246 | + } else { |
| 247 | + StringBuilder properUrl = new StringBuilder(location.length()).append(parts[0]).append("?"); |
| 248 | + |
| 249 | + String[] queryParams = parts[1].split("&"); |
| 250 | + for (int i = 0; i < queryParams.length; i++) { |
| 251 | + String queryParam = queryParams[i]; |
| 252 | + if (i != 0) |
| 253 | + properUrl.append("&"); |
| 254 | + String[] nameValue = queryParam.split("=", 2); |
| 255 | + UTF8UrlEncoder.appendEncoded(properUrl, nameValue[0]); |
| 256 | + if (nameValue.length == 2) { |
| 257 | + properUrl.append("="); |
| 258 | + UTF8UrlEncoder.appendEncoded(properUrl, nameValue[1]); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + locationURI = URI.create(properUrl.toString()); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + URI redirectUri = uri.resolve(locationURI); |
247 | 267 |
|
248 |
| - return newUri; |
249 |
| - } |
| 268 | + String scheme = redirectUri.getScheme(); |
| 269 | + |
| 270 | + if (scheme == null || !scheme.equalsIgnoreCase("http") |
| 271 | + && !scheme.equalsIgnoreCase("https") |
| 272 | + && !scheme.equals("ws") |
| 273 | + && !scheme.equals("wss")) { |
| 274 | + throw new IllegalArgumentException("The URI scheme, of the URI " + redirectUri |
| 275 | + + ", must be equal (ignoring case) to 'ws, 'wss', 'http', or 'https'"); |
| 276 | + } |
| 277 | + |
| 278 | + return redirectUri; |
| 279 | + } |
250 | 280 |
|
251 | 281 | public final static int getPort(URI uri) {
|
252 | 282 | int port = uri.getPort();
|
|
0 commit comments