h5ai icon indicating copy to clipboard operation
h5ai copied to clipboard

Request: support for PHP built-in server

Open vaites opened this issue 8 years ago • 22 comments

PHP 5.4 and greater comes with a basic built-in web server. h5ai doesn't support it but with a very simple router file works without major issues:

<?php

// full path of requested file
$path = dirname(__DIR__) . preg_replace('/\?(.*)/', '', $_SERVER['REQUEST_URI']);

// h5ai is required when user request a directory
if(file_exists($path) && is_dir($path))
{
    // directory must not contain index.php or index.html
    if(file_exists("$path/index.php") == false && file_exists("$path/index.html") == false)
    {
        // script name must be "tweaked" to detect path
        $_SERVER['SCRIPT_NAME'] = '/_h5ai/public/index.php';

        include __DIR__ . "/public/index.php";
        exit;
    }
}

// CSS is sent with text/html content type, so we need to fix it
if($path == __DIR__ . '/public/css/styles.css')
{
    header('Content-Type: text/css');
}

// return resource "as-is"
return false;

If you put this code in a _h5ai/router.php and run php -S localhost:8000 _h5ai/router.php being in the root of the web server, h5ai works like a charm.

Can this feature be integrated in h5ai? Didn't make a pull request because of don't know how do you want to integrate this code. I think the best way is a standalone router file...

Why use h5ai with built-in web server? Because it can be used to enhance very lightweight servers in embedded systems like OpenWRT or Android without the need of LightHTTPd, Nginx or Apache.

vaites avatar Aug 15 '16 23:08 vaites