Skip to content

Commit

Permalink
Update react/http to stable release
Browse files Browse the repository at this point in the history
  • Loading branch information
phpbg committed Mar 20, 2021
1 parent 2946b5d commit a604f34
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This framework is designed to build quick proofs of concepts.

It is **not** mature enough to run in production environments, because:
* it still contains synchronous blocking code
* react http itself is not stable
* it lacks a dependency (ioc) /configuration management

## License
MIT
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": ">=7.0",
"ext-zlib": "*",
"ext-json": "*",
"react/http": "^0.8.5",
"react/http": "^1.0",
"zendframework/zend-validator": "^2.10",
"zendframework/zend-filter": "^2.8",
"psr/log": "^1.0",
Expand Down
60 changes: 48 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/Middleware/StaticContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

namespace PhpBg\MiniHttpd\Middleware;

use GuzzleHttp\Psr7\Stream;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\LoopInterface;

/**
* Serve static files
Expand All @@ -48,11 +50,18 @@ class StaticContent
protected $mimeNamesByExtension;

/**
* @var LoopInterface
*/
protected $loop;

/**
* @param LoopInterface $loop
* @param string $publicPath Full path to the directory that contain files to serve. Ex: /var/www/public
* @param array $mimeNamesByExtension array of mimes names, by (unique) extension. E.g. ['html' => 'application/html']
*/
public function __construct(string $publicPath, array $mimeNamesByExtension)
public function __construct(LoopInterface $loop, string $publicPath, array $mimeNamesByExtension)
{
$this->loop = $loop;
if (!is_dir($publicPath)) {
throw new \RuntimeException();
}
Expand Down Expand Up @@ -80,12 +89,12 @@ public function __invoke(ServerRequestInterface $request, callable $next = null)
if ($mime !== null) {
$headers['Content-Type'] = $mime;
}
return new \React\Http\Response(
return new \React\Http\Message\Response(
200,
$headers,
// Beware that this won't achieve good performance and scalability, mainly because native php file streams may or not be blocking, who knows...?
// @see https://bugs.php.net/bug.php?id=75538
fopen($realPath, 'rb')
new Stream(fopen($realPath, 'rb'))
);
}

Expand All @@ -94,7 +103,7 @@ public function __invoke(ServerRequestInterface $request, callable $next = null)
}

// If no next middleware, then issue a 404 not found
return new \React\Http\Response(
return new \React\Http\Message\Response(
404
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

use PhpBg\MiniHttpd\Renderer\RendererInterface;
use Psr\Http\Message\ResponseInterface;
use React\Http\Response;
use React\Http\Message\Response;

/**
* This class is used to pass data between all middlewares in both direction
Expand Down
4 changes: 2 additions & 2 deletions src/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function createDefaultStack(ApplicationContext $applicationContext
if (isset($applicationContext->publicPath) && file_exists($applicationContext->publicPath)) {
$applicationContext->logger->notice("Serving *all* files from: $applicationContext->publicPath");
$applicationContext->logger->notice("Don't hide your secrets there");
$middlewares[] = new StaticContent($applicationContext->publicPath, $mimeDb->getNamesByExtension());
$middlewares[] = new StaticContent($applicationContext->loop, $applicationContext->publicPath, $mimeDb->getNamesByExtension());
}

// Render responses
Expand Down Expand Up @@ -111,7 +111,7 @@ public static function createDefaultStack(ApplicationContext $applicationContext
public static function create(ApplicationContext $applicationContext): Server
{
$middlewares = static::createDefaultStack($applicationContext);
$server = new Server($middlewares);
$server = new Server($applicationContext->loop, ...$middlewares);

// Log server errors
$server->on('error', function ($exception) use ($applicationContext) {
Expand Down

0 comments on commit a604f34

Please sign in to comment.