Skip to content

Commit

Permalink
cleanup after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
pukkandan committed Jun 24, 2022
1 parent eaca751 commit c21bc7d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion test/test_YoutubeDLCookieJar.py
Expand Up @@ -11,7 +11,7 @@
import re
import tempfile

from yt_dlp.utils import YoutubeDLCookieJar
from yt_dlp.cookies import YoutubeDLCookieJar


class TestYoutubeDLCookieJar(unittest.TestCase):
Expand Down
57 changes: 29 additions & 28 deletions test/test_networking.py
@@ -1,33 +1,34 @@
import functools
import gzip
import io
#!/usr/bin/env python3

# Allow direct execution
import os
import sys
import unittest
import urllib.request
from http.cookiejar import Cookie


from yt_dlp.networking import UrllibRH, UnsupportedRH, Request, HEADRequest
from yt_dlp.networking.utils import update_request
from yt_dlp.utils import HTTPError, SSLError, TransportError, IncompleteRead, urlencode_postdata

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from test.helper import http_server_port, FakeYDL
from yt_dlp import YoutubeDL
from yt_dlp.compat import compat_http_server
import functools
import gzip
import http.client
import http.server
import io
import ssl
import threading
import http.server
import http.client
import urllib.request
from http.cookiejar import Cookie

from test.helper import FakeYDL, http_server_port
from yt_dlp import YoutubeDL
from yt_dlp.networking import Request, UnsupportedRH, UrllibRH
from yt_dlp.networking.utils import update_request
from yt_dlp.utils import HTTPError, IncompleteRead, SSLError, urlencode_postdata

TEST_DIR = os.path.dirname(os.path.abspath(__file__))

HTTP_TEST_BACKEND_HANDLERS = [UrllibRH]


class FakeLogger(object):
class FakeLogger:
def debug(self, msg):
pass

Expand Down Expand Up @@ -67,7 +68,7 @@ def _method(self, method, payload=None):
self.wfile.write(payload)

def _status(self, status):
payload = f'<html>{status} NOT FOUND</html>'.encode('utf-8')
payload = f'<html>{status} NOT FOUND</html>'.encode()
self.send_response(int(status))
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('Content-Length', str(len(payload)))
Expand Down Expand Up @@ -170,7 +171,7 @@ def do_GET(self):
with gzip.GzipFile(fileobj=buf, mode='wb') as f:
f.write(payload)
compressed = buf.getvalue()
self.send_header('Content-Length', str(len(compressed)+len(b'trailing garbage')))
self.send_header('Content-Length', str(len(compressed) + len(b'trailing garbage')))
self.end_headers()
self.wfile.write(compressed + b'trailing garbage')
elif self.path == '/302-non-ascii-redirect':
Expand All @@ -194,11 +195,11 @@ def send_header(self, keyword, value):
if not hasattr(self, '_headers_buffer'):
self._headers_buffer = []

self._headers_buffer.append(f'{keyword}: {value}\r\n'.encode('utf-8'))
self._headers_buffer.append(f'{keyword}: {value}\r\n'.encode())


def _build_proxy_handler(name):
class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
class HTTPTestRequestHandler(http.server.BaseHTTPRequestHandler):
proxy_name = name

def log_message(self, format, *args):
Expand Down Expand Up @@ -237,7 +238,7 @@ def setUp(self):

