Skip to content

Commit

Permalink
Replaces fsockopen() with stream_socket_client(), adds more consisten…
Browse files Browse the repository at this point in the history
…cy to connection parameters, #9

Changed

* Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection`
* The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket`
  Only the socket path must be passed.
* Relaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm.
  • Loading branch information
hollodotme committed Jun 15, 2017
1 parent e8ac46d commit 24a1b5d
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 78 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com).

## [1.3.0] - 2017-06-15

### Changed

* Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection` - [#9]
* The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket`
Only the socket path must be passed. - [#9]
* Relaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm. - [#9]

## [1.2.0] - 2017-05-27

### Added
Expand Down Expand Up @@ -88,6 +97,7 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
* Getters/Setters for connect timeout, read/write timeout, keep alive, socket persistence from `Client` (now part of the socket connection)
* Method `Client->getValues()`

[1.3.0]: https://github.com/hollodotme/fast-cgi-client/compare/v1.2.0...v1.3.0
[1.2.0]: https://github.com/hollodotme/fast-cgi-client/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/hollodotme/fast-cgi-client/compare/v1.0.1...v1.1.0
[1.0.1]: https://github.com/hollodotme/fast-cgi-client/compare/v1.0.0...v1.0.1
Expand All @@ -97,3 +107,4 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
[#2]: https://github.com/hollodotme/fast-cgi-client/issues/2
[#5]: https://github.com/hollodotme/fast-cgi-client/issues/5
[#6]: https://github.com/hollodotme/fast-cgi-client/issues/6
[#9]: https://github.com/hollodotme/fast-cgi-client/issues/9
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/hollodotme/fast-cgi-client.svg?branch=master)](https://travis-ci.org/hollodotme/fast-cgi-client)
[![Build Status](https://travis-ci.org/hollodotme/fast-cgi-client.svg?branch=1.x-stable)](https://travis-ci.org/hollodotme/fast-cgi-client)
[![Tested PHP versions](https://php-eye.com/badge/hollodotme/fast-cgi-client/tested.svg?branch=1.x-stable)](https://php-eye.com/package/hollodotme/fast-cgi-client)
[![Latest Stable Version](https://poser.pugx.org/hollodotme/fast-cgi-client/v/stable)](https://packagist.org/packages/hollodotme/fast-cgi-client)
[![Total Downloads](https://poser.pugx.org/hollodotme/fast-cgi-client/downloads)](https://packagist.org/packages/hollodotme/fast-cgi-client)
Expand All @@ -16,6 +16,8 @@ You can find an experimental use-case in my related blog posts:
* [Experimental async PHP vol. 1](http://bit.ly/eapv1)
* [Experimental async PHP vol. 2](http://bit.ly/eapv2)

You can also find slides of my talks about this project on [speakerdeck.com](https://speakerdeck.com/hollodotme).

---

## Installation
Expand All @@ -41,7 +43,7 @@ The following examples assume a that the content of `/path/to/target/script.php`
```php
<?php declare(strict_types=1);

sleep((int)$_REQUEST['sleep'] ?? 0);
sleep((int)($_REQUEST['sleep'] ?? 0));
echo $_REQUEST['key'] ?? '';
```

Expand All @@ -56,14 +58,16 @@ use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;

$connection = new UnixDomainSocket(
'unix:///var/run/php/php7.0-fpm.sock', # Socket path to php-fpm
'/var/run/php/php7.0-fpm.sock', # Socket path to php-fpm
5000, # Connect timeout in milliseconds (default: 5000)
5000 # Read/write timeout in milliseconds (default: 5000)
);

$client = new Client( $connection );
```

**PLEASE NOTE:** In versions before 1.3.0 you also need to provide the transport protocol `unix://` in the first parameter.

### Init client with a network socket connection

```php
Expand Down Expand Up @@ -95,7 +99,7 @@ use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Requests\PostRequest;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;

$client = new Client( new UnixDomainSocket( 'unix:///var/run/php/php7.0-fpm.sock' ) );
$client = new Client( new UnixDomainSocket( '/var/run/php/php7.0-fpm.sock' ) );
$content = http_build_query(['key' => 'value']);

$request = new PostRequest('/path/to/target/script.php', $content);
Expand Down Expand Up @@ -652,4 +656,6 @@ Run a call through a network socket:

Run a call through a Unix Domain Socket

bin/fcgiget unix:/var/run/php/php7.0-fpm.sock/status
bin/fcgiget /var/run/php/php7.0-fpm.sock/status

This shows the response of the php-fpm status page, if enabled.
6 changes: 3 additions & 3 deletions bin/fcgiget
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if ( $_SERVER['argc'] < 2 )
{
echo 'Usage: ' . $_SERVER['argv'][0] . " URI\n\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " localhost:9000/status\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " unix:/var/run/php-fpm/web.sock/status\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " /var/run/php-fpm/web.sock/status\n";
exit( 1 );
}

Expand Down Expand Up @@ -90,8 +90,8 @@ else
}
if ( $sock )
{
$client = new Client( new UnixDomainSocket( "unix://{$sock}" ) );
echo "Call: {$uri} on UDS {$sock}\n\n";
$client = new Client( new UnixDomainSocket( (string)$sock ) );
echo "Call: {$uri} on UDS unix://{$sock}\n\n";
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"extra": {
"tools": {
"phpunit": {
"url": "https://phar.phpunit.de/phpunit-6.1.4.phar",
"url": "https://phar.phpunit.de/phpunit-6.2.2.phar",
"only-dev": true
},
"coveralls": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/Interfaces/ConfiguresSocketConnection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -29,9 +29,7 @@
*/
interface ConfiguresSocketConnection
{
public function getHost() : string;

public function getPort() : int;
public function getSocketAddress() : string;

public function getConnectTimeout() : int;

Expand Down
10 changes: 2 additions & 8 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,10 @@ public function sendRequest( ProvidesRequestData $request )

private function connect()
{
if ( is_resource( $this->resource ) )
{
return;
}

try
{
$this->resource = @fsockopen(
$this->connection->getHost(),
$this->connection->getPort(),
$this->resource = @stream_socket_client(
$this->connection->getSocketAddress(),
$errorNumber,
$errorString,
$this->connection->getConnectTimeout() / 1000
Expand Down
11 changes: 3 additions & 8 deletions src/SocketConnections/NetworkSocket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -56,14 +56,9 @@ public function __construct(
$this->readWriteTimeout = $readWriteTimeout;
}

public function getHost() : string
public function getSocketAddress() : string
{
return $this->host;
}

public function getPort() : int
{
return $this->port;
return sprintf( 'tcp://%s:%d', $this->host, $this->port );
}

public function getConnectTimeout() : int
Expand Down
19 changes: 5 additions & 14 deletions src/SocketConnections/UnixDomainSocket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -32,10 +32,7 @@
class UnixDomainSocket implements ConfiguresSocketConnection
{
/** @var string */
private $host;

/** @var int */
private $port;
private $socketPath;

/** @var int */
private $connectTimeout;
Expand All @@ -49,20 +46,14 @@ public function __construct(
int $readWriteTimeout = Defaults::READ_WRITE_TIMEOUT
)
{
$this->host = $socketPath;
$this->port = -1;
$this->socketPath = $socketPath;
$this->connectTimeout = $connectTimeout;
$this->readWriteTimeout = $readWriteTimeout;
}

public function getHost() : string
{
return $this->host;
}

public function getPort() : int
public function getSocketAddress() : string
{
return $this->port;
return 'unix://' . $this->socketPath;
}

public function getConnectTimeout() : int
Expand Down
24 changes: 12 additions & 12 deletions tests/Integration/UnixDomainSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class UnixDomainSocketTest extends TestCase
{
public function testCanSendAsyncRequestAndReceiveRequestId()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -51,7 +51,7 @@ public function testCanSendAsyncRequestAndReceiveRequestId()

public function testCanSendAsyncRequestAndReadResponse()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -69,7 +69,7 @@ public function testCanSendAsyncRequestAndReadResponse()

public function testCanSendSyncRequestAndReceiveResponse()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -88,7 +88,7 @@ public function testCanSendSyncRequestAndReceiveResponse()

public function testCanReceiveResponseInCallback()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -108,7 +108,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest )

public function testCanHandleExceptionsInFailureCallback()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand Down Expand Up @@ -136,7 +136,7 @@ function ( \Throwable $throwable ) use ( $unitTest )

public function testCanCheckForRequestIdsHavingResponses()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -151,7 +151,7 @@ public function testCanCheckForRequestIdsHavingResponses()

public function testCanReadResponses()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand Down Expand Up @@ -187,7 +187,7 @@ public function testCanReadResponses()
*/
public function testReadingSyncResponseCanTimeOut()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock', Defaults::CONNECT_TIMEOUT, 1000 );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock', Defaults::CONNECT_TIMEOUT, 1000 );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/sleepWorker.php', $content );
Expand All @@ -204,7 +204,7 @@ public function testReadingSyncResponseCanTimeOut()

public function testCanHandleReadyResponses()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -228,7 +228,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest )

public function testCanReadReadyResponses()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -248,7 +248,7 @@ public function testCanReadReadyResponses()

public function testCanWaitForResponse()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand All @@ -269,7 +269,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest )

public function testReadResponsesSkipsUnknownRequestIds()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' );
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
$client = new Client( $connection );
$content = http_build_query( [ 'test-key' => 'unit' ] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class ClientTest extends TestCase
*/
public function testConnectAttemptToNotExistingSocketThrowsException()
{
$connection = new UnixDomainSocket( 'unix:///tmp/not/existing.sock', 2000, 2000, true, true );
$connection = new UnixDomainSocket( '/tmp/not/existing.sock', 2000, 2000, true, true );
$client = new Client( $connection );

$client->sendRequest( new PostRequest( '/path/to/script.php', '' ) );
Expand All @@ -53,7 +53,7 @@ public function testConnectAttemptToInvalidSocketThrowsException()
{
$testSocket = realpath( __DIR__ . '/Fixtures/test.sock' );

$connection = new UnixDomainSocket( 'unix://' . $testSocket );
$connection = new UnixDomainSocket( '' . $testSocket );
$client = new Client( $connection );

$client->sendRequest( new PostRequest( '/path/to/script.php', '' ) );
Expand Down Expand Up @@ -109,11 +109,11 @@ public function testHandlingUnknownRequestsThrowsException()

/**
* @expectedException \hollodotme\FastCGI\Exceptions\ConnectException
* @expectedExceptionMessageRegExp #^Unable to connect to FastCGI application#
* @expectedExceptionMessageRegExp #.*unable to connect to.*#i
*/
public function testConnectAttemptToRestrictedUnixDomainSocketThrowsException()
{
$connection = new UnixDomainSocket( 'unix:///var/run/php7.1-ruds.sock' );
$connection = new UnixDomainSocket( '/var/run/php7.0-ruds.sock' );
$client = new Client( $connection );

$client->sendRequest( new PostRequest( '/path/to/script.php', '' ) );
Expand Down
8 changes: 3 additions & 5 deletions tests/Unit/SocketConnections/NetworkSocketTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -40,8 +40,7 @@ public function testCanGetDefaultValues()
{
$connection = new NetworkSocket( 'localhost', 9000 );

$this->assertSame( 'localhost', $connection->getHost() );
$this->assertSame( 9000, $connection->getPort() );
$this->assertSame( 'tcp://localhost:9000', $connection->getSocketAddress() );
$this->assertSame( Defaults::CONNECT_TIMEOUT, $connection->getConnectTimeout() );
$this->assertSame( Defaults::READ_WRITE_TIMEOUT, $connection->getReadWriteTimeout() );
}
Expand All @@ -50,8 +49,7 @@ public function testCanGetSetValues()
{
$connection = new NetworkSocket( '127.0.0.1', 9001, 2000, 3000 );

$this->assertSame( '127.0.0.1', $connection->getHost() );
$this->assertSame( 9001, $connection->getPort() );
$this->assertSame( 'tcp://127.0.0.1:9001', $connection->getSocketAddress() );
$this->assertSame( 2000, $connection->getConnectTimeout() );
$this->assertSame( 3000, $connection->getReadWriteTimeout() );
}
Expand Down
Loading

0 comments on commit 24a1b5d

Please sign in to comment.