mirror of
https://github.com/yt-dlp/yt-dlp
synced 2024-12-26 21:59:08 +01:00
[networking] Add extensions
attribute to Response
(#9756)
CurlCFFIRH now provides an `impersonate` field in its responses' extensions Authored by: bashonly
This commit is contained in:
parent
036e0d92c6
commit
bec9a59e8e
3 changed files with 34 additions and 1 deletions
|
@ -785,6 +785,25 @@ class TestHTTPImpersonateRequestHandler(TestRequestHandlerBase):
|
||||||
assert res.status == 200
|
assert res.status == 200
|
||||||
assert std_headers['user-agent'].lower() not in res.read().decode().lower()
|
assert std_headers['user-agent'].lower() not in res.read().decode().lower()
|
||||||
|
|
||||||
|
def test_response_extensions(self, handler):
|
||||||
|
with handler() as rh:
|
||||||
|
for target in rh.supported_targets:
|
||||||
|
request = Request(
|
||||||
|
f'http://127.0.0.1:{self.http_port}/gen_200', extensions={'impersonate': target})
|
||||||
|
res = validate_and_send(rh, request)
|
||||||
|
assert res.extensions['impersonate'] == rh._get_request_target(request)
|
||||||
|
|
||||||
|
def test_http_error_response_extensions(self, handler):
|
||||||
|
with handler() as rh:
|
||||||
|
for target in rh.supported_targets:
|
||||||
|
request = Request(
|
||||||
|
f'http://127.0.0.1:{self.http_port}/gen_404', extensions={'impersonate': target})
|
||||||
|
try:
|
||||||
|
validate_and_send(rh, request)
|
||||||
|
except HTTPError as e:
|
||||||
|
res = e.response
|
||||||
|
assert res.extensions['impersonate'] == rh._get_request_target(request)
|
||||||
|
|
||||||
|
|
||||||
class TestRequestHandlerMisc:
|
class TestRequestHandlerMisc:
|
||||||
"""Misc generic tests for request handlers, not related to request or validation testing"""
|
"""Misc generic tests for request handlers, not related to request or validation testing"""
|
||||||
|
|
|
@ -132,6 +132,16 @@ class CurlCFFIRH(ImpersonateRequestHandler, InstanceStoreMixin):
|
||||||
extensions.pop('cookiejar', None)
|
extensions.pop('cookiejar', None)
|
||||||
extensions.pop('timeout', None)
|
extensions.pop('timeout', None)
|
||||||
|
|
||||||
|
def send(self, request: Request) -> Response:
|
||||||
|
target = self._get_request_target(request)
|
||||||
|
try:
|
||||||
|
response = super().send(request)
|
||||||
|
except HTTPError as e:
|
||||||
|
e.response.extensions['impersonate'] = target
|
||||||
|
raise
|
||||||
|
response.extensions['impersonate'] = target
|
||||||
|
return response
|
||||||
|
|
||||||
def _send(self, request: Request):
|
def _send(self, request: Request):
|
||||||
max_redirects_exceeded = False
|
max_redirects_exceeded = False
|
||||||
session: curl_cffi.requests.Session = self._get_instance(
|
session: curl_cffi.requests.Session = self._get_instance(
|
||||||
|
|
|
@ -497,6 +497,7 @@ class Response(io.IOBase):
|
||||||
@param headers: response headers.
|
@param headers: response headers.
|
||||||
@param status: Response HTTP status code. Default is 200 OK.
|
@param status: Response HTTP status code. Default is 200 OK.
|
||||||
@param reason: HTTP status reason. Will use built-in reasons based on status code if not provided.
|
@param reason: HTTP status reason. Will use built-in reasons based on status code if not provided.
|
||||||
|
@param extensions: Dictionary of handler-specific response extensions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -505,7 +506,9 @@ class Response(io.IOBase):
|
||||||
url: str,
|
url: str,
|
||||||
headers: Mapping[str, str],
|
headers: Mapping[str, str],
|
||||||
status: int = 200,
|
status: int = 200,
|
||||||
reason: str = None):
|
reason: str = None,
|
||||||
|
extensions: dict = None
|
||||||
|
):
|
||||||
|
|
||||||
self.fp = fp
|
self.fp = fp
|
||||||
self.headers = Message()
|
self.headers = Message()
|
||||||
|
@ -517,6 +520,7 @@ class Response(io.IOBase):
|
||||||
self.reason = reason or HTTPStatus(status).phrase
|
self.reason = reason or HTTPStatus(status).phrase
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.reason = None
|
self.reason = None
|
||||||
|
self.extensions = extensions or {}
|
||||||
|
|
||||||
def readable(self):
|
def readable(self):
|
||||||
return self.fp.readable()
|
return self.fp.readable()
|
||||||
|
|
Loading…
Reference in a new issue