Skip to content
Draft
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5.25",
"laravel/pint": "^1.2",
"laravel/pint": "1.*",
"swoole/ide-helper": "4.8.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "1.*",
"phpbench/phpbench": "^1.2"
}
}
553 changes: 285 additions & 268 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Http/Adapter/FPM/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getRawPayload(): string
* @param string|null $default
* @return string|null
*/
public function getServer(string $key, string $default = null): ?string
public function getServer(string $key, ?string $default = null): ?string
{
return $_SERVER[$key] ?? $default;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/FPM/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function write(string $content): bool
* @param string $content
* @return void
*/
public function end(string $content = null): void
public function end(?string $content = null): void
{
if (!is_null($content)) {
echo $content;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/Swoole/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getRawPayload(): string
* @param string|null $default
* @return string|null
*/
public function getServer(string $key, string $default = null): ?string
public function getServer(string $key, ?string $default = null): ?string
{
return $this->swoole->server[$key] ?? $default;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/Swoole/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function write(string $content): bool
* @param string|null $content
* @return void
*/
public function end(string $content = null): void
public function end(?string $content = null): void
{
$this->swoole->end($content);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Adapter/Swoole/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Server extends Adapter
{
protected SwooleServer $server;

public function __construct(string $host, string $port = null, array $settings = [])
public function __construct(string $host, ?string $port = null, array $settings = [])
{
$this->server = new SwooleServer($host, $port);
$this->server->set(\array_merge($settings, [
Expand All @@ -42,7 +42,7 @@ public function onStart(callable $callback)

public function start()
{
if(Coroutine::getCid() === -1) {
if (Coroutine::getCid() === -1) {
run(fn () => $this->server->start());
} else {
$this->server->start();
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getCount(): int
*
* @throws \Exception
*/
public function load(string $directory, string $root = null): void
public function load(string $directory, ?string $root = null): void
{
if (!is_readable($directory)) {
throw new Exception("Failed to load directory: {$directory}");
Expand Down
17 changes: 11 additions & 6 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public static function error(): Hook
* @param string|null $default
* @return string|null
*/
public static function getEnv(string $key, string $default = null): ?string
public static function getEnv(string $key, ?string $default = null): ?string
{
return $_SERVER[$key] ?? $default;
}
Expand Down Expand Up @@ -366,7 +366,7 @@ public function getResource(string $name, string $context = 'utopia', bool $fres
$this->resources[$context] ??= [];

$resourcesCallback = &self::$resourcesCallbacks[$context] ?? [];
if(empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
if (empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
$resourcesCallback = &self::$resourcesCallbacks['utopia'];
}

Expand Down Expand Up @@ -514,7 +514,7 @@ public static function addRoute(string $method, string $url): Route
*
* @throws \Exception
*/
public function loadFiles(string $directory, string $root = null): void
public function loadFiles(string $directory, ?string $root = null): void
{
$this->files->load($directory, $root);
}
Expand Down Expand Up @@ -576,7 +576,7 @@ public function start()
try {
$this->run($request, $response, $context);
} finally {
if(isset(self::$resourcesCallbacks[$context])) {
if (isset(self::$resourcesCallbacks[$context])) {
unset(self::$resourcesCallbacks[$context]);
}
}
Expand All @@ -593,7 +593,7 @@ public function start()
$arguments = $this->getArguments($hook, 'utopia', [], []);
\call_user_func_array($hook->getAction(), $arguments);
}
} catch(\Exception $e) {
} catch (\Exception $e) {
self::setResource('error', fn () => $e);

foreach (self::$errors as $error) { // Global error hooks
Expand Down Expand Up @@ -628,6 +628,11 @@ public function match(Request $request, bool $fresh = true): ?Route
}

$url = \parse_url($request->getURI(), PHP_URL_PATH);

if ($url === null || $url === false) {
$url = '/'; // Default to root path for malformed URLs
}

$method = $request->getMethod();
$method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method;

Expand Down Expand Up @@ -793,7 +798,7 @@ public function run(Request $request, Response $response, string $context): stat
$arguments = $this->getArguments($hook, $context, [], []);
\call_user_func_array($hook->getAction(), $arguments);
}
} catch(\Exception $e) {
} catch (\Exception $e) {
self::setResource('error', fn () => $e, [], $context);

foreach (self::$errors as $error) { // Global error hooks
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ abstract public function getRawPayload(): string;
* @param string|null $default
* @return string|null
*/
abstract public function getServer(string $key, string $default = null): ?string;
abstract public function getServer(string $key, ?string $default = null): ?string;

/**
* Set server
Expand Down
20 changes: 10 additions & 10 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ public function getHeaders(): array
* Add an HTTP cookie to response header
*
* @param string $name
* @param string $value
* @param int $expire
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httponly
* @param string $sameSite
*/
public function addCookie(string $name, string $value = null, int $expire = null, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null, string $sameSite = null): static
* @param string|null $value
* @param int|null $expire
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool|null $httponly
* @param string|null $sameSite
*/
public function addCookie(string $name, ?string $value = null, ?int $expire = null, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, ?string $sameSite = null): static
{
$name = strtolower($name);

Expand Down Expand Up @@ -522,7 +522,7 @@ abstract public function write(string $content): bool;
* @param string $content
* @return void
*/
abstract public function end(string $content = null): void;
abstract public function end(?string $content = null): void;

/**
* Output response
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Validator/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(protected array $validators, protected string $type
*/
public function getDescription(): string
{
if(!(\is_null($this->failedRule))) {
if (!(\is_null($this->failedRule))) {
$description = $this->failedRule->getDescription();
} else {
$description = $this->validators[0]->getDescription();
Expand All @@ -51,7 +51,7 @@ public function isValid(mixed $value): bool
foreach ($this->validators as $rule) {
$valid = $rule->isValid($value);

if(!$valid) {
if (!$valid) {
$this->failedRule = $rule;
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Validator/AnyOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(protected array $validators, protected string $type
*/
public function getDescription(): string
{
if(!(\is_null($this->failedRule))) {
if (!(\is_null($this->failedRule))) {
$description = $this->failedRule->getDescription();
} else {
$description = $this->validators[0]->getDescription();
Expand All @@ -53,7 +53,7 @@ public function isValid(mixed $value): bool

$this->failedRule = $rule;

if($valid) {
if ($valid) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Validator/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getDescription(): string
{
$msg = 'Value must a valid array';

if($this->length > 0) {
if ($this->length > 0) {
$msg .= ' no longer than ' . $this->length . ' items';
}

Expand Down
6 changes: 5 additions & 1 deletion src/Http/Validator/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public function isValid($value): bool
return false;
}

if (\filter_var($value, FILTER_VALIDATE_DOMAIN) === false) {
if (\filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) {
return false;
}

if (\str_ends_with($value, '.') || \str_ends_with($value, '-')) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Validator/NoneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getDescription(): string
{
$description = '';

if(!(\is_null($this->failedRule))) {
if (!(\is_null($this->failedRule))) {
$description = $this->failedRule->getDescription();
} else {
$description = $this->validators[0]->getDescription();
Expand All @@ -53,7 +53,7 @@ public function isValid(mixed $value): bool
foreach ($this->validators as $rule) {
$valid = $rule->isValid($value);

if($valid) {
if ($valid) {
$this->failedRule = $rule;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testCanSetStatus()

try {
$this->response->setStatusCode(0); // Unknown status code
} catch(\Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('\Exception', $e);

return;
Expand Down
15 changes: 13 additions & 2 deletions tests/Validator/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ public function testIsValid()
$this->assertEquals(true, $this->domain->isValid('example.com'));
$this->assertEquals(true, $this->domain->isValid('subdomain.example.com'));
$this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com'));
$this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com'));
$this->assertEquals(false, $this->domain->isValid('subdomain.example_app.com'));
$this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com'));
$this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com'));
$this->assertEquals(false, $this->domain->isValid('subdomain_new.example.com'));
$this->assertEquals(true, $this->domain->isValid('localhost'));
$this->assertEquals(true, $this->domain->isValid('example.io'));
$this->assertEquals(true, $this->domain->isValid('example.org'));
$this->assertEquals(true, $this->domain->isValid('example.org'));
$this->assertEquals(false, $this->domain->isValid(false));
$this->assertEquals(false, $this->domain->isValid('api.appwrite.io.'));
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('api..appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('api-.appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('api.-appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('app write.io'));
$this->assertEquals(false, $this->domain->isValid(' appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('appwrite.io '));
$this->assertEquals(false, $this->domain->isValid('-appwrite.io'));
$this->assertEquals(false, $this->domain->isValid('appwrite.io-'));
$this->assertEquals(false, $this->domain->isValid('.'));
$this->assertEquals(false, $this->domain->isValid('..'));
$this->assertEquals(false, $this->domain->isValid(''));
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,27 @@ public function testFile()
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
$this->assertEquals(204, $response['headers']['status-code']);
}

public function testDoubleSlash()
{
$response = $this->client->call(Client::METHOD_GET, '//');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Hello World!', $response['body']);

$response = $this->client->call(Client::METHOD_GET, '//path-404');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Hello World!', $response['body']);

$response = $this->client->call(Client::METHOD_GET, '//value/123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);

$response = $this->client->call(Client::METHOD_GET, '/value//123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);

$response = $this->client->call(Client::METHOD_GET, '//value//123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
}
}
Loading