1+ import ipaddress
12import os
23import io
34import json
@@ -995,6 +996,28 @@ def _parse_host(host: Optional[str]) -> str:
995996 'https://example.com:56789/path'
996997 >>> _parse_host('example.com:56789/path/')
997998 'http://example.com:56789/path'
999+ >>> _parse_host('[0001:002:003:0004::1]')
1000+ 'http://[0001:002:003:0004::1]:11434'
1001+ >>> _parse_host('[0001:002:003:0004::1]:56789')
1002+ 'http://[0001:002:003:0004::1]:56789'
1003+ >>> _parse_host('http://[0001:002:003:0004::1]')
1004+ 'http://[0001:002:003:0004::1]:80'
1005+ >>> _parse_host('https://[0001:002:003:0004::1]')
1006+ 'https://[0001:002:003:0004::1]:443'
1007+ >>> _parse_host('https://[0001:002:003:0004::1]:56789')
1008+ 'https://[0001:002:003:0004::1]:56789'
1009+ >>> _parse_host('[0001:002:003:0004::1]/')
1010+ 'http://[0001:002:003:0004::1]:11434'
1011+ >>> _parse_host('[0001:002:003:0004::1]:56789/')
1012+ 'http://[0001:002:003:0004::1]:56789'
1013+ >>> _parse_host('[0001:002:003:0004::1]/path')
1014+ 'http://[0001:002:003:0004::1]:11434/path'
1015+ >>> _parse_host('[0001:002:003:0004::1]:56789/path')
1016+ 'http://[0001:002:003:0004::1]:56789/path'
1017+ >>> _parse_host('https://[0001:002:003:0004::1]:56789/path')
1018+ 'https://[0001:002:003:0004::1]:56789/path'
1019+ >>> _parse_host('[0001:002:003:0004::1]:56789/path/')
1020+ 'http://[0001:002:003:0004::1]:56789/path'
9981021 """
9991022
10001023 host , port = host or '' , 11434
@@ -1010,6 +1033,13 @@ def _parse_host(host: Optional[str]) -> str:
10101033 host = split .hostname or '127.0.0.1'
10111034 port = split .port or port
10121035
1036+ # Fix missing square brackets for IPv6 from urlsplit
1037+ try :
1038+ if isinstance (ipaddress .ip_address (host ), ipaddress .IPv6Address ):
1039+ host = f'[{ host } ]'
1040+ except ValueError :
1041+ ...
1042+
10131043 if path := split .path .strip ('/' ):
10141044 return f'{ scheme } ://{ host } :{ port } /{ path } '
10151045
0 commit comments