summaryrefslogtreecommitdiff
path: root/src/HttpClient.php
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-01-31 15:08:02 +0300
committerGitHub <[email protected]>2019-01-31 15:08:02 +0300
commit22f01fa5627d11f00652310abbddd7ba03947771 (patch)
tree9b4044c1cb771337e3198083467e5459ce4f3ad0 /src/HttpClient.php
parent7962c0b7cc01ac51ea6195dc5488da5ee408f513 (diff)
parentdf0acb7b531e77ec5c503a4996359b6c816060da (diff)
Merge pull request #106 from Alex-Bond/feature/http-client
Raw HTTP client (HttpClient), PSR7Client now depends on HttpClient
Diffstat (limited to 'src/HttpClient.php')
-rw-r--r--src/HttpClient.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/HttpClient.php b/src/HttpClient.php
new file mode 100644
index 00000000..70b46ef5
--- /dev/null
+++ b/src/HttpClient.php
@@ -0,0 +1,67 @@
+<?php
+declare(strict_types=1);
+
+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(int $status, string $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])
+ );
+ }
+}