Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit e5b455f

Browse files
author
Stephane Landelle
committed
Support Location header with raw query, close AsyncHttpClient#268
1 parent 091f4c6 commit e5b455f

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

api/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
package com.ning.http.util;
1414

1515
import static com.ning.http.util.DateUtil.millisTime;
16+
1617
import java.io.ByteArrayInputStream;
1718
import java.io.FileNotFoundException;
1819
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.io.SequenceInputStream;
2122
import java.io.UnsupportedEncodingException;
2223
import java.net.URI;
24+
import java.net.URISyntaxException;
2325
import java.text.ParsePosition;
2426
import java.text.SimpleDateFormat;
2527
import java.util.ArrayList;
@@ -49,6 +51,7 @@
4951
* The cookies's handling code is from the Netty framework.
5052
*/
5153
public class AsyncHttpProviderUtils {
54+
5255
private final static byte[] NO_BYTES = new byte[0];
5356

5457
public final static String DEFAULT_CHARSET = "ISO-8859-1";
@@ -280,19 +283,48 @@ public final static String getHost(URI uri) {
280283
public final static URI getRedirectUri(URI uri, String location) {
281284
if(location == null)
282285
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);
284316

285-
String scheme = newUri.getScheme();
317+
String scheme = redirectUri.getScheme();
286318

287319
if (scheme == null || !scheme.equalsIgnoreCase("http")
288320
&& !scheme.equalsIgnoreCase("https")
289321
&& !scheme.equals("ws")
290322
&& !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
292324
+ ", must be equal (ignoring case) to 'ws, 'wss', 'http', or 'https'");
293325
}
294326

295-
return newUri;
327+
return redirectUri;
296328
}
297329

298330
public final static int getPort(URI uri) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.util;
14+
15+
import java.net.URI;
16+
17+
import org.testng.Assert;
18+
import org.testng.annotations.Test;
19+
20+
public class AsyncHttpProviderUtilsTest {
21+
22+
@Test(groups = "fast")
23+
public void getRedirectUriShouldHandleProperlyEncodedLocation() {
24+
25+
String url = "http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC%20Lifebook%20E8310%20Core2Duo%20T8100%202%201GHz%204GB%20DVD%20RW&_itemId=150731406505";
26+
URI uri = AsyncHttpProviderUtils.getRedirectUri(URI.create("http://www.ebay.de"), url);
27+
Assert.assertEquals("http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC%20Lifebook%20E8310%20Core2Duo%20T8100%202%201GHz%204GB%20DVD%20RW&_itemId=150731406505", uri.toString());
28+
}
29+
30+
@Test(groups = "fast")
31+
public void getRedirectUriShouldHandleRawQueryParamsLocation() {
32+
33+
String url = "http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC Lifebook E8310 Core2Duo T8100 2 1GHz 4GB DVD RW&_itemId=150731406505";
34+
URI uri = AsyncHttpProviderUtils.getRedirectUri(URI.create("http://www.ebay.de"), url);
35+
Assert.assertEquals("http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC%20Lifebook%20E8310%20Core2Duo%20T8100%202%201GHz%204GB%20DVD%20RW&_itemId=150731406505", uri.toString());
36+
}
37+
38+
@Test(groups = "fast")
39+
public void getRedirectUriShouldHandleRelativeLocation() {
40+
41+
String url = "/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC Lifebook E8310 Core2Duo T8100 2 1GHz 4GB DVD RW&_itemId=150731406505";
42+
URI uri = AsyncHttpProviderUtils.getRedirectUri(URI.create("http://www.ebay.de"), url);
43+
Assert.assertEquals("http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC%20Lifebook%20E8310%20Core2Duo%20T8100%202%201GHz%204GB%20DVD%20RW&_itemId=150731406505", uri.toString());
44+
}
45+
}

0 commit comments

Comments
 (0)