diff options
Diffstat (limited to 'src/HttpClient.php')
-rw-r--r-- | src/HttpClient.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/HttpClient.php b/src/HttpClient.php new file mode 100644 index 00000000..94c70998 --- /dev/null +++ b/src/HttpClient.php @@ -0,0 +1,66 @@ +<?php + +namespace Spiral\RoadRunner; + +class HttpClient +{ + /** @var Worker */ + private $worker; + + /** + * @param Worker $worker + */ + public function __construct(Worker $worker) + { + $this->worker = $worker; + } + + /** + * @return Worker + */ + public function getWorker(): Worker + { + return $this->worker; + } + + /** + * @return array|null Request information as ['ctx'=>[], 'body'=>string] or null if termination request or invalid context. + */ + public function acceptRequest() + { + $body = $this->getWorker()->receive($ctx); + if (empty($body) && empty($ctx)) { + // termination request + return null; + } + + if (empty($ctx = json_decode($ctx, true))) { + // invalid context + return null; + } + + return ['ctx' => $ctx, 'body' => $body]; + } + + /** + * Send response to the application server. + * + * @param int $status Http status code + * @param string $body Body of response + * @param string[][] $headers An associative array of the message's headers. Each + * key MUST be a header name, and each value MUST be an array of strings + * for that header. + */ + public function respond($status, $body, $headers = []) + { + if (empty($headers)) { + // this is required to represent empty header set as map and not as array + $headers = new \stdClass(); + } + + $this->getWorker()->send( + $body, + json_encode(['status' => $status, 'headers' => $headers]) + ); + } +} |