HttpServer path matching fixes
Change-Id: I1429629210c16fc73d2a9e3d8c01e00d30de0c66
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1581
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: abdullah alamoudi <bamousaa@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServer.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServer.java
index 74361b8..cdfc492 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServer.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/HttpServer.java
@@ -218,23 +218,23 @@
if (i >= 0) {
uri = uri.substring(0, i);
}
- for (IServlet let : servlets) {
- for (String path : let.getPaths()) {
+ for (IServlet servlet : servlets) {
+ for (String path : servlet.getPaths()) {
if (match(path, uri)) {
- return let;
+ return servlet;
}
}
}
+ LOGGER.warning("No servlet for " + uri);
return null;
}
- private static boolean match(String pathSpec, String path) {
+ static boolean match(String pathSpec, String path) {
char c = pathSpec.charAt(0);
if (c == '/') {
- if (pathSpec.length() == 1 || pathSpec.equals(path)) {
+ if (pathSpec.equals(path) || (pathSpec.length() == 1 && path.isEmpty())) {
return true;
}
-
if (isPathWildcardMatch(pathSpec, path)) {
return true;
}
@@ -244,10 +244,14 @@
return false;
}
- private static boolean isPathWildcardMatch(String pathSpec, String path) {
- int cpl = pathSpec.length() - 2;
- return (pathSpec.endsWith("/*") && path.regionMatches(0, pathSpec, 0, cpl))
- && (path.length() == cpl || '/' == path.charAt(cpl));
+ static boolean isPathWildcardMatch(String pathSpec, String path) {
+ final int length = pathSpec.length();
+ if (length < 2) {
+ return false;
+ }
+ final int cpl = length - 2;
+ final boolean b = pathSpec.endsWith("/*") && path.regionMatches(0, pathSpec, 0, cpl);
+ return b && (path.length() == cpl || '/' == path.charAt(cpl));
}
public ExecutorService getExecutor() {
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/server/PathMatchTest.java b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/server/PathMatchTest.java
new file mode 100644
index 0000000..89260c5
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/server/PathMatchTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package org.apache.hyracks.http.server;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PathMatchTest {
+ @Test
+ public void test() {
+ Assert.assertTrue(HttpServer.match("/", ""));
+
+ Assert.assertTrue(HttpServer.match("/", "/"));
+ Assert.assertFalse(HttpServer.match("/", "/a"));
+
+ Assert.assertFalse(HttpServer.match("/a", "/"));
+ Assert.assertTrue(HttpServer.match("/a", "/a"));
+
+ Assert.assertTrue(HttpServer.match("/*", "/"));
+ Assert.assertTrue(HttpServer.match("/*", "/a"));
+
+ Assert.assertFalse(HttpServer.match("/a/*", "/"));
+ Assert.assertTrue(HttpServer.match("/a/*", "/a"));
+ }
+}