GithubHelp home page GithubHelp logo

amphp / http-server-static-content Goto Github PK

View Code? Open in Web Editor NEW
21.0 7.0 12.0 119 KB

An HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.

Home Page: https://amphp.org/http-server-static-content

License: MIT License

PHP 100.00%
amphp http static-files document-root php static-content http-server revolt

http-server-static-content's Introduction

http-server-static-content

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.

Installation

This package can be installed as a Composer dependency.

composer require amphp/http-server-static-content

Usage

This package provides two RequestHandler implementations:

  • DocumentRoot: Serves all files within a directory.
  • StaticResource: Serves a single specific file.

The example below combines static file serving and request routing to demonstrate how they work well together:

<?php

use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Http\Status;

$router = new Amp\Http\Server\Router;
// $server is an instance of HttpServer and $errorHandler an instance of ErrorHandler
$router->setFallback(new DocumentRoot($server, $errorHandler, __DIR__ . '/public'));
$router->addRoute('GET', '/', new ClosureRequestHandler(function () {
    return new Response(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));


$server->start($router, new DefaultErrorHandler());

A full example is found in examples/server.php.

Contributing

Please read our rules for details on our code of conduct, and the process for submitting pull requests to us.

Security

If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

http-server-static-content's People

Contributors

kelunik avatar peehaa avatar thgs avatar trowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

http-server-static-content's Issues

Why libuv?

In documentation there is a note that I should use libuv when using this. However the reason for that or what happens if it isn't used is missing.

Also if it is important it should be mentioned in the readme of this package.

I can send PRs if you can explain the reasons to me.

Extract remoteDotSegments

removeDotSegments() is currently a static function in DocumentRoot, it should be extracted to a function.

Issue with If-Modified-Since HTTP header

Hi there ๐Ÿ‘‹๐Ÿ™‚
I noticed that when I modify a file served by DocumentRoot, I have to force the refresh in my browser to actually see the updated file.
After some debugging, I think there is an inversion in the order of comparison:
https://github.com/amphp/http-server-static-content/blob/2.x/src/DocumentRoot.php#L304C49-L304C66

The code returns "unmodified" status if $mtime > $ifModifiedSince, but if I understand correctly it's the opposite, if $mtime > $ifModifiedSince it means that the file has been updated. (Corresponding doc for reference)

I'm currently running version 1, but as far as I can see the bug is present on branch 2 as well.
It doesn't seem to be covered by tests.

By the way, thank you very much for your work on this awesome library ๐Ÿ˜Š

Cached size is not updated

The cached size of files is not updated. If files are changed, this might result in a wrong content-length header.

Commit 9f43db6 content-length

PHP Recoverable fatal error:  Object of class Amp\Coroutine could not be converted to string in [...]/vendor/amphp/http-server-static-content/src/DocumentRoot.php on line 393

missed a yield there @ src/DocumentRoot.php:393

$headers["Content-Length"] = (string) $this->filesystem->size($fileInfo->path);

to

$headers["Content-Length"] = (string) yield $this->filesystem->size($fileInfo->path);

Serving from a subdirectory

If a path looks like /static/css/screen.css, and we want to only have a docroot for things inside /static, what's the way to go? (I.e. I have a file /foo/bar/css/screen.css and want to map /foo/bar onto /static)

Or another use case: in combination with prefixed Routers. Currently DocumentRoot looks at the full URI path and dispatches from there.

I know of amphp/http-server-router#3 which could solve the second case in particular, but we probably should provide a generic solution for this.

Refactor options

Instead of setOption() we should have an Options object similar to amphp/http-server and other repositories.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.