Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Socket to support HTTP over Unix domain sockets (UDS) #110

Merged
merged 1 commit into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();