You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2018/03/16 12:19:28 UTC

[1/2] httpcomponents-core git commit: Added Host class representing a DNS endpoint

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master 54f64e291 -> 831d48695


Added Host class representing a DNS endpoint


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/788b803e
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/788b803e
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/788b803e

Branch: refs/heads/master
Commit: 788b803eedadda15a2238a8d39f761193a78b6da
Parents: 54f64e2
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Fri Mar 16 13:05:57 2018 +0100
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Fri Mar 16 13:10:50 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/hc/core5/http/HttpHost.java |   6 +-
 .../main/java/org/apache/hc/core5/net/Host.java | 109 +++++++++++++++
 .../java/org/apache/hc/core5/net/Ports.java     |  18 ++-
 .../org/apache/hc/core5/net/URIAuthority.java   |   4 +-
 .../java/org/apache/hc/core5/net/TestHost.java  | 137 +++++++++++++++++++
 5 files changed, 268 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/788b803e/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
index f101c1c..dff7eab 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
@@ -79,7 +79,7 @@ public final class HttpHost implements NamedEndpoint, Serializable {
         this.hostname = hostname;
         this.lcHostname = hostname;
         this.schemeName = scheme;
-        this.port = Ports.check(port);
+        this.port = Ports.checkWithDefault(port);
         this.address = null;
     }
 
@@ -106,7 +106,7 @@ public final class HttpHost implements NamedEndpoint, Serializable {
         } else {
             this.schemeName = DEFAULT_SCHEME.id;
         }
-        this.port = Ports.check(port);
+        this.port = Ports.checkWithDefault(port);
         this.address = null;
     }
 
@@ -237,7 +237,7 @@ public final class HttpHost implements NamedEndpoint, Serializable {
         } else {
             this.schemeName = DEFAULT_SCHEME.id;
         }
