blob: ea0fd1034a90a7cf946a5407318060c56c3904d7 [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
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +010027 def debuglog(self, msg):
28 if self.debug:
29 with open("Xlspserver.log", "a") as myfile:
30 myfile.write(msg)
31
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010032 def send_lsp_msg(self, msgid, resp_dict):
33 v = {'jsonrpc': '2.0', 'result': resp_dict}
34 if msgid != -1:
35 v['id'] = msgid
36 s = json.dumps(v)
37 resp = "Content-Length: " + str(len(s)) + "\r\n"
38 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
39 resp += "\r\n"
40 resp += s
41 if self.debug:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +010042 self.debuglog("SEND: ({0} bytes) '{1}'\n".format(len(resp), resp))
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010043 self.request.sendall(resp.encode('utf-8'))
44
45 def send_wrong_payload(self):
46 v = 'wrong-payload'
47 s = json.dumps(v)
48 resp = "Content-Length: " + str(len(s)) + "\r\n"
49 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
50 resp += "\r\n"
51 resp += s
52 self.request.sendall(resp.encode('utf-8'))
53
54 def send_empty_header(self, msgid, resp_dict):
55 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
56 s = json.dumps(v)
57 resp = "\r\n"
58 resp += s
59 self.request.sendall(resp.encode('utf-8'))
60
61 def send_empty_payload(self):
62 resp = "Content-Length: 0\r\n"
63 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
64 resp += "\r\n"
65 self.request.sendall(resp.encode('utf-8'))
66
67 def send_extra_hdr_fields(self, msgid, resp_dict):
68 # test for sending extra fields in the http header
69 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
70 s = json.dumps(v)
71 resp = "Host: abc.vim.org\r\n"
72 resp += "User-Agent: Python\r\n"
73 resp += "Accept-Language: en-US,en\r\n"
74 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
75 resp += "Content-Length: " + str(len(s)) + "\r\n"
76 resp += "\r\n"
77 resp += s
78 self.request.sendall(resp.encode('utf-8'))
79
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +010080 def send_delayed_payload(self, msgid, resp_dict):
81 # test for sending the hdr first and then after some delay, send the
82 # payload
83 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
84 s = json.dumps(v)
85 resp = "Content-Length: " + str(len(s)) + "\r\n"
86 resp += "\r\n"
87 self.request.sendall(resp.encode('utf-8'))
88 time.sleep(0.05)
89 resp = s
90 self.request.sendall(resp.encode('utf-8'))
91
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010092 def send_hdr_without_len(self, msgid, resp_dict):
93 # test for sending the http header without length
94 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
95 s = json.dumps(v)
96 resp = "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
97 resp += "\r\n"
98 resp += s
99 self.request.sendall(resp.encode('utf-8'))
100
101 def send_hdr_with_wrong_len(self, msgid, resp_dict):
102 # test for sending the http header with wrong length
103 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
104 s = json.dumps(v)
105 resp = "Content-Length: 1000\r\n"
106 resp += "\r\n"
107 resp += s
108 self.request.sendall(resp.encode('utf-8'))
109
110 def send_hdr_with_negative_len(self, msgid, resp_dict):
111 # test for sending the http header with negative length
112 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
113 s = json.dumps(v)
114 resp = "Content-Length: -1\r\n"
115 resp += "\r\n"
116 resp += s
117 self.request.sendall(resp.encode('utf-8'))
118
119 def do_ping(self, payload):
120 time.sleep(0.2)
121 self.send_lsp_msg(payload['id'], 'alive')
122
123 def do_echo(self, payload):
124 self.send_lsp_msg(-1, payload)
125
126 def do_simple_rpc(self, payload):
127 # test for a simple RPC request
128 self.send_lsp_msg(payload['id'], 'simple-rpc')
129
130 def do_rpc_with_notif(self, payload):
131 # test for sending a notification before replying to a request message
132 self.send_lsp_msg(-1, 'rpc-with-notif-notif')
133 # sleep for some time to make sure the notification is delivered
134 time.sleep(0.2)
135 self.send_lsp_msg(payload['id'], 'rpc-with-notif-resp')
136
137 def do_wrong_payload(self, payload):
138 # test for sending a non dict payload
139 self.send_wrong_payload()
140 time.sleep(0.2)
141 self.send_lsp_msg(-1, 'wrong-payload')
142
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100143 def do_large_payload(self, payload):
144 # test for sending a large (> 64K) payload
145 self.send_lsp_msg(payload['id'], payload)
146
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100147 def do_rpc_resp_incorrect_id(self, payload):
148 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-1')
149 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-2')
150 self.send_lsp_msg(1, 'rpc-resp-incorrect-id-3')
151 time.sleep(0.2)
152 self.send_lsp_msg(payload['id'], 'rpc-resp-incorrect-id-4')
153
154 def do_simple_notif(self, payload):
155 # notification message test
156 self.send_lsp_msg(-1, 'simple-notif')
157
158 def do_multi_notif(self, payload):
159 # send multiple notifications
160 self.send_lsp_msg(-1, 'multi-notif1')
161 self.send_lsp_msg(-1, 'multi-notif2')
162
163 def do_msg_with_id(self, payload):
164 self.send_lsp_msg(payload['id'], 'msg-with-id')
165
166 def do_msg_specific_cb(self, payload):
167 self.send_lsp_msg(payload['id'], 'msg-specifc-cb')
168
169 def do_server_req(self, payload):
170 self.send_lsp_msg(201, {'method': 'checkhealth', 'params': {'a': 20}})
171
172 def do_extra_hdr_fields(self, payload):
173 self.send_extra_hdr_fields(payload['id'], 'extra-hdr-fields')
174
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +0100175 def do_delayad_payload(self, payload):
176 self.send_delayed_payload(payload['id'], 'delayed-payload')
177
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100178 def do_hdr_without_len(self, payload):
179 self.send_hdr_without_len(payload['id'], 'hdr-without-len')
180
181 def do_hdr_with_wrong_len(self, payload):
182 self.send_hdr_with_wrong_len(payload['id'], 'hdr-with-wrong-len')
183
184 def do_hdr_with_negative_len(self, payload):
185 self.send_hdr_with_negative_len(payload['id'], 'hdr-with-negative-len')
186
187 def do_empty_header(self, payload):
188 self.send_empty_header(payload['id'], 'empty-header')
189
190 def do_empty_payload(self, payload):
191 self.send_empty_payload()
192
193 def process_msg(self, msg):
194 try:
195 decoded = json.loads(msg)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100196 if 'method' in decoded:
197 test_map = {
198 'ping': self.do_ping,
199 'echo': self.do_echo,
200 'simple-rpc': self.do_simple_rpc,
201 'rpc-with-notif': self.do_rpc_with_notif,
202 'wrong-payload': self.do_wrong_payload,
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100203 'large-payload': self.do_large_payload,
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100204 'rpc-resp-incorrect-id': self.do_rpc_resp_incorrect_id,
205 'simple-notif': self.do_simple_notif,
206 'multi-notif': self.do_multi_notif,
207 'msg-with-id': self.do_msg_with_id,
208 'msg-specifc-cb': self.do_msg_specific_cb,
209 'server-req': self.do_server_req,
210 'extra-hdr-fields': self.do_extra_hdr_fields,
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +0100211 'delayed-payload': self.do_delayad_payload,
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100212 'hdr-without-len': self.do_hdr_without_len,
213 'hdr-with-wrong-len': self.do_hdr_with_wrong_len,
214 'hdr-with-negative-len': self.do_hdr_with_negative_len,
215 'empty-header': self.do_empty_header,
216 'empty-payload': self.do_empty_payload
217 }
218 if decoded['method'] in test_map:
219 test_map[decoded['method']](decoded)
220 else:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100221 self.debuglog("Error: Unsupported method - " + decoded['method'] + "\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100222 else:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100223 self.debuglog("Error: 'method' field is not found\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100224
225 except ValueError:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100226 self.debuglog("Error: json decoding failed\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100227
228 def process_msgs(self, msgbuf):
229 while True:
230 sidx = msgbuf.find('Content-Length: ')
231 if sidx == -1:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100232 # partial message received
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100233 return msgbuf
234 sidx += 16
235 eidx = msgbuf.find('\r\n')
236 if eidx == -1:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100237 # partial message received
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100238 return msgbuf
239 msglen = int(msgbuf[sidx:eidx])
240
241 hdrend = msgbuf.find('\r\n\r\n')
242 if hdrend == -1:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100243 # partial message received
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100244 return msgbuf
245
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100246 if msglen > len(msgbuf[hdrend + 4:]):
247 if self.debug:
248 self.debuglog("Partial message ({0} bytes)\n".format(len(msgbuf)))
249 # partial message received
250 return msgbuf
251
252 if self.debug:
253 self.debuglog("Complete message ({0} bytes) received\n".format(msglen))
254
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100255 # Remove the header
256 msgbuf = msgbuf[hdrend + 4:]
257 payload = msgbuf[:msglen]
258
259 self.process_msg(payload)
260
261 # Remove the processed message
262 msgbuf = msgbuf[msglen:]
263
264 def handle(self):
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100265 self.debug = False
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100266 self.debuglog("=== socket opened ===\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100267 msgbuf = ''
268 while True:
269 try:
270 received = self.request.recv(4096).decode('utf-8')
271 except socket.error:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100272 self.debuglog("=== socket error ===\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100273 break
274 except IOError:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100275 self.debuglog("=== socket closed ===\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100276 break
277 if received == '':
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100278 self.debuglog("=== socket closed ===\n")
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100279 break
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100280
281 # Write the received lines into the file for debugging
282 if self.debug:
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +0100283 self.debuglog("RECV: ({0} bytes) '{1}'\n".format(len(received), received))
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100284
285 # Can receive more than one line in a response or a partial line.
286 # Accumulate all the received characters and process one line at
287 # a time.
288 msgbuf += received
289 msgbuf = self.process_msgs(msgbuf)
290
291class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
292 pass
293
294def writePortInFile(port):
295 # Write the port number in Xportnr, so that the test knows it.
296 f = open("Xportnr", "w")
297 f.write("{0}".format(port))
298 f.close()
299
300def main(host, port, server_class=ThreadedTCPServer):
301 # Wait half a second before opening the port to test waittime in ch_open().
302 # We do want to get the port number, get that first. We cannot open the
303 # socket, guess a port is free.
304 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
305 port = 13684
306 writePortInFile(port)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100307 time.sleep(0.5)
308
309 server = server_class((host, port), ThreadedTCPRequestHandler)
310 ip, port = server.server_address[0:2]
311
312 # Start a thread with the server. That thread will then start a new thread
313 # for each connection.
314 server_thread = threading.Thread(target=server.serve_forever)
315 server_thread.start()
316
317 writePortInFile(port)
318
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100319 # Main thread terminates, but the server continues running
320 # until server.shutdown() is called.
321 try:
322 while server_thread.is_alive():
323 server_thread.join(1)
324 except (KeyboardInterrupt, SystemExit):
325 server.shutdown()
326
327if __name__ == "__main__":
328 main("localhost", 0)