From 2588e16238dfea30ccc0be1eeb425d4c67d37962 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 29 Feb 2008 09:43:28 +0000 Subject: [PATCH] HTTPCLIENT-755: Workaround for known bugs in java.net.URI.resolve() (Bug ID: 4708535) Contributed by Johannes Koch git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@632273 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 7 ++++ .../apache/http/client/utils/URLUtils.java | 40 +++++++++++++++++++ .../impl/client/DefaultRedirectHandler.java | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index f98e0c1d4..4434b1a0e 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,3 +1,10 @@ +Changes since 4.0 Alpha 3 +------------------- + +* [HTTPCLIENT-755] Workaround for known bugs in java.net.URI.resolve() + Bug ID: 4708535 + Contributed by Johannes Koch + Release 4.0 Alpha 3 ------------------- diff --git a/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java b/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java index 8677f3ea3..31cb8d226 100644 --- a/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java +++ b/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java @@ -194,6 +194,46 @@ public class URLUtils { return rewriteURI(uri, target, false); } + /** + * Resolves a URI reference against a base URI. Work-around for bug in + * java.net.URI () + * + * @param baseURI the base URI + * @param reference the URI reference + * @return the resulting URI + */ + public static URI resolve(final URI baseURI, final String reference) { + return URLUtils.resolve(baseURI, URI.create(reference)); + } + + /** + * Resolves a URI reference against a base URI. Work-around for bug in + * java.net.URI () + * + * @param baseURI the base URI + * @param reference the URI reference + * @return the resulting URI + */ + public static URI resolve(final URI baseURI, URI reference){ + if (baseURI == null) { + throw new IllegalArgumentException("Base URI may nor be null"); + } + if (reference == null) { + throw new IllegalArgumentException("Reference URI may nor be null"); + } + boolean emptyReference = reference.toString().length() == 0; + if (emptyReference) { + reference = URI.create("#"); + } + URI resolved = baseURI.resolve(reference); + if (emptyReference) { + String resolvedString = resolved.toString(); + resolved = URI.create(resolvedString.substring(0, + resolvedString.indexOf('#'))); + } + return resolved; + } + /** * This class should not be instantiated. */ diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java index 74409aa7a..e14f308e1 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java @@ -138,7 +138,7 @@ public class DefaultRedirectHandler implements RedirectHandler { try { URI requestURI = new URI(request.getRequestLine().getUri()); URI absoluteRequestURI = URLUtils.rewriteURI(requestURI, target, true); - uri = absoluteRequestURI.resolve(uri); + uri = URLUtils.resolve(absoluteRequestURI, uri); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); }