-        this.port = Ports.check(port);
+        this.port = Ports.checkWithDefault(port);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/788b803e/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java b/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
new file mode 100644
index 0000000..ba138a1
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/Host.java
@@ -0,0 +1,109 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.net;
+
+import java.io.Serializable;
+import java.net.URISyntaxException;
+import java.util.Locale;
+
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.LangUtils;
+import org.apache.hc.core5.util.TextUtils;
+
+@Contract(threading = ThreadingBehavior.IMMUTABLE)
+public final class Host implements NamedEndpoint, Serializable {
+
+    private final String name;
+    private final String lcName;
+    private final int port;
+
+    public Host(final String name, final int port) {
+        super();
+        this.name   = Args.containsNoBlanks(name, "Host name");
+        this.port = Ports.check(port);
+        this.lcName = this.name.toLowerCase(Locale.ROOT);
+    }
+
+    public static Host create(final String s) throws URISyntaxException {
+        Args.notEmpty(s, "HTTP Host");
+        final int portIdx = s.lastIndexOf(":");
+        int port;
+        if (portIdx > 0) {
+            try {
+                port = Integer.parseInt(s.substring(portIdx + 1));
+            } catch (final NumberFormatException ex) {
+                throw new URISyntaxException(s, "invalid port");
+            }
+            final String hostname = s.substring(0, portIdx);
+            if (TextUtils.containsBlanks(hostname)) {
+                throw new URISyntaxException(s, "hostname contains blanks");
+            }
+            return new Host(hostname, port);
+        } else {
+            throw new URISyntaxException(s, "port not found");
+        }
+    }
+
+    @Override
+    public String getHostName() {
+        return name;
+    }
+
+    @Override
+    public int getPort() {
+        return port;
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o instanceof Host) {
+            final Host that = (Host) o;
+            return this.lcName.equals(that.lcName) && this.port == that.port;
+        } else {
+            return false;
+        }
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = LangUtils.HASH_SEED;
+        hash = LangUtils.hashCode(hash, this.lcName);
+        hash = LangUtils.hashCode(hash, this.port);
+        return hash;
+    }
+
+    @Override
+    public String toString() {
+        return name + ":" + port;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/788b803e/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java b/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
index c8938c7..b367e3d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
@@ -61,8 +61,24 @@ public class Ports {
      *             If the port parameter is outside the specified range of valid port values, which is between 0 and
      *             65535, inclusive. {@code -1} indicates the scheme default port.
      */
-    public static int check(final int port) {
+    public static int checkWithDefault(final int port) {
         return Args.checkRange(port, SCHEME_DEFAULT, MAX_VALUE,
                 "Port number(Use -1 to specify the scheme default port)");
     }
+
+    /**
+     * Checks a port number.
+     *
+     * @param port
+     *            The port to check.
+     * @return the port
+     *
+     * @throws IllegalArgumentException
+     *             If the port parameter is outside the specified range of valid port values, which is between 0 and
+     *             65535, inclusive.
+     */
+    public static int check(final int port) {
+        return Args.checkRange(port, MIN_VALUE, MAX_VALUE, "Port number");
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/788b803e/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
index 74d7ab6..9484100 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
@@ -58,7 +58,7 @@ public final class URIAuthority implements NamedEndpoint, Serializable {
         super();
         this.userInfo = userInfo;
         this.hostname = hostname;
-        this.port = Ports.check(port);
+        this.port = Ports.checkWithDefault(port);
     }
 
     /**
@@ -74,7 +74,7 @@ public final class URIAuthority implements NamedEndpoint, Serializable {
         }
         this.userInfo = userInfo;
         this.hostname = hostname.toLowerCase(Locale.ROOT);
-        this.port = Ports.check(port);
+        this.port = Ports.checkWithDefault(port);
     }
 
     public URIAuthority(final String hostname, final int port) {

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/788b803e/httpcore5/src/test/java/org/apache/hc/core5/net/TestHost.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/net/TestHost.java b/httpcore5/src/test/java/org/apache/hc/core5/net/TestHost.java
new file mode 100644
index 0000000..131e63c
--- /dev/null
+++ b/httpcore5/src/test/java/org/apache/hc/core5/net/TestHost.java
@@ -0,0 +1,137 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.net;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.net.URISyntaxException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link Host}.
+ *
+ */
+public class TestHost {
+
+    @Test
+    public void testConstructor() {
+        final Host host1 = new Host("somehost", 8080);
+        Assert.assertEquals("somehost", host1.getHostName());
+        Assert.assertEquals(8080, host1.getPort());
+        final Host host2 = new Host("somehost", 0);
+        Assert.assertEquals("somehost", host2.getHostName());
+        Assert.assertEquals(0, host2.getPort());
+        try {
+            new Host(null, 0);
+            Assert.fail("IllegalArgumentException should have been thrown");
+        } catch (final IllegalArgumentException expected) {
+        }
+        try {
+            new Host("blah", -1);
+            Assert.fail("IllegalArgumentException should have been thrown");
+        } catch (final IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testHashCode() throws Exception {
+        final Host host1 = new Host("somehost", 8080);
+        final Host host2 = new Host("somehost", 80);
+        final Host host3 = new Host("someotherhost", 8080);
+        final Host host4 = new Host("somehost", 80);
+
+        Assert.assertTrue(host1.hashCode() == host1.hashCode());
+        Assert.assertTrue(host1.hashCode() != host2.hashCode());
+        Assert.assertTrue(host1.hashCode() != host3.hashCode());
+        Assert.assertTrue(host2.hashCode() == host4.hashCode());
+    }
+
+    @Test
+    public void testEquals() throws Exception {
+        final Host host1 = new Host("somehost", 8080);
+        final Host host2 = new Host("somehost", 80);
+        final Host host3 = new Host("someotherhost", 8080);
+        final Host host4 = new Host("somehost", 80);
+
+        Assert.assertTrue(host1.equals(host1));
+        Assert.assertFalse(host1.equals(host2));
+        Assert.assertFalse(host1.equals(host3));
+        Assert.assertTrue(host2.equals(host4));
+    }
+
+    @Test
+    public void testToString() throws Exception {
+        final Host host1 = new Host("somehost", 8888);
+        Assert.assertEquals("somehost:8888", host1.toString());
+    }
+
+    @Test
+    public void testSerialization() throws Exception {
+        final Host orig = new Host("somehost", 8080);
+        final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
+        final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
+        outstream.writeObject(orig);
+        outstream.close();
+        final byte[] raw = outbuffer.toByteArray();
+        final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
+        final ObjectInputStream instream = new ObjectInputStream(inbuffer);
+        final Host clone = (Host) instream.readObject();
+        Assert.assertEquals(orig, clone);
+    }
+
+    @Test
+    public void testCreateFromString() throws Exception {
+        Assert.assertEquals(new Host("somehost", 8080), Host.create("somehost:8080"));
+        Assert.assertEquals(new Host("somehost", 1234), Host.create("somehost:1234"));
+        Assert.assertEquals(new Host("somehost", 0), Host.create("somehost:0"));
+    }
+
+    @Test
+    public void testCreateFromStringInvalid() throws Exception {
+        try {
+            Host.create(" host ");
+            Assert.fail("URISyntaxException expected");
+        } catch (final URISyntaxException expected) {
+        }
+        try {
+            Host.create("host :8080");
+            Assert.fail("URISyntaxException expected");
+        } catch (final URISyntaxException expected) {
+        }
+        try {
+            Host.create("");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (final IllegalArgumentException expected) {
+        }
+    }
+
+}


[2/2] httpcomponents-core git commit: Information responses (1xx) processing implemented.

Posted by ol...@apache.org.
Information responses (1xx) processing implemented.


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/831d4869
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/831d4869
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/831d4869

Branch: refs/heads/master
Commit: 831d486953159ae455b81126371ebd7643521551
Parents: 788b803
Author: Kirill Usov <ki...@gmail.com>
Authored: Thu Mar 15 11:41:55 2018 +0300
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Fri Mar 16 13:11:20 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java | 2 ++
 .../java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java | 4 ++++
 .../hc/core5/http/nio/support/BasicClientExchangeHandler.java    | 1 +
 3 files changed, 7 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/831d4869/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
index 37c576e..2d2a133 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/AsyncResponseConsumer.java
@@ -42,6 +42,8 @@ public interface AsyncResponseConsumer<T> extends AsyncDataConsumer {
 
     void consumeResponse(HttpResponse response, EntityDetails entityDetails, FutureCallback<T> resultCallback) throws HttpException, IOException;
 
+    void informationResponse(HttpResponse response) throws HttpException, IOException;
+
     void failed(Exception cause);
 
     T getResult();

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/831d4869/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
index 9ede4b3..30fd3cd 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicResponseConsumer.java
@@ -95,6 +95,10 @@ public class BasicResponseConsumer<T> implements AsyncResponseConsumer<Message<H
     }
 
     @Override
+    public void informationResponse(final HttpResponse response) throws HttpException, IOException {
+    }
+
+    @Override
     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
         dataConsumer.updateCapacity(capacityChannel);
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/831d4869/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicClientExchangeHandler.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicClientExchangeHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicClientExchangeHandler.java
index 9052d55..eb00a6a 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicClientExchangeHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/BasicClientExchangeHandler.java
@@ -86,6 +86,7 @@ public class BasicClientExchangeHandler<T> implements AsyncClientExchangeHandler
 
     @Override
     public void consumeInformation(final HttpResponse response) throws HttpException, IOException {
+        responseConsumer.informationResponse(response);
     }
 
     @Override