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
2 changes: 1 addition & 1 deletion packages/http-client/src/Driver/Psr18Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function convertTempestRequestToPsrRequest(Request $tempestRequest): Req
$request = $this->requestFactory->createRequest(
method: $tempestRequest->method->value,
uri: $this->uriFactory->createUri(
$tempestRequest->uri,
$tempestRequest->uri->toString(),
),
);

Expand Down
39 changes: 15 additions & 24 deletions packages/http/src/IsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

/**
* @phpstan-require-implements \Tempest\Http\Request
*
* @mago-expect lint:too-many-properties
*/
trait IsRequest
{
#[SkipValidation]
private(set) Method $method;

#[SkipValidation]
private(set) string $uri;
private(set) Uri $uri;

#[SkipValidation]
private(set) ?string $raw = null;
Expand All @@ -37,12 +35,6 @@ trait IsRequest
#[SkipValidation]
private(set) RequestHeaders $headers;

#[SkipValidation]
private(set) string $path;

#[SkipValidation]
private(set) array $query;

/** @var array<array-key, \Tempest\Http\Upload|array> */
#[SkipValidation]
private(set) array $files;
Expand All @@ -60,31 +52,30 @@ trait IsRequest

public function __construct(
Method $method,
string $uri,
Uri|string $uri,
array $body = [],
array $headers = [],
array $files = [],
?string $raw = null,
IpAddress|string|null $ip = null,
) {
$this->method = $method;
$this->uri = $uri;
$this->uri = $uri instanceof Uri ? $uri : Uri::from($uri);
$this->body = $body;
$this->headers = RequestHeaders::normalizeFromArray($headers);
$this->files = $files;
$this->raw = $raw;
$this->ip = IpAddress::tryFrom($ip);

if ($this->method === Method::CONNECT) {
$this->path ??= '';
$this->query ??= [];

return;
}

$uri = Uri::from($this->uri);
$this->path ??= rawurldecode($uri->path ?? '');
$this->query ??= $uri->query;
// if ($this->method === Method::CONNECT) {
// $this->path ??= '';
// $this->query ??= [];
//
// return;
// }
//
// $this->path ??= rawurldecode($uri->path ?? '');
// $this->query ??= $uri->query;
}

public function get(string $key, mixed $default = null): mixed
Expand All @@ -93,8 +84,8 @@ public function get(string $key, mixed $default = null): mixed
return get_by_key($this->body, $key);
}

