55from socket import AddressFamily , SocketKind
66from ssl import SSLSocket , SSLObject
77from typing import Any , Union , Optional , Sequence
8+ from typing_extensions import Final
89
910from .tlv import ProxyProtocolTLV
10- from .typing import Address , StreamReaderProtocol
11+ from .typing import Address
1112
12- __all__ = ['__version__' , 'ProxyProtocolError' , 'ProxyProtocolResult ' ,
13- 'ProxyProtocol' ]
13+ __all__ = ['__version__' , 'ProxyProtocolError' , 'ProxyProtocolWantRead ' ,
14+ 'ProxyProtocolResult' , ' ProxyProtocol' ]
1415
1516#: The package version string.
1617__version__ : str = pkg_resources .require ('proxy-protocol' )[0 ].version
@@ -26,7 +27,30 @@ class ProxyProtocolError(ValueError):
2627 and closed.
2728
2829 """
29- pass
30+
31+ __slots__ : Sequence [str ] = []
32+
33+
34+ class ProxyProtocolWantRead (Exception ):
35+ """Thrown when the PROXY protocol header cannot be parsed because the
36+ provided data is not enough to be parsed. Additional data must be read
37+ before parsing can succeed or fail.
38+
39+ Either *want_bytes* or *want_line* must be given, but not both.
40+
41+ Args:
42+ want_bytes: Number of bytes needed before parsing may proceed.
43+ want_line: Additional data should be read until the end of a line.
44+
45+ """
46+
47+ __slots__ = ['want_bytes' , 'want_line' ]
48+
49+ def __init__ (self , want_bytes : Optional [int ] = None , * ,
50+ want_line : bool = False ) -> None :
51+ super ().__init__ ('Additional data needed' )
52+ self .want_bytes : Final = want_bytes
53+ self .want_line : Final = want_line
3054
3155
3256class ProxyProtocolResult (metaclass = ABCMeta ):
@@ -100,16 +124,16 @@ def is_valid(self, signature: bytes) -> bool:
100124 ...
101125
102126 @abstractmethod
103- async def read (self , reader : StreamReaderProtocol , * ,
104- signature : bytes = b'' ) -> ProxyProtocolResult :
105- """Read a PROXY protocol header from the given stream and return
127+ def parse (self , data : bytes ) -> ProxyProtocolResult :
128+ """Parse a PROXY protocol header from the given bytestring and return
106129 information about the original connection.
107130
108131 Args:
109- reader: The input stream.
110- signature: Any data that has already been read from the stream.
132+ data: The bytestring read for the header thus far.
111133
112134 Raises:
135+ :exc:`ProxyProtocolWantRead`: The header was incomplete and must
136+ be extended with additional bytes or lines to finish parsing.
113137 :exc:`ProxyProtocolError`: The header failed to parse due to a
114138 syntax error or unsupported format.
115139 :exc:`ValueError`: Malformed or out-of-range data was encountered
0 commit comments