| 1 | n/a | # -*- Mode: Python; tab-width: 4 -*- |
|---|
| 2 | n/a | # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp |
|---|
| 3 | n/a | # Author: Sam Rushing <rushing@nightmare.com> |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | # ====================================================================== |
|---|
| 6 | n/a | # Copyright 1996 by Sam Rushing |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | # All Rights Reserved |
|---|
| 9 | n/a | # |
|---|
| 10 | n/a | # Permission to use, copy, modify, and distribute this software and |
|---|
| 11 | n/a | # its documentation for any purpose and without fee is hereby |
|---|
| 12 | n/a | # granted, provided that the above copyright notice appear in all |
|---|
| 13 | n/a | # copies and that both that copyright notice and this permission |
|---|
| 14 | n/a | # notice appear in supporting documentation, and that the name of Sam |
|---|
| 15 | n/a | # Rushing not be used in advertising or publicity pertaining to |
|---|
| 16 | n/a | # distribution of the software without specific, written prior |
|---|
| 17 | n/a | # permission. |
|---|
| 18 | n/a | # |
|---|
| 19 | n/a | # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
|---|
| 20 | n/a | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
|---|
| 21 | n/a | # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
|---|
| 22 | n/a | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|---|
| 23 | n/a | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
|---|
| 24 | n/a | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|---|
| 25 | n/a | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 26 | n/a | # ====================================================================== |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | r"""A class supporting chat-style (command/response) protocols. |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | This class adds support for 'chat' style protocols - where one side |
|---|
| 31 | n/a | sends a 'command', and the other sends a response (examples would be |
|---|
| 32 | n/a | the common internet protocols - smtp, nntp, ftp, etc..). |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | The handle_read() method looks at the input stream for the current |
|---|
| 35 | n/a | 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' |
|---|
| 36 | n/a | for multi-line output), calling self.found_terminator() on its |
|---|
| 37 | n/a | receipt. |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | for example: |
|---|
| 40 | n/a | Say you build an async nntp client using this class. At the start |
|---|
| 41 | n/a | of the connection, you'll have self.terminator set to '\r\n', in |
|---|
| 42 | n/a | order to process the single-line greeting. Just before issuing a |
|---|
| 43 | n/a | 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST |
|---|
| 44 | n/a | command will be accumulated (using your own 'collect_incoming_data' |
|---|
| 45 | n/a | method) up to the terminator, and then control will be returned to |
|---|
| 46 | n/a | you - by calling your self.found_terminator() method. |
|---|
| 47 | n/a | """ |
|---|
| 48 | n/a | import asyncore |
|---|
| 49 | n/a | from collections import deque |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | class async_chat(asyncore.dispatcher): |
|---|
| 53 | n/a | """This is an abstract class. You must derive from this class, and add |
|---|
| 54 | n/a | the two methods collect_incoming_data() and found_terminator()""" |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | # these are overridable defaults |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | ac_in_buffer_size = 65536 |
|---|
| 59 | n/a | ac_out_buffer_size = 65536 |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | # we don't want to enable the use of encoding by default, because that is a |
|---|
| 62 | n/a | # sign of an application bug that we don't want to pass silently |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | use_encoding = 0 |
|---|
| 65 | n/a | encoding = 'latin-1' |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def __init__(self, sock=None, map=None): |
|---|
| 68 | n/a | # for string terminator matching |
|---|
| 69 | n/a | self.ac_in_buffer = b'' |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | # we use a list here rather than io.BytesIO for a few reasons... |
|---|
| 72 | n/a | # del lst[:] is faster than bio.truncate(0) |
|---|
| 73 | n/a | # lst = [] is faster than bio.truncate(0) |
|---|
| 74 | n/a | self.incoming = [] |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | # we toss the use of the "simple producer" and replace it with |
|---|
| 77 | n/a | # a pure deque, which the original fifo was a wrapping of |
|---|
| 78 | n/a | self.producer_fifo = deque() |
|---|
| 79 | n/a | asyncore.dispatcher.__init__(self, sock, map) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def collect_incoming_data(self, data): |
|---|
| 82 | n/a | raise NotImplementedError("must be implemented in subclass") |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def _collect_incoming_data(self, data): |
|---|
| 85 | n/a | self.incoming.append(data) |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | def _get_data(self): |
|---|
| 88 | n/a | d = b''.join(self.incoming) |
|---|
| 89 | n/a | del self.incoming[:] |
|---|
| 90 | n/a | return d |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def found_terminator(self): |
|---|
| 93 | n/a | raise NotImplementedError("must be implemented in subclass") |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def set_terminator(self, term): |
|---|
| 96 | n/a | """Set the input delimiter. |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | Can be a fixed string of any length, an integer, or None. |
|---|
| 99 | n/a | """ |
|---|
| 100 | n/a | if isinstance(term, str) and self.use_encoding: |
|---|
| 101 | n/a | term = bytes(term, self.encoding) |
|---|
| 102 | n/a | elif isinstance(term, int) and term < 0: |
|---|
| 103 | n/a | raise ValueError('the number of received bytes must be positive') |
|---|
| 104 | n/a | self.terminator = term |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | def get_terminator(self): |
|---|
| 107 | n/a | return self.terminator |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | # grab some more data from the socket, |
|---|
| 110 | n/a | # throw it to the collector method, |
|---|
| 111 | n/a | # check for the terminator, |
|---|
| 112 | n/a | # if found, transition to the next state. |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | def handle_read(self): |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | try: |
|---|
| 117 | n/a | data = self.recv(self.ac_in_buffer_size) |
|---|
| 118 | n/a | except BlockingIOError: |
|---|
| 119 | n/a | return |
|---|
| 120 | n/a | except OSError as why: |
|---|
| 121 | n/a | self.handle_error() |
|---|
| 122 | n/a | return |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | if isinstance(data, str) and self.use_encoding: |
|---|
| 125 | n/a | data = bytes(str, self.encoding) |
|---|
| 126 | n/a | self.ac_in_buffer = self.ac_in_buffer + data |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | # Continue to search for self.terminator in self.ac_in_buffer, |
|---|
| 129 | n/a | # while calling self.collect_incoming_data. The while loop |
|---|
| 130 | n/a | # is necessary because we might read several data+terminator |
|---|
| 131 | n/a | # combos with a single recv(4096). |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | while self.ac_in_buffer: |
|---|
| 134 | n/a | lb = len(self.ac_in_buffer) |
|---|
| 135 | n/a | terminator = self.get_terminator() |
|---|
| 136 | n/a | if not terminator: |
|---|
| 137 | n/a | # no terminator, collect it all |
|---|
| 138 | n/a | self.collect_incoming_data(self.ac_in_buffer) |
|---|
| 139 | n/a | self.ac_in_buffer = b'' |
|---|
| 140 | n/a | elif isinstance(terminator, int): |
|---|
| 141 | n/a | # numeric terminator |
|---|
| 142 | n/a | n = terminator |
|---|
| 143 | n/a | if lb < n: |
|---|
| 144 | n/a | self.collect_incoming_data(self.ac_in_buffer) |
|---|
| 145 | n/a | self.ac_in_buffer = b'' |
|---|
| 146 | n/a | self.terminator = self.terminator - lb |
|---|
| 147 | n/a | else: |
|---|
| 148 | n/a | self.collect_incoming_data(self.ac_in_buffer[:n]) |
|---|
| 149 | n/a | self.ac_in_buffer = self.ac_in_buffer[n:] |
|---|
| 150 | n/a | self.terminator = 0 |
|---|
| 151 | n/a | self.found_terminator() |
|---|
| 152 | n/a | else: |
|---|
| 153 | n/a | # 3 cases: |
|---|
| 154 | n/a | # 1) end of buffer matches terminator exactly: |
|---|
| 155 | n/a | # collect data, transition |
|---|
| 156 | n/a | # 2) end of buffer matches some prefix: |
|---|
| 157 | n/a | # collect data to the prefix |
|---|
| 158 | n/a | # 3) end of buffer does not match any prefix: |
|---|
| 159 | n/a | # collect data |
|---|
| 160 | n/a | terminator_len = len(terminator) |
|---|
| 161 | n/a | index = self.ac_in_buffer.find(terminator) |
|---|
| 162 | n/a | if index != -1: |
|---|
| 163 | n/a | # we found the terminator |
|---|
| 164 | n/a | if index > 0: |
|---|
| 165 | n/a | # don't bother reporting the empty string |
|---|
| 166 | n/a | # (source of subtle bugs) |
|---|
| 167 | n/a | self.collect_incoming_data(self.ac_in_buffer[:index]) |
|---|
| 168 | n/a | self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] |
|---|
| 169 | n/a | # This does the Right Thing if the terminator |
|---|
| 170 | n/a | # is changed here. |
|---|
| 171 | n/a | self.found_terminator() |
|---|
| 172 | n/a | else: |
|---|
| 173 | n/a | # check for a prefix of the terminator |
|---|
| 174 | n/a | index = find_prefix_at_end(self.ac_in_buffer, terminator) |
|---|
| 175 | n/a | if index: |
|---|
| 176 | n/a | if index != lb: |
|---|
| 177 | n/a | # we found a prefix, collect up to the prefix |
|---|
| 178 | n/a | self.collect_incoming_data(self.ac_in_buffer[:-index]) |
|---|
| 179 | n/a | self.ac_in_buffer = self.ac_in_buffer[-index:] |
|---|
| 180 | n/a | break |
|---|
| 181 | n/a | else: |
|---|
| 182 | n/a | # no prefix, collect it all |
|---|
| 183 | n/a | self.collect_incoming_data(self.ac_in_buffer) |
|---|
| 184 | n/a | self.ac_in_buffer = b'' |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def handle_write(self): |
|---|
| 187 | n/a | self.initiate_send() |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | def handle_close(self): |
|---|
| 190 | n/a | self.close() |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | def push(self, data): |
|---|
| 193 | n/a | if not isinstance(data, (bytes, bytearray, memoryview)): |
|---|
| 194 | n/a | raise TypeError('data argument must be byte-ish (%r)', |
|---|
| 195 | n/a | type(data)) |
|---|
| 196 | n/a | sabs = self.ac_out_buffer_size |
|---|
| 197 | n/a | if len(data) > sabs: |
|---|
| 198 | n/a | for i in range(0, len(data), sabs): |
|---|
| 199 | n/a | self.producer_fifo.append(data[i:i+sabs]) |
|---|
| 200 | n/a | else: |
|---|
| 201 | n/a | self.producer_fifo.append(data) |
|---|
| 202 | n/a | self.initiate_send() |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def push_with_producer(self, producer): |
|---|
| 205 | n/a | self.producer_fifo.append(producer) |
|---|
| 206 | n/a | self.initiate_send() |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | def readable(self): |
|---|
| 209 | n/a | "predicate for inclusion in the readable for select()" |
|---|
| 210 | n/a | # cannot use the old predicate, it violates the claim of the |
|---|
| 211 | n/a | # set_terminator method. |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size) |
|---|
| 214 | n/a | return 1 |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | def writable(self): |
|---|
| 217 | n/a | "predicate for inclusion in the writable for select()" |
|---|
| 218 | n/a | return self.producer_fifo or (not self.connected) |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | def close_when_done(self): |
|---|
| 221 | n/a | "automatically close this channel once the outgoing queue is empty" |
|---|
| 222 | n/a | self.producer_fifo.append(None) |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | def initiate_send(self): |
|---|
| 225 | n/a | while self.producer_fifo and self.connected: |
|---|
| 226 | n/a | first = self.producer_fifo[0] |
|---|
| 227 | n/a | # handle empty string/buffer or None entry |
|---|
| 228 | n/a | if not first: |
|---|
| 229 | n/a | del self.producer_fifo[0] |
|---|
| 230 | n/a | if first is None: |
|---|
| 231 | n/a | self.handle_close() |
|---|
| 232 | n/a | return |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | # handle classic producer behavior |
|---|
| 235 | n/a | obs = self.ac_out_buffer_size |
|---|
| 236 | n/a | try: |
|---|
| 237 | n/a | data = first[:obs] |
|---|
| 238 | n/a | except TypeError: |
|---|
| 239 | n/a | data = first.more() |
|---|
| 240 | n/a | if data: |
|---|
| 241 | n/a | self.producer_fifo.appendleft(data) |
|---|
| 242 | n/a | else: |
|---|
| 243 | n/a | del self.producer_fifo[0] |
|---|
| 244 | n/a | continue |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | if isinstance(data, str) and self.use_encoding: |
|---|
| 247 | n/a | data = bytes(data, self.encoding) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | # send the data |
|---|
| 250 | n/a | try: |
|---|
| 251 | n/a | num_sent = self.send(data) |
|---|
| 252 | n/a | except OSError: |
|---|
| 253 | n/a | self.handle_error() |
|---|
| 254 | n/a | return |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | if num_sent: |
|---|
| 257 | n/a | if num_sent < len(data) or obs < len(first): |
|---|
| 258 | n/a | self.producer_fifo[0] = first[num_sent:] |
|---|
| 259 | n/a | else: |
|---|
| 260 | n/a | del self.producer_fifo[0] |
|---|
| 261 | n/a | # we tried to send some actual data |
|---|
| 262 | n/a | return |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | def discard_buffers(self): |
|---|
| 265 | n/a | # Emergencies only! |
|---|
| 266 | n/a | self.ac_in_buffer = b'' |
|---|
| 267 | n/a | del self.incoming[:] |
|---|
| 268 | n/a | self.producer_fifo.clear() |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | class simple_producer: |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def __init__(self, data, buffer_size=512): |
|---|
| 274 | n/a | self.data = data |
|---|
| 275 | n/a | self.buffer_size = buffer_size |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | def more(self): |
|---|
| 278 | n/a | if len(self.data) > self.buffer_size: |
|---|
| 279 | n/a | result = self.data[:self.buffer_size] |
|---|
| 280 | n/a | self.data = self.data[self.buffer_size:] |
|---|
| 281 | n/a | return result |
|---|
| 282 | n/a | else: |
|---|
| 283 | n/a | result = self.data |
|---|
| 284 | n/a | self.data = b'' |
|---|
| 285 | n/a | return result |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | # Given 'haystack', see if any prefix of 'needle' is at its end. This |
|---|
| 289 | n/a | # assumes an exact match has already been checked. Return the number of |
|---|
| 290 | n/a | # characters matched. |
|---|
| 291 | n/a | # for example: |
|---|
| 292 | n/a | # f_p_a_e("qwerty\r", "\r\n") => 1 |
|---|
| 293 | n/a | # f_p_a_e("qwertydkjf", "\r\n") => 0 |
|---|
| 294 | n/a | # f_p_a_e("qwerty\r\n", "\r\n") => <undefined> |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | # this could maybe be made faster with a computed regex? |
|---|
| 297 | n/a | # [answer: no; circa Python-2.0, Jan 2001] |
|---|
| 298 | n/a | # new python: 28961/s |
|---|
| 299 | n/a | # old python: 18307/s |
|---|
| 300 | n/a | # re: 12820/s |
|---|
| 301 | n/a | # regex: 14035/s |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | def find_prefix_at_end(haystack, needle): |
|---|
| 304 | n/a | l = len(needle) - 1 |
|---|
| 305 | n/a | while l and not haystack.endswith(needle[:l]): |
|---|
| 306 | n/a | l -= 1 |
|---|
| 307 | n/a | return l |
|---|