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/04/25 22:50:15 UTC

svn commit: r1792674 - in /httpcomponents/httpcore/trunk: RELEASE_NOTES.txt httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java

Author: ggregory
Date: Tue Apr 25 22:50:15 2017
New Revision: 1792674

URL: http://svn.apache.org/viewvc?rev=1792674&view=rev
Log:
HTTPCORE-458: Validate port values. First use case.

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1792674&r1=1792673&r2=1792674&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Tue Apr 25 22:50:15 2017
@@ -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
 -------------------

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java?rev=1792674&r1=1792673&r2=1792674&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java Tue Apr 25 22:50:15 2017
@@ -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 N
         } else {
             this.schemeName = DEFAULT_SCHEME_NAME;
         }
-        this.port = port;
+        this.port = Ports.check(port);
     }
 
     /**

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java?rev=1792674&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/Ports.java Tue Apr 25 22:50:15 2017
@@ -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)");
+    }
+}