2017 © Pedro Peláez
 

library web

Web applications for the XP Framework

image

xp-forge/web

Web applications for the XP Framework

  • Sunday, July 22, 2018
  • by thekid
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,374 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 11 Open issues
  • 35 Versions
  • 35 % Grown

The README.md

Web applications for the XP Framework

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version, (*1)

Low-level functionality for serving HTTP requests, including the xp web runner., (*2)

Example

use web\Application;

class Service extends Application {

  public function routes() {
    return [
      '/hello' => function($req, $res) {
        $res->answer(200, 'OK');
        $res->send('Hello '.$req->param('name', 'Guest'), 'text/plain');
      }
    ];
  }
}

Run it using:, (*3)

$ xp -supervise web Service
@xp.web.srv.Standalone(HTTP @ peer.ServerSocket(Resource id #61 -> tcp://127.0.0.1:8080))
# ...

Supports a development webserver which is slower but allows an easy edit/save/reload development process. It uses the PHP development server in the background., (*4)

$ xp -supervise web -m develop Service
@xp.web.srv.Develop(HTTP @ `php -S 127.0.0.1:8080 -t  /home/example/devel/shorturl`)
# ...

Now open the website at http://localhost:8080/hello, (*5)

Server models

The four server models (selectable via -m <model> on the command line) are:, (*6)

  • async (the default since 3.0.0): A single-threaded web server. Handlers can yield control back to the server to serve other clients during lengthy operations such as file up- and downloads.
  • sequential: Same as above, but blocks until one client's HTTP request handler has finished executing before serving the next request.
  • prefork: Much like Apache, forks a given number of children to handle HTTP requests. Requires the pcntl extension.
  • develop: As mentioned above, built ontop of the PHP development wenserver. Application code is recompiled and application setup performed from scratch on every request, errors and debug output are handled by the development console.

Request and response

The web.Request class provides the following basic functionality:, (*7)

use web\Request;

$request= ...

$request->method();       // The HTTP method, e.g. "GET"
$request->uri();          // The request URI, a util.URI instance

$request->headers();      // All request headers as a map
$request->header($name);  // The value of a single header

$request->cookies();      // All cookies
$request->cookie($name);  // The value of a single cookie

$request->params();       // All request parameters as a map
$request->param($name);   // The value of a single parameter

The web.Response class provides the following basic functionality:, (*8)

use web\{Response, Cookie};

$response= ...

// Set status code, header(s) and cookie(s)
$response->answer($status);
$response->header($name, $value);
$response->cookie(new Cookie($name, $value));

// Sends body using a given content type
$response->send($body, $type);

// Transfers an input stream using a given content type. Uses
// chunked transfer-encoding.
yield from $response->transmit($in, $type);

// Same as above, but specifies content length before-hand
yield from $response->transmit($in, $type, $size);

Both Request and Response have a stream() method for accessing the underlying in- and output streams., (*9)

Handlers

A handler (also referred to as middleware in some frameworks) is a function which receives a request and response and uses the above functionality to handle communication., (*10)

use web\Handler;

$redirect= new class() implements Handler {

  public function handle($req, $res) {
    $req->status(302);
    $req->header('Location', 'https://example.com/');
  }
};

This library comes with web.handler.FilesFrom - a handler for serving files. It takes care of conditional requests (with If-Modified-Since) as well requests for content ranges, and makes use of the asynchronous capabilities if available, see here., (*11)

Filters

Filters wrap around handlers and can perform tasks before and after the handlers are invoked. You can use the request's pass() method to pass values - handlers can access these using value($name) / values()., (*12)

use web\Filter;
use util\profiling\Timer;
use util\log\{Logging, LogCategory};

$timer= new class(Logging::all()->toConsole()) implements Filter {
  private $timer;

  public function __construct(private LogCategory $cat) {
    $this->timer= new Timer();
  }

  public function filter($request, $response, $invocation) {
    $this->timer->start();
    try {
      yield from $invocation->proceed($request, $response);
    } finally {
      $this->cat->debugf('%s: %.3f seconds', $request->uri(), $this->timer->elapsedTime());
    }
  }
}

By using yield from, you guarantee asynchronous handlers will have completely executed before the time measurement is run on in the finally block., (*13)

File uploads

File uploads are handled by the request's multipart() method. In contrast to how PHP works, file uploads are streamed and your handler starts running with the first byte transmitted!, (*14)

use io\Folder;

$uploads= new Folder('...');
$handler= function($req, $res) use($uploads) {
  if ($multipart= $req->multipart()) {

    // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100
    if ('100-continue' === $req->header('Expect')) {
      $res->hint(100, 'Continue');
    }

    // Transmit files to uploads directory asynchronously
    $files= [];
    $bytes= 0;
    foreach ($multipart->files() as $name => $file) {
      $files[]= $name;
      $bytes+= yield from $file->transmit($uploads);
    }

    // Do something with files and bytes...
  }
};

Early hints

An experimental status code with which headers can be sent to a client early along for it to be able to make optimizations, e.g. preloading scripts and stylesheets., (*15)

$handler= function($req, $res) {
  $res->header('Link', [
    '</main.css>; rel=preload; as=style',
    '</script.js>; rel=preload; as=script'
  ]);
  $res->hint(103);

  // Do some processing here to render $html
  $html= ...

  $res->answer(200, 'OK');
  $res->send($html, 'text/html; charset=utf-8');
}

See https://evertpot.com/http/103-early-hints, (*16)

Internal redirects

On top of external redirects which are triggered by the 3XX status codes, requests can also be redirected internally using the dispatch() method. This has the benefit of not requiring clients to perfom an additional request., (*17)

use web\Application;

class Site extends Application {

  public function routes() {
    return [
      '/home' => function($req, $res) {
        // Home page
      },
      '/' => function($req, $res) {
        // Routes are re-evaluated as if user had called /home
        return $req->dispatch('/home');
      },
    ];
  }
}

Logging

By default, logging goes to standard output and will be visible in the console the xp web command was invoked from. It can be influenced via the command line as follows:, (*18)

  • -l server.log: Writes to the file server.log, creating it if necessary
  • -l -: Writes to standard output
  • -l - -l server.log: Writes to both of the above

More fine-grained control as well as integrating with the logging library can be achieved from inside the application, see here., (*19)

Performance

Because the code for the web application is only compiled once when using production servers, we achieve lightning-fast request/response roundtrip times:, (*20)

Network console screenshot, (*21)

See also

This library provides for the very basic functionality. To create web frontends or REST APIs, have a look at the following libraries built ontop of this:, (*22)

The Versions

22/07 2018

dev-feature/authenticate-request

dev-feature/authenticate-request http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/06 2018

dev-master

9999999-dev http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/06 2018

v1.3.0

1.3.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

30/05 2018

dev-refactor/behind-proxy-base

dev-refactor/behind-proxy-base http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

20/05 2018

dev-feature/reverse-proxy-filter

dev-feature/reverse-proxy-filter http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/04 2018

v1.2.0

1.2.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/04 2018

v1.1.0

1.1.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/04 2018

dev-feature/dispatch

dev-feature/dispatch http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/04 2018

dev-feature/segments

dev-feature/segments http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

22/04 2018

v1.0.1

1.0.1.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

10/04 2018

v1.0.0

1.0.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

13/02 2018

v0.14.1

0.14.1.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

12/02 2018

v0.14.0

0.14.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

05/02 2018

v0.13.0

0.13.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

31/01 2018

v0.12.0

0.12.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

28/01 2018

dev-feature/paths

dev-feature/paths http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

21/01 2018

v0.11.0

0.11.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

20/01 2018

v0.10.0

0.10.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

22/12 2017

v0.9.0

0.9.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

05/12 2017

v0.8.3

0.8.3.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

25/11 2017

v0.8.2

0.8.2.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

19/11 2017

v0.8.1

0.8.1.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

19/11 2017

v0.8.0

0.8.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

12/11 2017

v0.7.0

0.7.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

16/08 2017

v0.6.3

0.6.3.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/07 2017

dev-feature/annotation-based-delegates

dev-feature/annotation-based-delegates http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/07 2017

v0.6.2

0.6.2.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/07 2017

v0.6.1

0.6.1.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/07 2017

v0.6.0

0.6.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

04/07 2017

dev-feature/response-error

dev-feature/response-error http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

03/07 2017

v0.5.0

0.5.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

30/06 2017

v0.4.0

0.4.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

25/06 2017

v0.3.0

0.3.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

04/06 2017

v0.2.0

0.2.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

07/05 2017

v0.1.0

0.1.0.0 http://xp-framework.net/

Web applications for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp