diff options
author | Wolfy-J <[email protected]> | 2019-01-31 15:08:02 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2019-01-31 15:08:02 +0300 |
commit | 22f01fa5627d11f00652310abbddd7ba03947771 (patch) | |
tree | 9b4044c1cb771337e3198083467e5459ce4f3ad0 /src | |
parent | 7962c0b7cc01ac51ea6195dc5488da5ee408f513 (diff) | |
parent | df0acb7b531e77ec5c503a4996359b6c816060da (diff) |
Merge pull request #106 from Alex-Bond/feature/http-client
Raw HTTP client (HttpClient), PSR7Client now depends on HttpClient
Diffstat (limited to 'src')
-rw-r--r-- | src/HttpClient.php | 67 | ||||
-rw-r--r-- | src/PSR7Client.php | 65 |
2 files changed, 92 insertions, 40 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]) + ); + } +} diff --git a/src/PSR7Client.php b/src/PSR7Client.php index 6b280272..3276891b 100644 --- a/src/PSR7Client.php +++ b/src/PSR7Client.php @@ -20,8 +20,8 @@ use Psr\Http\Message\UploadedFileFactoryInterface; */ class PSR7Client { - /** @var Worker */ - private $worker; + /** @var HttpClient */ + private $httpClient; /** @var ServerRequestFactoryInterface */ private $requestFactory; @@ -38,18 +38,19 @@ class PSR7Client private static $allowedVersions = ['1.0', '1.1', '2',]; /** - * @param Worker $worker + * @param Worker $worker * @param ServerRequestFactoryInterface|null $requestFactory - * @param StreamFactoryInterface|null $streamFactory - * @param UploadedFileFactoryInterface|null $uploadsFactory + * @param StreamFactoryInterface|null $streamFactory + * @param UploadedFileFactoryInterface|null $uploadsFactory */ public function __construct( Worker $worker, ServerRequestFactoryInterface $requestFactory = null, StreamFactoryInterface $streamFactory = null, UploadedFileFactoryInterface $uploadsFactory = null - ) { - $this->worker = $worker; + ) + { + $this->httpClient = new HttpClient($worker); $this->requestFactory = $requestFactory ?? new Diactoros\ServerRequestFactory(); $this->streamFactory = $streamFactory ?? new Diactoros\StreamFactory(); $this->uploadsFactory = $uploadsFactory ?? new Diactoros\UploadedFileFactory(); @@ -61,7 +62,7 @@ class PSR7Client */ public function getWorker(): Worker { - return $this->worker; + return $this->httpClient->getWorker(); } /** @@ -69,46 +70,39 @@ class PSR7Client */ public function acceptRequest() { - $body = $this->worker->receive($ctx); - if (empty($body) && empty($ctx)) { - // termination request - return null; - } - - if (empty($ctx = json_decode($ctx, true))) { - // invalid context + $rawRequest = $this->httpClient->acceptRequest(); + if ($rawRequest === null) return null; - } - $_SERVER = $this->configureServer($ctx); + $_SERVER = $this->configureServer($rawRequest['ctx']); $request = $this->requestFactory->createServerRequest( - $ctx['method'], - $ctx['uri'], + $rawRequest['ctx']['method'], + $rawRequest['ctx']['uri'], $_SERVER ); - parse_str($ctx['rawQuery'], $query); + parse_str($rawRequest['ctx']['rawQuery'], $query); $request = $request - ->withProtocolVersion(static::fetchProtocolVersion($ctx['protocol'])) - ->withCookieParams($ctx['cookies']) + ->withProtocolVersion(static::fetchProtocolVersion($rawRequest['ctx']['protocol'])) + ->withCookieParams($rawRequest['ctx']['cookies']) ->withQueryParams($query) - ->withUploadedFiles($this->wrapUploads($ctx['uploads'])); + ->withUploadedFiles($this->wrapUploads($rawRequest['ctx']['uploads'])); - foreach ($ctx['attributes'] as $name => $value) { + foreach ($rawRequest['ctx']['attributes'] as $name => $value) { $request = $request->withAttribute($name, $value); } - foreach ($ctx['headers'] as $name => $value) { + foreach ($rawRequest['ctx']['headers'] as $name => $value) { $request = $request->withHeader($name, $value); } - if ($ctx['parsed']) { - $request = $request->withParsedBody(json_decode($body, true)); + if ($rawRequest['ctx']['parsed']) { + $request = $request->withParsedBody(json_decode($rawRequest['body'], true)); } else { - if ($body !== null) { - $request = $request->withBody($this->streamFactory->createStream($body)); + if ($rawRequest['body'] !== null) { + $request = $request->withBody($this->streamFactory->createStream($rawRequest['body'])); } } @@ -122,16 +116,7 @@ class PSR7Client */ public function respond(ResponseInterface $response) { - $headers = $response->getHeaders(); - if (empty($headers)) { - // this is required to represent empty header set as map and not as array - $headers = new \stdClass(); - } - - $this->worker->send( - $response->getBody()->__toString(), - json_encode(['status' => $response->getStatusCode(), 'headers' => $headers]) - ); + $this->httpClient->respond($response->getStatusCode(), $response->getBody()->__toString(), $response->getHeaders()); } /** |