Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ jobs:
uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main
cs:
uses: innmind/github-workflows/.github/workflows/cs.yml@main
with:
php-version: '8.2'
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [Unreleased]

### Changed

- Requires PHP `8.4`
- Requires `innmind/foundation:~2.1`
- Requires `innmind/http-parser:~4.0`
- The environment variables are now exposed as third argument of `Innmind\Async\HttpServer\Main::handle()`

## 4.0.0 - 2025-08-24

### Changed
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ use Innmind\Http\{
};
use Innmind\Filesystem\Name;
use Innmind\Url\Path;
use Innmind\Immutable\Map;

new class extends Main {
protected static function handle(ServerRequest $request, OperatingSystem $os): Response
{
/**
* @param Map<string, string> $env
*/
protected static function handle(
ServerRequest $request,
OperatingSystem $os,
Map $env,
): Response {
return $os
->filesystem()
->mount(Path::of('somewhere/'))
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"issues": "http://github.com/innmind/async-http-server/issues"
},
"require": {
"php": "~8.2",
"innmind/foundation": "~1.9",
"innmind/http-parser": "~3.0"
"php": "~8.4",
"innmind/foundation": "~2.1",
"innmind/http-parser": "~4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -30,7 +30,7 @@
}
},
"require-dev": {
"innmind/static-analysis": "^1.2.1",
"innmind/static-analysis": "~1.3",
"innmind/black-box": "~6.5",
"innmind/coding-standard": "~2.0"
}
Expand Down
8 changes: 6 additions & 2 deletions fixtures/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
Header\ContentLength,
};
use Innmind\Filesystem\File\Content;
use Innmind\Immutable\Map;

new class extends Main {
protected static function handle(ServerRequest $request, OperatingSystem $os): Response
{
protected static function handle(
ServerRequest $request,
OperatingSystem $os,
Map $env,
): Response {
return Response::of(
StatusCode::ok,
$request->protocolVersion(),
Expand Down
22 changes: 14 additions & 8 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Innmind\Async\HttpServer\{
Server,
Open,
InjectEnvironment,
Display\Nothing,
};
use Innmind\CLI\{
Expand All @@ -19,20 +18,22 @@
use Innmind\Http\{
ServerRequest,
Response,
ServerRequest\Environment as HttpEnv,
};
use Innmind\Url\Authority\Port;
use Innmind\IP\IP;
use Innmind\Immutable\Attempt;
use Innmind\Immutable\{
Attempt,
Map,
};

final class Serve implements Command
{
private OperatingSystem $os;
/** @var callable(ServerRequest, OperatingSystem): Response */
/** @var callable(ServerRequest, OperatingSystem, Map<string, string>): Response */
private $handle;

/**
* @param callable(ServerRequest, OperatingSystem): Response $handle
* @param callable(ServerRequest, OperatingSystem, Map<string, string>): Response $handle
*/
private function __construct(
OperatingSystem $os,
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __invoke(Console $console): Attempt
}

/**
* @param callable(ServerRequest, OperatingSystem): Response $handle
* @param callable(ServerRequest, OperatingSystem, Map<string, string>): Response $handle
*/
public static function of(
OperatingSystem $os,
Expand Down Expand Up @@ -97,11 +98,16 @@ public function usage(): Usage
*/
private function serve(Console $console, Open $open): Attempt
{
$handle = $this->handle;

$server = Server::of(
$this->os->clock(),
$open,
InjectEnvironment::of(HttpEnv::of($console->variables())),
$this->handle,
static fn(ServerRequest $request, OperatingSystem $os) => $handle(
$request,
$os,
$console->variables(),
),
);

if ($console->options()->contains('no-output')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Innmind\Async\HttpServer;

use Innmind\TimeContinuum\Clock;
use Innmind\Time\Clock;
use Innmind\Http\{
Response,
Header\Date,
Expand Down
40 changes: 0 additions & 40 deletions src/InjectEnvironment.php

This file was deleted.

8 changes: 7 additions & 1 deletion src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
ServerRequest,
Response,
};
use Innmind\Immutable\Attempt;
use Innmind\Immutable\{
Attempt,
Map,
};

abstract class Main extends Cli
{
Expand All @@ -28,9 +31,12 @@ protected function main(Environment $env, OperatingSystem $os): Attempt

/**
* The handler is static to prevent sharing state between requests
*
* @param Map<string, string> $env Environment variables
*/
abstract protected static function handle(
ServerRequest $request,
OperatingSystem $os,
Map $env,
): Response;
}
40 changes: 9 additions & 31 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Task\Discard,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\TimeContinuum\Clock;
use Innmind\Time\Clock;
use Innmind\Filesystem\File\Content;
use Innmind\HttpParser\{
Request\Parse,
Expand All @@ -28,52 +28,36 @@
use Innmind\IO\Sockets\Servers\Server as IOServer;
use Innmind\Immutable\{
Attempt,
Sequence,
Maybe,
Str,
};

final class Server
{
private Open $open;
private IOServer|IOServer\Pool|null $servers = null;
private InjectEnvironment $injectEnv;
private Encode $encode;
/** @var callable(ServerRequest, OperatingSystem): Response */
private $handle;
private Display $display;

/**
* @psalm-mutation-free
*
* @param callable(ServerRequest, OperatingSystem): Response $handle
* @param \Closure(ServerRequest, OperatingSystem): Response $handle
*/
private function __construct(
Open $open,
InjectEnvironment $injectEnv,
Encode $encode,
callable $handle,
Display $display,
private Open $open,
private Encode $encode,
private \Closure $handle,
private Display $display,
private IOServer|IOServer\Pool|null $servers = null,
) {
$this->open = $open;
$this->injectEnv = $injectEnv;
$this->encode = $encode;
$this->handle = $handle;
$this->display = $display;
}

/**
* @param Attempt<Console> $console
* @param Continuation<Attempt<Console>> $continuation
* @param Sequence<mixed> $results
*
* @return Continuation<Attempt<Console>>
*/
public function __invoke(
Attempt $console,
OperatingSystem $os,
Continuation $continuation,
Sequence $results,
): Continuation {
$failed = $console->match(
static fn() => false,
Expand Down Expand Up @@ -117,7 +101,6 @@ public function __invoke(
),
);

$injectEnv = $this->injectEnv;
$handle = $this->handle;
$encode = $this->encode;

Expand All @@ -126,7 +109,6 @@ public function __invoke(
->accept()
->map(static fn($connection) => static function(OperatingSystem $os) use (
$connection,
$injectEnv,
$handle,
$encode,
) {
Expand All @@ -141,7 +123,6 @@ public function __invoke(
->map(DecodeCookie::of())
->map(DecodeQuery::of())
->map(DecodeForm::of())
->map($injectEnv)
->map(static function($request) use ($os, $handle) {
try {
return $handle($request, $os);
Expand Down Expand Up @@ -195,17 +176,15 @@ public function __invoke(
}

/**
* @param callable(ServerRequest, OperatingSystem): Response $handle
* @param \Closure(ServerRequest, OperatingSystem): Response $handle
*/
public static function of(
Clock $clock,
Open $open,
InjectEnvironment $injectEnv,
callable $handle,
\Closure $handle,
): self {
return new self(
$open,
$injectEnv,
new Encode($clock),
$handle,
Display::of(),
Expand All @@ -219,7 +198,6 @@ public function withOutput(Output $output): self
{
return new self(
$this->open,
$this->injectEnv,
$this->encode,
$this->handle,
$this->display->with($output),
Expand Down
Loading