blob: 36aad2b778a0aa78c130382cdff6d8afdafce222 [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):
25 self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
26
Bram Moolenaard7ece102016-02-02 23:23:02 +010027 def handle(self):
28 print("=== socket opened ===")
Bram Moolenaard7ece102016-02-02 23:23:02 +010029 while True:
30 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010031 received = self.request.recv(4096).decode('utf-8')
Bram Moolenaard7ece102016-02-02 23:23:02 +010032 except socket.error:
33 print("=== socket error ===")
34 break
35 except IOError:
36 print("=== socket closed ===")
37 break
Bram Moolenaar608a8912016-02-03 22:39:51 +010038 if received == '':
Bram Moolenaard7ece102016-02-02 23:23:02 +010039 print("=== socket closed ===")
40 break
Bram Moolenaara63cdb52016-03-20 18:24:45 +010041 print("received: {0}".format(received))
Bram Moolenaard7ece102016-02-02 23:23:02 +010042
Bram Moolenaare7bed622016-02-03 22:20:29 +010043 # We may receive two messages at once. Take the part up to the
Bram Moolenaarf1f07922016-08-26 17:58:53 +020044 # newline, which should be after the matching "]".
Bram Moolenaar608a8912016-02-03 22:39:51 +010045 todo = received
46 while todo != '':
Bram Moolenaarf1f07922016-08-26 17:58:53 +020047 splitidx = todo.find('\n')
Bram Moolenaare7bed622016-02-03 22:20:29 +010048 if splitidx < 0:
Bram Moolenaar608a8912016-02-03 22:39:51 +010049 used = todo
50 todo = ''
Bram Moolenaard7ece102016-02-02 23:23:02 +010051 else:
Bram Moolenaarf1f07922016-08-26 17:58:53 +020052 used = todo[:splitidx]
Bram Moolenaar608a8912016-02-03 22:39:51 +010053 todo = todo[splitidx + 1:]
54 if used != received:
Bram Moolenaara63cdb52016-03-20 18:24:45 +010055 print("using: {0}".format(used))
Bram Moolenaard7ece102016-02-02 23:23:02 +010056
Bram Moolenaare7bed622016-02-03 22:20:29 +010057 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010058 decoded = json.loads(used)
Bram Moolenaare7bed622016-02-03 22:20:29 +010059 except ValueError:
60 print("json decoding failed")
61 decoded = [-1, '']
Bram Moolenaard7ece102016-02-02 23:23:02 +010062
Bram Moolenaare7bed622016-02-03 22:20:29 +010063 # Send a response if the sequence number is positive.
64 if decoded[0] >= 0:
65 if decoded[1] == 'hello!':
66 # simply send back a string
67 response = "got it"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +010068 elif decoded[1] == 'malformed1':
69 cmd = '["ex",":"]wrong!["ex","smi"]'
70 print("sending: {0}".format(cmd))
71 self.request.sendall(cmd.encode('utf-8'))
72 response = "ok"
73 # Need to wait for Vim to give up, otherwise it
74 # sometimes fails on OS X.
75 time.sleep(0.2)
76 elif decoded[1] == 'malformed2':
77 cmd = '"unterminated string'
78 print("sending: {0}".format(cmd))
79 self.request.sendall(cmd.encode('utf-8'))
80 response = "ok"
81 # Need to wait for Vim to give up, otherwise the double
82 # quote in the "ok" response terminates the string.
83 time.sleep(0.2)
84 elif decoded[1] == 'malformed3':
85 cmd = '["ex","missing ]"'
86 print("sending: {0}".format(cmd))
87 self.request.sendall(cmd.encode('utf-8'))
88 response = "ok"
89 # Need to wait for Vim to give up, otherwise the ]
90 # in the "ok" response terminates the list.
91 time.sleep(0.2)
92 elif decoded[1] == 'split':
93 cmd = '["ex","let '
94 print("sending: {0}".format(cmd))
95 self.request.sendall(cmd.encode('utf-8'))
96 time.sleep(0.01)
97 cmd = 'g:split = 123"]'
98 print("sending: {0}".format(cmd))
99 self.request.sendall(cmd.encode('utf-8'))
100 response = "ok"
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100101 elif decoded[1].startswith("echo "):
102 # send back the argument
103 response = decoded[1][5:]
Bram Moolenaare7bed622016-02-03 22:20:29 +0100104 elif decoded[1] == 'make change':
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100105 # Send two ex commands at the same time, before
106 # replying to the request.
Bram Moolenaare7bed622016-02-03 22:20:29 +0100107 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
108 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100109 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100110 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100111 response = "ok"
Bram Moolenaarb836f632021-07-01 22:11:28 +0200112 elif decoded[1] == 'echoerr':
113 cmd = '["ex","echoerr \\\"this is an error\\\""]'
114 print("sending: {0}".format(cmd))
115 self.request.sendall(cmd.encode('utf-8'))
116 response = "ok"
Bram Moolenaar890ee4e2021-07-30 21:56:10 +0200117 # Wait a bit, so that the "ex" command is handled
118 # before the "ch_evalexpr() returns. Otherwise we are
119 # outside the try/catch when the "ex" command is
120 # handled.
121 time.sleep(0.02)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100122 elif decoded[1] == 'bad command':
123 cmd = '["ex","foo bar"]'
124 print("sending: {0}".format(cmd))
125 self.request.sendall(cmd.encode('utf-8'))
126 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100127 elif decoded[1] == 'do normal':
128 # Send a normal command.
129 cmd = '["normal","G$s more\u001b"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100130 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100131 self.request.sendall(cmd.encode('utf-8'))
132 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100133 elif decoded[1] == 'eval-works':
134 # Send an eval request. We ignore the response.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100135 cmd = '["expr","\\"foo\\" . 123", -1]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100136 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100137 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100138 response = "ok"
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100139 elif decoded[1] == 'eval-special':
140 # Send an eval request. We ignore the response.
141 cmd = '["expr","\\"foo\x7f\x10\x01bar\\"", -2]'
142 print("sending: {0}".format(cmd))
143 self.request.sendall(cmd.encode('utf-8'))
144 response = "ok"
145 elif decoded[1] == 'eval-getline':
146 # Send an eval request. We ignore the response.
147 cmd = '["expr","getline(3)", -3]'
148 print("sending: {0}".format(cmd))
149 self.request.sendall(cmd.encode('utf-8'))
150 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100151 elif decoded[1] == 'eval-fails':
152 # Send an eval request that will fail.
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100153 cmd = '["expr","xxx", -4]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100154 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100155 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100156 response = "ok"
Bram Moolenaar55fab432016-02-07 16:53:13 +0100157 elif decoded[1] == 'eval-error':
158 # Send an eval request that works but the result can't
159 # be encoded.
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100160 cmd = '["expr","function(\\"tr\\")", -5]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100161 print("sending: {0}".format(cmd))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100162 self.request.sendall(cmd.encode('utf-8'))
163 response = "ok"
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100164 elif decoded[1] == 'eval-bad':
165 # Send an eval request missing the third argument.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100166 cmd = '["expr","xxx"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100167 print("sending: {0}".format(cmd))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100168 self.request.sendall(cmd.encode('utf-8'))
169 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100170 elif decoded[1] == 'an expr':
171 # Send an expr request.
172 cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100173 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100174 self.request.sendall(cmd.encode('utf-8'))
175 response = "ok"
Bram Moolenaarece61b02016-02-20 21:39:05 +0100176 elif decoded[1] == 'call-func':
177 cmd = '["call","MyFunction",[1,2,3], 0]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100178 print("sending: {0}".format(cmd))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100179 self.request.sendall(cmd.encode('utf-8'))
180 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100181 elif decoded[1] == 'redraw':
182 cmd = '["redraw",""]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100183 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100184 self.request.sendall(cmd.encode('utf-8'))
185 response = "ok"
186 elif decoded[1] == 'redraw!':
187 cmd = '["redraw","force"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100188 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100189 self.request.sendall(cmd.encode('utf-8'))
190 response = "ok"
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100191 elif decoded[1] == 'empty-request':
192 cmd = '[]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100193 print("sending: {0}".format(cmd))
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100194 self.request.sendall(cmd.encode('utf-8'))
195 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100196 elif decoded[1] == 'eval-result':
197 # Send back the last received eval result.
198 response = last_eval
Bram Moolenaarf6157282016-02-10 21:07:14 +0100199 elif decoded[1] == 'call me':
200 cmd = '[0,"we called you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100201 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100202 self.request.sendall(cmd.encode('utf-8'))
203 response = "ok"
204 elif decoded[1] == 'call me again':
205 cmd = '[0,"we did call you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100206 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100207 self.request.sendall(cmd.encode('utf-8'))
208 response = ""
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100209 elif decoded[1] == 'send zero':
210 cmd = '[0,"zero index"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100211 print("sending: {0}".format(cmd))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100212 self.request.sendall(cmd.encode('utf-8'))
213 response = "sent zero"
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100214 elif decoded[1] == 'close me':
215 print("closing")
216 self.request.close()
217 response = ""
Bram Moolenaarece61b02016-02-20 21:39:05 +0100218 elif decoded[1] == 'wait a bit':
219 time.sleep(0.2)
220 response = "waited"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100221 elif decoded[1] == '!quit!':
222 # we're done
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100223 self.server.shutdown()
Bram Moolenaarb92abad2016-02-08 22:37:24 +0100224 return
Bram Moolenaare7bed622016-02-03 22:20:29 +0100225 elif decoded[1] == '!crash!':
226 # Crash!
227 42 / 0
228 else:
229 response = "what?"
230
Bram Moolenaarf6157282016-02-10 21:07:14 +0100231 if response == "":
232 print("no response")
233 else:
234 encoded = json.dumps([decoded[0], response])
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100235 print("sending: {0}".format(encoded))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100236 self.request.sendall(encoded.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100237
238 # Negative numbers are used for "eval" responses.
239 elif decoded[0] < 0:
240 last_eval = decoded
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100241
Bram Moolenaard7ece102016-02-02 23:23:02 +0100242class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
243 pass
244
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100245def writePortInFile(port):
246 # Write the port number in Xportnr, so that the test knows it.
247 f = open("Xportnr", "w")
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100248 f.write("{0}".format(port))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100249 f.close()
250
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200251def main(host, port, server_class=ThreadedTCPServer):
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100252 # Wait half a second before opening the port to test waittime in ch_open().
253 # We do want to get the port number, get that first. We cannot open the
254 # socket, guess a port is free.
255 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200256 port = 13684
257 writePortInFile(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100258
259 print("Wait for it...")
260 time.sleep(0.5)
261
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200262 server = server_class((host, port), ThreadedTCPRequestHandler)
263 ip, port = server.server_address[0:2]
Bram Moolenaard7ece102016-02-02 23:23:02 +0100264
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100265 # Start a thread with the server. That thread will then start a new thread
266 # for each connection.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100267 server_thread = threading.Thread(target=server.serve_forever)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100268 server_thread.start()
269
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100270 writePortInFile(port)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100271
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100272 print("Listening on port {0}".format(port))
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100273
274 # Main thread terminates, but the server continues running
275 # until server.shutdown() is called.
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100276 try:
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200277 while server_thread.is_alive():
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100278 server_thread.join(1)
279 except (KeyboardInterrupt, SystemExit):
280 server.shutdown()
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200281
282if __name__ == "__main__":
283 main("localhost", 0)