mirror of
				https://github.com/yt-dlp/yt-dlp.git
				synced 2025-10-31 14:45:14 +00:00 
			
		
		
		
	[utils] Correct per-request proxy handling
This commit is contained in:
		| @@ -1771,7 +1771,8 @@ class YoutubeDL(object): | |||||||
|         https_handler = make_HTTPS_handler(self.params, debuglevel=debuglevel) |         https_handler = make_HTTPS_handler(self.params, debuglevel=debuglevel) | ||||||
|         ydlh = YoutubeDLHandler(self.params, debuglevel=debuglevel) |         ydlh = YoutubeDLHandler(self.params, debuglevel=debuglevel) | ||||||
|         opener = compat_urllib_request.build_opener( |         opener = compat_urllib_request.build_opener( | ||||||
|             https_handler, proxy_handler, cookie_processor, ydlh) |             proxy_handler, https_handler, cookie_processor, ydlh) | ||||||
|  |  | ||||||
|         # Delete the default user-agent header, which would otherwise apply in |         # Delete the default user-agent header, which would otherwise apply in | ||||||
|         # cases where our custom HTTP handler doesn't come into play |         # cases where our custom HTTP handler doesn't come into play | ||||||
|         # (See https://github.com/rg3/youtube-dl/issues/1309 for details) |         # (See https://github.com/rg3/youtube-dl/issues/1309 for details) | ||||||
|   | |||||||
| @@ -40,9 +40,6 @@ class LetvIE(InfoExtractor): | |||||||
|             'title': '美人天下01', |             'title': '美人天下01', | ||||||
|             'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda', |             'description': 'md5:f88573d9d7225ada1359eaf0dbf8bcda', | ||||||
|         }, |         }, | ||||||
|         'expected_warnings': [ |  | ||||||
|             'publish time' |  | ||||||
|         ] |  | ||||||
|     }, { |     }, { | ||||||
|         'note': 'This video is available only in Mainland China, thus a proxy is needed', |         'note': 'This video is available only in Mainland China, thus a proxy is needed', | ||||||
|         'url': 'http://www.letv.com/ptv/vplay/1118082.html', |         'url': 'http://www.letv.com/ptv/vplay/1118082.html', | ||||||
| @@ -53,11 +50,8 @@ class LetvIE(InfoExtractor): | |||||||
|             'title': '与龙共舞 完整版', |             'title': '与龙共舞 完整版', | ||||||
|             'description': 'md5:7506a5eeb1722bb9d4068f85024e3986', |             'description': 'md5:7506a5eeb1722bb9d4068f85024e3986', | ||||||
|         }, |         }, | ||||||
|         'expected_warnings': [ |  | ||||||
|             'publish time' |  | ||||||
|         ], |  | ||||||
|         'params': { |         'params': { | ||||||
|             'cn_verification_proxy': 'proxy.uku.im:8888' |             'cn_verification_proxy': 'http://proxy.uku.im:8888' | ||||||
|         }, |         }, | ||||||
|     }] |     }] | ||||||
|  |  | ||||||
| @@ -95,7 +89,7 @@ class LetvIE(InfoExtractor): | |||||||
|             'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params) |             'http://api.letv.com/mms/out/video/playJson?' + compat_urllib_parse.urlencode(params) | ||||||
|         ) |         ) | ||||||
|         play_json_req.add_header( |         play_json_req.add_header( | ||||||
|             'Ytdl-Request-Proxy', |             'Ytdl-request-proxy', | ||||||
|             self._downloader.params.get('cn_verification_proxy')) |             self._downloader.params.get('cn_verification_proxy')) | ||||||
|         play_json = self._download_json( |         play_json = self._download_json( | ||||||
|             play_json_req, |             play_json_req, | ||||||
|   | |||||||
| @@ -1771,10 +1771,21 @@ def match_filter_func(filter_str): | |||||||
|  |  | ||||||
|  |  | ||||||
| class PerRequestProxyHandler(compat_urllib_request.ProxyHandler): | class PerRequestProxyHandler(compat_urllib_request.ProxyHandler): | ||||||
|  |     def __init__(self, proxies=None): | ||||||
|  |         # Set default handlers | ||||||
|  |         for type in ('http', 'https'): | ||||||
|  |             setattr(self, '%s_open' % type, | ||||||
|  |                     lambda r, proxy='__noproxy__', type=type, meth=self.proxy_open: | ||||||
|  |                         meth(r, proxy, type)) | ||||||
|  |         return compat_urllib_request.ProxyHandler.__init__(self, proxies) | ||||||
|  |  | ||||||
|     def proxy_open(self, req, proxy, type): |     def proxy_open(self, req, proxy, type): | ||||||
|         req_proxy = req.headers.get('Ytdl-Request-Proxy') |         req_proxy = req.headers.get('Ytdl-request-proxy') | ||||||
|         if req_proxy is not None: |         if req_proxy is not None: | ||||||
|             proxy = req_proxy |             proxy = req_proxy | ||||||
|             del req.headers['Ytdl-Request-Proxy'] |             del req.headers['Ytdl-request-proxy'] | ||||||
|  |  | ||||||
|  |         if proxy == '__noproxy__': | ||||||
|  |             return None  # No Proxy | ||||||
|         return compat_urllib_request.ProxyHandler.proxy_open( |         return compat_urllib_request.ProxyHandler.proxy_open( | ||||||
|             self, req, proxy, type) |             self, req, proxy, type) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Philipp Hagemeister
					Philipp Hagemeister