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
12 changes: 11 additions & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@
);
});

$app->get('/uri', function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
[
'Content-Type' => 'text/plain'
],
(string) $request->getUri() . "\n"
);
});

$app->get('/debug', function (ServerRequestInterface $request) {
ob_start();
var_dump($request);
$info = ob_get_clean();

if (PHP_SAPI !== 'cli' && !xdebug_is_enabled()) {
if (PHP_SAPI !== 'cli' && (!function_exists('xdebug_is_enabled') || !xdebug_is_enabled())) {
$info = htmlspecialchars($info, 0, 'utf-8');
}

Expand Down
23 changes: 19 additions & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,18 @@ private function runLoop()
});
}

private function runOnce()
private function requestFromGlobals(): ServerRequestInterface
{
$host = null;
$headers = array();
foreach ($_SERVER as $key => $value) {
if (\strpos($key, 'HTTP_') === 0) {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
$headers[$key] = $value;

if ($host === null && $key === 'Host') {
$host = $value;
}
}
}

Expand All @@ -212,15 +217,25 @@ private function runOnce()
$body = file_get_contents('php://input');

$request = new ServerRequest(
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_SERVER['REQUEST_METHOD'] ?? 'GET',
($_SERVER['HTTPS'] ?? null === 'on' ? 'https://' : 'http://') . ($host ?? 'localhost') . ($_SERVER['REQUEST_URI'] ?? '/'),
$headers,
$body,
substr($_SERVER['SERVER_PROTOCOL'], 5),
substr($_SERVER['SERVER_PROTOCOL'] ?? 'http/1.1', 5),
$_SERVER
);
if ($host === null) {
$request = $request->withoutHeader('Host');
}
$request = $request->withParsedBody($_POST);

return $request;
}

private function runOnce()
{
$request = $this->requestFromGlobals();

$dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->router->getData());

$response = $this->handleRequest($request, $dispatcher);
Expand Down
97 changes: 97 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,103 @@ public function testRedirectMethodWithCustomRedirectCodeAddsGetRouteOnRouterWhic
$this->assertEquals("See /users...\n", (string) $response->getBody());
}

public function testRequestFromGlobalsWithNoServerVariablesDefaultsToGetRequestToLocalhost()
{
$loop = $this->createMock(LoopInterface::class);
$app = new App($loop);

// $request = $app->requestFromGlobals();
$ref = new ReflectionMethod($app, 'requestFromGlobals');
$ref->setAccessible(true);
$request = $ref->invoke($app);

/** @var ServerRequestInterface $request */
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('http://localhost/', (string) $request->getUri());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('', $request->getHeaderLine('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithHeadRequest()
{
$loop = $this->createMock(LoopInterface::class);
$app = new App($loop);

$_SERVER['REQUEST_METHOD'] = 'HEAD';
$_SERVER['REQUEST_URI'] = '//';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.0';
$_SERVER['HTTP_HOST'] = 'example.com';

// $request = $app->requestFromGlobals();
$ref = new ReflectionMethod($app, 'requestFromGlobals');
$ref->setAccessible(true);
$request = $ref->invoke($app);

/** @var ServerRequestInterface $request */
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertEquals('HEAD', $request->getMethod());
$this->assertEquals('http://example.com//', (string) $request->getUri());
$this->assertEquals('1.0', $request->getProtocolVersion());
$this->assertEquals('example.com', $request->getHeaderLine('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithGetRequestOverCustomPort()
{
$loop = $this->createMock(LoopInterface::class);
$app = new App($loop);

$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/path';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
$_SERVER['HTTP_HOST'] = 'localhost:8080';

// $request = $app->requestFromGlobals();
$ref = new ReflectionMethod($app, 'requestFromGlobals');
$ref->setAccessible(true);
$request = $ref->invoke($app);

/** @var ServerRequestInterface $request */
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('http://localhost:8080/path', (string) $request->getUri());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('localhost:8080', $request->getHeaderLine('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithGetRequestOverHttps()
{
$loop = $this->createMock(LoopInterface::class);
$app = new App($loop);

$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['HTTPS'] = 'on';

// $request = $app->requestFromGlobals();
$ref = new ReflectionMethod($app, 'requestFromGlobals');
$ref->setAccessible(true);
$request = $ref->invoke($app);

/** @var ServerRequestInterface $request */
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('https://localhost/', (string) $request->getUri());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('localhost', $request->getHeaderLine('Host'));
}

public function testHandleRequestWithProxyRequestReturnsResponseWithMessageThatProxyRequestAreNotAllowed()
{
$loop = $this->createMock(LoopInterface::class);
Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ match() {
out=$(curl -v $base/ 2>&1); match "HTTP/.* 200"
out=$(curl -v $base/test 2>&1); match -i "Location: /"
out=$(curl -v $base/invalid 2>&1); match "HTTP/.* 404"
out=$(curl -v $base/uri 2>&1); match "HTTP/.* 200" && match "$base/uri"
out=$(curl -v $base// 2>&1); match "HTTP/.* 404"
out=$(curl -v $base/ 2>&1 -X POST); match "HTTP/.* 405"
out=$(curl -v $base/users/foo 2>&1); match "HTTP/.* 200" && match "Hello foo!"
out=$(curl -v $base/users 2>&1); match "HTTP/.* 404"
Expand Down