From 0f81a080a8f1f7f68e06f78ed93049f97ea2fc36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Sat, 28 Sep 2019 22:31:43 +0200 Subject: [PATCH] Improve address normalization in connection manager --- .../lib/jellyfin/core/connection_manager.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/resources/lib/jellyfin/core/connection_manager.py b/resources/lib/jellyfin/core/connection_manager.py index 9875694c..410f9f80 100644 --- a/resources/lib/jellyfin/core/connection_manager.py +++ b/resources/lib/jellyfin/core/connection_manager.py @@ -4,12 +4,13 @@ import json import logging -import hashlib import socket import time from datetime import datetime from distutils.version import LooseVersion +import urllib3 + from credentials import Credentials from http import HTTP @@ -109,7 +110,7 @@ class ConnectionManager(object): if not server: raise AttributeError("server cannot be empty") - + try: request = { 'type': "POST", @@ -134,6 +135,8 @@ class ConnectionManager(object): if not address: return False + address = self._normalize_address(address) + def _on_fail(): LOG.error("connectToAddress %s failed", address) return self._resolve_failure() @@ -470,13 +473,18 @@ class ConnectionManager(object): def _normalize_address(self, address): # Attempt to correct bad input - address = address.strip() - address = address.lower() + url = urllib3.util.parse_url(address.strip()) - if 'http' not in address: - address = "http://%s" % address + if url.scheme is None: + url = url._replace(scheme='http') - return address + if url.scheme == 'http' and url.port == 80: + url = url._replace(port=None) + + if url.scheme == 'https' and url.port == 443: + url = url._replace(port=None) + + return url.url def _save_user_info_into_credentials(self, server, user):