Skip to content

Commit

Permalink
Consistent parsing for HTTP CONNECT request method (PHP SAPI)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Apr 12, 2024
1 parent 9b17ad9 commit 81fd4b3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
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 81fd4b3

Please sign in to comment.