From c07da71fa09dce49b6470d70224497d2734006be Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:16:15 +0100 Subject: [PATCH] Remove the socket transport The socket transport implemented HTTP/1.x by hand on top of fsockopen(): it could not be configured for TLS, did not support proxies and did not decode chunked responses. It is deprecated in 4.x; remove it and document the change in a new v4-to-v5 update guide. Co-Authored-By: Claude Opus 5.5 --- Tests/HttpFactoryTest.php | 2 +- Tests/TransportTest.php | 11 -- docs/classes/TransportInterface.md | 6 +- docs/index.md | 1 + docs/overview.md | 5 - docs/v4-to-v5-update.md | 43 +++++ src/Transport/Socket.php | 291 ----------------------------- 7 files changed, 46 insertions(+), 313 deletions(-) create mode 100644 docs/v4-to-v5-update.md delete mode 100644 src/Transport/Socket.php diff --git a/Tests/HttpFactoryTest.php b/Tests/HttpFactoryTest.php index 76b4fda6..4edb0a9d 100644 --- a/Tests/HttpFactoryTest.php +++ b/Tests/HttpFactoryTest.php @@ -130,7 +130,7 @@ public function testGetAvailableDriverDisallowsNonArrayObjects() */ public function testGetHttpTransports() { - $transports = ['Stream', 'Socket', 'Curl']; + $transports = ['Stream', 'Curl']; sort($transports); $this->assertEquals( diff --git a/Tests/TransportTest.php b/Tests/TransportTest.php index 3ba14a0d..0c16f828 100644 --- a/Tests/TransportTest.php +++ b/Tests/TransportTest.php @@ -8,7 +8,6 @@ namespace Joomla\Http\Tests; use Joomla\Http\Transport\Curl; -use Joomla\Http\Transport\Socket; use Joomla\Http\Transport\Stream; use Joomla\Http\TransportInterface; use Joomla\Uri\Uri; @@ -33,7 +32,6 @@ class TransportTest extends TestCase */ protected $options = [ 'transport.curl' => [CURLOPT_SSL_VERIFYPEER => false], - 'transport.socket' => ['X-Joomla-Test: true'], 'transport.stream' => ['ignore_errors' => true], ]; @@ -70,7 +68,6 @@ public static function transportProvider(): array { return [ 'curl adapter' => [Curl::class], - 'socket adapter' => [Socket::class], 'stream adapter' => [Stream::class], ]; } @@ -93,7 +90,6 @@ public static function relevantOnlyForStreamTransportProvider(): array * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -116,7 +112,6 @@ public function testConstructorWithBadDataObject(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -224,7 +219,6 @@ public function testRequestGetWhenBlockingModeIsDisabled(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -249,7 +243,6 @@ public function testBadDomainRequestGet(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -277,7 +270,6 @@ public function testRequestGet404(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -312,7 +304,6 @@ public function testRequestPut(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -356,7 +347,6 @@ public function testRequestCredentials(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ @@ -396,7 +386,6 @@ public function testRequestPost(string $transportClass) * @param string $transportClass The transport class to test * * @covers \Joomla\Http\Transport\Curl - * @covers \Joomla\Http\Transport\Socket * @covers \Joomla\Http\Transport\Stream * @uses \Joomla\Http\AbstractTransport */ diff --git a/docs/classes/TransportInterface.md b/docs/classes/TransportInterface.md index ffb364a5..4ff4de72 100644 --- a/docs/classes/TransportInterface.md +++ b/docs/classes/TransportInterface.md @@ -2,7 +2,7 @@ The contract every transport implements. `Http` holds one and delegates each request to it, so the interface is the seam where you replace how requests are actually sent — with cURL, with a stream, -with a socket, or with a stub in tests. +or with a stub in tests. ## The interface @@ -35,7 +35,6 @@ directly. |---|---|---| | `Transport\Curl` | `ext-curl` loaded | The default when available | | `Transport\Stream` | `allow_url_fopen` enabled | Configurable TLS via a stream context | -| `Transport\Socket` | `fsockopen()` exists | Cannot be configured for TLS — see below | `HttpFactory` picks one for you: @@ -47,9 +46,6 @@ $http = (new HttpFactory())->getHttp([], 'curl'); // force one $http = (new HttpFactory())->getHttp([], ['curl', 'stream']); // first supported of these ``` -> Prefer `curl` or `stream`. `Socket` connects with `fsockopen()`, which accepts no stream context, -> so no CA bundle, minimum TLS version or client certificate can be supplied for it. - ## Writing a transport ```php diff --git a/docs/index.md b/docs/index.md index d2a41e46..d76fcb59 100644 --- a/docs/index.md +++ b/docs/index.md @@ -8,3 +8,4 @@ * [Updating from v1 to v2](v1-to-v2-update.md) * [Updating from v2 to v3](v2-to-v3-update.md) * [Updating from v3 to v4](v3-to-v4-update.md) + * [Updating from v4 to v5](v4-to-v5-update.md) diff --git a/docs/overview.md b/docs/overview.md index b49bb6d1..e27cb446 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -146,11 +146,6 @@ $http = (new HttpFactory())->getHttp([ so a large or hostile response can exhaust `memory_limit`. Fetch untrusted URLs with your own limit in place. -**The socket transport cannot be configured for TLS.** It connects with `fsockopen()`, which takes -no stream context, so no CA bundle, minimum TLS version or client certificate can be supplied — and -the connection error is suppressed, so a certificate failure is indistinguishable from an -unreachable host. Prefer the cURL or stream transport. - **The stream transport accepts any wrapper.** The URI goes to `fopen()` unfiltered, so a `file://`, `php://` or `phar://` URL is opened rather than rejected. Check the scheme before calling if the URL is not your own. diff --git a/docs/v4-to-v5-update.md b/docs/v4-to-v5-update.md new file mode 100644 index 00000000..495aa840 --- /dev/null +++ b/docs/v4-to-v5-update.md @@ -0,0 +1,43 @@ +# Updating from v4 to v5 + +Release 5.0.0 removes the socket transport, which was deprecated in 4.x. + +## At a glance + +| | v4 | v5 (5.0.0) | +|---|---|---| +| `Joomla\Http\Transport\Socket` | deprecated, works | **removed** | + +## The socket transport was removed + +`Joomla\Http\Transport\Socket` implemented HTTP/1.x by hand on top of `fsockopen()`. It could not be +configured for TLS (no CA bundle, minimum TLS version or client certificate), did not support +proxies and did not decode chunked responses. It was deprecated in 4.x, where creating it triggers +a deprecation notice, and is removed in 5.0.0. + +`HttpFactory` now chooses between the cURL and the stream transport only. Code that names the +socket transport has to use one of the others: + +```php +use Joomla\Http\HttpFactory; +use Joomla\Http\Transport\Stream; + +// Removed in 5.0.0 +$http = (new HttpFactory())->getHttp($options, 'socket'); +$transport = new \Joomla\Http\Transport\Socket($options); + +// Use the cURL or the stream transport +$http = (new HttpFactory())->getHttp($options, ['curl', 'stream']); +$transport = new Stream($options); +``` + +Asking the factory for `'socket'` alone now finds no transport: `getAvailableDriver()` returns +`false` and `getHttp()` throws a `RuntimeException`. The stream transport only needs +`allow_url_fopen`; options that were specific to the socket transport (`transport.socket`) have no +equivalent and can be dropped. + +To find the call sites: + +```bash +grep -rnE "Transport\\\\Socket|'socket'|transport\.socket" src/ +``` diff --git a/src/Transport/Socket.php b/src/Transport/Socket.php deleted file mode 100644 index 9aedd4c8..00000000 --- a/src/Transport/Socket.php +++ /dev/null @@ -1,291 +0,0 @@ -connect($uri, $timeout); - - // Make sure the connection is alive and valid. - if (!\is_resource($connection)) { - throw new \RuntimeException('Not connected to server.'); - } - - // Make sure the connection has not timed out. - $meta = stream_get_meta_data($connection); - - if ($meta['timed_out']) { - throw new \RuntimeException('Server connection timed out.'); - } - - // Get the request path from the URI object. - $path = $uri->toString(['path', 'query']); - - // If we have data to send make sure our request is setup for it. - if (!empty($data)) { - // If the data is not a scalar value encode it to be sent with the request. - if (!is_scalar($data)) { - $data = http_build_query($data); - } - - if (!isset($headers['Content-Type'])) { - $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; - } - - // Add the relevant headers. - $headers['Content-Length'] = \strlen($data); - } - - // Configure protocol version, use transport's default if not set otherwise. - $protocolVersion = $this->getOption('protocolVersion', '1.0'); - - // HTTP/1.1 streams using the socket wrapper require a Connection: close header - if ($protocolVersion == '1.1' && !isset($headers['Connection'])) { - $headers['Connection'] = 'close'; - } - - // Build the request payload. - $request = []; - $request[] = strtoupper($method) . ' ' . ((empty($path)) ? '/' : $path) . ' HTTP/' . $protocolVersion; - - if (!isset($headers['Host'])) { - $request[] = 'Host: ' . $uri->getHost(); - } - - // If an explicit user agent is given use it. - if (isset($userAgent)) { - $headers['User-Agent'] = $userAgent; - } - - // If we have a username then we include basic authentication credentials. - if ($uri->getUser()) { - $authString = $uri->getUser() . ':' . $uri->getPass(); - $headers['Authorization'] = 'Basic ' . base64_encode($authString); - } - - // If there are custom headers to send add them to the request payload. - if (!empty($headers)) { - foreach ($headers as $key => $value) { - if (\is_array($value)) { - foreach ($value as $header) { - $request[] = "$key: $header"; - } - } else { - $request[] = "$key: $value"; - } - } - } - - // Authentication, if needed - if ($this->getOption('userauth') && $this->getOption('passwordauth')) { - $request[] = 'Authorization: Basic ' . base64_encode($this->getOption('userauth') . ':' . $this->getOption('passwordauth')); - } - - // Set any custom transport options - foreach ($this->getOption('transport.socket', []) as $value) { - $request[] = $value; - } - - // If we have data to send add it to the request payload. - if (!empty($data)) { - $request[] = null; - $request[] = $data; - } - - // Send the request to the server. - fwrite($connection, implode("\r\n", $request) . "\r\n\r\n"); - - // Get the response data from the server. - $content = ''; - - while (!feof($connection)) { - $content .= fgets($connection, 4096); - } - - $content = $this->getResponse($content); - - // Follow Http redirects - if ($content->getStatusCode() >= 301 && $content->getStatusCode() < 400 && $content->hasHeader('Location')) { - return $this->request($method, new Uri($content->getHeaderLine('Location')), $data, $headers, $timeout, $userAgent); - } - - return $content; - } - - /** - * Method to get a response object from a server response. - * - * @param string $content The complete server response, including headers. - * - * @return Response - * - * @since 1.0 - * @throws \UnexpectedValueException - * @throws InvalidResponseCodeException - */ - protected function getResponse($content) - { - if (empty($content)) { - throw new \UnexpectedValueException('No content in response.'); - } - - // Split the response into headers and body. - $response = explode("\r\n\r\n", $content, 2); - - // Get the response headers as an array. - $headers = explode("\r\n", $response[0]); - - // Set the body for the response. - $body = empty($response[1]) ? '' : $response[1]; - - // Get the response code from the first offset of the response headers. - preg_match('/[0-9]{3}/', array_shift($headers), $matches); - $code = $matches[0]; - - if (!is_numeric($code)) { - // No valid response code was detected. - throw new InvalidResponseCodeException('No HTTP response code found.'); - } - - $statusCode = (int) $code; - $verifiedHeaders = $this->processHeaders($headers); - - $streamInterface = new StreamResponse('php://memory', 'rw'); - $streamInterface->write($body); - - return new Response($streamInterface, $statusCode, $verifiedHeaders); - } - - /** - * Method to connect to a server and get the resource. - * - * @param UriInterface $uri The URI to connect with. - * @param integer $timeout Read timeout in seconds. - * - * @return resource Socket connection resource. - * - * @since 1.0 - * @throws \RuntimeException - */ - protected function connect(UriInterface $uri, $timeout = null) - { - $errno = null; - $err = null; - - // Get the host from the uri. - $host = ($uri->isSsl()) ? 'ssl://' . $uri->getHost() : $uri->getHost(); - - // If the port is not explicitly set in the URI detect it. - if (!$uri->getPort()) { - $port = ($uri->getScheme() == 'https') ? 443 : 80; - } else { - // Use the set port. - $port = $uri->getPort(); - } - - // Build the connection key for resource memory caching. - $key = md5($host . $port); - - // If the connection already exists, use it. - if (!empty($this->connections[$key]) && \is_resource($this->connections[$key])) { - // Connection reached EOF, cannot be used anymore - $meta = stream_get_meta_data($this->connections[$key]); - - if ($meta['eof']) { - if (!fclose($this->connections[$key])) { - throw new \RuntimeException('Cannot close connection'); - } - } elseif (!$meta['timed_out']) { - // Make sure the connection has not timed out. - return $this->connections[$key]; - } - } - - if (!is_numeric($timeout)) { - $timeout = ini_get('default_socket_timeout'); - } - - // Capture PHP errors - error_clear_last(); - - // PHP sends a warning if the uri does not exists; we silence it and throw an exception instead. - // Attempt to connect to the server - $connection = @fsockopen($host, $port, $errno, $err, $timeout); - - if (!$connection) { - $error = error_get_last(); - - if ($error === null || $error['message'] === '') { - // Error but nothing from php? Create our own - $error = [ - 'message' => sprintf('Could not connect to resource %s: %s (%d)', $uri, $err, $errno), - ]; - } - - throw new \RuntimeException($error['message']); - } - - // Since the connection was successful let's store it in case we need to use it later. - $this->connections[$key] = $connection; - - stream_set_timeout($this->connections[$key], (int) $timeout); - - return $this->connections[$key]; - } - - /** - * Method to check if http transport socket available for use - * - * @return boolean True if available else false - * - * @since 1.0 - */ - public static function isSupported() - { - return \function_exists('fsockopen'); - } -}