getRequestUrl()); } catch (\InvalidArgumentException $e) { if (Environment::isCli()) { throw new InvalidRequestUrlOnCliException( 'Usage of ' . __METHOD__ . ' on CLI is discouraged. In case you rely on the method, you have to fake a valid request URL using $_SERVER.', 1701105725, $e, ); } throw $e; } $request = new ServerRequest( $uri, $method, 'php://input', $headers, $serverParameters, static::normalizeUploadedFiles($_FILES) ); if (!empty($_COOKIE)) { $request = $request->withCookieParams($_COOKIE); } if (!empty($_GET)) { $request = $request->withQueryParams($_GET); } $parsedBody = $_POST; if (empty($parsedBody) && in_array($method, ['PUT', 'PATCH', 'DELETE'])) { parse_str((string)file_get_contents('php://input'), $parsedBody); } if (!empty($parsedBody)) { $request = $request->withParsedBody($parsedBody); } return $request; } /** * Fetch headers from $_SERVER variables * which are only the ones starting with HTTP_* and CONTENT_* * * @return array */ protected static function prepareHeaders(array $server) { $headers = []; foreach ($server as $key => $value) { if (!is_string($key)) { continue; } if (str_starts_with($key, 'HTTP_COOKIE')) { // Cookies are handled using the $_COOKIE superglobal continue; } if ($value !== '') { if (str_starts_with($key, 'HTTP_')) { $name = str_replace('_', ' ', substr($key, 5)); $name = str_replace(' ', '-', ucwords(strtolower($name))); $name = strtolower($name); $headers[$name] = $value; } elseif (str_starts_with($key, 'CONTENT_')) { $name = substr($key, 8); // Content- $name = 'Content-' . (($name === 'MD5') ? $name : ucfirst(strtolower($name))); $name = strtolower($name); $headers[$name] = $value; } } } return $headers; } /** * Normalize uploaded files * * Transforms each value into an UploadedFileInterface instance, and ensures that nested arrays are normalized. * * @param array $files * @return array * @throws \InvalidArgumentException for unrecognized values */ protected static function normalizeUploadedFiles(array $files) { $normalizedFileUploads = []; foreach ($files as $key => $value) { if ($value instanceof UploadedFileInterface) { $normalizedFileUploads[$key] = $value; } elseif (is_array($value)) { if (isset($value['tmp_name'])) { $uploadedFiles = self::createUploadedFile($value); if ($uploadedFiles) { $normalizedFileUploads[$key] = $uploadedFiles; } } else { $normalizedFileUploads[$key] = self::normalizeUploadedFiles($value); } } else { throw new \InvalidArgumentException('Invalid value in files specification.', 1436717282); } } return $normalizedFileUploads; } /** * Create and return an UploadedFile instance from a $_FILES specification. * * If the specification represents an array of values, this method will * recursively resolve uploaded files. * * @param array $value $_FILES structure * @return UploadedFileInterface[]|UploadedFileInterface|null */ protected static function createUploadedFile(array $value) { if (is_array($value['tmp_name'])) { $files = []; foreach (array_keys($value['tmp_name']) as $key) { $data = [ 'tmp_name' => $value['tmp_name'][$key], 'error' => $value['error'][$key], 'name' => $value['name'][$key], 'type' => $value['type'][$key], ]; if (isset($value['size'][$key])) { $data['size'] = $value['size'][$key]; } $result = self::createUploadedFile($data); if ($result) { $files[$key] = $result; } } return $files; } if (!empty($value['tmp_name'])) { return new UploadedFile($value['tmp_name'], $value['size'] ?? 0, $value['error'], $value['name'], $value['type']); } return null; } }