Skip to content

Commit 4a84d83

Browse files
committed
Refactor headers handling in execute_request to avoid modifying original session headers
1 parent 439e7d4 commit 4a84d83

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

async_tls_client/sessions.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -386,30 +386,30 @@ def build_payload():
386386
request_body = data
387387
content_type = None
388388

389-
if content_type is not None and "content-type" not in self.headers:
390-
self.headers["Content-Type"] = content_type
389+
# Create a copy of the session headers to avoid modifying the original
390+
merged_headers = CaseInsensitiveDict(self.headers.copy())
391391

392-
if self.headers is None:
393-
merged_headers = CaseInsensitiveDict(headers)
394-
elif headers is None:
395-
merged_headers = self.headers
396-
else:
397-
merged_headers = CaseInsensitiveDict(self.headers)
398-
merged_headers.update(headers)
392+
# Set Content-Type header if applicable
393+
if content_type is not None and "content-type" not in merged_headers:
394+
merged_headers["Content-Type"] = content_type
399395

400-
# Remove items where the key or value is set to None.
396+
# Merge headers from the session and the request
397+
if headers is not None:
398+
merged_headers.update(headers)
399+
# Remove keys with None values
401400
none_keys = [k for (k, v) in merged_headers.items() if v is None or k is None]
402401
for key in none_keys:
403402
del merged_headers[key]
404403

404+
# Merge cookies from the session and the request
405405
merged_cookies = merge_cookies(self.cookies, cookies or {})
406406
request_cookies = [
407407
{
408-
'domain': c.domain,
409-
'expires': c.expires,
410-
'name': c.name,
411-
'path': c.path,
412-
'value': c.value.replace('"', "")
408+
"domain": c.domain,
409+
"expires": c.expires,
410+
"name": c.name,
411+
"path": c.path,
412+
"value": c.value.replace('"', "")
413413
}
414414
for c in merged_cookies
415415
]

0 commit comments

Comments
 (0)