Skip to content

Commit f702515

Browse files
committed
refactor: streamline header normalization in from_binary function
Signed-off-by: Tudor Plugaru <[email protected]>
1 parent 4383da5 commit f702515

File tree

1 file changed

+8
-18
lines changed
  • src/cloudevents/core/bindings

1 file changed

+8
-18
lines changed

src/cloudevents/core/bindings/http.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ class HTTPMessage:
4444
body: bytes
4545

4646

47-
def _normalize_headers(headers: dict[str, str]) -> dict[str, str]:
48-
"""
49-
Normalize HTTP headers by converting all keys to lowercase.
50-
51-
:param headers: Original headers dictionary
52-
:return: New dictionary with lowercase header names
53-
"""
54-
return {key.lower(): value for key, value in headers.items()}
55-
56-
5747
def _encode_header_value(value: Any) -> str:
5848
"""
5949
Encode a CloudEvent attribute value for use in an HTTP header.
@@ -166,17 +156,17 @@ def from_binary(
166156
:param event_factory: Factory function to create CloudEvent instances
167157
:return: CloudEvent instance
168158
"""
169-
normalized_headers = _normalize_headers(message.headers)
170-
171159
attributes: dict[str, Any] = {}
172160

173-
for header_name, header_value in normalized_headers.items():
174-
if header_name.startswith(CE_PREFIX):
175-
attr_name = header_name[len(CE_PREFIX) :]
176-
attributes[attr_name] = _decode_header_value(attr_name, header_value)
161+
# Single pass: normalize headers and extract attributes
162+
for header_name, header_value in message.headers.items():
163+
normalized_name = header_name.lower()
177164

178-
if CONTENT_TYPE_HEADER in normalized_headers:
179-
attributes["datacontenttype"] = normalized_headers[CONTENT_TYPE_HEADER]
165+
if normalized_name.startswith(CE_PREFIX):
166+
attr_name = normalized_name[len(CE_PREFIX) :]
167+
attributes[attr_name] = _decode_header_value(attr_name, header_value)
168+
elif normalized_name == CONTENT_TYPE_HEADER:
169+
attributes["datacontenttype"] = header_value
180170

181171
datacontenttype = attributes.get("datacontenttype")
182172
data = event_format.read_data(message.body, datacontenttype)

0 commit comments

Comments
 (0)