Parse an allowlist entry into (scheme, hostname, port). ``port`` is the literal string ``'*'`` if the entry uses the port wildcard (e.g. ``http://localhost:*``), otherwise an int. Returns None if the entry is malformed.
(entry)
| 104 | |
| 105 | |
| 106 | def _parse_allowlist_entry(entry): |
| 107 | """ |
| 108 | Parse an allowlist entry into (scheme, hostname, port). |
| 109 | |
| 110 | ``port`` is the literal string ``'*'`` if the entry uses the |
| 111 | port wildcard (e.g. ``http://localhost:*``), otherwise an int. |
| 112 | |
| 113 | Returns None if the entry is malformed. |
| 114 | """ |
| 115 | from urllib.parse import urlparse |
| 116 | |
| 117 | port_wildcard = False |
| 118 | raw = entry |
| 119 | if raw.endswith(':*'): |
| 120 | port_wildcard = True |
| 121 | raw = raw[:-2] |
| 122 | |
| 123 | parsed = urlparse(raw) |
| 124 | scheme = parsed.scheme.lower() |
| 125 | hostname = parsed.hostname |
| 126 | if hostname: |
| 127 | hostname = hostname.lower() |
| 128 | |
| 129 | if not scheme or not hostname or scheme not in ('http', 'https'): |
| 130 | return None |
| 131 | |
| 132 | if port_wildcard: |
| 133 | return scheme, hostname, '*' |
| 134 | |
| 135 | try: |
| 136 | port = parsed.port |
| 137 | except ValueError: |
| 138 | return None |
| 139 | if port is None: |
| 140 | port = 443 if scheme == 'https' else 80 |
| 141 | return scheme, hostname, port |
| 142 | |
| 143 | |
| 144 | def validate_api_url(url): |