_interfaces.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import zope.interface
  8. class IProxyInfo(zope.interface.Interface):
  9. """
  10. Data container for PROXY protocol header data.
  11. """
  12. header = zope.interface.Attribute(
  13. "The raw byestring that represents the PROXY protocol header.",
  14. )
  15. source = zope.interface.Attribute(
  16. "An L{twisted.internet.interfaces.IAddress} representing the "
  17. "connection source."
  18. )
  19. destination = zope.interface.Attribute(
  20. "An L{twisted.internet.interfaces.IAddress} representing the "
  21. "connection destination."
  22. )
  23. class IProxyParser(zope.interface.Interface):
  24. """
  25. Streaming parser that handles PROXY protocol headers.
  26. """
  27. def feed(self, data):
  28. """
  29. Consume a chunk of data and attempt to parse it.
  30. @param data: A bytestring.
  31. @type data: bytes
  32. @return: A two-tuple containing, in order, an L{IProxyInfo} and any
  33. bytes fed to the parser that followed the end of the header. Both
  34. of these values are None until a complete header is parsed.
  35. @raises InvalidProxyHeader: If the bytes fed to the parser create an
  36. invalid PROXY header.
  37. """
  38. def parse(self, line):
  39. """
  40. Parse a bytestring as a full PROXY protocol header line.
  41. @param line: A bytestring that represents a valid HAProxy PROXY
  42. protocol header line.
  43. @type line: bytes
  44. @return: An L{IProxyInfo} containing the parsed data.
  45. @raises InvalidProxyHeader: If the bytestring does not represent a
  46. valid PROXY header.
  47. """