Skip to content

Commit d9b6bdb

Browse files
committed
Organize and rename client and server examples
1 parent 73139a8 commit d9b6bdb

34 files changed

+77
-49
lines changed

README.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
# Http
1+
# HTTP
22

33
[![Build Status](https://travis-ci.org/reactphp/http.svg?branch=master)](https://travis-ci.org/reactphp/http)
44

5-
Event-driven, streaming plaintext HTTP and secure HTTPS server for [ReactPHP](https://reactphp.org/).
5+
Event-driven, streaming HTTP client and server implementation for [ReactPHP](https://reactphp.org/).
6+
7+
This HTTP library provides re-usable implementations for an HTTP client and
8+
server based on ReactPHP's [`Socket`](https://github.com/reactphp/socket) and
9+
[`EventLoop`](https://github.com/reactphp/event-loop) components.
10+
Its client component allows you to send any number of async HTTP/HTTPS requests
11+
concurrently.
12+
Its server component allows you to build plaintext HTTP and secure HTTPS servers
13+
that accept incoming HTTP requests from HTTP clients (such as web browsers).
14+
This library provides async, streaming means for all of this, so you can handle
15+
multiple concurrent HTTP requests without blocking.
616

717
**Table of contents**
818

@@ -91,8 +101,8 @@ This is an HTTP server which responds with `Hello World!` to every request.
91101
```php
92102
$loop = React\EventLoop\Factory::create();
93103

94-
$server = new Server(function (ServerRequestInterface $request) {
95-
return new Response(
104+
$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) {
105+
return new React\Http\Response(
96106
200,
97107
array(
98108
'Content-Type' => 'text/plain'
@@ -107,7 +117,7 @@ $server->listen($socket);
107117
$loop->run();
108118
```
109119

110-
See also the [examples](examples).
120+
See also the [examples](examples/).
111121

112122
## Client Usage
113123

@@ -487,8 +497,8 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
487497
});
488498
```
489499

490-
See also the [stream download example](examples/91-benchmark-download.php) and
491-
the [stream forwarding example](examples/21-stream-forwarding.php).
500+
See also the [stream download benchmark example](examples/91-client-benchmark-download.php) and
501+
the [stream forwarding example](examples/21-client-request-streaming-to-stdout.php).
492502

493503
You can invoke the following methods on the message body:
494504

@@ -607,7 +617,7 @@ $connector = new React\Socket\Connector($loop, array(
607617
$browser = new React\Http\Browser($loop, $connector);
608618
```
609619

610-
See also the [HTTP CONNECT proxy example](examples/11-http-proxy.php).
620+
See also the [HTTP CONNECT proxy example](examples/11-http-connect-proxy.php).
611621

612622
### SOCKS proxy
613623

@@ -738,7 +748,8 @@ $socket = new React\Socket\Server('0.0.0.0:8080', $loop);
738748
$server->listen($socket);
739749
```
740750

741-
See also the [`listen()`](#listen) method and the [first example](../examples/)
751+
See also the [`listen()`](#listen) method and the
752+
[hello world server example](examples/51-server-hello-world.php)
742753
for more details.
743754

744755
By default, the `Server` buffers and parses the complete incoming HTTP
@@ -846,7 +857,8 @@ $socket = new React\Socket\Server('0.0.0.0:8080', $loop);
846857
$server->listen($socket);
847858
```
848859

849-
See also [example #1](examples) for more details.
860+
See also [hello world server example](examples/51-server-hello-world.php)
861+
for more details.
850862

851863
This example will start listening for HTTP requests on the alternative
852864
HTTP port `8080` on all interfaces (publicly). As an alternative, it is
@@ -873,7 +885,8 @@ $socket = new React\Socket\Server('tls://0.0.0.0:8443', $loop, array(
873885
$server->listen($socket);
874886
```
875887

876-
See also [example #11](examples) for more details.
888+
See also [hello world HTTPS example](examples/61-server-hello-world-https.php)
889+
for more details.
877890

878891
### Server Request
879892

@@ -945,7 +958,7 @@ $server = new Server(function (ServerRequestInterface $request) {
945958
});
946959
```
947960

948-
See also [example #3](examples).
961+
See also [whatsmyip server example](examples/53-server-whatsmyip.php).
949962

950963
> Advanced: Note that address parameters will not be set if you're listening on
951964
a Unix domain socket (UDS) path as this protocol lacks the concept of
@@ -983,7 +996,7 @@ Use [`htmlentities`](https://www.php.net/manual/en/function.htmlentities.php)
983996
like in this example to prevent
984997
[Cross-Site Scripting (abbreviated as XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting).
985998

986-
See also [example #4](examples).
999+
See also [server query parameters example](examples/54-server-query-parameter.php).
9871000

9881001
#### Request body
9891002

@@ -1022,7 +1035,7 @@ $server = new Server(function (ServerRequestInterface $request) {
10221035
});
10231036
```
10241037

1025-
See also [example #12](examples) for more details.
1038+
See also [form upload example](examples/62-server-form-upload.php) for more details.
10261039

10271040
The `getBody(): StreamInterface` method can be used to
10281041
get the raw data from this request body, similar to
@@ -1047,7 +1060,7 @@ $server = new Server(function (ServerRequestInterface $request) {
10471060
});
10481061
```
10491062

1050-
See also [example #9](examples) for more details.
1063+
See also [JSON API server example](examples/59-server-json-api.php) for more details.
10511064

10521065
The `getUploadedFiles(): array` method can be used to
10531066
get the uploaded files in this request, similar to
@@ -1070,7 +1083,7 @@ $server = new Server(function (ServerRequestInterface $request) {
10701083
});
10711084
```
10721085

1073-
See also [example #12](examples) for more details.
1086+
See also [form upload server example](examples/62-server-form-upload.php) for more details.
10741087

10751088
The `getSize(): ?int` method can be used to
10761089
get the size of the request body, similar to PHP's `$_SERVER['CONTENT_LENGTH']` variable.
@@ -1169,7 +1182,7 @@ $server = new React\Http\Server(array(
11691182
The above example simply counts the number of bytes received in the request body.
11701183
This can be used as a skeleton for buffering or processing the request body.
11711184

1172-
See also [example #13](examples) for more details.
1185+
See also [streaming request server example](examples/63-server-streaming-request.php) for more details.
11731186

11741187
The `data` event will be emitted whenever new data is available on the request
11751188
body stream.
@@ -1307,7 +1320,7 @@ non-alphanumeric characters.
13071320
This encoding is also used internally when decoding the name and value of cookies
13081321
(which is in line with other implementations, such as PHP's cookie functions).
13091322

1310-
See also [example #5](examples) for more details.
1323+
See also [cookie server example](examples/55-server-cookie-handling.php) for more details.
13111324

13121325
#### Invalid request
13131326

@@ -1467,7 +1480,7 @@ in this case (if applicable).
14671480
to look into using [Ratchet](http://socketo.me/) instead.
14681481
If you want to handle a custom protocol, you will likely want to look into the
14691482
[HTTP specs](https://tools.ietf.org/html/rfc7230#section-6.7) and also see
1470-
[examples #31 and #32](examples) for more details.
1483+
[examples #81 and #82](examples/) for more details.
14711484
In particular, the `101` (Switching Protocols) response code MUST NOT be used
14721485
unless you send an `Upgrade` response header value that is also present in
14731486
the corresponding HTTP/1.1 `Upgrade` request header value.
@@ -1488,7 +1501,7 @@ in this case (if applicable).
14881501
requests, one may still be present. Normal request body processing applies
14891502
here and the connection will only turn to "tunneling mode" after the request
14901503
body has been processed (which should be empty in most cases).
1491-
See also [example #22](examples) for more details.
1504+
See also [HTTP CONNECT server example](examples/72-server-http-connect-proxy.php) for more details.
14921505

14931506
#### Response length
14941507

@@ -1851,7 +1864,7 @@ $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response
18511864
});
18521865
```
18531866

1854-
See also [example 01](examples/01-google.php).
1867+
See also [GET request client example](examples/01-client-get-request.php).
18551868

18561869
#### post()
18571870

@@ -1870,7 +1883,7 @@ $browser->post(
18701883
});
18711884
```
18721885

1873-
See also [example 04](examples/04-post-json.php).
1886+
See also [POST JSON client example](examples/04-client-post-json.php).
18741887

18751888
This method is also commonly used to submit HTML form data:
18761889

@@ -1964,7 +1977,7 @@ $browser->put(
19641977
});
19651978
```
19661979

1967-
See also [example 05](examples/05-put-xml.php).
1980+
See also [PUT XML client example](examples/05-client-put-xml.php).
19681981

19691982
This method will automatically add a matching `Content-Length` request
19701983
header if the outgoing request body is a `string`. If you're using a
@@ -2538,7 +2551,7 @@ $server = new Server(array(
25382551
));
25392552
```
25402553

2541-
See also [example #12](examples) for more details.
2554+
See also [form upload server example](examples/62-server-form-upload.php) for more details.
25422555

25432556
By default, this middleware respects the
25442557
[`upload_max_filesize`](https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize)

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react/http",
3-
"description": "Event-driven, streaming plaintext HTTP and secure HTTPS server for ReactPHP",
4-
"keywords": ["event-driven", "streaming", "HTTP", "HTTPS", "server", "ReactPHP"],
3+
"description": "Event-driven, streaming HTTP client and server implementation for ReactPHP",
4+
"keywords": ["HTTP client", "HTTP server", "HTTP", "HTTPS", "event-driven", "streaming", "client", "server", "PSR-7", "async", "ReactPHP"],
55
"license": "MIT",
66
"require": {
77
"php": ">=5.3.0",
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/11-http-proxy.php renamed to examples/11-client-http-connect-proxy.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
// not already running a HTTP CONNECT proxy server?
4+
// Try LeProxy.org or this:
5+
//
6+
// $ php examples/72-server-http-connect-proxy.php 8080
7+
// $ php examples/11-client-http-connect-proxy.php
8+
39
use React\Http\Browser;
410
use Clue\React\HttpProxy\ProxyConnector as HttpConnectClient;
511
use Psr\Http\Message\ResponseInterface;
@@ -11,7 +17,6 @@
1117
$loop = LoopFactory::create();
1218

1319
// create a new HTTP CONNECT proxy client which connects to a HTTP CONNECT proxy server listening on localhost:8080
14-
// not already running a HTTP CONNECT proxy server? Try LeProxy.org!
1520
$proxy = new HttpConnectClient('127.0.0.1:8080', new Connector($loop));
1621

1722
// create a Browser object that uses the HTTP CONNECT proxy client for connections

examples/12-socks-proxy.php renamed to examples/12-client-socks-proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
// not already running a SOCKS proxy server?
4+
// Try LeProxy.org or this: `ssh -D 1080 localhost`
5+
36
use React\Http\Browser;
47
use Clue\React\Socks\Client as SocksClient;
58
use Psr\Http\Message\ResponseInterface;
@@ -11,7 +14,6 @@
1114
$loop = LoopFactory::create();
1215

1316
// create a new SOCKS proxy client which connects to a SOCKS proxy server listening on localhost:1080
14-
// not already running a SOCKS proxy server? Try LeProxy.org or this: `ssh -D 1080 localhost`
1517
$proxy = new SocksClient('127.0.0.1:1080', new Connector($loop));
1618

1719
// create a Browser object that uses the SOCKS proxy client for connections
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/09-json-api.php renamed to examples/59-server-json-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Simple JSON-based HTTP API example as a base to build RESTful/RESTish APIs
44
// Launch demo and use your favorite CLI tool to test API requests
55
//
6-
// $ php examples/09-json-api.php 8080
6+
// $ php examples/59-server-json-api.php 8080
77
// $ curl -v http://localhost:8080/ -H 'Content-Type: application/json' -d '{"name":"Alice"}'
88

99
use Psr\Http\Message\ServerRequestInterface;

examples/12-upload.php renamed to examples/62-server-form-upload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Simple HTML form with file upload
44
// Launch demo and use your favorite browser or CLI tool to test form submissions
55
//
6-
// $ php examples/12-upload.php 8080
6+
// $ php examples/62-server-form-upload.php 8080
77
// $ curl --form name=test --form age=30 http://localhost:8080/
88
// $ curl --form name=hi --form [email protected] http://localhost:8080/
99

examples/21-http-proxy.php renamed to examples/71-server-http-proxy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
// $ php examples/71-server-http-proxy.php 8080
4+
// $ curl -v --proxy http://localhost:8080 http://reactphp.org/
5+
36
use Psr\Http\Message\RequestInterface;
47
use React\EventLoop\Factory;
58
use React\Http\Response;

examples/22-connect-proxy.php renamed to examples/72-server-http-connect-proxy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
// $ php examples/72-server-http-connect-proxy.php 8080
4+
// $ curl -v --proxy http://localhost:8080 https://reactphp.org/
5+
36
use Psr\Http\Message\ServerRequestInterface;
47
use React\EventLoop\Factory;
58
use React\Http\Response;
File renamed without changes.
File renamed without changes.

examples/91-benchmark-download.php renamed to examples/91-client-benchmark-download.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22

33
// a) simple download benchmark against public HTTP endpoint:
4-
// $ php examples/91-benchmark-download.php http://httpbin.org/get
4+
// $ php examples/91-client-benchmark-download.php http://httpbin.org/get
55

66
// b) local 10 GB download benchmark against localhost address to avoid network overhead
77
//
8-
// b1) first run example HTTP server, e.g. from react/http:
9-
// $ cd workspace/reactphp-http
10-
// $ php examples/99-benchmark-download.php 8080
8+
// b1) first run example HTTP server:
9+
// $ php examples/99-server-benchmark-download.php 8080
1110
//
1211
// b2) run HTTP client receiving a 10 GB download:
13-
// $ php examples/91-benchmark-download.php http://localhost:8080/10g.bin
12+
// $ php examples/91-client-benchmark-download.php http://localhost:8080/10g.bin
1413

1514
use React\Http\Browser;
1615
use Psr\Http\Message\ResponseInterface;

examples/92-benchmark-upload.php renamed to examples/92-client-benchmark-upload.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22

33
// a) simple 1 MB upload benchmark against public HTTP endpoint
4-
// $ php examples/92-benchmark-upload.php http://httpbin.org/post 1
4+
// $ php examples/92-client-benchmark-upload.php http://httpbin.org/post 1
55
//
66
// b) local 10 GB upload benchmark against localhost address to avoid network overhead
77
//
8-
// b1) first run example HTTP server, e.g. from react/http
9-
// $ cd workspace/reactphp-http
10-
// $ php examples/13-stream-request.php 8080
8+
// b1) first run example HTTP server:
9+
// $ php examples/63-server-streaming-request.php 8080
1110
//
1211
// b2) run HTTP client sending a 10 GB upload
13-
// $ php examples/92-benchmark-upload.php http://localhost:8080/ 10000
12+
// $ php examples/92-client-benchmark-upload.php http://localhost:8080/ 10000
1413

1514
use React\Http\Browser;
1615
use Evenement\EventEmitter;

examples/99-benchmark-download.php renamed to examples/99-server-benchmark-download.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
// $ php examples/99-benchmark-download.php 8080
3+
// $ php examples/99-server-benchmark-download.php 8080
44
// $ curl http://localhost:8080/10g.bin > /dev/null
55
// $ wget http://localhost:8080/10g.bin -O /dev/null
66
// $ ab -n10 -c10 http://localhost:8080/1g.bin
7-
// $ docker run -it --rm --net=host jordi/ab ab -n10 -c10 http://localhost:8080/1g.bin
7+
// $ docker run -it --rm --net=host jordi/ab -n100000 -c10 http://localhost:8080/
8+
// $ docker run -it --rm --net=host jordi/ab -n10 -c10 http://localhost:8080/1g.bin
89

910
use Evenement\EventEmitter;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -118,7 +119,7 @@ public function getSize()
118119
);
119120
});
120121

121-
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
122+
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop, array('tcp' => array('backlog' => 511)));
122123
$server->listen($socket);
123124

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

src/Browser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
7373
* });
7474
* ```
7575
*
76-
* See also [example 01](../examples/01-google.php).
76+
* See also [GET request client example](../examples/01-client-get-request.php).
7777
*
7878
* @param string $url URL for the request.
7979
* @param array $headers
@@ -99,7 +99,7 @@ public function get($url, array $headers = array())
9999
* });
100100
* ```
101101
*
102-
* See also [example 04](../examples/04-post-json.php).
102+
* See also [POST JSON client example](../examples/04-client-post-json.php).
103103
*
104104
* This method is also commonly used to submit HTML form data:
105105
*
@@ -216,7 +216,7 @@ public function patch($url, array $headers = array(), $contents = '')
216216
* });
217217
* ```
218218
*
219-
* See also [example 05](../examples/05-put-xml.php).
219+
* See also [PUT XML client example](../examples/05-client-put-xml.php).
220220
*
221221
* This method will automatically add a matching `Content-Length` request
222222
* header if the outgoing request body is a `string`. If you're using a

0 commit comments

Comments
 (0)