Mercurial > hg > documentViewer
diff SrvTxtUtils.py @ 623:6012fe93f78c
better scheme-less URL code.
author | casties |
---|---|
date | Mon, 15 Dec 2014 16:10:05 +0100 |
parents | bc68ca0d2c0a |
children | 447251b5af65 |
line wrap: on
line diff
--- a/SrvTxtUtils.py Fri Dec 12 16:27:58 2014 +0100 +++ b/SrvTxtUtils.py Mon Dec 15 16:10:05 2014 +0100 @@ -7,6 +7,7 @@ import os import stat import urllib +from urlparse import urlparse, urlunparse import logging import time import re @@ -21,7 +22,7 @@ import xml.etree.ElementTree as ET -srvTxtUtilsVersion = "1.12.3" +srvTxtUtilsVersion = "1.13" map_months = {'en': [u"", u"January", @@ -370,3 +371,35 @@ return s +def sslifyUrl(url, app=None, force=False): + """returns URL with http or https scheme. + + Looks at app.REQUEST.URL to find the scheme of the current page. + Changes only schemeless (starting with //) URLs unless force=True. + """ + thatUrl = urlparse(url) + if hasattr(app, 'REQUEST'): + # get current page URL + thisUrl = urlparse(app.REQUEST['URL']) + if thatUrl.scheme == '': + # schemeless URL -> use this scheme + return "%s:%s"%(thisUrl.scheme, url) + elif force: + # use this scheme + if thisUrl.scheme != thatUrl.scheme: + return urlunparse((thisUrl.scheme,)+thatUrl[1:]) + else: + # keep scheme + return url + + else: + # keep scheme + return url + + else: + # no current page URL + if force: + # use https for force + return urlunparse(('https',)+thatUrl[1:]) + + return url