commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Donal Murtagh (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (VALIDATOR-342) URLValidator returns false for http://example.rocks
Date Mon, 20 Oct 2014 11:21:33 GMT

    [ https://issues.apache.org/jira/browse/VALIDATOR-342?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14176818#comment-14176818
] 

Donal Murtagh commented on VALIDATOR-342:
-----------------------------------------

I worked around this problem by writing my own validator method.
{code}
    /**
     * Indicates whether a string is a valid HTTP/HTTPS URL
     * @param str
     * @param allowNull
     * @return
     */
    static boolean isWebUrl(String str, boolean allowNull = true) {
        try {
            if (str == null) {
                return allowNull
            }

            str = str.trim()

            def validProtocol = ['http://', 'https://'].any {
                str.toLowerCase().startsWith(it)
            }

            if (!validProtocol) {
                return false
            }

            URL url = new URL(str)
            url.toURI()

        } catch (MalformedURLException | URISyntaxException ex) {
            return false
        }
    }
{code}

The code above is Groovy, but it can be easily converted to Java. It passes the following
unit tests:

{code}
class StringUtilsTests {

    void testInvalidHttpUrls() {

        ['ftp://example.org', ' ', '//example.org', 'http:/example.org'].each {
            assertFalse StringUtils.isWebUrl(it)
        }

        assertFalse StringUtils.isWebUrl(null, false)
    }

    void testValidHttpUrls() {

        ['http://example.org', 'https://example.org', 'https://example.rocks', null].each
{
            assertTrue StringUtils.isWebUrl(it)
        }
    }
}
{code}



> URLValidator returns false for http://example.rocks
> ---------------------------------------------------
>
>                 Key: VALIDATOR-342
>                 URL: https://issues.apache.org/jira/browse/VALIDATOR-342
>             Project: Commons Validator
>          Issue Type: Bug
>            Reporter: Donal Murtagh
>
> It seems the (valid) subdomain is the problem because it returns true for http://example.com



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message