Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

120 rader
4.5 KiB

  1. """
  2. """
  3. # Created on 2016.07.10
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2016 - 2020 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. try:
  25. from queue import Queue
  26. except ImportError: # Python 2
  27. # noinspection PyUnresolvedReferences
  28. from Queue import Queue
  29. from io import StringIO
  30. from os import linesep
  31. from ..protocol.rfc2849 import decode_persistent_search_control
  32. from ..strategy.asynchronous import AsyncStrategy
  33. from ..core.exceptions import LDAPLDIFError
  34. from ..utils.conv import prepare_for_stream
  35. from ..protocol.rfc2849 import persistent_search_response_to_ldif, add_ldif_header
  36. # noinspection PyProtectedMember
  37. class AsyncStreamStrategy(AsyncStrategy):
  38. """
  39. This strategy is asynchronous. It streams responses in a generator as they appear in the self._responses container
  40. """
  41. def __init__(self, ldap_connection):
  42. AsyncStrategy.__init__(self, ldap_connection)
  43. self.can_stream = True
  44. self.line_separator = linesep
  45. self.all_base64 = False
  46. self.stream = None
  47. self.order = dict()
  48. self._header_added = False
  49. self.persistent_search_message_id = None
  50. self.streaming = False
  51. self.callback = None
  52. if ldap_connection.pool_size:
  53. self.events = Queue(ldap_connection.pool_size)
  54. else:
  55. self.events = Queue()
  56. del self._requests # remove _requests dict from Async Strategy
  57. def _start_listen(self):
  58. AsyncStrategy._start_listen(self)
  59. if self.streaming:
  60. if not self.stream or (isinstance(self.stream, StringIO) and self.stream.closed):
  61. self.set_stream(StringIO())
  62. def _stop_listen(self):
  63. AsyncStrategy._stop_listen(self)
  64. if self.streaming:
  65. self.stream.close()
  66. def accumulate_stream(self, message_id, change):
  67. if message_id == self.persistent_search_message_id:
  68. with self.async_lock:
  69. self._responses[message_id] = []
  70. if self.streaming:
  71. if not self._header_added and self.stream.tell() == 0:
  72. header = add_ldif_header(['-'])[0]
  73. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  74. ldif_lines = persistent_search_response_to_ldif(change)
  75. if self.stream and ldif_lines and not self.connection.closed:
  76. fragment = self.line_separator.join(ldif_lines)
  77. if not self._header_added and self.stream.tell() == 0:
  78. self._header_added = True
  79. header = add_ldif_header(['-'])[0]
  80. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  81. self.stream.write(prepare_for_stream(fragment + self.line_separator + self.line_separator))
  82. else: # strategy is not streaming, events are added to a queue
  83. notification = decode_persistent_search_control(change)
  84. if notification:
  85. change.update(notification)
  86. del change['controls']['2.16.840.1.113730.3.4.7']
  87. if not self.callback:
  88. self.events.put(change)
  89. else:
  90. self.callback(change)
  91. def get_stream(self):
  92. if self.streaming:
  93. return self.stream
  94. return None
  95. def set_stream(self, value):
  96. error = False
  97. try:
  98. if not value.writable():
  99. error = True
  100. except (ValueError, AttributeError):
  101. error = True
  102. if error:
  103. raise LDAPLDIFError('stream must be writable')
  104. self.stream = value
  105. self.streaming = True