Esempio n. 1
0
File: string.py Progetto: rsms/smisk
def normalize_url(url, ref_base_url=None):
  '''
  :param url:
    An absolute URL, absolute path or relative path.
  :type  url:
    URL | string
  :param ref_base_url:
    Default absolute URL used to expand a path to a full URL.
    Uses ``smisk.core.request.url`` if not set.
  :type  ref_base_url:
    URL
  :rtype:
    URL
  '''
  is_relative_uri = False
  if '/' not in url:
    is_relative_uri = True
    u = URL()
    u.path = url
    url = u
  else:
    url = URL(url) # make a copy so we don't modify the original
  
  if not ref_base_url:
    if Application.current and Application.current.request:
      ref_base_url = Application.current.request.url
    else:
      ref_base_url = URL()
  
  if url.scheme is None:
    url.scheme = ref_base_url.scheme
  if url.user is None:
    url.user = ref_base_url.user
    if url.user is not None and url.password is None:
      url.password = ref_base_url.password
  if url.host is None:
    url.host = ref_base_url.host
  if url.port == 0:
    url.port = ref_base_url.port
  if is_relative_uri:
    base_path = ref_base_url.path
    if not base_path:
      base_path = '/'
    elif not base_path.endswith('/'):
      base_path = base_path + '/'
    url.path = base_path + url.path
  
  return url