Skip to content

Commit

Permalink
Merge pull request #255 from clue-labs/connect-request
Browse files Browse the repository at this point in the history
Consistent parsing for HTTP `CONNECT` request method (PHP SAPI)
  • Loading branch information
SimonFrings authored Apr 15, 2024
2 parents 9b17ad9 + 2b2c499 commit 6ee8435
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"php": ">=7.1",
"nikic/fast-route": "^1.3",
"react/async": "^4 || ^3",
"react/http": "^1.9",
"react/http": "^1.10",
"react/promise": "^3 || ^2.10",
"react/socket": "^1.13"
},
Expand Down
2 changes: 2 additions & 0 deletions src/Io/SapiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function requestFromGlobals(): ServerRequestInterface
$url = $target;
if (($target[0] ?? '/') === '/' || $target === '*') {
$url = (($_SERVER['HTTPS'] ?? null) === 'on' ? 'https://' : 'http://') . ($host ?? 'localhost') . ($target === '*' ? '' : $target);
} elseif (($_SERVER['REQUEST_METHOD'] ?? null) === 'CONNECT') {
$url = (($_SERVER['HTTPS'] ?? null) === 'on' ? 'https://' : 'http://') . $target;
}

$body = file_get_contents('php://input');
Expand Down
62 changes: 61 additions & 1 deletion tests/Io/SapiHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,67 @@ public function testRequestFromGlobalsWithConnectProxy(): void
$request = $sapi->requestFromGlobals();

$this->assertEquals('CONNECT', $request->getMethod());
$this->assertEquals('example.com:443', (string) $request->getUri());
$this->assertEquals('http://example.com:443', (string) $request->getUri());
$this->assertEquals('example.com:443', $request->getRequestTarget());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('example.com:443', $request->getHeaderLine('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithConnectProxyWithDefaultHttpPort(): void
{
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
$_SERVER['REQUEST_URI'] = 'example.com:80';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
$_SERVER['HTTP_HOST'] = 'example.com';

$sapi = new SapiHandler();
$request = $sapi->requestFromGlobals();

$this->assertEquals('CONNECT', $request->getMethod());
$this->assertEquals('http://example.com', (string) $request->getUri());
$this->assertEquals('example.com:80', $request->getRequestTarget());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('example.com', $request->getHeaderLine('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithConnectProxyWithoutHostHeader(): void
{
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
$_SERVER['REQUEST_URI'] = 'example.com:8080';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';

$sapi = new SapiHandler();
$request = $sapi->requestFromGlobals();

$this->assertEquals('CONNECT', $request->getMethod());
$this->assertEquals('http://example.com:8080', (string) $request->getUri());
$this->assertEquals('example.com:8080', $request->getRequestTarget());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertFalse($request->hasHeader('Host'));
}

/**
* @backupGlobals enabled
*/
public function testRequestFromGlobalsWithConnectProxyOverHttps(): void
{
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
$_SERVER['REQUEST_URI'] = 'example.com:443';
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
$_SERVER['HTTP_HOST'] = 'example.com:443';
$_SERVER['HTTPS'] = 'on';

$sapi = new SapiHandler();
$request = $sapi->requestFromGlobals();

$this->assertEquals('CONNECT', $request->getMethod());
$this->assertEquals('https://example.com', (string) $request->getUri());
$this->assertEquals('example.com:443', $request->getRequestTarget());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('example.com:443', $request->getHeaderLine('Host'));
Expand Down

0 comments on commit 6ee8435

Please sign in to comment.