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 2017/05/09 19:58:22 UTC

[30/50] [abbrv] httpcomponents-core git commit: HTTPCORE-458: Validate port values. First use case.

HTTPCORE-458: Validate port values. First use case.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk@1792674 13f79535-47bb-0310-9956-ffa450edef68


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

Branch: refs/heads/trunk
Commit: eb2c139390c5ff69cfc36f083c6760c5e38b2db0
Parents: f29e5fe
Author: Gary D. Gregory <gg...@apache.org>
Authored: Tue Apr 25 22:50:15 2017 +0000
Committer: Gary D. Gregory <gg...@apache.org>
Committed: Tue Apr 25 22:50:15 2017 +0000

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |  5 +-
 .../java/org/apache/hc/core5/http/HttpHost.java |  3 +-
 .../java/org/apache/hc/core5/net/Ports.java     | 68 ++++++++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/eb2c1393/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 0579ff5..7c1c829 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -89,8 +89,9 @@ Changelog
 * Non-blocking connection initializer to support multi-homed remote endpoints.
   Contributed by Oleg Kalnichevski <olegk at apache.org>
 
-
-
+* HTTPCORE-458: Validate port values
+  Contributed by Gary Gregory <ggregory at apache.org>
+  
 
 Release 5.0-ALPHA2
 -------------------

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/eb2c1393/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 5c74d91..94d37c7 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
@@ -35,6 +35,7 @@ import java.util.Locale;
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.net.NamedEndpoint;
+import org.apache.hc.core5.net.Ports;
 import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.LangUtils;
@@ -207,7 +208,7 @@ public final class HttpHost implements NamedEndpoint, Serializable {
         } else {
             this.schemeName = DEFAULT_SCHEME_NAME;
         }
-        this.port = port;
+        this.port = Ports.check(port);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/eb2c1393/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
new file mode 100644
index 0000000..156c40f
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
@@ -0,0 +1,68 @@
+/*
+ * ====================================================================
+ * 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 org.apache.hc.core5.util.Args;
+
+/**
+ * Port helper methods.
+ *
+ * @since 5.0
+ */
+public class Ports {
+
+    /**
+     * The scheme default port.
+     */
+    public final static int SCHEME_DEFAULT = -1;
+
+    /**
+     * The minimum port value per https://tools.ietf.org/html/rfc6335.
+     */
+    public final static int MIN_VALUE = 0;
+
+    /**
+     * The maxium port value per https://tools.ietf.org/html/rfc6335.
+     */
+    public final static int MAX_VALUE = 65535;
+
+    /**
+     * Checks a port number where {@code -1} indicates the scheme default port.
+     *
+     * @param port
+     *            The port to check where {@code -1} indicates the scheme default port.
+     * @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, SCHEME_DEFAULT, MAX_VALUE,
+                "Port number(Use -1 to specify the scheme default port)");
+    }
+}