diff --git a/src/Cookies.php b/src/Cookies.php index 02d4e3c..894bdb1 100644 --- a/src/Cookies.php +++ b/src/Cookies.php @@ -170,7 +170,10 @@ protected function toHeader(string $name, array $properties): string $result .= '; HttpOnly'; } - if (isset($properties['samesite']) && in_array(strtolower($properties['samesite']), ['lax', 'strict', 'none'], true)) { + if ( + isset($properties['samesite']) + && in_array(strtolower($properties['samesite']), ['lax', 'strict', 'none'], true) + ) { // While strtolower is needed for correct comparison, the RFC doesn't care about case $result .= '; SameSite=' . $properties['samesite']; } diff --git a/src/Uri.php b/src/Uri.php index 2dedd01..a22f38e 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -484,7 +484,20 @@ public function __toString(): string $query = $this->getQuery(); $fragment = $this->getFragment(); - $path = '/' . ltrim($path, '/'); + if ($path !== '') { + if ($path[0] !== '/') { + if ($authority !== '') { + // If the path is rootless and an authority is present, the path MUST be prefixed by "/". + $path = '/' . $path; + } + } elseif (isset($path[1]) && $path[1] === '/') { + if ($authority === '') { + // If the path is starting with more than one "/" and no authority is present, + // the starting slashes MUST be reduced to one. + $path = '/' . ltrim($path, '/'); + } + } + } return ($scheme !== '' ? $scheme . ':' : '') . ($authority !== '' ? '//' . $authority : '')