Skip to content

Commit

Permalink
attempt on solving #458 - use requests.CaseInsensitiveDict everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
tobixen committed Nov 27, 2024
1 parent acb97ba commit 0ae40db
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions caldav/davclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def __init__(
timeout: Optional[int] = None,
ssl_verify_cert: Union[bool, str] = True,
ssl_cert: Union[str, Tuple[str, str], None] = None,
headers: Dict[str, str] = None,
headers: Mapping[str, str] = None,
huge_tree: bool = False,
) -> None:
"""
Expand All @@ -396,8 +396,6 @@ def __init__(
username and password may be omitted. Known bug: .netrc is honored
even if a username/password is given, ref https://github.com/python-caldav/caldav/issues/206
"""
headers = headers or {}

self.session = niquests.Session(multiplexed=True)

log.debug("url: " + str(url))
Expand All @@ -421,12 +419,12 @@ def __init__(
self.proxy = _proxy

# Build global headers
self.headers = {
self.headers = CaseInsensitiveDict({
"User-Agent": "python-caldav/" + __version__,
"Content-Type": "text/xml",
"Accept": "text/xml, text/calendar",
}
self.headers.update(headers)
})
self.headers.update(headers or {})
if self.url.username is not None:
username = unquote(self.url.username)
password = unquote(self.url.password)
Expand Down Expand Up @@ -640,7 +638,7 @@ def request(
headers = headers or {}

combined_headers = self.headers.copy()
combined_headers.update(headers)
combined_headers.update(headers or {})
if (body is None or body == "") and "Content-Type" in combined_headers:
del combined_headers["Content-Type"]

Expand Down Expand Up @@ -691,13 +689,15 @@ def request(
if not r.status_code == 401:
raise

## Returned headers
r_headers = CaseInsensitiveDict(r.headers)
if (
r.status_code == 401
and "WWW-Authenticate" in r.headers
and "WWW-Authenticate" in r_headers
and not self.auth
and self.username
):
auth_types = self.extract_auth_types(r.headers["WWW-Authenticate"])
auth_types = self.extract_auth_types(r_headers["WWW-Authenticate"])

if self.password and self.username and "digest" in auth_types:
self.auth = niquests.auth.HTTPDigestAuth(self.username, self.password)
Expand All @@ -715,7 +715,7 @@ def request(

elif (
r.status_code == 401
and "WWW-Authenticate" in r.headers
and "WWW-Authenticate" in r_headers
and self.auth
and self.password
and isinstance(self.password, bytes)
Expand All @@ -729,7 +729,7 @@ def request(
## sequence and not a string (see commit 13a4714, which
## introduced this regression)

auth_types = self.extract_auth_types(r.headers["WWW-Authenticate"])
auth_types = self.extract_auth_types(r_headers["WWW-Authenticate"])

if self.password and self.username and "digest" in auth_types:
self.auth = niquests.auth.HTTPDigestAuth(
Expand Down

0 comments on commit 0ae40db

Please sign in to comment.