From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- services/app/src/SandboxExec/ExecutorClient.php | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 services/app/src/SandboxExec/ExecutorClient.php (limited to 'services/app/src/SandboxExec/ExecutorClient.php') diff --git a/services/app/src/SandboxExec/ExecutorClient.php b/services/app/src/SandboxExec/ExecutorClient.php new file mode 100644 index 0000000..783c5ae --- /dev/null +++ b/services/app/src/SandboxExec/ExecutorClient.php @@ -0,0 +1,93 @@ + $code, + 'input' => $input, + 'timeout' => $this->timeoutMsec, + ]); + $context = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'follow_location' => 0, + 'header' => [ + 'Content-type: application/json', + 'Accept: application/json', + ], + 'content' => $bodyJson, + 'timeout' => ($this->timeoutMsec + 1000) / 1000, + ], + ]); + $result = file_get_contents( + $this->apiEndpoint . '/exec', + context: $context, + ); + if ($result === false) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to connect to the executor service', + ); + } + $json = json_decode($result, true); + if ($json === null) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to parse the response from the executor service: invalid JSON', + ); + } + + if (!is_array($json)) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to parse the response from the executor service: root object is not an array', + ); + } + if (!isset($json['status']) || !is_string($json['status'])) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to parse the response from the executor service: "status" is not a string', + ); + } + if (!isset($json['stdout']) || !is_string($json['stdout'])) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to parse the response from the executor service: "stdout" is not a string', + ); + } + if (!isset($json['stderr']) || !is_string($json['stderr'])) { + return new ExecutionResult( + status: ExecutionStatus::IE, + stdout: '', + stderr: 'Failed to parse the response from the executor service: "stderr" is not a string', + ); + } + + return new ExecutionResult( + status: ExecutionStatus::fromString($json['status']), + stdout: $json['stdout'], + stderr: $json['stderr'], + ); + } +} -- cgit v1.2.3-70-g09d2