Skip to content

Commit e802d27

Browse files
authored
Merge pull request #60 from lode/fix-request-with-partial-server-context
2 parents 825d83f + 11cbd20 commit e802d27

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/helpers/RequestParser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public function __construct($selfLink='', array $queryParameters=[], array $docu
4646
* @return self
4747
*/
4848
public static function fromSuperglobals() {
49-
$selfLink = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
49+
$selfLink = '';
50+
if (isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
51+
$selfLink = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
52+
}
5053

5154
$queryParameters = $_GET;
5255

5356
$document = $_POST;
54-
if ($document === []) {
57+
if ($document === [] && isset($_SERVER['CONTENT_TYPE'])) {
5558
$documentIsJsonapi = (strpos($_SERVER['CONTENT_TYPE'], Document::CONTENT_TYPE_OFFICIAL) !== false);
5659
$documentIsJson = (strpos($_SERVER['CONTENT_TYPE'], Document::CONTENT_TYPE_DEBUG) !== false);
5760

tests/helpers/RequestParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public function testFromSuperglobals_WithPhpInputStream() {
9090
$this->assertSame([], $requestParser->getDocument());
9191
}
9292

93+
public function testFromSuperglobals_WithoutServerContext() {
94+
unset($_SERVER['REQUEST_SCHEME']);
95+
unset($_SERVER['HTTP_HOST']);
96+
unset($_SERVER['REQUEST_URI']);
97+
unset($_SERVER['CONTENT_TYPE']);
98+
99+
$_GET = [];
100+
$_POST = [];
101+
102+
$requestParser = RequestParser::fromSuperglobals();
103+
104+
$this->assertSame([], $requestParser->getDocument());
105+
}
106+
93107
public function testFromPsrRequest_WithRequestInterface() {
94108
$queryParameters = [
95109
'include' => 'ship,ship.wing',

0 commit comments

Comments
 (0)