Changeset 623:6012fe93f78c in documentViewer for SrvTxtUtils.py


Ignore:
Timestamp:
Dec 15, 2014, 3:10:05 PM (9 years ago)
Author:
casties
Branch:
default
Message:

better scheme-less URL code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • SrvTxtUtils.py

    r622 r623  
    88import stat
    99import urllib
     10from urlparse import urlparse, urlunparse
    1011import logging
    1112import time
     
    2223import xml.etree.ElementTree as ET
    2324
    24 srvTxtUtilsVersion = "1.12.3"
     25srvTxtUtilsVersion = "1.13"
    2526
    2627map_months = {'en': [u"",
     
    371372
    372373
     374def sslifyUrl(url, app=None, force=False):
     375    """returns URL with http or https scheme.
     376   
     377    Looks at app.REQUEST.URL to find the scheme of the current page.
     378    Changes only schemeless (starting with //) URLs unless force=True. 
     379    """
     380    thatUrl = urlparse(url)
     381    if hasattr(app, 'REQUEST'):
     382        # get current page URL
     383        thisUrl = urlparse(app.REQUEST['URL'])
     384        if thatUrl.scheme == '':
     385            # schemeless URL -> use this scheme
     386            return "%s:%s"%(thisUrl.scheme, url)
     387        elif force:
     388            # use this scheme
     389            if thisUrl.scheme != thatUrl.scheme:
     390                return urlunparse((thisUrl.scheme,)+thatUrl[1:])
     391            else:
     392                # keep scheme
     393                return url
     394           
     395        else:
     396            # keep scheme
     397            return url
     398       
     399    else:
     400        # no current page URL
     401        if force:
     402            # use https for force
     403            return urlunparse(('https',)+thatUrl[1:])
     404       
     405    return url
Note: See TracChangeset for help on using the changeset viewer.