mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Rewrite parse_address util and add one test
This commit is contained in:
parent
a8963ef1a5
commit
2b07f2be28
@ -247,7 +247,7 @@ def is_ipv6(addr):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def parse_address(netloc, default_port=8000):
|
def parse_address(netloc, default_port='8000'):
|
||||||
if re.match(r'unix:(//)?', netloc):
|
if re.match(r'unix:(//)?', netloc):
|
||||||
return re.split(r'unix:(//)?', netloc)[-1]
|
return re.split(r'unix:(//)?', netloc)[-1]
|
||||||
|
|
||||||
@ -260,27 +260,22 @@ def parse_address(netloc, default_port=8000):
|
|||||||
|
|
||||||
if netloc.startswith("tcp://"):
|
if netloc.startswith("tcp://"):
|
||||||
netloc = netloc.split("tcp://")[1]
|
netloc = netloc.split("tcp://")[1]
|
||||||
|
host, port = netloc, default_port
|
||||||
|
|
||||||
# get host
|
|
||||||
if '[' in netloc and ']' in netloc:
|
if '[' in netloc and ']' in netloc:
|
||||||
host = netloc.split(']')[0][1:].lower()
|
host = netloc.split(']')[0][1:]
|
||||||
|
port = (netloc.split(']:') + [default_port])[1]
|
||||||
elif ':' in netloc:
|
elif ':' in netloc:
|
||||||
host = netloc.split(':')[0].lower()
|
host, port = (netloc.split(':') + [default_port])[:2]
|
||||||
elif netloc == "":
|
elif netloc == "":
|
||||||
host = "0.0.0.0"
|
host, port = "0.0.0.0", default_port
|
||||||
else:
|
|
||||||
host = netloc.lower()
|
|
||||||
|
|
||||||
#get port
|
try:
|
||||||
netloc = netloc.split(']')[-1]
|
|
||||||
if ":" in netloc:
|
|
||||||
port = netloc.split(':', 1)[1]
|
|
||||||
if not port.isdigit():
|
|
||||||
raise RuntimeError("%r is not a valid port number." % port)
|
|
||||||
port = int(port)
|
port = int(port)
|
||||||
else:
|
except ValueError:
|
||||||
port = default_port
|
raise RuntimeError("%r is not a valid port number." % port)
|
||||||
return (host, port)
|
|
||||||
|
return host.lower(), port
|
||||||
|
|
||||||
|
|
||||||
def close_on_exec(fd):
|
def close_on_exec(fd):
|
||||||
|
|||||||
@ -13,8 +13,12 @@ from urllib.parse import SplitResult
|
|||||||
@pytest.mark.parametrize('test_input, expected', [
|
@pytest.mark.parametrize('test_input, expected', [
|
||||||
('unix://var/run/test.sock', 'var/run/test.sock'),
|
('unix://var/run/test.sock', 'var/run/test.sock'),
|
||||||
('unix:/var/run/test.sock', '/var/run/test.sock'),
|
('unix:/var/run/test.sock', '/var/run/test.sock'),
|
||||||
|
('tcp://localhost', ('localhost', 8000)),
|
||||||
|
('tcp://localhost:5000', ('localhost', 5000)),
|
||||||
('', ('0.0.0.0', 8000)),
|
('', ('0.0.0.0', 8000)),
|
||||||
('[::1]:8000', ('::1', 8000)),
|
('[::1]:8000', ('::1', 8000)),
|
||||||
|
('[::1]:5000', ('::1', 5000)),
|
||||||
|
('[::1]', ('::1', 8000)),
|
||||||
('localhost:8000', ('localhost', 8000)),
|
('localhost:8000', ('localhost', 8000)),
|
||||||
('127.0.0.1:8000', ('127.0.0.1', 8000)),
|
('127.0.0.1:8000', ('127.0.0.1', 8000)),
|
||||||
('localhost', ('localhost', 8000)),
|
('localhost', ('localhost', 8000)),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user