MCPcopy
hub / github.com/abi/screenshot-to-code / normalize_url

Function normalize_url

backend/routes/screenshot.py:10–37  ·  view source on GitHub ↗

Normalize URL to ensure it has a proper protocol. If no protocol is specified, default to https://

(url: str)

Source from the content-addressed store, hash-verified

8
9
10def 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
40def bytes_to_data_url(image_bytes: bytes, mime_type: str) -> str:

Callers 10

test_localhost_urlsMethod · 0.90
test_ip_address_urlsMethod · 0.90
test_complex_urlsMethod · 0.90
app_screenshotFunction · 0.85

Calls

no outgoing calls