You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2017/05/19 00:03:18 UTC
httpcomponents-client git commit: [HTTPCLIENT-1852] Add APIs
URIBuilder.localhost() and setHost(InetAddress)
Repository: httpcomponents-client
Updated Branches:
refs/heads/dev/4.5.x/HTTPCLIENT-1852 [created] 891c1129d
[HTTPCLIENT-1852] Add APIs URIBuilder.localhost() and
setHost(InetAddress)
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/891c1129
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/891c1129
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/891c1129
Branch: refs/heads/dev/4.5.x/HTTPCLIENT-1852
Commit: 891c1129dfa3d2860503346fd55822e24162535d
Parents: 0b6adcc
Author: Gary Gregory <gg...@apache.org>
Authored: Thu May 18 17:03:08 2017 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Thu May 18 17:03:08 2017 -0700
----------------------------------------------------------------------
.../apache/http/client/utils/URIBuilder.java | 23 ++++++++++++++
.../org/apache/http/client/utils/URIUtils.java | 2 +-
.../http/client/utils/TestURIBuilder.java | 32 +++++++++++++++++++-
3 files changed, 55 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/891c1129/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
index 91f198c..b2b4121 100644
--- a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
+++ b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
@@ -26,8 +26,10 @@
*/
package org.apache.http.client.utils;
+import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
@@ -46,6 +48,15 @@ import org.apache.http.util.TextUtils;
*/
public class URIBuilder {
+ /**
+ * Creates a new builder for the host {@link InetAddress#getLocalHost()}.
+ *
+ * @since 4.5.4
+ */
+ public static URIBuilder localhost() throws UnknownHostException {
+ return new URIBuilder().setHost(InetAddress.getLocalHost());
+ }
+
private String scheme;
private String encodedSchemeSpecificPart;
private String encodedAuthority;
@@ -248,6 +259,18 @@ public class URIBuilder {
/**
* Sets URI host.
+ *
+ * @since 4.5.4
+ */
+ public URIBuilder setHost(final InetAddress host) {
+ this.host = host.getHostAddress();
+ this.encodedSchemeSpecificPart = null;
+ this.encodedAuthority = null;
+ return this;
+ }
+
+ /**
+ * Sets URI host.
*/
public URIBuilder setHost(final String host) {
this.host = host;
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/891c1129/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
----------------------------------------------------------------------
diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
index 02f8c1a..2ab4b58 100644
--- a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
+++ b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
@@ -141,7 +141,7 @@ public class URIUtils {
uribuilder.setPort(target.getPort());
} else {
uribuilder.setScheme(null);
- uribuilder.setHost(null);
+ uribuilder.setHost((String) null);
uribuilder.setPort(-1);
}
if (dropFragment) {
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/891c1129/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
index 8bca89e..de4cf3c 100644
--- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
+++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
@@ -26,6 +26,7 @@
*/
package org.apache.http.client.utils;
+import java.net.InetAddress;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
@@ -51,7 +52,7 @@ public class TestURIBuilder {
@Test
public void testMutationToRelativeUri() throws Exception {
final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
- final URIBuilder uribuilder = new URIBuilder(uri).setHost(null);
+ final URIBuilder uribuilder = new URIBuilder(uri).setHost((String) null);
final URI result = uribuilder.build();
Assert.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
}
@@ -211,6 +212,35 @@ public class TestURIBuilder {
}
@Test
+ public void testLocalhost() throws Exception {
+ // Check that the URI generated by URI builder agrees with that generated by using URI directly
+ final String scheme="https";
+ final InetAddress host=InetAddress.getLocalHost();
+ final String specials="/abcd!$&*()_-+.,=:;'~@[]?<>|#^%\"{}\\\u00a3`\u00ac\u00a6xyz"; // N.B. excludes space
+ final URI uri = new URI(scheme, specials, host.getHostAddress(), 80, specials, specials, specials);
+
+ final URI bld = new URIBuilder()
+ .setScheme(scheme)
+ .setHost(host)
+ .setUserInfo(specials)
+ .setPath(specials)
+ .setCustomQuery(specials)
+ .setFragment(specials)
+ .build();
+
+ Assert.assertEquals(uri.getHost(), bld.getHost());
+
+ Assert.assertEquals(uri.getUserInfo(), bld.getUserInfo());
+
+ Assert.assertEquals(uri.getPath(), bld.getPath());
+
+ Assert.assertEquals(uri.getQuery(), bld.getQuery());
+
+ Assert.assertEquals(uri.getFragment(), bld.getFragment());
+
+ }
+
+ @Test
public void testAgainstURIEncoded() throws Exception {
// Check that the encoded URI generated by URI builder agrees with that generated by using URI directly
final String scheme="https";