# HTTPS server
certfn = os.path.join(TEST_DIR, 'testcert.pem')
self.https_httpd = compat_http_server.ThreadingHTTPServer(
self.https_httpd = http.server.ThreadingHTTPServer(
('127.0.0.1', 0), HTTPTestRequestHandler)
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
sslctx.load_cert_chain(certfn, None)
Expand Down Expand Up @@ -273,22 +274,22 @@ def test_nocheckcertificate(self):
self.assertEqual(r['entries'][0]['url'], 'https://127.0.0.1:%d/vid.mp4' % self.https_port)

def test_http_proxy(self):
geo_proxy = '127.0.0.1:{0}'.format(self.geo_port)
geo_proxy2 = 'localhost:{0}'.format(self.geo_port) # ensure backend can support this format
geo_proxy = f'127.0.0.1:{self.geo_port}'
geo_proxy2 = f'localhost:{self.geo_port}' # ensure backend can support this format

with self.make_ydl({
'proxy': '//127.0.0.1:{0}'.format(self.proxy_port),
'proxy': f'//127.0.0.1:{self.proxy_port}',
'geo_verification_proxy': geo_proxy,
}) as ydl:
url = 'http://foo.com/bar'
response = ydl.urlopen(url).read().decode('utf-8')
self.assertEqual(response, 'normal: {0}'.format(url))
self.assertEqual(response, f'normal: {url}')
req = Request(url)
req.add_header('Ytdl-request-proxy', geo_proxy2)
response1 = ydl.urlopen(req).read().decode('utf-8')
response2 = ydl.urlopen(Request(url, proxies={'http': geo_proxy})).read().decode('utf-8')
self.assertEqual(response1, 'geo: {0}'.format(url))
self.assertEqual(response2, 'geo: {0}'.format(url))
self.assertEqual(response1, f'geo: {url}')
self.assertEqual(response2, f'geo: {url}')
# test that __noproxy__ disables all proxies for that request
real_url = 'http://127.0.0.1:%d/headers' % self.http_port
response3 = ydl.urlopen(
Expand All @@ -299,7 +300,7 @@ def test_http_proxy(self):

def test_http_proxy_with_idn(self):
with self.make_ydl({
'proxy': '127.0.0.1:{0}'.format(self.proxy_port),
'proxy': f'127.0.0.1:{self.proxy_port}',
}) as ydl:
url = 'http://中文.tw/'
response = ydl.urlopen(url).read().decode('utf-8')
Expand Down
6 changes: 5 additions & 1 deletion test/test_networking_utils.py
@@ -1,11 +1,15 @@
#!/usr/bin/env python3

# Allow direct execution
import os
import sys
import unittest
from yt_dlp.networking.utils import select_proxy

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


from yt_dlp.networking.utils import select_proxy

TEST_DIR = os.path.dirname(os.path.abspath(__file__))


Expand Down
1 change: 1 addition & 0 deletions yt_dlp/YoutubeDL.py
Expand Up @@ -3700,6 +3700,7 @@ def python_implementation():
'You are using an outdated version (newest version: %s)! '
'See https://yt-dl.org/update if you need help updating.' %
latest_version)

@property
def proxies(self) -> dict:
"""Global proxy configuration"""
Expand Down
2 changes: 2 additions & 0 deletions yt_dlp/cookies.py
@@ -1,3 +1,5 @@
from __future__ import annotations

import base64
import collections
import contextlib
Expand Down
2 changes: 2 additions & 0 deletions yt_dlp/networking/__init__.py
@@ -1,4 +1,5 @@
from __future__ import annotations

import urllib.parse

from ._urllib import UrllibRH
Expand All @@ -20,6 +21,7 @@ class UnsupportedRH(BackendRH):
E.g. a dependency is required.
"""

def prepare_request(self, request: Request):
scheme = urllib.parse.urlparse(request.url).scheme.lower()
if scheme == 'file':
Expand Down
3 changes: 2 additions & 1 deletion yt_dlp/networking/_urllib.py
Expand Up @@ -391,6 +391,7 @@ class UrllibHTTPResponseAdapter(Response):
"""
HTTP Response adapter class for urllib addinfourl and http.client.HTTPResponse
"""

def __init__(self, res: Union[http.client.HTTPResponse, urllib.response.addinfourl]):
# addinfourl: In Python 3.9+, .status was introduced and .getcode() was deprecated [1]
# HTTPResponse: .getcode() was deprecated, .status always existed [2]
Expand Down Expand Up @@ -469,7 +470,7 @@ def file_open(*args, **kwargs):
opener = urllib.request.OpenerDirector()

handlers = [proxy_handler, cookie_processor, ydlh, redirect_handler, data_handler, file_handler,
UnknownHandler(), HTTPDefaultErrorHandler(), FTPHandler(), HTTPErrorProcessor()]
UnknownHandler(), HTTPDefaultErrorHandler(), FTPHandler(), HTTPErrorProcessor()]

for handler in handlers:
opener.add_handler(handler)
Expand Down
4 changes: 2 additions & 2 deletions yt_dlp/networking/common.py
@@ -1,6 +1,5 @@
from __future__ import annotations

import email.policy
import io
import ssl
import typing
Expand Down Expand Up @@ -48,6 +47,7 @@ class Request:
@param compression: whether to include content-encoding header on request.
@param timeout: socket timeout value for this request.
"""

def __init__(
self,
url: str,
Expand Down Expand Up @@ -407,7 +407,7 @@ def send(self, request: Union[Request, str, urllib.request.Request]) -> Response
except Exception as e:
# something went very wrong, try fallback to next handler
self.ydl.report_error(
f'Unexpected error from "{handler.name}" request handler' + bug_reports_message(),
f'Unexpected error from "{handler.name}" request handler: {e}' + bug_reports_message(),
is_error=False)
continue
except UnsupportedRequest as e:
Expand Down
7 changes: 5 additions & 2 deletions yt_dlp/utils.py
@@ -1,3 +1,5 @@
from __future__ import annotations

import atexit
import base64
import binascii
Expand Down Expand Up @@ -4958,6 +4960,7 @@ class CaseInsensitiveDict(collections.UserDict):
All keys are assumed to be a string, and are converted to title case.
Latter headers are prioritized in constructor
"""

def __init__(self, *args, **kwargs):
super().__init__()
for dct in args:
Expand Down Expand Up @@ -5133,7 +5136,7 @@ def YoutubeDLHTTPSHandler(*args, **kwargs):
# TODO: compat (moved to networking._urllib)
def YoutubeDLCookieProcessor(*args, **kwargs):
from .networking._urllib import YoutubeDLCookieProcessor
return YoutubeDLCookieProcessor(*args **kwargs)
return YoutubeDLCookieProcessor(*args, **kwargs)


# TODO: compat (moved to networking._urllib)
Expand Down Expand Up @@ -5161,7 +5164,7 @@ def update_Request(*args, **kwargs):


def YoutubeDLCookieJar(*args, **kwargs):
from cookies import YoutubeDLCookieJar
from .cookies import YoutubeDLCookieJar
return YoutubeDLCookieJar(*args, **kwargs)


Expand Down

0 comments on commit c21bc7d

Please sign in to comment.