Skip to content

Commit

Permalink
Reverted StreamStub changes. Fixed the problem by not invoking `strea…
Browse files Browse the repository at this point in the history
…m_get_contents` when it is not needed.
  • Loading branch information
bbankowski committed Jan 15, 2024
1 parent c53547f commit e89b351
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/Ouzo/Core/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ public static function getRequestType(): string

public static function getRequestParameters($stream = 'php://input'): array
{
$parameters = self::parseRequest(stream_get_contents(fopen($stream, 'r')));
return Arrays::toArray($parameters);
if (self::isJsonContentType() || self::isPutFormUrlEncoded()) {
$parameters = self::parseRequest(stream_get_contents(fopen($stream, 'r')));
return Arrays::toArray($parameters);
}
return [];
}

private static function parseRequest(string $content): array
Expand All @@ -152,18 +155,12 @@ private static function parseRequest(string $content): array

private static function jsonParameters(string $content): array
{
if (Strings::equalsIgnoreCase(ContentType::value(), MediaType::APPLICATION_JSON)) {
return Arrays::toArray(Json::decode($content, true));
}
return [];
return self::isJsonContentType() ? Arrays::toArray(Json::decode($content, true)) : [];
}

private static function putRequestParameters(string $content): array
{
if (
Strings::equal(Arrays::getValue($_SERVER, 'REQUEST_METHOD'), 'PUT')
&& Strings::equalsIgnoreCase(ContentType::value(), 'application/x-www-form-urlencoded')
) {
if (self::isPutFormUrlEncoded()) {
parse_str($content, $parameters);
return Arrays::toArray($parameters);
}
Expand All @@ -185,7 +182,6 @@ public static function removePrefix(string $url): string
return Strings::removePrefix(Strings::removePrefix($url, $prefix), $prefixForGetMethod);
}


public static function getProtocol(): string
{
return (
Expand All @@ -194,14 +190,25 @@ public static function getProtocol(): string
) ? 'https://' : 'http://';
}

public static function getHost(): ?string
{
return Arrays::getValue($_SERVER, 'HTTP_HOST');
}

private static function isServerVariableSetAndHasValue(string $variableName, array|string $values): bool
{
$value = Arrays::getValue($_SERVER, $variableName);
return in_array($value, Arrays::toArray($values));
}

public static function getHost(): ?string
private static function isJsonContentType(): bool
{
return Arrays::getValue($_SERVER, 'HTTP_HOST');
return Strings::equalsIgnoreCase(ContentType::value(), MediaType::APPLICATION_JSON);
}

private static function isPutFormUrlEncoded(): bool
{
return Strings::equal(Arrays::getValue($_SERVER, 'REQUEST_METHOD'), 'PUT')
&& Strings::equalsIgnoreCase(ContentType::value(), 'application/x-www-form-urlencoded');
}
}
1 change: 0 additions & 1 deletion src/Ouzo/Goodies/Tests/StreamStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class StreamStub

public function stream_open($path, $mode, $options, &$opened_path)
{
self::$position = 0;
return true;
}

Expand Down

0 comments on commit e89b351

Please sign in to comment.