1
+ import ipaddress
1
2
import os
2
3
import io
3
4
import json
@@ -995,6 +996,28 @@ def _parse_host(host: Optional[str]) -> str:
995
996
'https://example.com:56789/path'
996
997
>>> _parse_host('example.com:56789/path/')
997
998
'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'
998
1021
"""
999
1022
1000
1023
host , port = host or '' , 11434
@@ -1010,6 +1033,13 @@ def _parse_host(host: Optional[str]) -> str:
1010
1033
host = split .hostname or '127.0.0.1'
1011
1034
port = split .port or port
1012
1035
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
+
1013
1043
if path := split .path .strip ('/' ):
1014
1044
return f'{ scheme } ://{ host } :{ port } /{ path } '
1015
1045
0 commit comments