Path validation rules that depend on if the URL contains a scheme or authority component. See https://datatracker.ietf.org/doc/html/rfc3986.html#section-3.3
(path: str, has_scheme: bool, has_authority: bool)
| 420 | |
| 421 | |
| 422 | def validate_path(path: str, has_scheme: bool, has_authority: bool) -> None: |
| 423 | """ |
| 424 | Path validation rules that depend on if the URL contains |
| 425 | a scheme or authority component. |
| 426 | |
| 427 | See https://datatracker.ietf.org/doc/html/rfc3986.html#section-3.3 |
| 428 | """ |
| 429 | if has_authority: |
| 430 | # If a URI contains an authority component, then the path component |
| 431 | # must either be empty or begin with a slash ("/") character." |
| 432 | if path and not path.startswith("/"): |
| 433 | raise InvalidURL("For absolute URLs, path must be empty or begin with '/'") |
| 434 | |
| 435 | if not has_scheme and not has_authority: |
| 436 | # If a URI does not contain an authority component, then the path cannot begin |
| 437 | # with two slash characters ("//"). |
| 438 | if path.startswith("//"): |
| 439 | raise InvalidURL("Relative URLs cannot have a path starting with '//'") |
| 440 | |
| 441 | # In addition, a URI reference (Section 4.1) may be a relative-path reference, |
| 442 | # in which case the first path segment cannot contain a colon (":") character. |
| 443 | if path.startswith(":"): |
| 444 | raise InvalidURL("Relative URLs cannot have a path starting with ':'") |
| 445 | |
| 446 | |
| 447 | def normalize_path(path: str) -> str: |