Skip to content

Commit

Permalink
Merge pull request #410 from clue-labs/default-loop
Browse files Browse the repository at this point in the history
Simplify usage by supporting new default loop
  • Loading branch information
clue authored Jul 11, 2021
2 parents bc53727 + d9401a5 commit f37c964
Show file tree
Hide file tree
Showing 38 changed files with 240 additions and 329 deletions.
151 changes: 70 additions & 81 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"php": ">=5.3.0",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"psr/http-message": "^1.0",
"react/event-loop": "^1.0 || ^0.5",
"react/event-loop": "^1.2",
"react/promise": "^2.3 || ^1.2.1",
"react/promise-stream": "^1.1",
"react/socket": "^1.6",
"react/stream": "^1.1",
"react/socket": "^1.8",
"react/stream": "^1.2",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
Expand Down
5 changes: 1 addition & 4 deletions examples/01-client-get-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

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

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

$client->get('http://google.com/')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/02-client-concurrent-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

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

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

$client->head('http://www.github.com/clue/http-react')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
Expand All @@ -19,5 +18,3 @@
$client->get('http://www.lueck.tv/psocksd')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/03-client-request-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

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

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

$promises = array(
$client->head('http://www.github.com/clue/http-react'),
Expand All @@ -28,5 +27,3 @@
var_dump($response->getHeaders());
echo PHP_EOL . $response->getBody();
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/04-client-post-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

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

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

$data = array(
'name' => array(
Expand All @@ -25,5 +24,3 @@
)->then(function (ResponseInterface $response) {
echo (string)$response->getBody();
}, 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/05-client-put-xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

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

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

$xml = new SimpleXMLElement('<users></users>');
$child = $xml->addChild('user');
Expand All @@ -22,5 +21,3 @@
)->then(function (ResponseInterface $response) {
echo (string)$response->getBody();
}, 'printf');

$loop->run();
13 changes: 4 additions & 9 deletions examples/11-client-http-connect-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@
// $ php examples/72-server-http-connect-proxy.php 8080
// $ php examples/11-client-http-connect-proxy.php

use React\Http\Browser;
use Clue\React\HttpProxy\ProxyConnector as HttpConnectClient;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory as LoopFactory;
use React\Http\Browser;
use React\Socket\Connector;

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

$loop = LoopFactory::create();

// create a new HTTP CONNECT proxy client which connects to a HTTP CONNECT proxy server listening on localhost:8080
$proxy = new HttpConnectClient('127.0.0.1:8080', new Connector($loop));
$proxy = new HttpConnectClient('127.0.0.1:8080', new Connector());

// create a Browser object that uses the HTTP CONNECT proxy client for connections
$connector = new Connector($loop, array(
$connector = new Connector(null, array(
'tcp' => $proxy,
'dns' => false
));
$browser = new Browser($loop, $connector);
$browser = new Browser(null, $connector);

// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');

$loop->run();
13 changes: 4 additions & 9 deletions examples/12-client-socks-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
// not already running a SOCKS proxy server?
// Try LeProxy.org or this: `ssh -D 1080 localhost`

use React\Http\Browser;
use Clue\React\Socks\Client as SocksClient;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory as LoopFactory;
use React\Http\Browser;
use React\Socket\Connector;

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

$loop = LoopFactory::create();

// create a new SOCKS proxy client which connects to a SOCKS proxy server listening on localhost:1080
$proxy = new SocksClient('127.0.0.1:1080', new Connector($loop));
$proxy = new SocksClient('127.0.0.1:1080', new Connector());

// create a Browser object that uses the SOCKS proxy client for connections
$connector = new Connector($loop, array(
$connector = new Connector(null, array(
'tcp' => $proxy,
'dns' => false
));
$browser = new Browser($loop, $connector);
$browser = new Browser(null, $connector);

// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');

$loop->run();
14 changes: 5 additions & 9 deletions examples/13-client-ssh-proxy.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
<?php

use React\Http\Browser;
use Clue\React\SshProxy\SshSocksConnector;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory as LoopFactory;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Socket\Connector;

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

$loop = LoopFactory::create();

// create a new SSH proxy client which connects to a SSH server listening on localhost:22
// You can pass any SSH server address as first argument, e.g. user@example.com
$proxy = new SshSocksConnector(isset($argv[1]) ? $argv[1] : 'localhost:22', $loop);
$proxy = new SshSocksConnector(isset($argv[1]) ? $argv[1] : 'localhost:22', Loop::get());

// create a Browser object that uses the SSH proxy client for connections
$connector = new Connector($loop, array(
$connector = new Connector(null, array(
'tcp' => $proxy,
'dns' => false
));
$browser = new Browser($loop, $connector);
$browser = new Browser(null, $connector);

// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');

$loop->run();
11 changes: 3 additions & 8 deletions examples/14-client-unix-domain-sockets.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?php

use React\Http\Browser;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory as LoopFactory;
use React\Http\Browser;
use React\Socket\FixedUriConnector;
use React\Socket\UnixConnector;
use RingCentral\Psr7;

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

$loop = LoopFactory::create();

// create a Browser object that uses the a Unix Domain Sockets (UDS) path for all requests
$connector = new FixedUriConnector(
'unix:///var/run/docker.sock',
new UnixConnector($loop)
new UnixConnector()
);

$browser = new Browser($loop, $connector);
$browser = new Browser(null, $connector);

// demo fetching HTTP headers (or bail out otherwise)
$browser->get('http://localhost/info')->then(function (ResponseInterface $response) {
echo Psr7\str($response);
}, 'printf');

$loop->run();
9 changes: 3 additions & 6 deletions examples/21-client-request-streaming-to-stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
exit(1);
}

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

$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);

$url = isset($argv[1]) ? $argv[1] : 'http://google.com/';
$info->write('Requesting ' . $url . '' . PHP_EOL);
Expand All @@ -29,5 +28,3 @@
assert($body instanceof ReadableStreamInterface);
$body->pipe($out);
}, 'printf');

$loop->run();
9 changes: 3 additions & 6 deletions examples/22-client-stream-upload-from-stdin.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use React\Http\Browser;
use Psr\Http\Message\ResponseInterface;
use React\Http\Browser;
use React\Stream\ReadableResourceStream;
use RingCentral\Psr7;

Expand All @@ -12,16 +12,13 @@
exit(1);
}

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

$in = new ReadableResourceStream(STDIN, $loop);
$in = new ReadableResourceStream(STDIN);

$url = isset($argv[1]) ? $argv[1] : 'https://httpbin.org/post';
echo 'Sending STDIN as POST to ' . $url . '' . PHP_EOL;

$client->post($url, array(), $in)->then(function (ResponseInterface $response) {
echo 'Received' . PHP_EOL . Psr7\str($response);
}, 'printf');

$loop->run();
9 changes: 2 additions & 7 deletions examples/51-server-hello-world.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Server;

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

$loop = Factory::create();

$server = new Server($loop, function (ServerRequestInterface $request) {
$server = new Server(function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -19,9 +16,7 @@
);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

$loop->run();
9 changes: 2 additions & 7 deletions examples/52-server-count-visitors.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Server;

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

$loop = Factory::create();

$counter = 0;
$server = new Server($loop, function (ServerRequestInterface $request) use (&$counter) {
$server = new Server(function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
array(
Expand All @@ -20,9 +17,7 @@
);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

$loop->run();
9 changes: 2 additions & 7 deletions examples/53-server-whatsmyip.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Server;

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

$loop = Factory::create();

$server = new Server($loop, function (ServerRequestInterface $request) {
$server = new Server(function (ServerRequestInterface $request) {
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];

return new Response(
Expand All @@ -21,9 +18,7 @@
);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

$loop->run();
9 changes: 2 additions & 7 deletions examples/54-server-query-parameter.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Server;

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

$loop = Factory::create();

$server = new Server($loop, function (ServerRequestInterface $request) {
$server = new Server(function (ServerRequestInterface $request) {
$queryParams = $request->getQueryParams();

$body = 'The query parameter "foo" is not set. Click the following link ';
Expand All @@ -28,9 +25,7 @@
);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

$loop->run();
Loading

0 comments on commit f37c964

Please sign in to comment.