Skip to content

Commit

Permalink
Merge pull request #183 from mrow4a/bugfix/empty-input-exception
Browse files Browse the repository at this point in the history
xml cannot be empty while reading
  • Loading branch information
DeepDiver1975 authored Apr 15, 2020
2 parents 03a31ca + f03e438 commit 96c35ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
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

0 comments on commit 96c35ed

Please sign in to comment.