Browse Source

fix urllib deprecation warning, base64 encoding in _build_request() (#1490)

* fix urllib deprecation warning, base64 encoding in _build_request()

* set user_passwd from urllib supplied values
pull/1493/head
jcfp 5 years ago
committed by GitHub
parent
commit
c6ccffd1df
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      sabnzbd/urlgrabber.py

20
sabnzbd/urlgrabber.py

@ -29,6 +29,7 @@ import urllib.error
import urllib.parse import urllib.parse
from http.client import IncompleteRead from http.client import IncompleteRead
from threading import Thread from threading import Thread
import base64
import sabnzbd import sabnzbd
from sabnzbd.constants import DEF_TIMEOUT, FUTURE_Q_FOLDER, VALID_NZB_FILES, Status from sabnzbd.constants import DEF_TIMEOUT, FUTURE_Q_FOLDER, VALID_NZB_FILES, Status
@ -40,6 +41,7 @@ from sabnzbd.postproc import PostProcessor
import sabnzbd.cfg as cfg import sabnzbd.cfg as cfg
import sabnzbd.emailer as emailer import sabnzbd.emailer as emailer
import sabnzbd.notifier as notifier import sabnzbd.notifier as notifier
from sabnzbd.encoding import ubtou, utob
_BAD_GZ_HOSTS = ('.zip', 'nzbsa.co.za', 'newshost.za.net') _BAD_GZ_HOSTS = ('.zip', 'nzbsa.co.za', 'newshost.za.net')
@ -325,12 +327,16 @@ def _build_request(url):
# Detect basic auth # Detect basic auth
# Adapted from python-feedparser # Adapted from python-feedparser
user_passwd = None user_passwd = None
urltype, rest = urllib.parse.splittype(url) u = urllib.parse.urlparse(url)
realhost, rest = urllib.parse.splithost(rest) if u.username or u.password:
if realhost: if u.username and u.password:
user_passwd, realhost = urllib.parse.splituser(realhost) user_passwd = '%s:%s' % (u.username, u.password)
if user_passwd: host_port = u.hostname
url = '%s://%s%s' % (urltype, realhost, rest) if u.port:
host_port += ':' + str(u.port)
url = urllib.parse.urlunparse(
urllib.parse.ParseResult(u.scheme, host_port, u.path, u.params, u.query, u.fragment)
)
# Start request # Start request
req = urllib.request.Request(url) req = urllib.request.Request(url)
@ -340,7 +346,7 @@ def _build_request(url):
if not any(item in url for item in _BAD_GZ_HOSTS): if not any(item in url for item in _BAD_GZ_HOSTS):
req.add_header('Accept-encoding', 'gzip') req.add_header('Accept-encoding', 'gzip')
if user_passwd: if user_passwd:
req.add_header('Authorization', 'Basic ' + user_passwd.encode('base64').strip()) req.add_header('Authorization', 'Basic ' + ubtou(base64.b64encode(utob(user_passwd))).strip())
return urllib.request.urlopen(req) return urllib.request.urlopen(req)

Loading…
Cancel
Save