Normalize URL to ensure it has a proper protocol. If no protocol is specified, default to https://
(url: str)
| 8 | |
| 9 | |
| 10 | def normalize_url(url: str) -> str: |
| 11 | """ |
| 12 | Normalize URL to ensure it has a proper protocol. |
| 13 | If no protocol is specified, default to https:// |
| 14 | """ |
| 15 | url = url.strip() |
| 16 | |
| 17 | # Parse the URL |
| 18 | parsed = urlparse(url) |
| 19 | |
| 20 | # Check if we have a scheme |
| 21 | if not parsed.scheme: |
| 22 | # No scheme, add https:// |
| 23 | url = f"https://{url}" |
| 24 | elif parsed.scheme in ['http', 'https']: |
| 25 | # Valid scheme, keep as is |
| 26 | pass |
| 27 | else: |
| 28 | # Check if this might be a domain with port (like example.com:8080) |
| 29 | # urlparse treats this as scheme:netloc, but we want to handle it as domain:port |
| 30 | if ':' in url and not url.startswith(('http://', 'https://', 'ftp://', 'file://')): |
| 31 | # Likely a domain:port without protocol |
| 32 | url = f"https://{url}" |
| 33 | else: |
| 34 | # Invalid protocol |
| 35 | raise ValueError(f"Unsupported protocol: {parsed.scheme}") |
| 36 | |
| 37 | return url |
| 38 | |
| 39 | |
| 40 | def bytes_to_data_url(image_bytes: bytes, mime_type: str) -> str: |
no outgoing calls