blob: b0c3140927efed8b85d13139c71c9b0f0be030a6 [file] [log] [blame]
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001#!/usr/bin/env python
Bram Moolenaard7ece102016-02-02 23:23:02 +01002#
3# Server that will accept connections from a Vim channel.
Bram Moolenaar77073442016-02-13 23:23:53 +01004# Used by test_channel.vim.
Bram Moolenaard7ece102016-02-02 23:23:02 +01005#
6# This requires Python 2.6 or later.
7
8from __future__ import print_function
9import json
10import socket
11import sys
Bram Moolenaar81661fb2016-02-18 22:23:34 +010012import time
Bram Moolenaard7ece102016-02-02 23:23:02 +010013import threading
14
15try:
16 # Python 3
17 import socketserver
18except ImportError:
19 # Python 2
20 import SocketServer as socketserver
21
Bram Moolenaard7ece102016-02-02 23:23:02 +010022class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
23
Bram Moolenaar0b39ec32020-05-17 22:33:53 +020024 def setup(self):
LemonBoycc766a82022-04-04 15:46:58 +010025 if self.server.address_family != socket.AF_UNIX:
26 self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Bram Moolenaar0b39ec32020-05-17 22:33:53 +020027
Bram Moolenaard7ece102016-02-02 23:23:02 +010028 def handle(self):
29 print("=== socket opened ===")
Bram Moolenaard7ece102016-02-02 23:23:02 +010030 while True:
31 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010032 received = self.request.recv(4096).decode('utf-8')
Bram Moolenaard7ece102016-02-02 23:23:02 +010033 except socket.error:
34 print("=== socket error ===")
35 break
36 except IOError:
37 print("=== socket closed ===")
38 break
Bram Moolenaar608a8912016-02-03 22:39:51 +010039 if received == '':
Bram Moolenaard7ece102016-02-02 23:23:02 +010040 print("=== socket closed ===")
41 break
Bram Moolenaara63cdb52016-03-20 18:24:45 +010042 print("received: {0}".format(received))
Bram Moolenaard7ece102016-02-02 23:23:02 +010043
Bram Moolenaare7bed622016-02-03 22:20:29 +010044 # We may receive two messages at once. Take the part up to the
Bram Moolenaarf1f07922016-08-26 17:58:53 +020045 # newline, which should be after the matching "]".
Bram Moolenaar608a8912016-02-03 22:39:51 +010046 todo = received
47 while todo != '':
Bram Moolenaarf1f07922016-08-26 17:58:53 +020048 splitidx = todo.find('\n')
Bram Moolenaare7bed622016-02-03 22:20:29 +010049 if splitidx < 0:
Bram Moolenaar608a8912016-02-03 22:39:51 +010050 used = todo
51 todo = ''
Bram Moolenaard7ece102016-02-02 23:23:02 +010052 else:
Bram Moolenaarf1f07922016-08-26 17:58:53 +020053 used = todo[:splitidx]
Bram Moolenaar608a8912016-02-03 22:39:51 +010054 todo = todo[splitidx + 1:]
55 if used != received:
Bram Moolenaara63cdb52016-03-20 18:24:45 +010056 print("using: {0}".format(used))
Bram Moolenaard7ece102016-02-02 23:23:02 +010057
Bram Moolenaare7bed622016-02-03 22:20:29 +010058 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010059 decoded = json.loads(used)
Bram Moolenaare7bed622016-02-03 22:20:29 +010060 except ValueError:
61 print("json decoding failed")
62 decoded = [-1, '']
Bram Moolenaard7ece102016-02-02 23:23:02 +010063
Bram Moolenaare7bed622016-02-03 22:20:29 +010064 # Send a response if the sequence number is positive.
65 if decoded[0] >= 0:
66 if decoded[1] == 'hello!':
67 # simply send back a string
68 response = "got it"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +010069 elif decoded[1] == 'malformed1':
70 cmd = '["ex",":"]wrong!["ex","smi"]'
71 print("sending: {0}".format(cmd))
72 self.request.sendall(cmd.encode('utf-8'))
73 response = "ok"
74 # Need to wait for Vim to give up, otherwise it
75 # sometimes fails on OS X.
76 time.sleep(0.2)
77 elif decoded[1] == 'malformed2':
78 cmd = '"unterminated string'
79 print("sending: {0}".format(cmd))
80 self.request.sendall(cmd.encode('utf-8'))
81 response = "ok"
82 # Need to wait for Vim to give up, otherwise the double
83 # quote in the "ok" response terminates the string.
84 time.sleep(0.2)
85 elif decoded[1] == 'malformed3':
86 cmd = '["ex","missing ]"'
87 print("sending: {0}".format(cmd))
88 self.request.sendall(cmd.encode('utf-8'))
89 response = "ok"
90 # Need to wait for Vim to give up, otherwise the ]
91 # in the "ok" response terminates the list.
92 time.sleep(0.2)
93 elif decoded[1] == 'split':
94 cmd = '["ex","let '
95 print("sending: {0}".format(cmd))
96 self.request.sendall(cmd.encode('utf-8'))
97 time.sleep(0.01)
98 cmd = 'g:split = 123"]'
99 print("sending: {0}".format(cmd))
100 self.request.sendall(cmd.encode('utf-8'))
101 response = "ok"
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100102 elif decoded[1].startswith("echo "):
103 # send back the argument
104 response = decoded[1][5:]
Bram Moolenaare7bed622016-02-03 22:20:29 +0100105 elif decoded[1] == 'make change':
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100106 # Send two ex commands at the same time, before
107 # replying to the request.
Bram Moolenaare7bed622016-02-03 22:20:29 +0100108 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
109 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100110 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100111 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100112 response = "ok"
Bram Moolenaarb836f632021-07-01 22:11:28 +0200113 elif decoded[1] == 'echoerr':
114 cmd = '["ex","echoerr \\\"this is an error\\\""]'
115 print("sending: {0}".format(cmd))
116 self.request.sendall(cmd.encode('utf-8'))
117 response = "ok"
Bram Moolenaar890ee4e2021-07-30 21:56:10 +0200118 # Wait a bit, so that the "ex" command is handled
119 # before the "ch_evalexpr() returns. Otherwise we are
120 # outside the try/catch when the "ex" command is
121 # handled.
122 time.sleep(0.02)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100123 elif decoded[1] == 'bad command':
124 cmd = '["ex","foo bar"]'
125 print("sending: {0}".format(cmd))
126 self.request.sendall(cmd.encode('utf-8'))
127 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100128 elif decoded[1] == 'do normal':
129 # Send a normal command.
130 cmd = '["normal","G$s more\u001b"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100131 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100132 self.request.sendall(cmd.encode('utf-8'))
133 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100134 elif decoded[1] == 'eval-works':
135 # Send an eval request. We ignore the response.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100136 cmd = '["expr","\\"foo\\" . 123", -1]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100137 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100138 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100139 response = "ok"
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100140 elif decoded[1] == 'eval-special':
141 # Send an eval request. We ignore the response.
142 cmd = '["expr","\\"foo\x7f\x10\x01bar\\"", -2]'
143 print("sending: {0}".format(cmd))
144 self.request.sendall(cmd.encode('utf-8'))
145 response = "ok"
146 elif decoded[1] == 'eval-getline':
147 # Send an eval request. We ignore the response.
148 cmd = '["expr","getline(3)", -3]'
149 print("sending: {0}".format(cmd))
150 self.request.sendall(cmd.encode('utf-8'))
151 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100152 elif decoded[1] == 'eval-fails':
153 # Send an eval request that will fail.
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100154 cmd = '["expr","xxx", -4]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100155 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100156 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100157 response = "ok"
Bram Moolenaar55fab432016-02-07 16:53:13 +0100158 elif decoded[1] == 'eval-error':
159 # Send an eval request that works but the result can't
160 # be encoded.
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100161 cmd = '["expr","function(\\"tr\\")", -5]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100162 print("sending: {0}".format(cmd))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100163 self.request.sendall(cmd.encode('utf-8'))
164 response = "ok"
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100165 elif decoded[1] == 'eval-bad':
166 # Send an eval request missing the third argument.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100167 cmd = '["expr","xxx"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100168 print("sending: {0}".format(cmd))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100169 self.request.sendall(cmd.encode('utf-8'))
170 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100171 elif decoded[1] == 'an expr':
172 # Send an expr request.
173 cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100174 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100175 self.request.sendall(cmd.encode('utf-8'))
176 response = "ok"
Bram Moolenaarece61b02016-02-20 21:39:05 +0100177 elif decoded[1] == 'call-func':
178 cmd = '["call","MyFunction",[1,2,3], 0]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100179 print("sending: {0}".format(cmd))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100180 self.request.sendall(cmd.encode('utf-8'))
181 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100182 elif decoded[1] == 'redraw':
183 cmd = '["redraw",""]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100184 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100185 self.request.sendall(cmd.encode('utf-8'))
186 response = "ok"
187 elif decoded[1] == 'redraw!':
188 cmd = '["redraw","force"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100189 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100190 self.request.sendall(cmd.encode('utf-8'))
191 response = "ok"
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100192 elif decoded[1] == 'empty-request':
193 cmd = '[]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100194 print("sending: {0}".format(cmd))
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100195 self.request.sendall(cmd.encode('utf-8'))
196 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100197 elif decoded[1] == 'eval-result':
198 # Send back the last received eval result.
199 response = last_eval
Bram Moolenaarf6157282016-02-10 21:07:14 +0100200 elif decoded[1] == 'call me':
201 cmd = '[0,"we called you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100202 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100203 self.request.sendall(cmd.encode('utf-8'))
204 response = "ok"
205 elif decoded[1] == 'call me again':
206 cmd = '[0,"we did call you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100207 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100208 self.request.sendall(cmd.encode('utf-8'))
209 response = ""
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100210 elif decoded[1] == 'send zero':
211 cmd = '[0,"zero index"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100212 print("sending: {0}".format(cmd))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100213 self.request.sendall(cmd.encode('utf-8'))
214 response = "sent zero"
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100215 elif decoded[1] == 'close me':
216 print("closing")
217 self.request.close()
218 response = ""
Bram Moolenaarece61b02016-02-20 21:39:05 +0100219 elif decoded[1] == 'wait a bit':
220 time.sleep(0.2)
221 response = "waited"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100222 elif decoded[1] == '!quit!':
223 # we're done
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100224 self.server.shutdown()
Bram Moolenaarb92abad2016-02-08 22:37:24 +0100225 return
Bram Moolenaare7bed622016-02-03 22:20:29 +0100226 elif decoded[1] == '!crash!':
227 # Crash!
228 42 / 0
229 else:
230 response = "what?"
231
Bram Moolenaarf6157282016-02-10 21:07:14 +0100232 if response == "":
233 print("no response")
234 else:
235 encoded = json.dumps([decoded[0], response])
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100236 print("sending: {0}".format(encoded))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100237 self.request.sendall(encoded.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100238
239 # Negative numbers are used for "eval" responses.
240 elif decoded[0] < 0:
241 last_eval = decoded
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100242
Bram Moolenaard7ece102016-02-02 23:23:02 +0100243class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
244 pass
245
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246def writePortInFile(port):
247 # Write the port number in Xportnr, so that the test knows it.
248 f = open("Xportnr", "w")
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100249 f.write("{0}".format(port))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100250 f.close()
251
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200252def main(host, port, server_class=ThreadedTCPServer):
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100253 # Wait half a second before opening the port to test waittime in ch_open().
254 # We do want to get the port number, get that first. We cannot open the
255 # socket, guess a port is free.
256 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200257 port = 13684
258 writePortInFile(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100259
260 print("Wait for it...")
261 time.sleep(0.5)
262
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200263 server = server_class((host, port), ThreadedTCPRequestHandler)
264 ip, port = server.server_address[0:2]
Bram Moolenaard7ece102016-02-02 23:23:02 +0100265
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100266 # Start a thread with the server. That thread will then start a new thread
267 # for each connection.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100268 server_thread = threading.Thread(target=server.serve_forever)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100269 server_thread.start()
270
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100271 writePortInFile(port)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100272
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100273 print("Listening on port {0}".format(port))
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100274
275 # Main thread terminates, but the server continues running
276 # until server.shutdown() is called.
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100277 try:
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200278 while server_thread.is_alive():
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100279 server_thread.join(1)
280 except (KeyboardInterrupt, SystemExit):
281 server.shutdown()
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200282
283if __name__ == "__main__":
284 main("localhost", 0)