提交 195fb203 编写于 作者: A Allen Wang

Merge branch 'master' of https://github.com/Netflix/ribbon into cp

......@@ -394,7 +394,6 @@ public abstract class LoadBalancerContext<T extends ClientRequest, S extends IRe
*/
@SuppressWarnings("unchecked")
protected T computeFinalUriWithLoadBalancer(T original) throws ClientException{
URI newURI;
URI theUrl = original.getUri();
if (theUrl == null){
......@@ -514,43 +513,29 @@ public abstract class LoadBalancerContext<T extends ClientRequest, S extends IRe
// just verify that at this point we have a full URL
try {
String urlPath = "";
if (theUrl.getRawPath() != null && theUrl.getRawPath().startsWith("/")) {
urlPath = theUrl.getRawPath();
} else {
urlPath = "/" + theUrl.getRawPath();
}
newURI = new URI(scheme, theUrl.getUserInfo(), host, port, urlPath, theUrl.getQuery(), theUrl.getFragment());
if (isURIEncoded(theUrl)) {
StringBuilder sb = new StringBuilder();
sb.append(newURI.getScheme())
.append("://")
.append(newURI.getRawAuthority())
.append(theUrl.getRawPath());
sb.append(scheme).append("://");
if (!Strings.isNullOrEmpty(theUrl.getRawUserInfo())) {
sb.append(theUrl.getRawUserInfo()).append("@");
}
sb.append(host);
if (port >= 0) {
sb.append(":").append(port);
}
sb.append(theUrl.getRawPath());
if (!Strings.isNullOrEmpty(theUrl.getRawQuery())) {
sb.append("?").append(theUrl.getRawQuery());
}
if (!Strings.isNullOrEmpty(theUrl.getRawFragment())) {
sb.append("#").append(theUrl.getRawFragment());
}
newURI = new URI(sb.toString());
}
URI newURI = new URI(sb.toString());
return (T) original.replaceUri(newURI);
} catch (URISyntaxException e) {
throw new ClientException(ClientException.ErrorType.GENERAL, e.getMessage());
}
}
private boolean isURIEncoded(URI uri) {
String original = uri.toString();
try {
return !URLEncoder.encode(original, "UTF-8").equals(original);
} catch (Exception e) {
return false;
}
}
protected boolean isRetriable(T request) {
if (request.isRetriable()) {
return true;
......
......@@ -61,6 +61,23 @@ public class LoadBalancerContextTest {
assertEquals(uri, newRequest.getUri().toString());
}
@Test
public void testPreservesUserInfo() throws ClientException {
// %3A == ":" -- ensure user info is not decoded
String uri = "http://us%3Aer:pass@localhost:8080?foo=bar";
HttpRequest request = HttpRequest.newBuilder().uri(uri).build();
HttpRequest newRequest = context.computeFinalUriWithLoadBalancer(request);
assertEquals(uri, newRequest.getUri().toString());
}
@Test
public void testQueryWithoutPath() throws ClientException {
String uri = "?foo=bar";
HttpRequest request = HttpRequest.newBuilder().uri(uri).build();
HttpRequest newRequest = context.computeFinalUriWithLoadBalancer(request);
assertEquals("http://www.example.com:8080?foo=bar", newRequest.getUri().toString());
}
@Test
public void testEncodedPathAndHostChange() throws ClientException {
String uri = "/abc%2Fxyz";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册