Skip to content

Commit 6e0e6eb

Browse files
authored
Fix remote image length warnings using requests instead of urllib (standard lib) (#289)
Related to #257
2 parents 7e51e12 + b4cfa0f commit 6e0e6eb

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

mkdocs_rss_plugin/util.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
# standard library
88
import logging
9-
import ssl
109
from collections.abc import Iterable
1110
from datetime import date, datetime
1211
from email.utils import format_datetime
1312
from mimetypes import guess_type
1413
from pathlib import Path
1514
from typing import Any
16-
from urllib import request
17-
from urllib.error import HTTPError, URLError
1815
from urllib.parse import urlencode, urlparse, urlunparse
1916

2017
# 3rd party
@@ -24,6 +21,8 @@
2421
from mkdocs.plugins import get_plugin_logger
2522
from mkdocs.structure.pages import Page
2623
from mkdocs.utils import get_build_datetime
24+
from requests import Session
25+
from requests.exceptions import HTTPError
2726

2827
# package
2928
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME, REMOTE_REQUEST_HEADERS
@@ -106,7 +105,11 @@ def __init__(
106105
# save integrations
107106
self.social_cards = integration_material_social_cards
108107

109-
def build_url(self, base_url: str, path: str, args_dict: dict = None) -> str:
108+
# http/s session
109+
self.req_session = Session()
110+
self.req_session.headers.update(REMOTE_REQUEST_HEADERS)
111+
112+
def build_url(self, base_url: str, path: str, args_dict: dict | None = None) -> str:
110113
"""Build URL using base URL, cumulating existing and passed path, \
111114
then adding URL arguments.
112115
@@ -604,51 +607,44 @@ def get_remote_image_length(
604607
image_url: str,
605608
http_method: str = "HEAD",
606609
attempt: int = 0,
607-
ssl_context: ssl.SSLContext = None,
610+
ssl_verify: bool = True,
608611
) -> int | None:
609-
"""Retrieve length for remote images (starting with 'http' \
610-
in meta.image or meta.illustration). \
611-
It tries to perform a HEAD request and get the length from the headers. \
612-
If it fails, it tries again with a GET and disabling SSL verification.
613-
614-
:param image_url: remote image URL
615-
:type image_url: str
616-
:param http_method: HTTP method used to perform request, defaults to "HEAD"
617-
:type http_method: str, optional
618-
:param attempt: request tries counter, defaults to 0
619-
:type attempt: int, optional
620-
:param ssl_context: SSL context, defaults to None
621-
:type ssl_context: ssl.SSLContext, optional
622-
623-
:return: image length as str or None
624-
:rtype: Optional[int]
612+
"""Retrieve length for remote images (starting with 'http').
613+
614+
Firstly, it tries to perform a HEAD request and get the length from the headers. \
615+
If it fails, it tries again with a GET and disabling SSL verification.
616+
617+
Args:
618+
image_url (str): image URL
619+
http_method (str, optional): HTTP method to use for the request.
620+
Defaults to "HEAD".
621+
attempt (int, optional): request tries counter. Defaults to 0.
622+
ssl_verify (bool, optional): option to perform SSL verification or not.
623+
Defaults to True.
624+
625+
Returns:
626+
int | None: image length as int or None
625627
"""
626-
# prepare request
627-
req = request.Request(
628-
image_url,
629-
method=http_method,
630-
headers=REMOTE_REQUEST_HEADERS,
631-
)
632628
# first, try HEAD request to avoid downloading the image
633629
try:
634630
attempt += 1
635-
remote_img = request.urlopen(url=req, context=ssl_context)
636-
img_length = remote_img.getheader("content-length")
637-
except (HTTPError, URLError) as err:
638-
logging.warning(
631+
req_response = self.req_session.request(
632+
method=http_method, url=image_url, verify=ssl_verify
633+
)
634+
req_response.raise_for_status()
635+
img_length = req_response.headers.get("content-length")
636+
except HTTPError as err:
637+
logger.debug(
639638
f"Remote image could not been reached: {image_url}. "
640639
f"Trying again with GET and disabling SSL verification. Attempt: {attempt}. "
641640
f"Trace: {err}"
642641
)
643642
if attempt < 2:
644643
return self.get_remote_image_length(
645-
image_url,
646-
http_method="GET",
647-
attempt=attempt,
648-
ssl_context=ssl._create_unverified_context(),
644+
image_url, http_method="GET", attempt=attempt, ssl_verify=False
649645
)
650646
else:
651-
logging.error(
647+
logger.info(
652648
f"Remote image is not reachable: {image_url} after "
653649
f"{attempt} attempts. Trace: {err}"
654650
)

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
GitPython>=3.1,<3.2
66
mkdocs>=1.5,<2
7+
requests>=2.31,<3
78
tzdata==2024.* ; python_version >= "3.9" and sys_platform == "win32"

0 commit comments

Comments
 (0)