Drop support for Python 3.6 and old urllib3

This commit is contained in:
Mike Edmunds
2023-05-02 13:59:01 -07:00
parent 485766182e
commit 9fba58237d
6 changed files with 28 additions and 70 deletions

View File

@@ -1,33 +1,12 @@
from datetime import datetime
from email.utils import encode_rfc2231
from urllib.parse import quote
from requests import Request
from ..exceptions import AnymailError, AnymailRequestsAPIError
from ..message import AnymailRecipientStatus
from ..utils import get_anymail_setting, rfc2822date
from .base_requests import AnymailRequestsBackend, RequestsPayload
# Feature-detect whether requests (urllib3) correctly uses RFC 7578 encoding for non-
# ASCII filenames in Content-Disposition headers. (This was fixed in urllib3 v1.25.)
# See MailgunPayload.get_request_params for info (and a workaround on older versions).
# (Note: when this workaround is removed, please also remove "old_urllib3" tox envs.)
def is_requests_rfc_5758_compliant():
request = Request(
method="POST",
url="https://www.example.com",
files=[("attachment", ("\N{NOT SIGN}.txt", "test", "text/plain"))],
)
prepared = request.prepare()
form_data = prepared.body # bytes
return b"filename*=" not in form_data
REQUESTS_IS_RFC_7578_COMPLIANT = is_requests_rfc_5758_compliant()
class EmailBackend(AnymailRequestsBackend):
"""
Mailgun API Email Backend
@@ -163,37 +142,6 @@ class MailgunPayload(RequestsPayload):
)
return "%s/messages" % quote(self.sender_domain, safe="")
def get_request_params(self, api_url):
params = super().get_request_params(api_url)
non_ascii_filenames = [
filename
for (field, (filename, content, mimetype)) in params["files"]
if filename is not None and not isascii(filename)
]
if non_ascii_filenames and not REQUESTS_IS_RFC_7578_COMPLIANT:
# Workaround https://github.com/requests/requests/issues/4652:
# Mailgun expects RFC 7578 compliant multipart/form-data, and is confused
# by Requests/urllib3's improper use of RFC 2231 encoded filename parameters
# ("filename*=utf-8''...") in Content-Disposition headers.
# The workaround is to pre-generate the (non-compliant) form-data body, and
# replace 'filename*={RFC 2231 encoded}' with 'filename="{UTF-8 bytes}"'.
# Replace _only_ filenames that will be problems (not all "filename*=...")
# to minimize potential side effects--e.g., in attached messages that might
# have their own attachments with (correctly) RFC 2231 encoded filenames.
prepared = Request(**params).prepare()
form_data = prepared.body # bytes
for filename in non_ascii_filenames: # text
rfc2231_filename = encode_rfc2231(filename, charset="utf-8")
form_data = form_data.replace(
b"filename*=" + rfc2231_filename.encode("utf-8"),
b'filename="' + filename.encode("utf-8") + b'"',
)
params["data"] = form_data
# Content-Type: multipart/form-data; boundary=...
params["headers"]["Content-Type"] = prepared.headers["Content-Type"]
params["files"] = None # these are now in the form_data body
return params
def serialize_data(self):
self.populate_recipient_variables()
return self.data