comparison SrvTxtUtils.py @ 623:6012fe93f78c

better scheme-less URL code.
author casties
date Mon, 15 Dec 2014 16:10:05 +0100
parents bc68ca0d2c0a
children 447251b5af65
comparison
equal deleted inserted replaced
622:bc68ca0d2c0a 623:6012fe93f78c
5 5
6 import sys 6 import sys
7 import os 7 import os
8 import stat 8 import stat
9 import urllib 9 import urllib
10 from urlparse import urlparse, urlunparse
10 import logging 11 import logging
11 import time 12 import time
12 import re 13 import re
13 import datetime 14 import datetime
14 try: 15 try:
19 import urllib2 20 import urllib2
20 httplib = 'urllib2' 21 httplib = 'urllib2'
21 22
22 import xml.etree.ElementTree as ET 23 import xml.etree.ElementTree as ET
23 24
24 srvTxtUtilsVersion = "1.12.3" 25 srvTxtUtilsVersion = "1.13"
25 26
26 map_months = {'en': [u"", 27 map_months = {'en': [u"",
27 u"January", 28 u"January",
28 u"February", 29 u"February",
29 u"March", 30 u"March",
368 return s.lower() 369 return s.lower()
369 370
370 return s 371 return s
371 372
372 373
374 def 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