🔍 Code Extractor

function _origin_url

Maturity: 22

Removes the protocol scheme (http/https) from a URL string, returning only the domain and path portion.

File:
/tf/active/vicechatdev/patches/server.py
Lines:
61 - 64
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function _server_url 57.7% similar

    Constructs a properly formatted server URL by combining a base URL with a port number, handling both HTTP-prefixed and non-prefixed URLs.

    From: /tf/active/vicechatdev/patches/server.py
  • function clean_text 44.2% similar

    Cleans and normalizes text content by removing HTML tags, normalizing whitespace, and stripping markdown formatting elements.

    From: /tf/active/vicechatdev/improved_convert_disclosures_to_table.py
  • function sanitize_filename 42.0% similar

    Sanitizes a filename string by replacing invalid filesystem characters with underscores and ensuring a valid output.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function capitalize_unicode_name 41.4% similar

    Transforms Unicode character name strings by removing the word 'capital' and capitalizing the following word, converting strings like 'capital delta' to 'Delta'.

    From: /tf/active/vicechatdev/patches/util.py
  • function validate_sharepoint_url 41.3% similar

    Validates that a given URL string conforms to SharePoint site URL format requirements, checking for proper protocol, domain, and path structure.

    From: /tf/active/vicechatdev/SPFCsync/validate_config.py
← Back to Browse