diff --git a/lib/Service.php b/lib/Service.php index 66ec010..e7c0e81 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -112,7 +112,14 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN if (!is_string($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. - $input = (string) stream_get_contents($input); + if (is_resource($input)) { + $input = (string) stream_get_contents($input); + } else { + // Input is not a string and not a resource. + // Therefore, it has to be a closed resource. + // Effectively empty input has been passed in. + $input = ''; + } } // If input is empty, then it's safe to throw an exception @@ -156,7 +163,14 @@ public function expect($rootElementName, $input, ?string $contextUri = null) if (!is_string($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. - $input = (string) stream_get_contents($input); + if (is_resource($input)) { + $input = (string) stream_get_contents($input); + } else { + // Input is not a string and not a resource. + // Therefore, it has to be a closed resource. + // Effectively empty input has been passed in. + $input = ''; + } } // If input is empty, then it's safe to throw an exception diff --git a/tests/Sabre/Xml/ServiceTest.php b/tests/Sabre/Xml/ServiceTest.php index aa225b5..fae475b 100644 --- a/tests/Sabre/Xml/ServiceTest.php +++ b/tests/Sabre/Xml/ServiceTest.php @@ -382,6 +382,19 @@ public function providesEmptyInput(): array $data[] = [$emptyResource]; $data[] = ['']; + // Also test trying to parse a resource stream that has already been closed. + $xml = << + value + +XML; + $stream = fopen('php://memory', 'r+'); + self:assertIsResource($stream); + fwrite($stream, $xml); + rewind($stream); + fclose($stream); + $data[] = [$stream]; + return $data; }