Skip to content
Merged
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
18 changes: 10 additions & 8 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ public function parse($input, string $contextUri = null, string &$rootElementNam
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
}

// If input is an empty string, then its safe to throw exception
if ('' === $input) {
throw new ParseException('The input element to parse is empty. Do not attempt to parse');
}
// If input is empty, then its safe to throw exception
if (empty($input)) {
throw new ParseException('The input element to parse is empty. Do not attempt to parse');
}

$r = $this->getReader();
$r->contextUri = $contextUri;
$r->XML($input, null, $this->options);
Expand Down Expand Up @@ -158,12 +159,13 @@ public function expect($rootElementName, $input, string $contextUri = null)
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
}

// If input is empty string, then its safe to throw exception
if ('' === $input) {
throw new ParseException('The input element to parse is empty. Do not attempt to parse');
}
// If input is empty, then its safe to throw exception
if (empty($input)) {
throw new ParseException('The input element to parse is empty. Do not attempt to parse');
}

$r = $this->getReader();
$r->contextUri = $contextUri;
$r->XML($input, null, $this->options);
Expand Down
36 changes: 27 additions & 9 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ public function testGetWriter()
$this->assertEquals($ns, $writer->namespaceMap);
}

public function testEmptyInputParse()
/**
* @dataProvider providesEmptyInput
*
* @param string|resource $input
*/
public function testEmptyInputParse($input)
{
$resource = fopen('php://input', 'r');
$util = new Service();
$this->expectException('\Sabre\Xml\ParseException');
$this->expectExceptionMessage('The input element to parse is empty. Do not attempt to parse');
$util->parse($resource, '/sabre.io/ns');

$util = new Service();
$util->parse($input, '/sabre.io/ns');
}

/**
Expand Down Expand Up @@ -105,14 +110,18 @@ public function testParseStream()
);
}

public function testEmptyInputExpect()
/**
* @dataProvider providesEmptyInput
*
* @param string|resource $input
*/
public function testEmptyInputExpect($input)
{
//$resource = \fopen('')
$resource = fopen('php://input', 'r');
$util = new Service();
$this->expectException('\Sabre\Xml\ParseException');
$this->expectExceptionMessage('The input element to parse is empty. Do not attempt to parse');
$util->expect('foo', $resource, '/sabre.io/ns');

$util = new Service();
$util->expect('foo', $input, '/sabre.io/ns');
}

/**
Expand Down Expand Up @@ -350,6 +359,15 @@ public function testParseClarkNotationFail()
Service::parseClarkNotation('http://sabredav.org/ns}elem');
}

public function providesEmptyInput()
{
$emptyResource = fopen('php://input', 'r');
$data[] = [$emptyResource];
$data[] = [''];

return $data;
}

public function providesEmptyPropfinds()
{
return [
Expand Down