Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aiosmtpd/docs/proxyprotocol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Enums
Valid only for address family of :attr:`AF.INET` or :attr:`AF.INET6`

.. py:attribute:: rest
:type: ByteString
:type: bytearray

The contents depend on the version of the PROXY header *and* (for version 2)
the address family.
Expand Down Expand Up @@ -374,7 +374,7 @@ Enums
.. py:classmethod:: from_raw(raw) -> Optional[ProxyTLV]

:param raw: The raw bytes containing the TLV Vectors
:type raw: ByteString
:type raw: bytearray
:return: A new instance of ProxyTLV, or ``None`` if parsing failed

This triggers the parsing of raw bytes/bytearray into a ProxyTLV instance.
Expand All @@ -387,7 +387,7 @@ Enums
.. py:classmethod:: parse(chunk, partial_ok=True) -> Dict[str, Any]

:param chunk: The bytes to parse into TLV Vectors
:type chunk: ByteString
:type chunk: bytearray
:param partial_ok: If ``True``, return partially-parsed TLV Vectors as is.
If ``False``, (re)raise ``MalformedTLV``
:type partial_ok: bool
Expand Down
14 changes: 7 additions & 7 deletions aiosmtpd/proxy_protocol.py
Copy link
Member

@Dreamsorcerer Dreamsorcerer Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bytearray? There are defaults here which are clearly bytes, not bytearray, and the docs suggest replacing it with Buffer.
https://docs.python.org/3/library/typing.html#typing.ByteString

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; I'll have to take a closer look, but Buffer is probably the more correct option

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from enum import IntEnum
from functools import partial
from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Any, ByteString, Dict, Optional, Protocol, Tuple, Union
from typing import Any, Dict, Optional, Protocol, Tuple, Union

import attr
from public import public
Expand Down Expand Up @@ -165,7 +165,7 @@ def same_attribs(self, _raises: bool = False, **kwargs) -> bool:
@classmethod
def parse(
cls,
data: ByteString,
data: bytearray,
partial_ok: bool = True,
strict: bool = False,
) -> Tuple[Dict[str, Any], Dict[str, int]]:
Expand All @@ -179,7 +179,7 @@ def parse(
rslt: Dict[str, Any] = {}
tlv_loc: Dict[str, int] = {}

def _pars(chunk: ByteString, *, offset: int) -> None:
def _pars(chunk: bytearray, *, offset: int) -> None:
i = 0
while i < len(chunk):
typ = chunk[i]
Expand Down Expand Up @@ -218,7 +218,7 @@ def _pars(chunk: ByteString, *, offset: int) -> None:

@classmethod
def from_raw(
cls, raw: ByteString, strict: bool = False
cls, raw: bytearray, strict: bool = False
) -> Optional["ProxyTLV"]:
"""
Parses raw bytes for TLV Vectors, decode them and giving them human-readable
Expand Down Expand Up @@ -265,7 +265,7 @@ class ProxyData:
dst_addr: Optional[EndpointAddress] = _anoinit(default=None)
src_port: Optional[int] = _anoinit(default=None)
dst_port: Optional[int] = _anoinit(default=None)
rest: ByteString = _anoinit(default=b"")
rest: bytearray = _anoinit(default=b"")
"""
Rest of PROXY Protocol data following UNKNOWN (v1) or UNSPEC (v2), or containing
undecoded TLV (v2). If the latter, you can use the ProxyTLV class to parse the
Expand Down Expand Up @@ -340,7 +340,7 @@ def __bool__(self) -> bool:
# Reference: https://github.com/haproxy/haproxy/blob/v2.3.0/doc/proxy-protocol.txt


async def _get_v1(reader: AsyncReader, initial: ByteString = b"") -> ProxyData:
async def _get_v1(reader: AsyncReader, initial: bytearray = b"") -> ProxyData:
proxy_data = ProxyData(version=1)
proxy_data.whole_raw = bytearray(initial)

Expand Down Expand Up @@ -424,7 +424,7 @@ async def get_ap(matcher: "re.Pattern[str]") -> str:
return proxy_data


async def _get_v2(reader: AsyncReader, initial: ByteString = b"") -> ProxyData:
async def _get_v2(reader: AsyncReader, initial: bytearray = b"") -> ProxyData:
proxy_data = ProxyData(version=2)
whole_raw = bytearray()

Expand Down
Loading