_interfaces.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- test-case-name: twisted.protocols.haproxy.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Interfaces used by the PROXY protocol modules.
  6. """
  7. from typing import Tuple, Union
  8. import zope.interface
  9. class IProxyInfo(zope.interface.Interface):
  10. """
  11. Data container for PROXY protocol header data.
  12. """
  13. header = zope.interface.Attribute(
  14. "The raw byestring that represents the PROXY protocol header.",
  15. )
  16. source = zope.interface.Attribute(
  17. "An L{twisted.internet.interfaces.IAddress} representing the "
  18. "connection source."
  19. )
  20. destination = zope.interface.Attribute(
  21. "An L{twisted.internet.interfaces.IAddress} representing the "
  22. "connection destination."
  23. )
  24. class IProxyParser(zope.interface.Interface):
  25. """
  26. Streaming parser that handles PROXY protocol headers.
  27. """
  28. def feed(data: bytes) -> Union[Tuple[IProxyInfo, bytes], Tuple[None, None]]:
  29. """
  30. Consume a chunk of data and attempt to parse it.
  31. @param data: A bytestring.
  32. @type data: bytes
  33. @return: A two-tuple containing, in order, an L{IProxyInfo} and any
  34. bytes fed to the parser that followed the end of the header. Both
  35. of these values are None until a complete header is parsed.
  36. @raises InvalidProxyHeader: If the bytes fed to the parser create an
  37. invalid PROXY header.
  38. """
  39. def parse(line: bytes) -> IProxyInfo:
  40. """
  41. Parse a bytestring as a full PROXY protocol header line.
  42. @param line: A bytestring that represents a valid HAProxy PROXY
  43. protocol header line.
  44. @type line: bytes
  45. @return: An L{IProxyInfo} containing the parsed data.
  46. @raises InvalidProxyHeader: If the bytestring does not represent a
  47. valid PROXY header.
  48. """