if (array_key_exists($key, $this->query)) {
return get_by_key($this->query, $key);
if (array_key_exists($key, $this->uri->query)) {
return get_by_key($this->uri->query, $key);
}

return $default;
Expand Down Expand Up @@ -133,7 +124,7 @@ public function hasBody(?string $key = null): bool

public function hasQuery(string $key): bool
{
return has_key($this->query, $key);
return has_key($this->uri->query, $key);
}

public function accepts(ContentType ...$contentTypes): bool
Expand Down
16 changes: 11 additions & 5 deletions packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Tempest\Http\Upload;
use Tempest\Mapper\Mapper;
use Tempest\Support\Arr;
use Tempest\Support\Uri\Uri;
use Throwable;

use function Tempest\Mapper\map;
Expand Down Expand Up @@ -53,18 +54,23 @@ public function map(mixed $from, mixed $to): GenericRequest

$headers = RequestHeaders::normalizeFromArray($headersAsString);

parse_str($from->getUri()->getQuery(), $query);

$uploads = $this->createUploads($from->getUploadedFiles());

$uri = $from->getUri();

return map([
'method' => $this->requestMethod($from, $data),
'uri' => (string) $from->getUri(),
'uri' => new Uri(
scheme: $uri->getScheme(),
host: $uri->getHost(),
port: $uri->getPort(),
path: $uri->getPath(),
queryString: $uri->getQuery(),
fragment: $uri->getFragment(),
),
'raw' => $raw,
'body' => $data,
'headers' => $headers,
'path' => $from->getUri()->getPath(),
'query' => $query,
'files' => $uploads,
'ip' => $this->clientIpResolver->resolve($from->getServerParams()['REMOTE_ADDR'] ?? null, $headers),
'cookies' => Arr\filter(Arr\map(
Expand Down
4 changes: 1 addition & 3 deletions packages/http/src/Mappers/RequestToObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function canMap(mixed $from, mixed $to): bool
public function map(mixed $from, mixed $to): array|object
{
/** @var Request $from */
$data = [...$from->files, ...$from->body, ...$from->query];
$data = [...$from->files, ...$from->body, ...$from->uri->query];

if (is_a($to, Request::class, true)) {
$invalidReservedProperties = arr(new ClassReflector(Request::class)->getProperties())
Expand All @@ -46,8 +46,6 @@ public function map(mixed $from, mixed $to): array|object
'uri' => $from->uri,
'body' => $from->body,
'headers' => $from->headers,
'path' => $from->path,
'query' => $from->query,
'files' => $from->files,
'cookies' => $from->cookies,
'ip' => $from->ip,
Expand Down
4 changes: 2 additions & 2 deletions packages/http/src/Mappers/RequestToPsrRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public function map(mixed $from, mixed $to): PsrRequest
$request = new ServerRequest(
serverParams: $from->ip === null ? [] : ['REMOTE_ADDR' => $from->ip->toString()],
uploadedFiles: $from->files,
uri: $from->uri,
uri: $from->uri->toString(),
method: $from->method->value,
headers: $from->headers->toArray(),
cookieParams: $from->cookies,
queryParams: $from->query,
queryParams: $from->uri->query,
parsedBody: $from->body,
);

Expand Down
7 changes: 2 additions & 5 deletions packages/http/src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@

use Tempest\Http\Cookie\Cookie;
use Tempest\Support\Ip\IpAddress;
use Tempest\Support\Uri\Uri;

interface Request
{
public Method $method { get; }

public string $uri { get; }
public Uri $uri { get; }

public ?string $raw { get; }

public array $body { get; }

public RequestHeaders $headers { get; }

public string $path { get; }

public array $query { get; }

/** @var array<array-key, \Tempest\Http\Upload|array> $files */
public array $files { get; }

Expand Down
3 changes: 2 additions & 1 deletion packages/http/src/Session/PreviousUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function track(Request $request): void
return;
}

$this->session->set(self::PREVIOUS_URL_SESSION_KEY, $request->uri);
// TODO(neoisrecursive): figure out if a full uri is OK here
$this->session->set(self::PREVIOUS_URL_SESSION_KEY, $request->uri->toString());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/http/tests/GenericRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@
uri: $uri,
);

$this->assertSame($uri, $request->uri);
$this->assertSame('', $request->path);
$this->assertSame([], $request->query);
$this->assertSame($uri, $request->uri->path);

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: http - PHP 8.5 - prefer-stable

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: http - PHP 8.5 - prefer-lowest

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-lowest - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - postgres - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - mysql - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 190 in packages/http/tests/GenericRequestTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-stable - windows-latest

Failed asserting that two strings are identical.
$this->assertSame([], $request->uri->query);
}

#[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function generate(Request $request): string
{
$payload = [
'method' => $request->method->value,
'path' => $request->path,
'query' => $this->normalize($request->query),
'path' => $request->uri->path,
'query' => $this->normalize($request->uri->query),
'body' => $request->raw ?? $this->normalize($request->body),
];

Expand Down
2 changes: 1 addition & 1 deletion packages/mcp/src/McpHttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
$server = $this->config->getServerByPath($request->path);
$server = $this->config->getServerByPath($request->uri->path);

if (! $server instanceof McpServerDefinition) {
return new NotFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function match(Request $request): ?MatchedRoute

private function matchStaticRoute(Request $request): ?MatchedRoute
{
$staticRoute = $this->routeConfig->staticRoutes[$request->method->value][$request->path] ?? null;
$staticRoute = $this->routeConfig->staticRoutes[$request->method->value][$request->uri->path] ?? null;

if ($staticRoute === null) {
return null;
Expand All @@ -50,7 +50,7 @@ private function matchDynamicRoute(Request $request): ?MatchedRoute
$matchingRegexForMethod = $this->routeConfig->matchingRegexes[$request->method->value];

// Then we'll use this regex to see whether we have a match or not
$matchResult = $matchingRegexForMethod->match($request->path);
$matchResult = $matchingRegexForMethod->match($request->uri->path ?? '');

if ($matchResult === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/UriGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function hasValidSignature(Request $request): bool
}

return $this->signer->verify(
data: $this->createUri($request->path, ...Arr\remove_keys($request->query, 'signature')),
data: $this->createUri($request->uri->path, ...Arr\remove_keys($request->uri->query, 'signature')),
signature: Signature::from($signature),
);
}
Expand Down
Loading