Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pukkandan committed Jul 22, 2023
1 parent eb953e0 commit a9674a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
26 changes: 13 additions & 13 deletions test/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ def test_httplib_validation_errors(self, handler):
assert not isinstance(exc_info.value, TransportError)


def run_validation(handler, fail, req, **handler_kwargs):
def run_validation(handler, error, req, **handler_kwargs):
with handler(**handler_kwargs) as rh:
if fail:
with pytest.raises(UnsupportedRequest if type(fail) == bool else fail):
if error:
with pytest.raises(error):
rh.validate(req)
else:
rh.validate(req)
Expand Down Expand Up @@ -837,26 +837,26 @@ class HTTPSupportedRH(ValidationRH):
('https', False, {}),
('data', False, {}),
('ftp', False, {}),
('file', True, {}),
('file', UnsupportedRequest, {}),
('file', False, {'enable_file_urls': True}),
]),
(NoCheckRH, [('http', False, {})]),
(ValidationRH, [('http', True, {})])
(ValidationRH, [('http', UnsupportedRequest, {})])
]

PROXY_SCHEME_TESTS = [
# scheme, expected to fail
('Urllib', [
('http', False),
('https', True),
('https', UnsupportedRequest),
('socks4', False),
('socks4a', False),
('socks5', False),
('socks5h', False),
('socks', True),
('socks', UnsupportedRequest),
]),
(NoCheckRH, [('http', False)]),
(HTTPSupportedRH, [('http', True)]),
(HTTPSupportedRH, [('http', UnsupportedRequest)]),
]

PROXY_KEY_TESTS = [
Expand All @@ -866,8 +866,8 @@ class HTTPSupportedRH(ValidationRH):
('unrelated', False),
]),
(NoCheckRH, [('all', False)]),
(HTTPSupportedRH, [('all', True)]),
(HTTPSupportedRH, [('no', True)]),
(HTTPSupportedRH, [('all', UnsupportedRequest)]),
(HTTPSupportedRH, [('no', UnsupportedRequest)]),
]

EXTENSION_TESTS = [
Expand Down Expand Up @@ -924,7 +924,7 @@ def test_empty_proxy(self, handler):
@pytest.mark.parametrize('proxy_url', ['//example.com', 'example.com', '127.0.0.1'])
@pytest.mark.parametrize('handler', ['Urllib'], indirect=True)
def test_missing_proxy_scheme(self, handler, proxy_url):
run_validation(handler, True, Request('http://', proxies={'http': 'example.com'}))
run_validation(handler, UnsupportedRequest, Request('http://', proxies={'http': 'example.com'}))

@pytest.mark.parametrize('handler,extensions,fail', [
(handler_tests[0], extensions, fail)
Expand All @@ -951,8 +951,8 @@ def __init__(self, request):

class FakeRH(RequestHandler):

def _validate(self, request):
return
def validate(self, request):
assert isinstance(request, Request)

def _send(self, request: Request):
if request.url.startswith('ssl://'):
Expand Down
4 changes: 2 additions & 2 deletions yt_dlp/networking/_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
make_socks_proxy_opts,
select_proxy,
)
from .common import Features, RequestHandler, Response, register
from .common import Features, RequestHandler, Response, register_rh
from .exceptions import (
CertificateVerifyError,
HTTPError,
Expand Down Expand Up @@ -372,7 +372,7 @@ def handle_response_read_exceptions(e):
raise TransportError(cause=e) from e


@register
@register_rh
class UrllibRH(RequestHandler, InstanceStoreMixin):
_SUPPORTED_URL_SCHEMES = ('http', 'https', 'data', 'ftp')
_SUPPORTED_PROXY_SCHEMES = ('http', 'socks4', 'socks4a', 'socks5', 'socks5h')
Expand Down
26 changes: 12 additions & 14 deletions yt_dlp/networking/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from email.message import Message
from http import HTTPStatus
from http.cookiejar import CookieJar
from types import NoneType

from ._helper import make_ssl_context, wrap_request_errors
from .exceptions import (
Expand Down Expand Up @@ -105,7 +106,7 @@ def send(self, request: Request) -> Response:
_REQUEST_HANDLERS = {}


def register(handler):
def register_rh(handler):
"""Register a RequestHandler class"""
assert issubclass(handler, RequestHandler), f'{handler} must be a subclass of RequestHandler'
assert handler.RH_KEY not in _REQUEST_HANDLERS, f'RequestHandler {handler.RH_KEY} already registered'
Expand Down Expand Up @@ -139,7 +140,7 @@ class RequestHandler(abc.ABC):
If a Request is not supported by the handler, an UnsupportedRequest
should be raised with a reason.
By default, some checks are done on the request in _validate() based on the following class variables:
By default, some checks are done on the request in validate() based on the following class variables:
- `_SUPPORTED_URL_SCHEMES`: a tuple of supported url schemes.
Any Request with an url scheme not in this list will raise an UnsupportedRequest.
Expand Down Expand Up @@ -170,7 +171,7 @@ class RequestHandler(abc.ABC):
Requests may have additional optional parameters defined as extensions.
RequestHandler subclasses may choose to support custom extensions.
If an extension is supported, subclasses should override _check_extensions(extensions)
If an extension is supported, subclasses should extend _check_extensions(extensions)
to pop and validate the extension.
- Extensions left in `extensions` are treated as unsupported and UnsupportedRequest will be raised.
Expand Down Expand Up @@ -270,25 +271,22 @@ def _check_proxies(self, proxies):
raise UnsupportedRequest(f'Unsupported proxy type: "{scheme}"')

def _check_extensions(self, extensions):
"""Check extensions for unsupported extensions. Subclasses should override this."""
assert isinstance(extensions.get('cookiejar'), (CookieJar, type(None)))
assert isinstance(extensions.get('timeout'), (float, int, type(None)))
"""Check extensions for unsupported extensions. Subclasses should extend this."""
assert isinstance(extensions.get('cookiejar'), (CookieJar, NoneType))
assert isinstance(extensions.get('timeout'), (float, int, NoneType))

def _validate(self, request):
@wrap_request_errors
def validate(self, request: Request):
if not isinstance(request, Request):
raise TypeError('Expected an instance of Request')
self._check_url_scheme(request)
self._check_proxies(request.proxies or self.proxies)
extensions = request.extensions.copy()
self._check_extensions(extensions)
if extensions:
# TODO(future): add support for optional extensions
# TODO: add support for optional extensions
raise UnsupportedRequest(f'Unsupported extensions: {", ".join(extensions.keys())}')

@wrap_request_errors
def validate(self, request: Request):
if not isinstance(request, Request):
raise TypeError('Expected an instance of Request')
self._validate(request)

@wrap_request_errors
def send(self, request: Request) -> Response:
if not isinstance(request, Request):
Expand Down

0 comments on commit a9674a0

Please sign in to comment.