1010"""
1111
1212import base64
13+ import warnings
1314from copy import deepcopy
1415import calendar
1516from datetime import datetime
@@ -254,27 +255,25 @@ def get_self_url_host(request_data):
254255 :rtype: string
255256 """
256257 current_host = OneLogin_Saml2_Utils .get_self_host (request_data )
257- port = ''
258- if OneLogin_Saml2_Utils .is_https (request_data ):
259- protocol = 'https'
260- else :
261- protocol = 'http'
262-
263- if 'server_port' in request_data and request_data ['server_port' ] is not None :
264- port_number = str (request_data ['server_port' ])
265- port = ':' + port_number
258+ protocol = 'https' if OneLogin_Saml2_Utils .is_https (request_data ) else 'http'
266259
267- if protocol == 'http' and port_number == '80' :
268- port = ''
269- elif protocol == 'https' and port_number == '443' :
270- port = ''
260+ if request_data .get ('server_port' ) is not None :
261+ warnings .warn (
262+ 'The server_port key in request data is deprecated. '
263+ 'The http_host key should include a port, if required.' ,
264+ category = DeprecationWarning ,
265+ )
266+ port_suffix = ':%s' % request_data ['server_port' ]
267+ if not current_host .endswith (port_suffix ):
268+ if not ((protocol == 'https' and port_suffix == ':443' ) or (protocol == 'http' and port_suffix == ':80' )):
269+ current_host += port_suffix
271270
272- return '%s://%s%s ' % (protocol , current_host , port )
271+ return '%s://%s' % (protocol , current_host )
273272
274273 @staticmethod
275274 def get_self_host (request_data ):
276275 """
277- Returns the current host.
276+ Returns the current host (which may include a port number part) .
278277
279278 :param request_data: The request as a dict
280279 :type: dict
@@ -283,22 +282,11 @@ def get_self_host(request_data):
283282 :rtype: string
284283 """
285284 if 'http_host' in request_data :
286- current_host = request_data ['http_host' ]
285+ return request_data ['http_host' ]
287286 elif 'server_name' in request_data :
288- current_host = request_data ['server_name' ]
289- else :
290- raise Exception ('No hostname defined' )
291-
292- if ':' in current_host :
293- current_host_data = current_host .split (':' )
294- possible_port = current_host_data [- 1 ]
295- try :
296- int (possible_port )
297- current_host = current_host_data [0 ]
298- except ValueError :
299- current_host = ':' .join (current_host_data )
300-
301- return current_host
287+ warnings .warn ("The server_name key in request data is undocumented & deprecated." , category = DeprecationWarning )
288+ return request_data ['server_name' ]
289+ raise Exception ('No hostname defined' )
302290
303291 @staticmethod
304292 def is_https (request_data ):
@@ -312,6 +300,7 @@ def is_https(request_data):
312300 :rtype: boolean
313301 """
314302 is_https = 'https' in request_data and request_data ['https' ] != 'off'
303+ # TODO: this use of server_port should be removed too
315304 is_https = is_https or ('server_port' in request_data and str (request_data ['server_port' ]) == '443' )
316305 return is_https
317306
0 commit comments