diff --git a/composer.json b/composer.json index 7788b7d..51c5721 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ "require": { "php": ">=5.3", "react/promise": "^3.2 || ^2.1 || ^1.2.1", - "react/socket": "^1.16", - "ringcentral/psr7": "^1.2" + "react/socket": "^1.16" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 0e7bcdb..bedba0b 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -5,7 +5,6 @@ use Exception; use InvalidArgumentException; use RuntimeException; -use RingCentral\Psr7; use React\Promise; use React\Promise\Deferred; use React\Socket\ConnectionInterface; @@ -183,30 +182,31 @@ public function connect($uri) $fn = null; // try to parse headers as response message - try { - $response = Psr7\parse_response(substr($buffer, 0, $pos)); - } catch (Exception $e) { + $start = strtok($buffer, "\r\n"); + if (!$start || !preg_match('/^HTTP\/.* ([0-9]{3})( .*)?$/', $start, $matchs)) { $deferred->reject(new RuntimeException( 'Connection to ' . $uri . ' failed because proxy returned invalid response (EBADMSG)', - defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71, - $e + defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71 )); $stream->close(); return; } - if ($response->getStatusCode() === 407) { + $statusCode = (int) $matchs[1]; + $reasonPhrase = isset($matchs[2]) ? trim($matchs[2]) : ''; + + if ($statusCode === 407) { // map status code 407 (Proxy Authentication Required) to EACCES $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (EACCES)', + 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (EACCES)', defined('SOCKET_EACCES') ? SOCKET_EACCES : 13 )); $stream->close(); return; - } elseif ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { + } elseif ($statusCode < 200 || $statusCode >= 300) { // map non-2xx status code to ECONNREFUSED $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (ECONNREFUSED)', + 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 )); $stream->close();