-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from clue-labs/http-client
Add HTTP client implementation (import clue/reactphp-buzz v2.9.0)
- Loading branch information
Showing
60 changed files
with
8,287 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$client->get('http://google.com/')->then(function (ResponseInterface $response) { | ||
var_dump($response->getHeaders(), (string)$response->getBody()); | ||
}); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$client->head('http://www.github.com/clue/http-react')->then(function (ResponseInterface $response) { | ||
var_dump($response->getHeaders(), (string)$response->getBody()); | ||
}); | ||
|
||
$client->get('http://google.com/')->then(function (ResponseInterface $response) { | ||
var_dump($response->getHeaders(), (string)$response->getBody()); | ||
}); | ||
|
||
$client->get('http://www.lueck.tv/psocksd')->then(function (ResponseInterface $response) { | ||
var_dump($response->getHeaders(), (string)$response->getBody()); | ||
}); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
// concurrently request a number of URLs. | ||
// return immediately once the first is completed, cancel all others. | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$promises = array( | ||
$client->head('http://www.github.com/clue/http-react'), | ||
$client->get('https://httpbin.org/'), | ||
$client->get('https://google.com'), | ||
$client->get('http://www.lueck.tv/psocksd'), | ||
$client->get('http://www.httpbin.org/absolute-redirect/5') | ||
); | ||
|
||
React\Promise\any($promises)->then(function (ResponseInterface $response) use ($promises) { | ||
// first response arrived => cancel all other pending requests | ||
foreach ($promises as $promise) { | ||
$promise->cancel(); | ||
} | ||
|
||
var_dump($response->getHeaders()); | ||
echo PHP_EOL . $response->getBody(); | ||
}); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$data = array( | ||
'name' => array( | ||
'first' => 'Alice', | ||
'name' => 'Smith' | ||
), | ||
'email' => '[email protected]' | ||
); | ||
|
||
$client->post( | ||
'https://httpbin.org/post', | ||
array( | ||
'Content-Type' => 'application/json' | ||
), | ||
json_encode($data) | ||
)->then(function (ResponseInterface $response) { | ||
echo (string)$response->getBody(); | ||
}, 'printf'); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$xml = new SimpleXMLElement('<users></users>'); | ||
$child = $xml->addChild('user'); | ||
$child->alias = 'clue'; | ||
$child->name = 'Christian Lück'; | ||
|
||
$client->put( | ||
'https://httpbin.org/put', | ||
array( | ||
'Content-Type' => 'text/xml' | ||
), | ||
$xml->asXML() | ||
)->then(function (ResponseInterface $response) { | ||
echo (string)$response->getBody(); | ||
}, 'printf'); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
// not already running a HTTP CONNECT proxy server? | ||
// Try LeProxy.org or this: | ||
// | ||
// $ 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\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)); | ||
|
||
// create a Browser object that uses the HTTP CONNECT proxy client for connections | ||
$connector = new Connector($loop, array( | ||
'tcp' => $proxy, | ||
'dns' => false | ||
)); | ||
$browser = new Browser($loop, $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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
// 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\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)); | ||
|
||
// create a Browser object that uses the SOCKS proxy client for connections | ||
$connector = new Connector($loop, array( | ||
'tcp' => $proxy, | ||
'dns' => false | ||
)); | ||
$browser = new Browser($loop, $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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Clue\React\SshProxy\SshSocksConnector; | ||
use Psr\Http\Message\ResponseInterface; | ||
use React\EventLoop\Factory as LoopFactory; | ||
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. [email protected] | ||
$proxy = new SshSocksConnector(isset($argv[1]) ? $argv[1] : 'localhost:22', $loop); | ||
|
||
// create a Browser object that uses the SSH proxy client for connections | ||
$connector = new Connector($loop, array( | ||
'tcp' => $proxy, | ||
'dns' => false | ||
)); | ||
$browser = new Browser($loop, $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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
use React\EventLoop\Factory as LoopFactory; | ||
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) | ||
); | ||
|
||
$browser = new Browser($loop, $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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
use React\Stream\ReadableStreamInterface; | ||
use React\Stream\WritableResourceStream; | ||
use RingCentral\Psr7; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
if (DIRECTORY_SEPARATOR === '\\') { | ||
fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL); | ||
exit(1); | ||
} | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$out = new WritableResourceStream(STDOUT, $loop); | ||
$info = new WritableResourceStream(STDERR, $loop); | ||
|
||
$url = isset($argv[1]) ? $argv[1] : 'http://google.com/'; | ||
$info->write('Requesting ' . $url . '…' . PHP_EOL); | ||
|
||
$client->requestStreaming('GET', $url)->then(function (ResponseInterface $response) use ($info, $out) { | ||
$info->write('Received' . PHP_EOL . Psr7\str($response)); | ||
|
||
$body = $response->getBody(); | ||
assert($body instanceof ReadableStreamInterface); | ||
$body->pipe($out); | ||
}, 'printf'); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use React\Http\Browser; | ||
use Psr\Http\Message\ResponseInterface; | ||
use React\Stream\ReadableResourceStream; | ||
use RingCentral\Psr7; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
if (DIRECTORY_SEPARATOR === '\\') { | ||
fwrite(STDERR, 'Non-blocking console I/O not supported on Windows' . PHP_EOL); | ||
exit(1); | ||
} | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Browser($loop); | ||
|
||
$in = new ReadableResourceStream(STDIN, $loop); | ||
|
||
$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(); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
// Simple HTML form with file upload | ||
// Launch demo and use your favorite browser or CLI tool to test form submissions | ||
// | ||
// $ php examples/12-upload.php 8080 | ||
// $ php examples/62-server-form-upload.php 8080 | ||
// $ curl --form name=test --form age=30 http://localhost:8080/ | ||
// $ curl --form name=hi --form [email protected] http://localhost:8080/ | ||
|
||
|
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
examples/21-http-proxy.php → examples/71-server-http-proxy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
examples/22-connect-proxy.php → examples/72-server-http-connect-proxy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.