Skip to content

Commit

Permalink
Fixed bug that the root path of swagger server cannot work. (#6987)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored Aug 5, 2024
1 parent d430124 commit 4dd2bf1
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Hyperf\HttpServer\ResponseEmitter;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;

class HttpServer implements OnRequestInterface
{
Expand All @@ -45,24 +46,33 @@ public function __construct(protected ContainerInterface $container, protected R

public function onRequest($request, $response): void
{
if ($request instanceof ServerRequestInterface) {
$psr7Request = $request;
} else {
$psr7Request = Psr7Request::loadFromSwooleRequest($request);
}
try {
if ($request instanceof ServerRequestInterface) {
$psr7Request = $request;
} else {
$psr7Request = Psr7Request::loadFromSwooleRequest($request);
}

$path = $psr7Request->getUri()->getPath();
if ($path === $this->config['url']) {
$stream = new Stream($this->getHtml());
$contentType = 'text/html;charset=utf-8';
} else {
$stream = new Stream($this->getMetadata($path));
$contentType = 'application/json;charset=utf-8';
}
$path = $psr7Request->getUri()->getPath();
if ($path === $this->config['url']) {
$stream = new Stream($this->getHtml());
$contentType = 'text/html;charset=utf-8';
} else {
$stream = new Stream($this->getMetadata($path));
$contentType = 'application/json;charset=utf-8';
}

$psrResponse = (new Response())->setBody($stream)->setHeader('content-type', $contentType);
$psrResponse = (new Response())->setBody($stream)->setHeader('content-type', $contentType);

$this->emitter->emit($psrResponse, $response);
$this->emitter->emit($psrResponse, $response);
} catch (Throwable) {
$this->emitter->emit(
(new Response())
->setBody(new Stream('Server Error'))
->setHeader('content-type', 'text/html;charset=utf-8'),
$response
);
}
}

protected function getMetadata(string $path): string
Expand All @@ -73,7 +83,7 @@ protected function getMetadata(string $path): string
return $this->metadata[$id];
}

if (file_exists($path)) {
if (is_file($path) && file_exists($path)) {
$metadata = file_get_contents($path);
} else {
$metadata = Json::encode([
Expand Down

0 comments on commit 4dd2bf1

Please sign in to comment.