Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Next Next commit
handle static resource before dispatching to laravel
  • Loading branch information
albertcht committed May 5, 2018
commit 47bb40b94af297551d89f662eb7e6d664651fa87
1 change: 1 addition & 0 deletions config/swoole_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'server' => [
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
'port' => env('SWOOLE_HTTP_PORT', '1215'),
'public_path' => base_path('public'),
'options' => [
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
Expand Down
37 changes: 35 additions & 2 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,18 @@ public function onRequest($swooleRequest, $swooleResponse)

$this->resetOnRequest();

$application = $this->getApplication();

try {
// transform swoole request to illuminate request
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();

// handle static file request first
if ($this->handleStaticRequest($illuminateRequest, $swooleResponse)) {
return;
}

// get laravel/lumen's application instance
$application = $this->getApplication();

// use cloned application if sandbox mode is on
if ($this->isSandbox) {
$application->getApplication()->instance('request', $illuminateRequest);
Expand Down Expand Up @@ -277,6 +283,33 @@ public function onRequest($swooleRequest, $swooleResponse)
}
}

/**
* Handle static file request.
*
* @param \Illuminate\Http\Request $illuminateRequest
* @param \Swoole\Http\Response $swooleResponse
*/
protected function handleStaticRequest($illuminateRequest, $swooleResponse)
{
$uri = $illuminateRequest->getRequestUri();
if (strpos($uri, '.php') || strpos($uri, '.htaccess') || strpos($uri, '.config')) {
return;
}

$publicPath = $this->container['config']->get('swoole_http.server.public_path', base_path('public'));
$filename = $publicPath . $uri;

if (! file_exists($filename)) {
return;
}

$swooleResponse->status(200);
$swooleResponse->header('Content-Type', mime_content_type($filename));
$swooleResponse->sendfile($filename);

return true;
}

/**
* Reset on every request.
*/
Expand Down