4.0.5.4修复SessionPage的headers设置问题

This commit is contained in:
g1879 2024-06-28 11:29:32 +08:00
parent c5a9c1fbff
commit 989c558e05
2 changed files with 8 additions and 6 deletions

View File

@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.5.3' __version__ = '4.0.5.4'

View File

@ -292,23 +292,25 @@ class SessionPage(BasePage):
""" """
kwargs = CaseInsensitiveDict(kwargs) kwargs = CaseInsensitiveDict(kwargs)
if 'headers' not in kwargs: if 'headers' not in kwargs:
kwargs['headers'] = {} kwargs['headers'] = CaseInsensitiveDict()
else: else:
kwargs['headers'] = CaseInsensitiveDict(format_headers(kwargs['headers'])) kwargs['headers'] = CaseInsensitiveDict(format_headers(kwargs['headers']))
# 设置referer和host值 # 设置referer和host值
parsed_url = urlparse(url) parsed_url = urlparse(url)
hostname = parsed_url.hostname hostname = parsed_url.netloc
scheme = parsed_url.scheme scheme = parsed_url.scheme
if not check_headers(kwargs['headers'], self._headers, 'Referer'): if not check_headers(kwargs['headers'], self._headers, 'Referer'):
kwargs['headers']['Referer'] = self.url if self.url else f'{scheme}://{hostname}' kwargs['headers']['Referer'] = self.url if self.url else f'{scheme}://{hostname}'
if 'Host' not in kwargs['headers']: if not check_headers(kwargs['headers'], self._headers, 'Host'):
kwargs['headers']['Host'] = hostname kwargs['headers']['Host'] = hostname
if not check_headers(kwargs, self._headers, 'timeout'): if not check_headers(kwargs, self._headers, 'timeout'):
kwargs['timeout'] = self.timeout kwargs['timeout'] = self.timeout
kwargs['headers'] = {**self._headers, **kwargs['headers']} h = CaseInsensitiveDict(self._headers)
for k, v in kwargs['headers'].items():
h[k] = v
kwargs['headers'] = h
r = err = None r = err = None
retry = retry if retry is not None else self.retry_times retry = retry if retry is not None else self.retry_times