1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-11-30 06:01:50 +00:00

[utils] Fix update_Request() with empty data (not None)

This commit is contained in:
dirkf 2023-07-24 03:30:28 +01:00
parent 411652355f
commit 84ea70f370
2 changed files with 16 additions and 4 deletions

View file

@ -41,6 +41,7 @@ from youtube_dl.compat import (
from youtube_dl.utils import (
sanitized_Request,
update_Request,
urlencode_postdata,
)
@ -395,6 +396,18 @@ class TestHTTP(unittest.TestCase):
headers = ydl.urlopen(r).read().decode('utf-8')
self.assertIn('Content-Type: application/x-www-form-urlencoded', headers)
def test_update_req(self):
req = sanitized_Request('http://example.com')
assert req.data is None
assert req.get_method() == 'GET'
assert not req.has_header('Content-Type')
# Test that zero-byte payloads will be sent
req = update_Request(req, data=b'')
assert req.data == b''
assert req.get_method() == 'POST'
# yt-dl expects data to be encoded and Content-Type to be added by sender
# assert req.get_header('Content-Type') == 'application/x-www-form-urlencoded'
def test_cookiejar(self):
with FakeYDL() as ydl:
ydl.cookiejar.set_cookie(compat_http_cookiejar_Cookie(

View file

@ -2996,8 +2996,7 @@ class YoutubeDLRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
# Technically the Cookie header should be in unredirected_hdrs;
# however in practice some may set it in normal headers anyway.
# We will remove it here to prevent any leaks.
# Also remove unwanted and undocumented Host header for old URL
remove_headers = ['Cookie', 'Host']
remove_headers = ['Cookie']
# A 303 must either use GET or HEAD for subsequent request
# https://datatracker.ietf.org/doc/html/rfc7231#section-6.4.4
@ -3016,7 +3015,7 @@ class YoutubeDLRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
remove_headers.extend(['Content-Length', 'Content-Type'])
# NB: don't use dict comprehension for python 2.6 compatibility
new_headers = dict((k, v) for k, v in req.header_items()
new_headers = dict((k, v) for k, v in req.headers.items()
if k.title() not in remove_headers)
return compat_urllib_request.Request(
@ -4187,7 +4186,7 @@ def update_url_query(url, query):
def update_Request(req, url=None, data=None, headers={}, query={}):
req_headers = req.headers.copy()
req_headers.update(headers)
req_data = data or req.data
req_data = data if data is not None else req.data
req_url = update_url_query(url or req.get_full_url(), query)
req_get_method = req.get_method()
if req_get_method == 'HEAD':