10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit d4d487d1 authored by Eckhart Arnold's avatar Eckhart Arnold

- server.py: bugfix

parent 056be947
......@@ -78,19 +78,29 @@ class Server:
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter):
data = await reader.read(self.max_source_size + 1)
rpc_error = None
json_id = 'null'
if len(data) > self.max_source_size:
writer.write('{"jsonrpc": "2.0", "error": {"code": -32600, "message": '
'"Invaild Request: Source code too large! Only %i MB allowed"}, '
'"id": null}' % (self.max_source_size // (1024**2)))
else:
obj = json.loads(data)
rpc_error = None
json_id = obj.get('id', 'null') if isinstance(obj, Dict) else 'null'
if not isinstance(obj, Dict):
rpc_error = -32600, "Invaild Request: Source code too large! Only %i MB allowed" \
% (self.max_source_size // (1024**2))
if rpc_error is None:
try:
obj = json.loads(data)
except json.decoder.JSONDecodeError as e:
rpc_error = -32700, "JSONDecodeError: " + str(e)
if rpc_error is None:
if isinstance(obj, Dict):
json_id = obj.get('id', 'null') if isinstance(obj, Dict) else 'null'
else:
rpc_error = -32700, 'Parse error: Request does not appear to be an RPC-call!?'
elif obj.get('jsonrpc', 'unknown') != '2.0':
rpc_error = -32600, 'Invalid Request: jsonrpc version 2.0 needed, version "%s" ' \
'found.' % obj.get('jsonrpc', 'unknown')
if rpc_error is None:
if obj.get('jsonrpc', 'unknown') != '2.0':
rpc_error = -32600, 'Invalid Request: jsonrpc version 2.0 needed, version "' \
' "%s" found.' % obj.get('jsonrpc', b'unknown')
elif not 'method' in obj:
rpc_error = -32600, 'Invalid Request: No method specified.'
elif obj['method'] not in self.rpc_table:
......@@ -106,12 +116,12 @@ class Server:
except Exception as e:
rpc_error = -32602, "Invalid Params: " + str(e)
if rpc_error is None:
json_result = {"jsonrpc": "2.0", "result": result, "id": json_id}
json.dump(writer, json_result)
else:
writer.write(b'{"jsonrpc": "2.0", "error": {"code": %i, "message": %s}, "id": %s '
% (rpc_error[0], rpc_error[1], json_id))
if rpc_error is None:
json_result = {"jsonrpc": "2.0", "result": result, "id": json_id}
json.dump(writer, json_result)
else:
writer.write(('{"jsonrpc": "2.0", "error": {"code": %i, "message": "%s"}, "id": %s '
% (rpc_error[0], rpc_error[1], json_id)).encode())
await writer.drain()
writer.close()
# TODO: add these lines in case a terminate signal is received, i.e. exit server coroutine
......@@ -120,7 +130,6 @@ class Server:
async def serve(self, address: str = '127.0.0.1', port: int = 8888):
self.server = await asyncio.start_server(self.handle_compilation_request, address, port)
print(type(self.server))
async with self.server:
self.stage.value = SERVER_ONLINE
self.server_messages.put(SERVER_ONLINE)
......
......@@ -25,6 +25,7 @@ cdef class Node:
# cpdef select_by_tag(self, tag_names, include_root)
cpdef pick(self, tag_names)
# cpdef tree_size(self)
cpdef to_json_obj(self)
cdef class FrozenNode(Node):
......
......@@ -753,9 +753,9 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def to_json_obj(self) -> Dict:
"""Seralize a node or tree as json-object"""
data = [ self.tag_name,
[child.to_json_obj() for child in self.children] if self.children
else str(self._result)]
data = [self.tag_name,
[child.to_json_obj() for child in self.children]
if self.children else str(self._result)]
has_attr = self.attr_active()
if self._pos >= 0 or has_attr:
data.append(self._pos)
......
......@@ -28,7 +28,7 @@ from typing import Tuple
sys.path.extend(['../', './'])
from DHParser.server import CompilerServer
from DHParser.server import Server
scriptdir = os.path.dirname(os.path.realpath(__file__))
......@@ -40,13 +40,13 @@ def compiler_dummy(src: str, log_dir: str) -> Tuple[str, str]:
class TestServer:
def test_server(self):
cs = CompilerServer(compiler_dummy)
cs = Server(compiler_dummy)
cs.run_as_process()
async def compile(src, log_dir):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(src.encode())
data = await reader.read(100)
data = await reader.read(500)
print(f'Received: {data.decode()!r}')
writer.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment