blob: de26ffa648ffecd3b98beebd45e0846267e6edd7 [file] [log] [blame]
Guido van Rossum0039d7b1999-01-12 20:19:27 +00001# -*- Mode: Python; tab-width: 4 -*-
Tim Peters658cba62001-02-09 20:06:00 +00002# Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
Tim Peters146965a2001-01-14 18:09:23 +00003# Author: Sam Rushing <rushing@nightmare.com>
Guido van Rossum0039d7b1999-01-12 20:19:27 +00004
5# ======================================================================
6# Copyright 1996 by Sam Rushing
Tim Peters146965a2001-01-14 18:09:23 +00007#
Guido van Rossum0039d7b1999-01-12 20:19:27 +00008# All Rights Reserved
Tim Peters146965a2001-01-14 18:09:23 +00009#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000010# Permission to use, copy, modify, and distribute this software and
11# its documentation for any purpose and without fee is hereby
12# granted, provided that the above copyright notice appear in all
13# copies and that both that copyright notice and this permission
14# notice appear in supporting documentation, and that the name of Sam
15# Rushing not be used in advertising or publicity pertaining to
16# distribution of the software without specific, written prior
17# permission.
Tim Peters146965a2001-01-14 18:09:23 +000018#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000019# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
21# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
23# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
24# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26# ======================================================================
27
Guido van Rossume4a1b6d2001-04-06 15:30:33 +000028r"""A class supporting chat-style (command/response) protocols.
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000029
30This class adds support for 'chat' style protocols - where one side
31sends a 'command', and the other sends a response (examples would be
32the common internet protocols - smtp, nntp, ftp, etc..).
33
34The handle_read() method looks at the input stream for the current
35'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
36for multi-line output), calling self.found_terminator() on its
37receipt.
38
39for example:
40Say you build an async nntp client using this class. At the start
41of the connection, you'll have self.terminator set to '\r\n', in
42order to process the single-line greeting. Just before issuing a
43'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST
44command will be accumulated (using your own 'collect_incoming_data'
45method) up to the terminator, and then control will be returned to
46you - by calling your self.found_terminator() method.
47"""
Guido van Rossum0039d7b1999-01-12 20:19:27 +000048import asyncore
Raymond Hettingerac093c62004-02-07 03:19:10 +000049from collections import deque
Guido van Rossum0039d7b1999-01-12 20:19:27 +000050
Miss Islington (bot)a80a38e2021-06-24 12:57:55 -070051from warnings import warn
52warn(
53 'The asynchat module is deprecated. '
54 'The recommended replacement is asyncio',
55 DeprecationWarning,
56 stacklevel=2)
57
58
Josiah Carlsond74900e2008-07-07 04:15:08 +000059
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020060class async_chat(asyncore.dispatcher):
Tim Peters146965a2001-01-14 18:09:23 +000061 """This is an abstract class. You must derive from this class, and add
62 the two methods collect_incoming_data() and found_terminator()"""
Guido van Rossum0039d7b1999-01-12 20:19:27 +000063
Tim Peters146965a2001-01-14 18:09:23 +000064 # these are overridable defaults
Guido van Rossum0039d7b1999-01-12 20:19:27 +000065
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020066 ac_in_buffer_size = 65536
67 ac_out_buffer_size = 65536
Guido van Rossum0039d7b1999-01-12 20:19:27 +000068
Josiah Carlsond74900e2008-07-07 04:15:08 +000069 # we don't want to enable the use of encoding by default, because that is a
70 # sign of an application bug that we don't want to pass silently
71
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020072 use_encoding = 0
73 encoding = 'latin-1'
Josiah Carlsond74900e2008-07-07 04:15:08 +000074
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020075 def __init__(self, sock=None, map=None):
Josiah Carlsond74900e2008-07-07 04:15:08 +000076 # for string terminator matching
Guido van Rossum076da092007-07-12 07:58:54 +000077 self.ac_in_buffer = b''
Josiah Carlsond74900e2008-07-07 04:15:08 +000078
Serhiy Storchaka50254c52013-08-29 11:35:43 +030079 # we use a list here rather than io.BytesIO for a few reasons...
80 # del lst[:] is faster than bio.truncate(0)
81 # lst = [] is faster than bio.truncate(0)
Josiah Carlsond74900e2008-07-07 04:15:08 +000082 self.incoming = []
83
84 # we toss the use of the "simple producer" and replace it with
85 # a pure deque, which the original fifo was a wrapping of
86 self.producer_fifo = deque()
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020087 asyncore.dispatcher.__init__(self, sock, map)
Guido van Rossum0039d7b1999-01-12 20:19:27 +000088
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +000089 def collect_incoming_data(self, data):
Collin Winterce36ad82007-08-30 01:19:48 +000090 raise NotImplementedError("must be implemented in subclass")
Tim Peters863ac442002-04-16 01:38:40 +000091
Josiah Carlsond74900e2008-07-07 04:15:08 +000092 def _collect_incoming_data(self, data):
93 self.incoming.append(data)
94
95 def _get_data(self):
96 d = b''.join(self.incoming)
97 del self.incoming[:]
98 return d
99
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +0000100 def found_terminator(self):
Collin Winterce36ad82007-08-30 01:19:48 +0000101 raise NotImplementedError("must be implemented in subclass")
Tim Peters863ac442002-04-16 01:38:40 +0000102
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200103 def set_terminator(self, term):
104 """Set the input delimiter.
105
106 Can be a fixed string of any length, an integer, or None.
107 """
Josiah Carlsond74900e2008-07-07 04:15:08 +0000108 if isinstance(term, str) and self.use_encoding:
109 term = bytes(term, self.encoding)
Victor Stinner630a4f62014-07-08 00:26:36 +0200110 elif isinstance(term, int) and term < 0:
111 raise ValueError('the number of received bytes must be positive')
Tim Peters146965a2001-01-14 18:09:23 +0000112 self.terminator = term
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000113
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200114 def get_terminator(self):
Tim Peters146965a2001-01-14 18:09:23 +0000115 return self.terminator
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000116
Tim Peters146965a2001-01-14 18:09:23 +0000117 # grab some more data from the socket,
118 # throw it to the collector method,
119 # check for the terminator,
120 # if found, transition to the next state.
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000121
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200122 def handle_read(self):
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000123
Tim Peters146965a2001-01-14 18:09:23 +0000124 try:
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200125 data = self.recv(self.ac_in_buffer_size)
Victor Stinner45cff662014-07-24 18:49:36 +0200126 except BlockingIOError:
127 return
Pablo Galindo293dd232019-11-19 21:34:03 +0000128 except OSError:
Tim Peters146965a2001-01-14 18:09:23 +0000129 self.handle_error()
130 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000131
Josiah Carlsond74900e2008-07-07 04:15:08 +0000132 if isinstance(data, str) and self.use_encoding:
133 data = bytes(str, self.encoding)
134 self.ac_in_buffer = self.ac_in_buffer + data
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000135
Tim Peters146965a2001-01-14 18:09:23 +0000136 # Continue to search for self.terminator in self.ac_in_buffer,
137 # while calling self.collect_incoming_data. The while loop
138 # is necessary because we might read several data+terminator
Josiah Carlsond74900e2008-07-07 04:15:08 +0000139 # combos with a single recv(4096).
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000140
Tim Peters146965a2001-01-14 18:09:23 +0000141 while self.ac_in_buffer:
142 lb = len(self.ac_in_buffer)
143 terminator = self.get_terminator()
Andrew M. Kuchlingca69f022005-06-09 14:59:45 +0000144 if not terminator:
Tim Peters146965a2001-01-14 18:09:23 +0000145 # no terminator, collect it all
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200146 self.collect_incoming_data(self.ac_in_buffer)
Guido van Rossum806c2462007-08-06 23:33:07 +0000147 self.ac_in_buffer = b''
Josiah Carlsond74900e2008-07-07 04:15:08 +0000148 elif isinstance(terminator, int):
Tim Peters146965a2001-01-14 18:09:23 +0000149 # numeric terminator
150 n = terminator
151 if lb < n:
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200152 self.collect_incoming_data(self.ac_in_buffer)
Guido van Rossum076da092007-07-12 07:58:54 +0000153 self.ac_in_buffer = b''
Tim Peters146965a2001-01-14 18:09:23 +0000154 self.terminator = self.terminator - lb
155 else:
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200156 self.collect_incoming_data(self.ac_in_buffer[:n])
Tim Peters146965a2001-01-14 18:09:23 +0000157 self.ac_in_buffer = self.ac_in_buffer[n:]
158 self.terminator = 0
159 self.found_terminator()
160 else:
161 # 3 cases:
162 # 1) end of buffer matches terminator exactly:
163 # collect data, transition
164 # 2) end of buffer matches some prefix:
165 # collect data to the prefix
166 # 3) end of buffer does not match any prefix:
167 # collect data
168 terminator_len = len(terminator)
Tim Petersb5d13922001-04-05 22:38:32 +0000169 index = self.ac_in_buffer.find(terminator)
Tim Peters146965a2001-01-14 18:09:23 +0000170 if index != -1:
171 # we found the terminator
172 if index > 0:
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200173 # don't bother reporting the empty string
174 # (source of subtle bugs)
175 self.collect_incoming_data(self.ac_in_buffer[:index])
Tim Peters146965a2001-01-14 18:09:23 +0000176 self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200177 # This does the Right Thing if the terminator
178 # is changed here.
Tim Peters146965a2001-01-14 18:09:23 +0000179 self.found_terminator()
180 else:
181 # check for a prefix of the terminator
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200182 index = find_prefix_at_end(self.ac_in_buffer, terminator)
Tim Peters146965a2001-01-14 18:09:23 +0000183 if index:
184 if index != lb:
185 # we found a prefix, collect up to the prefix
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200186 self.collect_incoming_data(self.ac_in_buffer[:-index])
Tim Peters146965a2001-01-14 18:09:23 +0000187 self.ac_in_buffer = self.ac_in_buffer[-index:]
188 break
189 else:
190 # no prefix, collect it all
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200191 self.collect_incoming_data(self.ac_in_buffer)
Guido van Rossum076da092007-07-12 07:58:54 +0000192 self.ac_in_buffer = b''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000193
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200194 def handle_write(self):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000195 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000196
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200197 def handle_close(self):
Tim Peters146965a2001-01-14 18:09:23 +0000198 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000199
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200200 def push(self, data):
Victor Stinnerd9e810a2014-07-08 00:00:30 +0200201 if not isinstance(data, (bytes, bytearray, memoryview)):
202 raise TypeError('data argument must be byte-ish (%r)',
203 type(data))
Josiah Carlsond74900e2008-07-07 04:15:08 +0000204 sabs = self.ac_out_buffer_size
205 if len(data) > sabs:
206 for i in range(0, len(data), sabs):
207 self.producer_fifo.append(data[i:i+sabs])
208 else:
209 self.producer_fifo.append(data)
Tim Peters146965a2001-01-14 18:09:23 +0000210 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000211
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200212 def push_with_producer(self, producer):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000213 self.producer_fifo.append(producer)
Tim Peters146965a2001-01-14 18:09:23 +0000214 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000215
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200216 def readable(self):
Tim Peters146965a2001-01-14 18:09:23 +0000217 "predicate for inclusion in the readable for select()"
Josiah Carlsond74900e2008-07-07 04:15:08 +0000218 # cannot use the old predicate, it violates the claim of the
219 # set_terminator method.
220
221 # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
222 return 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000223
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200224 def writable(self):
Tim Peters146965a2001-01-14 18:09:23 +0000225 "predicate for inclusion in the writable for select()"
Josiah Carlsond74900e2008-07-07 04:15:08 +0000226 return self.producer_fifo or (not self.connected)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000227
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200228 def close_when_done(self):
Tim Peters146965a2001-01-14 18:09:23 +0000229 "automatically close this channel once the outgoing queue is empty"
Josiah Carlsond74900e2008-07-07 04:15:08 +0000230 self.producer_fifo.append(None)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000231
Josiah Carlsond74900e2008-07-07 04:15:08 +0000232 def initiate_send(self):
233 while self.producer_fifo and self.connected:
234 first = self.producer_fifo[0]
235 # handle empty string/buffer or None entry
236 if not first:
237 del self.producer_fifo[0]
238 if first is None:
Josiah Carlsond74900e2008-07-07 04:15:08 +0000239 self.handle_close()
Tim Peters146965a2001-01-14 18:09:23 +0000240 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000241
Josiah Carlsond74900e2008-07-07 04:15:08 +0000242 # handle classic producer behavior
243 obs = self.ac_out_buffer_size
Tim Peters146965a2001-01-14 18:09:23 +0000244 try:
Giampaolo Rodola'd9f38bc2012-08-04 14:38:16 +0200245 data = first[:obs]
Josiah Carlsond74900e2008-07-07 04:15:08 +0000246 except TypeError:
247 data = first.more()
248 if data:
249 self.producer_fifo.appendleft(data)
250 else:
251 del self.producer_fifo[0]
252 continue
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000253
Josiah Carlsond74900e2008-07-07 04:15:08 +0000254 if isinstance(data, str) and self.use_encoding:
255 data = bytes(data, self.encoding)
256
257 # send the data
258 try:
259 num_sent = self.send(data)
Andrew Svetlov0832af62012-12-18 23:10:48 +0200260 except OSError:
Tim Peters146965a2001-01-14 18:09:23 +0000261 self.handle_error()
262 return
263
Josiah Carlsond74900e2008-07-07 04:15:08 +0000264 if num_sent:
265 if num_sent < len(data) or obs < len(first):
266 self.producer_fifo[0] = first[num_sent:]
267 else:
268 del self.producer_fifo[0]
269 # we tried to send some actual data
270 return
271
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200272 def discard_buffers(self):
Tim Peters146965a2001-01-14 18:09:23 +0000273 # Emergencies only!
Guido van Rossum076da092007-07-12 07:58:54 +0000274 self.ac_in_buffer = b''
Josiah Carlsond74900e2008-07-07 04:15:08 +0000275 del self.incoming[:]
276 self.producer_fifo.clear()
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000277
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200278
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000279class simple_producer:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000280
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200281 def __init__(self, data, buffer_size=512):
Tim Peters146965a2001-01-14 18:09:23 +0000282 self.data = data
283 self.buffer_size = buffer_size
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000284
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200285 def more(self):
286 if len(self.data) > self.buffer_size:
Tim Peters146965a2001-01-14 18:09:23 +0000287 result = self.data[:self.buffer_size]
288 self.data = self.data[self.buffer_size:]
289 return result
290 else:
291 result = self.data
Guido van Rossum076da092007-07-12 07:58:54 +0000292 self.data = b''
Tim Peters146965a2001-01-14 18:09:23 +0000293 return result
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000294
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200295
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000296# Given 'haystack', see if any prefix of 'needle' is at its end. This
297# assumes an exact match has already been checked. Return the number of
298# characters matched.
299# for example:
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200300# f_p_a_e("qwerty\r", "\r\n") => 1
301# f_p_a_e("qwertydkjf", "\r\n") => 0
302# f_p_a_e("qwerty\r\n", "\r\n") => <undefined>
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000303
304# this could maybe be made faster with a computed regex?
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000305# [answer: no; circa Python-2.0, Jan 2001]
Andrew M. Kuchlingc63a3962002-03-20 02:22:58 +0000306# new python: 28961/s
307# old python: 18307/s
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000308# re: 12820/s
309# regex: 14035/s
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000310
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200311def find_prefix_at_end(haystack, needle):
Tim Peters863ac442002-04-16 01:38:40 +0000312 l = len(needle) - 1
313 while l and not haystack.endswith(needle[:l]):
314 l -= 1
315 return l