Skip to content

Commit 6ee8435

Browse files
authored
Merge pull request #255 from clue-labs/connect-request
Consistent parsing for HTTP `CONNECT` request method (PHP SAPI)
2 parents 9b17ad9 + 2b2c499 commit 6ee8435

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": ">=7.1",
1515
"nikic/fast-route": "^1.3",
1616
"react/async": "^4 || ^3",
17-
"react/http": "^1.9",
17+
"react/http": "^1.10",
1818
"react/promise": "^3 || ^2.10",
1919
"react/socket": "^1.13"
2020
},

src/Io/SapiHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function requestFromGlobals(): ServerRequestInterface
6969
$url = $target;
7070
if (($target[0] ?? '/') === '/' || $target === '*') {
7171
$url = (($_SERVER['HTTPS'] ?? null) === 'on' ? 'https://' : 'http://') . ($host ?? 'localhost') . ($target === '*' ? '' : $target);
72+
} elseif (($_SERVER['REQUEST_METHOD'] ?? null) === 'CONNECT') {
73+
$url = (($_SERVER['HTTPS'] ?? null) === 'on' ? 'https://' : 'http://') . $target;
7274
}
7375

7476
$body = file_get_contents('php://input');

tests/Io/SapiHandlerTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,67 @@ public function testRequestFromGlobalsWithConnectProxy(): void
133133
$request = $sapi->requestFromGlobals();
134134

135135
$this->assertEquals('CONNECT', $request->getMethod());
136-
$this->assertEquals('example.com:443', (string) $request->getUri());
136+
$this->assertEquals('http://example.com:443', (string) $request->getUri());
137+
$this->assertEquals('example.com:443', $request->getRequestTarget());
138+
$this->assertEquals('1.1', $request->getProtocolVersion());
139+
$this->assertEquals('example.com:443', $request->getHeaderLine('Host'));
140+
}
141+
142+
/**
143+
* @backupGlobals enabled
144+
*/
145+
public function testRequestFromGlobalsWithConnectProxyWithDefaultHttpPort(): void
146+
{
147+
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
148+
$_SERVER['REQUEST_URI'] = 'example.com:80';
149+
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
150+
$_SERVER['HTTP_HOST'] = 'example.com';
151+
152+
$sapi = new SapiHandler();
153+
$request = $sapi->requestFromGlobals();
154+
155+
$this->assertEquals('CONNECT', $request->getMethod());
156+
$this->assertEquals('http://example.com', (string) $request->getUri());
157+
$this->assertEquals('example.com:80', $request->getRequestTarget());
158+
$this->assertEquals('1.1', $request->getProtocolVersion());
159+
$this->assertEquals('example.com', $request->getHeaderLine('Host'));
160+
}
161+
162+
/**
163+
* @backupGlobals enabled
164+
*/
165+
public function testRequestFromGlobalsWithConnectProxyWithoutHostHeader(): void
166+
{
167+
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
168+
$_SERVER['REQUEST_URI'] = 'example.com:8080';
169+
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
170+
171+
$sapi = new SapiHandler();
172+
$request = $sapi->requestFromGlobals();
173+
174+
$this->assertEquals('CONNECT', $request->getMethod());
175+
$this->assertEquals('http://example.com:8080', (string) $request->getUri());
176+
$this->assertEquals('example.com:8080', $request->getRequestTarget());
177+
$this->assertEquals('1.1', $request->getProtocolVersion());
178+
$this->assertFalse($request->hasHeader('Host'));
179+
}
180+
181+
/**
182+
* @backupGlobals enabled
183+
*/
184+
public function testRequestFromGlobalsWithConnectProxyOverHttps(): void
185+
{
186+
$_SERVER['REQUEST_METHOD'] = 'CONNECT';
187+
$_SERVER['REQUEST_URI'] = 'example.com:443';
188+
$_SERVER['SERVER_PROTOCOL'] = 'http/1.1';
189+
$_SERVER['HTTP_HOST'] = 'example.com:443';
190+
$_SERVER['HTTPS'] = 'on';
191+
192+
$sapi = new SapiHandler();
193+
$request = $sapi->requestFromGlobals();
194+
195+
$this->assertEquals('CONNECT', $request->getMethod());
196+
$this->assertEquals('https://example.com', (string) $request->getUri());
137197
$this->assertEquals('example.com:443', $request->getRequestTarget());
138198
$this->assertEquals('1.1', $request->getProtocolVersion());
139199
$this->assertEquals('example.com:443', $request->getHeaderLine('Host'));

0 commit comments

Comments
 (0)