function _origin_url
Removes the protocol scheme (http/https) from a URL string, returning only the domain and path portion.
/tf/active/vicechatdev/patches/server.py
61 - 64
simple
Purpose
This utility function normalizes URLs by stripping the protocol prefix. It's designed to extract the origin portion of a URL (everything after 'http://' or 'https://') for comparison, storage, or display purposes where the protocol is not needed. Commonly used in web server configurations, URL routing, or when comparing URLs regardless of their protocol.
Source Code
def _origin_url(url):
if url.startswith("http"):
url = url.split("//")[1]
return url
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
url |
- | - | positional_or_keyword |
Parameter Details
url: A string representing a URL. Can be a full URL with protocol (e.g., 'http://example.com/path') or a URL without protocol (e.g., 'example.com/path'). If the URL starts with 'http', the function will remove the protocol portion; otherwise, it returns the URL unchanged.
Return Value
Returns a string containing the URL without the protocol scheme. If the input starts with 'http', returns everything after the '//' delimiter (e.g., 'example.com/path'). If the input doesn't start with 'http', returns the original input unchanged. The return value includes the domain, port (if present), path, query parameters, and fragment.
Usage Example
# Example 1: URL with http protocol
url1 = 'http://example.com/path'
result1 = _origin_url(url1)
print(result1) # Output: 'example.com/path'
# Example 2: URL with https protocol
url2 = 'https://api.example.com:8080/endpoint?param=value'
result2 = _origin_url(url2)
print(result2) # Output: 'api.example.com:8080/endpoint?param=value'
# Example 3: URL without protocol
url3 = 'example.com/path'
result3 = _origin_url(url3)
print(result3) # Output: 'example.com/path'
# Example 4: Relative path
url4 = '/relative/path'
result4 = _origin_url(url4)
print(result4) # Output: '/relative/path'
Best Practices
- This function uses a simple string split approach rather than proper URL parsing, which may not handle all edge cases correctly (e.g., URLs with multiple '//' in the path)
- Consider using urllib.parse.urlparse for more robust URL parsing if handling complex or untrusted URLs
- The function assumes that if a URL starts with 'http', it will have a '//' delimiter, which is true for standard HTTP/HTTPS URLs but may fail for malformed URLs
- Does not validate whether the input is a valid URL - it performs a simple string operation
- The function name starts with underscore, suggesting it's intended for internal use within a module
- For production code, consider adding input validation and error handling for edge cases like empty strings or None values
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function _server_url 57.7% similar
-
function clean_text 44.2% similar
-
function sanitize_filename 42.0% similar
-
function capitalize_unicode_name 41.4% similar
-
function validate_sharepoint_url 41.3% similar