2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# Licensed 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.
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2018-07-04 13:15:03 -04:00
|
|
|
import re
|
|
|
|
|
2019-01-22 05:59:27 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-01-20 06:34:09 -05:00
|
|
|
|
|
|
|
|
2018-07-03 09:36:14 -04:00
|
|
|
def parse_server_name(server_name):
|
|
|
|
"""Split a server name into host/port parts.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
server_name (str): server name to parse
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[str, int|None]: host/port parts.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError if the server name could not be parsed.
|
|
|
|
"""
|
|
|
|
try:
|
2019-06-20 05:32:02 -04:00
|
|
|
if server_name[-1] == "]":
|
2018-07-03 09:36:14 -04:00
|
|
|
# ipv6 literal, hopefully
|
|
|
|
return server_name, None
|
|
|
|
|
|
|
|
domain_port = server_name.rsplit(":", 1)
|
|
|
|
domain = domain_port[0]
|
|
|
|
port = int(domain_port[1]) if domain_port[1:] else None
|
|
|
|
return domain, port
|
|
|
|
except Exception:
|
|
|
|
raise ValueError("Invalid server name '%s'" % server_name)
|
|
|
|
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
VALID_HOST_REGEX = re.compile("\\A[0-9a-zA-Z.-]+\\Z")
|
2018-07-04 13:15:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def parse_and_validate_server_name(server_name):
|
|
|
|
"""Split a server name into host/port parts and do some basic validation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
server_name (str): server name to parse
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[str, int|None]: host/port parts.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError if the server name could not be parsed.
|
|
|
|
"""
|
|
|
|
host, port = parse_server_name(server_name)
|
|
|
|
|
|
|
|
# these tests don't need to be bulletproof as we'll find out soon enough
|
|
|
|
# if somebody is giving us invalid data. What we *do* need is to be sure
|
|
|
|
# that nobody is sneaking IP literals in that look like hostnames, etc.
|
|
|
|
|
|
|
|
# look for ipv6 literals
|
2019-06-20 05:32:02 -04:00
|
|
|
if host[0] == "[":
|
|
|
|
if host[-1] != "]":
|
|
|
|
raise ValueError("Mismatched [...] in server name '%s'" % (server_name,))
|
2018-07-04 13:15:03 -04:00
|
|
|
return host, port
|
|
|
|
|
|
|
|
# otherwise it should only be alphanumerics.
|
|
|
|
if not VALID_HOST_REGEX.match(host):
|
2019-06-20 05:32:02 -04:00
|
|
|
raise ValueError(
|
|
|
|
"Server name '%s' contains invalid characters" % (server_name,)
|
|
|
|
)
|
2018-07-04 13:15:03 -04:00
|
|
|
|
|
|
|
return host, port
|