Skip to content

Commit

Permalink
Correctly handle headers of different types
Browse files Browse the repository at this point in the history
  • Loading branch information
justmobilize committed Jan 26, 2024
1 parent 178dbc3 commit 3bf143f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,18 @@ def _get_socket(
self._socket_free[sock] = False
return sock

@staticmethod
def _check_headers(headers: Dict[str, str]):
if not isinstance(headers, dict):
raise AttributeError("headers must be in dict format")

for key, value in headers.items():
if isinstance(value, (str, bytes)) or value is None:
continue
raise AttributeError(
f"Header part ({value}) from {key} must be of type str or bytes, not {type(value)}"
)

@staticmethod
def _send(socket: SocketType, data: bytes):
total_sent = 0
Expand All @@ -555,9 +567,14 @@ def _send_as_bytes(self, socket: SocketType, data: str):
return self._send(socket, bytes(data, "utf-8"))

def _send_header(self, socket, header, value):
if value is None:
return
self._send_as_bytes(socket, header)
self._send(socket, b": ")
self._send_as_bytes(socket, value)
if isinstance(value, bytes):
self._send(socket, value)
else:
self._send_as_bytes(socket, value)
self._send(socket, b"\r\n")

# pylint: disable=too-many-arguments
Expand All @@ -571,6 +588,9 @@ def _send_request(
data: Any,
json: Any,
):
# Check headers
self._check_headers(headers)

# Convert data
content_type_header = None

Expand Down

0 comments on commit 3bf143f

Please sign in to comment.