Skip to content

Commit 154ebdc

Browse files
author
Stephane Landelle
committed
Support Location header with raw query, backport AsyncHttpClient#268
1 parent 74a21de commit 154ebdc

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

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

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import java.io.InputStream;
2121
import java.io.UnsupportedEncodingException;
2222
import java.net.URI;
23+
import java.net.URISyntaxException;
2324
import java.text.ParsePosition;
2425
import java.text.SimpleDateFormat;
2526
import java.util.Collection;
2627
import java.util.Date;
2728
import java.util.List;
2829
import java.util.Locale;
2930

30-
import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
3131
import com.ning.http.client.AsyncHttpClientConfig;
3232
import com.ning.http.client.AsyncHttpProvider;
3333
import com.ning.http.client.ByteArrayPart;
@@ -41,6 +41,7 @@
4141
import com.ning.http.multipart.ByteArrayPartSource;
4242
import com.ning.http.multipart.MultipartRequestEntity;
4343
import com.ning.http.multipart.PartSource;
44+
import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
4445

4546
/**
4647
* {@link com.ning.http.client.AsyncHttpProvider} common utilities.
@@ -231,22 +232,51 @@ public final static String getHost(URI uri) {
231232
}
232233

233234
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);
247267

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+
}
250280

251281
public final static int getPort(URI uri) {
252282
int port = uri.getPort();
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)