blob: fb8ed2243f3915c9eec47becc8c3a63df15109bb [file] [log] [blame]
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001#!/usr/bin/env python
2#
3# Server that will accept connections from a Vim channel.
4# Used by test_channel.vim to test LSP functionality.
5#
6# This requires Python 2.6 or later.
7
8from __future__ import print_function
9import json
10import socket
11import sys
12import time
13import threading
14
15try:
16 # Python 3
17 import socketserver
18except ImportError:
19 # Python 2
20 import SocketServer as socketserver
21
22class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
23
24 def setup(self):
25 self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
26
27 def send_lsp_msg(self, msgid, resp_dict):
28 v = {'jsonrpc': '2.0', 'result': resp_dict}
29 if msgid != -1:
30 v['id'] = msgid
31 s = json.dumps(v)
32 resp = "Content-Length: " + str(len(s)) + "\r\n"
33 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
34 resp += "\r\n"
35 resp += s
36 if self.debug:
37 with open("Xlspdebug.log", "a") as myfile:
38 myfile.write("\n=> send\n" + resp)
39 self.request.sendall(resp.encode('utf-8'))
40
41 def send_wrong_payload(self):
42 v = 'wrong-payload'
43 s = json.dumps(v)
44 resp = "Content-Length: " + str(len(s)) + "\r\n"
45 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
46 resp += "\r\n"
47 resp += s
48 self.request.sendall(resp.encode('utf-8'))
49
50 def send_empty_header(self, msgid, resp_dict):
51 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
52 s = json.dumps(v)
53 resp = "\r\n"
54 resp += s
55 self.request.sendall(resp.encode('utf-8'))
56
57 def send_empty_payload(self):
58 resp = "Content-Length: 0\r\n"
59 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
60 resp += "\r\n"
61 self.request.sendall(resp.encode('utf-8'))
62
63 def send_extra_hdr_fields(self, msgid, resp_dict):
64 # test for sending extra fields in the http header
65 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
66 s = json.dumps(v)
67 resp = "Host: abc.vim.org\r\n"
68 resp += "User-Agent: Python\r\n"
69 resp += "Accept-Language: en-US,en\r\n"
70 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
71 resp += "Content-Length: " + str(len(s)) + "\r\n"
72 resp += "\r\n"
73 resp += s
74 self.request.sendall(resp.encode('utf-8'))
75
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +010076 def send_delayed_payload(self, msgid, resp_dict):
77 # test for sending the hdr first and then after some delay, send the
78 # payload
79 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
80 s = json.dumps(v)
81 resp = "Content-Length: " + str(len(s)) + "\r\n"
82 resp += "\r\n"
83 self.request.sendall(resp.encode('utf-8'))
84 time.sleep(0.05)
85 resp = s
86 self.request.sendall(resp.encode('utf-8'))
87
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010088 def send_hdr_without_len(self, msgid, resp_dict):
89 # test for sending the http header without length
90 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
91 s = json.dumps(v)
92 resp = "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
93 resp += "\r\n"
94 resp += s
95 self.request.sendall(resp.encode('utf-8'))
96
97 def send_hdr_with_wrong_len(self, msgid, resp_dict):
98 # test for sending the http header with wrong length
99 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
100 s = json.dumps(v)
101 resp = "Content-Length: 1000\r\n"
102 resp += "\r\n"
103 resp += s
104 self.request.sendall(resp.encode('utf-8'))
105
106 def send_hdr_with_negative_len(self, msgid, resp_dict):
107 # test for sending the http header with negative length
108 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
109 s = json.dumps(v)
110 resp = "Content-Length: -1\r\n"
111 resp += "\r\n"
112 resp += s
113 self.request.sendall(resp.encode('utf-8'))
114
115 def do_ping(self, payload):
116 time.sleep(0.2)
117 self.send_lsp_msg(payload['id'], 'alive')
118
119 def do_echo(self, payload):
120 self.send_lsp_msg(-1, payload)
121
122 def do_simple_rpc(self, payload):
123 # test for a simple RPC request
124 self.send_lsp_msg(payload['id'], 'simple-rpc')
125
126 def do_rpc_with_notif(self, payload):
127 # test for sending a notification before replying to a request message
128 self.send_lsp_msg(-1, 'rpc-with-notif-notif')
129 # sleep for some time to make sure the notification is delivered
130 time.sleep(0.2)
131 self.send_lsp_msg(payload['id'], 'rpc-with-notif-resp')
132
133 def do_wrong_payload(self, payload):
134 # test for sending a non dict payload
135 self.send_wrong_payload()
136 time.sleep(0.2)
137 self.send_lsp_msg(-1, 'wrong-payload')
138
139 def do_rpc_resp_incorrect_id(self, payload):
140 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-1')
141 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-2')
142 self.send_lsp_msg(1, 'rpc-resp-incorrect-id-3')
143 time.sleep(0.2)
144 self.send_lsp_msg(payload['id'], 'rpc-resp-incorrect-id-4')
145
146 def do_simple_notif(self, payload):
147 # notification message test
148 self.send_lsp_msg(-1, 'simple-notif')
149
150 def do_multi_notif(self, payload):
151 # send multiple notifications
152 self.send_lsp_msg(-1, 'multi-notif1')
153 self.send_lsp_msg(-1, 'multi-notif2')
154
155 def do_msg_with_id(self, payload):
156 self.send_lsp_msg(payload['id'], 'msg-with-id')
157
158 def do_msg_specific_cb(self, payload):
159 self.send_lsp_msg(payload['id'], 'msg-specifc-cb')
160
161 def do_server_req(self, payload):
162 self.send_lsp_msg(201, {'method': 'checkhealth', 'params': {'a': 20}})
163
164 def do_extra_hdr_fields(self, payload):
165 self.send_extra_hdr_fields(payload['id'], 'extra-hdr-fields')
166
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +0100167 def do_delayad_payload(self, payload):
168 self.send_delayed_payload(payload['id'], 'delayed-payload')
169
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100170 def do_hdr_without_len(self, payload):
171 self.send_hdr_without_len(payload['id'], 'hdr-without-len')
172
173 def do_hdr_with_wrong_len(self, payload):
174 self.send_hdr_with_wrong_len(payload['id'], 'hdr-with-wrong-len')
175
176 def do_hdr_with_negative_len(self, payload):
177 self.send_hdr_with_negative_len(payload['id'], 'hdr-with-negative-len')
178
179 def do_empty_header(self, payload):
180 self.send_empty_header(payload['id'], 'empty-header')
181
182 def do_empty_payload(self, payload):
183 self.send_empty_payload()
184
185 def process_msg(self, msg):
186 try:
187 decoded = json.loads(msg)
188 print("Decoded:")
189 print(str(decoded))
190 if 'method' in decoded:
191 test_map = {
192 'ping': self.do_ping,
193 'echo': self.do_echo,
194 'simple-rpc': self.do_simple_rpc,
195 'rpc-with-notif': self.do_rpc_with_notif,
196 'wrong-payload': self.do_wrong_payload,
197 'rpc-resp-incorrect-id': self.do_rpc_resp_incorrect_id,
198 'simple-notif': self.do_simple_notif,
199 'multi-notif': self.do_multi_notif,
200 'msg-with-id': self.do_msg_with_id,
201 'msg-specifc-cb': self.do_msg_specific_cb,
202 'server-req': self.do_server_req,
203 'extra-hdr-fields': self.do_extra_hdr_fields,
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +0100204 'delayed-payload': self.do_delayad_payload,
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100205 'hdr-without-len': self.do_hdr_without_len,
206 'hdr-with-wrong-len': self.do_hdr_with_wrong_len,
207 'hdr-with-negative-len': self.do_hdr_with_negative_len,
208 'empty-header': self.do_empty_header,
209 'empty-payload': self.do_empty_payload
210 }
211 if decoded['method'] in test_map:
212 test_map[decoded['method']](decoded)
213 else:
214 print("Error: Unsupported method: " + decoded['method'])
215 else:
216 print("Error: 'method' field is not found")
217
218 except ValueError:
219 print("json decoding failed")
220
221 def process_msgs(self, msgbuf):
222 while True:
223 sidx = msgbuf.find('Content-Length: ')
224 if sidx == -1:
225 return msgbuf
226 sidx += 16
227 eidx = msgbuf.find('\r\n')
228 if eidx == -1:
229 return msgbuf
230 msglen = int(msgbuf[sidx:eidx])
231
232 hdrend = msgbuf.find('\r\n\r\n')
233 if hdrend == -1:
234 return msgbuf
235
236 # Remove the header
237 msgbuf = msgbuf[hdrend + 4:]
238 payload = msgbuf[:msglen]
239
240 self.process_msg(payload)
241
242 # Remove the processed message
243 msgbuf = msgbuf[msglen:]
244
245 def handle(self):
246 print("=== socket opened ===")
247 self.debug = False
248 msgbuf = ''
249 while True:
250 try:
251 received = self.request.recv(4096).decode('utf-8')
252 except socket.error:
253 print("=== socket error ===")
254 break
255 except IOError:
256 print("=== socket closed ===")
257 break
258 if received == '':
259 print("=== socket closed ===")
260 break
261 print("\nReceived:\n{0}".format(received))
262
263 # Write the received lines into the file for debugging
264 if self.debug:
265 with open("Xlspdebug.log", "a") as myfile:
266 myfile.write("\n<= recv\n" + received)
267
268 # Can receive more than one line in a response or a partial line.
269 # Accumulate all the received characters and process one line at
270 # a time.
271 msgbuf += received
272 msgbuf = self.process_msgs(msgbuf)
273
274class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
275 pass
276
277def writePortInFile(port):
278 # Write the port number in Xportnr, so that the test knows it.
279 f = open("Xportnr", "w")
280 f.write("{0}".format(port))
281 f.close()
282
283def main(host, port, server_class=ThreadedTCPServer):
284 # Wait half a second before opening the port to test waittime in ch_open().
285 # We do want to get the port number, get that first. We cannot open the
286 # socket, guess a port is free.
287 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
288 port = 13684
289 writePortInFile(port)
290
291 print("Wait for it...")
292 time.sleep(0.5)
293
294 server = server_class((host, port), ThreadedTCPRequestHandler)
295 ip, port = server.server_address[0:2]
296
297 # Start a thread with the server. That thread will then start a new thread
298 # for each connection.
299 server_thread = threading.Thread(target=server.serve_forever)
300 server_thread.start()
301
302 writePortInFile(port)
303
304 print("Listening on port {0}".format(port))
305
306 # Main thread terminates, but the server continues running
307 # until server.shutdown() is called.
308 try:
309 while server_thread.is_alive():
310 server_thread.join(1)
311 except (KeyboardInterrupt, SystemExit):
312 server.shutdown()
313
314if __name__ == "__main__":
315 main("localhost", 0)