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

Commit 3b58429f authored by eckhart's avatar eckhart
Browse files

- server.py: typing info added

parent f5de30af
No preview for this file type
...@@ -51,6 +51,7 @@ from DHParser.toolkit import get_config_value, re ...@@ -51,6 +51,7 @@ from DHParser.toolkit import get_config_value, re
RPC_Table = Dict[str, Callable] RPC_Table = Dict[str, Callable]
RPC_Type = Union[RPC_Table, List[Callable], Callable] RPC_Type = Union[RPC_Table, List[Callable], Callable]
JSON_Type = Union[Dict, Sequence, str, int, None]
SERVER_ERROR = "COMPILER-SERVER-ERROR" SERVER_ERROR = "COMPILER-SERVER-ERROR"
...@@ -60,7 +61,7 @@ SERVER_ONLINE = 2 ...@@ -60,7 +61,7 @@ SERVER_ONLINE = 2
SERVER_TERMINATE = 3 SERVER_TERMINATE = 3
RX_MAYBE_JSON = re.compile('\s*(?:\{|\[|"|\d|true|false|null)') RX_MAYBE_JSON = re.compile(r'\s*(?:{|\[|"|\d|true|false|null)')
response = b'''HTTP/1.1 200 OK response = b'''HTTP/1.1 200 OK
...@@ -95,24 +96,24 @@ class Server: ...@@ -95,24 +96,24 @@ class Server:
else: else:
assert isinstance(rpc_functions, Callable) assert isinstance(rpc_functions, Callable)
func = cast(Callable, rpc_functions) func = cast(Callable, rpc_functions)
self.rpc_table = { func.__name__: func } self.rpc_table = {func.__name__: func}
self.max_source_size = get_config_value('max_rpc_size') self.max_source_size = get_config_value('max_rpc_size') #type: int
self.stage = Value('b', SERVER_OFFLINE) self.stage = Value('b', SERVER_OFFLINE) # type: Value
self.server = None self.server = None # type: Optional[asyncio.AbstractServer]
self.server_messages = Queue() # type: Queue self.server_messages = Queue() # type: Queue
self.server_process = None # type: Optional[Process] self.server_process = None # type: Optional[Process]
async def handle_compilation_request(self, async def handle_compilation_request(self,
reader: asyncio.StreamReader, reader: asyncio.StreamReader,
writer: asyncio.StreamWriter): writer: asyncio.StreamWriter):
data = await reader.read(self.max_source_size + 1) rpc_error = None # type: Optional[Tuple[int, str]]
# writer.write(response) json_id = 'null' # type: Tuple[int, str]
# await writer.drain() obj = {} # type: Dict
# writer.close() result = None # type: JSON_Type
raw = None # type: JSON_Type
rpc_error = None data = await reader.read(self.max_source_size + 1)
json_id = 'null'
if len(data) > self.max_source_size: if len(data) > self.max_source_size:
rpc_error = -32600, "Invaild Request: Source code too large! Only %i MB allowed" \ rpc_error = -32600, "Invaild Request: Source code too large! Only %i MB allowed" \
...@@ -120,21 +121,22 @@ class Server: ...@@ -120,21 +121,22 @@ class Server:
if rpc_error is None: if rpc_error is None:
try: try:
obj = json.loads(data) raw = json.loads(data)
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:
rpc_error = -32700, "JSONDecodeError: " + str(e) rpc_error = -32700, "JSONDecodeError: " + str(e)
if rpc_error is None: if rpc_error is None:
if isinstance(obj, Dict): if isinstance(raw, Dict):
json_id = obj.get('id', 'null') if isinstance(obj, Dict) else 'null' obj = cast(Dict, raw)
json_id = obj.get('id', 'null')
else: else:
rpc_error = -32700, 'Parse error: Request does not appear to be an RPC-call!?' rpc_error = -32700, 'Parse error: Request does not appear to be an RPC-call!?'
if rpc_error is None: if rpc_error is None:
if obj.get('jsonrpc', 'unknown') != '2.0': if obj.get('jsonrpc', 'unknown') != '2.0':
rpc_error = -32600, 'Invalid Request: jsonrpc version 2.0 needed, version "' \ rpc_error = -32600, 'Invalid Request: jsonrpc version 2.0 needed, version "' \
' "%s" found.' % obj.get('jsonrpc', b'unknown') ' "%s" found.' % obj.get('jsonrpc', b'unknown')
elif not 'method' in obj: elif 'method' not in obj:
rpc_error = -32600, 'Invalid Request: No method specified.' rpc_error = -32600, 'Invalid Request: No method specified.'
elif obj['method'] not in self.rpc_table: elif obj['method'] not in self.rpc_table:
rpc_error = -32601, 'Method not found: ' + str(obj['method']) rpc_error = -32601, 'Method not found: ' + str(obj['method'])
...@@ -162,7 +164,8 @@ class Server: ...@@ -162,7 +164,8 @@ class Server:
# self.server.cancel() # self.server.cancel()
async def serve(self, address: str = '127.0.0.1', port: int = 8888): 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) self.server = cast(asyncio.base_events.Server,
await asyncio.start_server(self.handle_compilation_request, address, port))
async with self.server: async with self.server:
self.stage.value = SERVER_ONLINE self.stage.value = SERVER_ONLINE
self.server_messages.put(SERVER_ONLINE) self.server_messages.put(SERVER_ONLINE)
......
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