-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-761004 Added URL Validator and URL escaping of strings (#1480)
- Loading branch information
1 parent
4b1d474
commit 1cdbd3b
Showing
8 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# | ||
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. | ||
# | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
import urllib.parse | ||
from logging import getLogger | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
URL_VALIDATOR = re.compile( | ||
"^http(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z@:])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\&\\(\\)\\/\\\\\\+&%\\$#_=@]*)?$" | ||
) | ||
|
||
|
||
def is_valid_url(url: str) -> bool: | ||
"""Confirms if the provided URL is a valid HTTP/ HTTPs URL | ||
Args: | ||
url: the URL that needs to be validated | ||
Returns: | ||
true/ false depending on whether the URL is valid or not | ||
""" | ||
return bool(URL_VALIDATOR.match(url)) | ||
|
||
|
||
def url_encode_str(target: str | None) -> str: | ||
"""Converts a target string into escaped URL safe string | ||
Args: | ||
target: string to be URL encoded | ||
Returns: | ||
URL encoded string | ||
""" | ||
if target is None: | ||
logger.debug("The string to be URL encoded is None") | ||
return "" | ||
return urllib.parse.quote_plus(target, safe="") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# | ||
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. | ||
# | ||
|
||
try: | ||
from snowflake.connector.url_util import is_valid_url, url_encode_str | ||
except ImportError: | ||
|
||
def is_valid_url(s): | ||
return False | ||
|
||
|
||
def test_url_validator(): | ||
assert is_valid_url("https://ssoTestURL.okta.com") | ||
assert is_valid_url("https://ssoTestURL.okta.com:8080") | ||
assert is_valid_url("https://ssoTestURL.okta.com/testpathvalue") | ||
|
||
assert not is_valid_url("-a Calculator") | ||
assert not is_valid_url("This is a random text") | ||
assert not is_valid_url("file://TestForFile") | ||
|
||
|
||
def test_encoder(): | ||
assert url_encode_str("Hello @World") == "Hello+%40World" | ||
assert url_encode_str("Test//String") == "Test%2F%2FString" | ||
assert url_encode_str(None) == "" |