Initialize Addr from string. :param str str_addr: nginx address string :returns: parsed nginx address :rtype: Addr :raises SocketAddrError: if a UNIX-domain socket address is given
(cls, str_addr: str)
| 54 | |
| 55 | @classmethod |
| 56 | def fromstring(cls, str_addr: str) -> "Addr": |
| 57 | """Initialize Addr from string. |
| 58 | |
| 59 | :param str str_addr: nginx address string |
| 60 | :returns: parsed nginx address |
| 61 | :rtype: Addr |
| 62 | :raises SocketAddrError: if a UNIX-domain socket address is given |
| 63 | |
| 64 | """ |
| 65 | parts = str_addr.split(' ') |
| 66 | ssl = False |
| 67 | default = False |
| 68 | ipv6 = False |
| 69 | ipv6only = False |
| 70 | host = '' |
| 71 | port = '' |
| 72 | |
| 73 | # The first part must be the address |
| 74 | addr = parts.pop(0) |
| 75 | |
| 76 | # Raise for UNIX-domain sockets |
| 77 | if addr.startswith('unix:'): |
| 78 | raise SocketAddrError(f'encountered UNIX-domain socket address {str_addr}') |
| 79 | |
| 80 | # IPv6 check |
| 81 | ipv6_match = re.match(r'\[.*\]', addr) |
| 82 | if ipv6_match: |
| 83 | ipv6 = True |
| 84 | # IPv6 handling |
| 85 | host = ipv6_match.group() |
| 86 | # The rest of the addr string will be the port, if any |
| 87 | port = addr[ipv6_match.end()+1:] |
| 88 | else: |
| 89 | # IPv4 handling |
| 90 | tup = addr.partition(':') |
| 91 | if re.match(r'^\d+$', tup[0]): |
| 92 | # This is a bare port, not a hostname. E.g. listen 80 |
| 93 | host = '' |
| 94 | port = tup[0] |
| 95 | else: |
| 96 | # This is a host-port tuple. E.g. listen 127.0.0.1:* |
| 97 | host = tup[0] |
| 98 | port = tup[2] |
| 99 | |
| 100 | # The rest of the parts are options; we only care about ssl and default |
| 101 | while parts: |
| 102 | nextpart = parts.pop() |
| 103 | if nextpart == 'ssl': |
| 104 | ssl = True |
| 105 | elif nextpart == 'default_server': |
| 106 | default = True |
| 107 | elif nextpart == 'default': |
| 108 | default = True |
| 109 | elif nextpart == "ipv6only=on": |
| 110 | ipv6only = True |
| 111 | |
| 112 | return cls(host, port, ssl, default, ipv6, ipv6only) |
| 113 |