Skip to content

Commit

Permalink
Update Socket to support HTTP over Unix domain sockets (UDS)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Sep 16, 2017
1 parent be92b9e commit b1f13f8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Event-driven, streaming HTTP client for [ReactPHP](http://reactphp.org)

* [Basic usage](#basic-usage)
* [Example](#example)
* [Advanced usage](#advanced-usage)
* [Unix domain sockets](#unix-domain-sockets)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
Expand Down Expand Up @@ -98,6 +100,31 @@ $loop->run();

See also the [examples](examples).

## Advanced Usage

### Unix domain sockets

By default, this library supports transport over plaintext TCP/IP and secure
TLS connections for the `http://` and `https://` URI schemes respectively.
This library also supports Unix domain sockets (UDS) when explicitly configured.

In order to use a UDS path, you have to explicitly configure the connector to
override the destination URI so that the hostname given in the request URI will
no longer be used to establish the connection:

```php
$connector = new FixedUriConnector(
'unix:///var/run/docker.sock',
new UnixConnector($loop)
);

$client = new Client($loop, $connector);

$request = $client->request('GET', 'http://localhost/info');
```

See also [example #11](examples/11-unix-domain-sockets.php).

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=5.4.0",
"guzzlehttp/psr7": "^1.0",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/socket": "^1.0 || ^0.8.3",
"react/socket": "^1.0 || ^0.8.4",
"react/stream": "^1.0 || ^0.7.1",
"react/promise": "~2.2",
"evenement/evenement": "^3.0 || ^2.0"
Expand Down
39 changes: 39 additions & 0 deletions examples/11-unix-domain-sockets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use React\HttpClient\Client;
use React\HttpClient\Response;
use React\Socket\FixedUriConnector;
use React\Socket\UnixConnector;

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$connector = new FixedUriConnector(
'unix:///var/run/docker.sock',
new UnixConnector($loop)
);

$client = new Client($loop, $connector);

$request = $client->request('GET', 'http://localhost/info');

$request->on('response', function (Response $response) {
var_dump($response->getHeaders());

$response->on('data', function ($chunk) {
echo $chunk;
});

$response->on('end', function () {
echo 'DONE' . PHP_EOL;
});
});

$request->on('error', function (\Exception $e) {
echo $e;
});

$request->end();

$loop->run();

0 comments on commit b1f13f8

Please sign in to comment.