1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
14 from six.moves.urllib.parse import urljoin
15 from six.moves.urllib.parse import urlparse
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.utils.gettextutils import _
21 import urllib.request as urllib
27 class UrlUtils(object):
30 def validate_url(path):
31 """Validates whether the given path is a URL or not.
33 If the given path includes a scheme (http, https, ftp, ...) and a net
34 location (a domain name such as www.github.com) it is validated as a
37 parsed = urlparse(path)
38 return bool(parsed.scheme) and bool(parsed.netloc)
41 def join_url(url, relative_path):
42 """Builds a new URL from the given URL and the relative path.
45 url: http://www.githib.com/openstack/heat
46 relative_path: heat-translator
47 - joined: http://www.githib.com/openstack/heat-translator
49 if not UrlUtils.validate_url(url):
50 ExceptionCollector.appendException(
51 ValueError(_('"%s" is not a valid URL.') % url))
52 return urljoin(url, relative_path)
55 def url_accessible(url):
56 """Validates whether the given URL is accessible.
58 Returns true if the get call returns a 200 response code.
59 Otherwise, returns false.
61 return urllib.urlopen(url).getcode() == 200