Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Streaming Request #196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Http\Middleware\StreamingRequestMiddleware;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Socket\SocketServer;
Expand Down Expand Up @@ -231,9 +232,12 @@ public function run()

private function runLoop()
{
$http = new HttpServer(function (ServerRequestInterface $request) {
return $this->handleRequest($request);
});
$http = new HttpServer(...array_merge(
($this->handler->hastStreamingRequest() ? [new StreamingRequestMiddleware()] : []),
[function (ServerRequestInterface $request) {
return $this->handleRequest($request);
}]
));

$listen = $this->container->getEnv('X_LISTEN') ?? '127.0.0.1:8080';

Expand Down
11 changes: 11 additions & 0 deletions src/Io/MiddlewareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
namespace FrameworkX\Io;

use Psr\Http\Message\ServerRequestInterface;
use React\Http\Middleware\StreamingRequestMiddleware;

/**
* @internal
*/
class MiddlewareHandler
{
private $handlers;
private $hastStreamingRequest = false;

public function __construct(array $handlers)
{
assert(count($handlers) >= 2);

$this->handlers = $handlers;
foreach($this->handlers as $handler) {
if ($this->hastStreamingRequest = ($handler instanceof StreamingRequestMiddleware)) {
break;
}
}
}

public function __invoke(ServerRequestInterface $request)
Expand All @@ -33,4 +40,8 @@ private function call(ServerRequestInterface $request, int $position)
return $this->call($request, $position + 1);
});
}

public function hastStreamingRequest() {
return $this->hastStreamingRequest;
}
}