You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "dan-s1 (via GitHub)" <gi...@apache.org> on 2023/12/25 19:06:45 UTC

[PR] NIFI-12513 Added UriUtils class which allows the construction of a valid java.net.URI with a single string even though there maybe illegal characters in the path, query and/or fragment sections(s) of the URI. [nifi]

dan-s1 opened a new pull request, #8189:
URL: https://github.com/apache/nifi/pull/8189

   …
   
   <!-- 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. -->
   
   # Summary
   
   [NIFI-12513](https://issues.apache.org/jira/browse/NIFI-12513)
   
   # Tracking
   
   Please complete the following tracking steps prior to pull request creation.
   
   ### Issue Tracking
   
   - [ ] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
   
   ### Pull Request Tracking
   
   - [ ] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
   - [ ] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
   
   ### Pull Request Formatting
   
   - [ ] Pull Request based on current revision of the `main` branch
   - [ ] Pull Request refers to a feature branch with one commit containing changes
   
   # Verification
   
   Please indicate the verification steps performed prior to pull request creation.
   
   ### Build
   
   - [ ] Build completed using `mvn clean install -P contrib-check`
     - [ ] JDK 21
   
   ### Licensing
   
   - [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
   - [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
   
   ### Documentation
   
   - [ ] Documentation formatting appears as expected in rendered files
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] NIFI-12513 Added UriUtils class which allows the construction of a valid java.net.URI with a single string even though there maybe illegal characters in the path, query and/or fragment sections(s) of the URI. [nifi]

Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8189:
URL: https://github.com/apache/nifi/pull/8189#discussion_r1436535660


##########
nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/UriUtils.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.nifi.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+/**
+ * Utility class providing java.net.URI utilities.
+ * The regular expressions in this class used to capture the various components of a URI were adapted from
+ * <a href="https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java">UriComponentsBuilder</a>
+ */
+public class UriUtils {
+    private static final String SCHEME_PATTERN = "([^:/?#]+):";
+    private static final String USERINFO_PATTERN = "([^@\\[/?#]*)";
+    private static final String HOST_IPV4_PATTERN = "[^\\[/?#:]*";
+    private static final String HOST_IPV6_PATTERN = "\\[[\\p{XDigit}:.]*[%\\p{Alnum}]*]";
+    private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")";
+    private static final String PORT_PATTERN = "(\\{[^}]+\\}?|[^/?#]*)";
+    private static final String PATH_PATTERN = "([^?#]*)";
+    private static final String QUERY_PATTERN = "([^#]*)";
+    private static final String LAST_PATTERN = "(.*)";
+
+    // Regex patterns that matches URIs. See RFC 3986, appendix B
+    private static final Pattern URI_PATTERN = Pattern.compile(
+            "^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
+                    ")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");
+
+    private UriUtils() {}
+
+    /**
+     * This method provides an alternative to the use of java.net.URI's single argument constructor and 'create' method.
+     * The drawbacks of the java.net.URI's single argument constructor and 'create' method are:
+     *   <ul>
+     *      <li>They do not provide quoting in the path section for any character not in the unreserved, punct, escaped, or other categories,
+     *          and not equal to the slash character ('/') or the commercial-at character ('{@literal @}').</li>
+     *      <li>They do not provide quoting for any illegal characters found in the query and fragment sections.</li>
+     *  </ul>
+     *  On the other hand, java.net.URI's seven argument constructor provides these quoting capabilities. In order
+     *  to take advantage of this constructor, this method parses the given string into the arguments needed
+     *  thereby allowing for instantiating a java.net.URI with the quoting of all illegal characters.
+     * @param uri String representing a URI.
+     * @return Instance of java.net.URI
+     */
+    public static URI create(String uri) throws URISyntaxException {
+        final Matcher matcher = URI_PATTERN.matcher(uri);
+        if(matcher.matches()) {

Review Comment:
   Spacing:
   ```suggestion
           if (matcher.matches()) {
   ```



##########
nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/UriUtils.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.nifi.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+/**
+ * Utility class providing java.net.URI utilities.
+ * The regular expressions in this class used to capture the various components of a URI were adapted from
+ * <a href="https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java">UriComponentsBuilder</a>
+ */
+public class UriUtils {
+    private static final String SCHEME_PATTERN = "([^:/?#]+):";
+    private static final String USERINFO_PATTERN = "([^@\\[/?#]*)";
+    private static final String HOST_IPV4_PATTERN = "[^\\[/?#:]*";
+    private static final String HOST_IPV6_PATTERN = "\\[[\\p{XDigit}:.]*[%\\p{Alnum}]*]";
+    private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")";
+    private static final String PORT_PATTERN = "(\\{[^}]+\\}?|[^/?#]*)";
+    private static final String PATH_PATTERN = "([^?#]*)";
+    private static final String QUERY_PATTERN = "([^#]*)";
+    private static final String LAST_PATTERN = "(.*)";
+
+    // Regex patterns that matches URIs. See RFC 3986, appendix B
+    private static final Pattern URI_PATTERN = Pattern.compile(
+            "^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
+                    ")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");
+
+    private UriUtils() {}
+
+    /**
+     * This method provides an alternative to the use of java.net.URI's single argument constructor and 'create' method.
+     * The drawbacks of the java.net.URI's single argument constructor and 'create' method are:
+     *   <ul>
+     *      <li>They do not provide quoting in the path section for any character not in the unreserved, punct, escaped, or other categories,
+     *          and not equal to the slash character ('/') or the commercial-at character ('{@literal @}').</li>
+     *      <li>They do not provide quoting for any illegal characters found in the query and fragment sections.</li>
+     *  </ul>
+     *  On the other hand, java.net.URI's seven argument constructor provides these quoting capabilities. In order
+     *  to take advantage of this constructor, this method parses the given string into the arguments needed
+     *  thereby allowing for instantiating a java.net.URI with the quoting of all illegal characters.
+     * @param uri String representing a URI.
+     * @return Instance of java.net.URI

Review Comment:
   A new line should be added along the lines of `@throws URISyntaxException Thrown on parsing failures`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] NIFI-12513 Added UriUtils class which allows the construction of a valid java.net.URI with a single string even though there maybe illegal characters in the path, query and/or fragment sections(s) of the URI. [nifi]

Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory closed pull request #8189: NIFI-12513 Added UriUtils class which allows the construction of a valid java.net.URI with a single string even though there maybe illegal characters in the path, query and/or fragment sections(s) of the URI.
URL: https://github.com/apache/nifi/pull/8189


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] NIFI-12513 Added UriUtils class which allows the construction of a valid java.net.URI with a single string even though there maybe illegal characters in the path, query and/or fragment sections(s) of the URI. [nifi]

Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on PR #8189:
URL: https://github.com/apache/nifi/pull/8189#issuecomment-1869749673

   @exceptionfactory 
   The failure on `ci-workflow / Windows Zulu JDK 21 FR ` below does not seem related to my changes
   ```
    Failures: 
   Error:    TestFTP.testListFtpHostPortVariablesFileFound:317 expected: <1> but was: <0>
   Error:  Tests run: 1786, Failures: 1, Errors: 0, Skipped: 129
   Error:  Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.1.2:test (default-test) on project nifi-standard-processors: There are test failures.
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org