1
- # Http
1
+ # HTTP
2
2
3
3
[ ![ Build Status] ( https://travis-ci.org/reactphp/http.svg?branch=master )] ( https://travis-ci.org/reactphp/http )
4
4
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.
6
16
7
17
** Table of contents**
8
18
@@ -91,8 +101,8 @@ This is an HTTP server which responds with `Hello World!` to every request.
91
101
``` php
92
102
$loop = React\EventLoop\Factory::create();
93
103
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(
96
106
200,
97
107
array(
98
108
'Content-Type' => 'text/plain'
@@ -107,7 +117,7 @@ $server->listen($socket);
107
117
$loop->run();
108
118
```
109
119
110
- See also the [ examples] ( examples ) .
120
+ See also the [ examples] ( examples/ ) .
111
121
112
122
## Client Usage
113
123
@@ -487,8 +497,8 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
487
497
});
488
498
```
489
499
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 ) .
492
502
493
503
You can invoke the following methods on the message body:
494
504
@@ -607,7 +617,7 @@ $connector = new React\Socket\Connector($loop, array(
607
617
$browser = new React\Http\Browser($loop, $connector);
608
618
```
609
619
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 ) .
611
621
612
622
### SOCKS proxy
613
623
@@ -738,7 +748,8 @@ $socket = new React\Socket\Server('0.0.0.0:8080', $loop);
738
748
$server->listen($socket);
739
749
```
740
750
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 )
742
753
for more details.
743
754
744
755
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);
846
857
$server->listen($socket);
847
858
```
848
859
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.
850
862
851
863
This example will start listening for HTTP requests on the alternative
852
864
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(
873
885
$server->listen($socket);
874
886
```
875
887
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.
877
890
878
891
### Server Request
879
892
@@ -945,7 +958,7 @@ $server = new Server(function (ServerRequestInterface $request) {
945
958
});
946
959
```
947
960
948
- See also [ example # 3 ] ( examples ) .
961
+ See also [ whatsmyip server example ] ( examples/53-server-whatsmyip.php ) .
949
962
950
963
> Advanced: Note that address parameters will not be set if you're listening on
951
964
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)
983
996
like in this example to prevent
984
997
[ Cross-Site Scripting (abbreviated as XSS)] ( https://en.wikipedia.org/wiki/Cross-site_scripting ) .
985
998
986
- See also [ example # 4 ] ( examples ) .
999
+ See also [ server query parameters example ] ( examples/54-server-query-parameter.php ) .
987
1000
988
1001
#### Request body
989
1002
@@ -1022,7 +1035,7 @@ $server = new Server(function (ServerRequestInterface $request) {
1022
1035
});
1023
1036
```
1024
1037
1025
- See also [ example # 12 ] ( examples ) for more details.
1038
+ See also [ form upload example ] ( examples/62-server-form-upload.php ) for more details.
1026
1039
1027
1040
The ` getBody(): StreamInterface ` method can be used to
1028
1041
get the raw data from this request body, similar to
@@ -1047,7 +1060,7 @@ $server = new Server(function (ServerRequestInterface $request) {
1047
1060
});
1048
1061
```
1049
1062
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.
1051
1064
1052
1065
The ` getUploadedFiles(): array ` method can be used to
1053
1066
get the uploaded files in this request, similar to
@@ -1070,7 +1083,7 @@ $server = new Server(function (ServerRequestInterface $request) {
1070
1083
});
1071
1084
```
1072
1085
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.
1074
1087
1075
1088
The ` getSize(): ?int ` method can be used to
1076
1089
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(
1169
1182
The above example simply counts the number of bytes received in the request body.
1170
1183
This can be used as a skeleton for buffering or processing the request body.
1171
1184
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.
1173
1186
1174
1187
The ` data ` event will be emitted whenever new data is available on the request
1175
1188
body stream.
@@ -1307,7 +1320,7 @@ non-alphanumeric characters.
1307
1320
This encoding is also used internally when decoding the name and value of cookies
1308
1321
(which is in line with other implementations, such as PHP's cookie functions).
1309
1322
1310
- See also [ example # 5 ] ( examples ) for more details.
1323
+ See also [ cookie server example ] ( examples/55-server-cookie-handling.php ) for more details.
1311
1324
1312
1325
#### Invalid request
1313
1326
@@ -1467,7 +1480,7 @@ in this case (if applicable).
1467
1480
to look into using [ Ratchet] ( http://socketo.me/ ) instead.
1468
1481
If you want to handle a custom protocol, you will likely want to look into the
1469
1482
[ 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.
1471
1484
In particular, the ` 101 ` (Switching Protocols) response code MUST NOT be used
1472
1485
unless you send an ` Upgrade ` response header value that is also present in
1473
1486
the corresponding HTTP/1.1 ` Upgrade ` request header value.
@@ -1488,7 +1501,7 @@ in this case (if applicable).
1488
1501
requests, one may still be present. Normal request body processing applies
1489
1502
here and the connection will only turn to "tunneling mode" after the request
1490
1503
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.
1492
1505
1493
1506
#### Response length
1494
1507
@@ -1851,7 +1864,7 @@ $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response
1851
1864
});
1852
1865
```
1853
1866
1854
- See also [ example 01 ] ( examples/01-google .php ) .
1867
+ See also [ GET request client example ] ( examples/01-client-get-request .php ) .
1855
1868
1856
1869
#### post()
1857
1870
@@ -1870,7 +1883,7 @@ $browser->post(
1870
1883
});
1871
1884
```
1872
1885
1873
- See also [ example 04 ] ( examples/04-post-json.php ) .
1886
+ See also [ POST JSON client example ] ( examples/04-client -post-json.php ) .
1874
1887
1875
1888
This method is also commonly used to submit HTML form data:
1876
1889
@@ -1964,7 +1977,7 @@ $browser->put(
1964
1977
});
1965
1978
```
1966
1979
1967
- See also [ example 05 ] ( examples/05-put-xml.php ) .
1980
+ See also [ PUT XML client example ] ( examples/05-client -put-xml.php ) .
1968
1981
1969
1982
This method will automatically add a matching ` Content-Length ` request
1970
1983
header if the outgoing request body is a ` string ` . If you're using a
@@ -2538,7 +2551,7 @@ $server = new Server(array(
2538
2551
));
2539
2552
```
2540
2553
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.
2542
2555
2543
2556
By default, this middleware respects the
2544
2557
[ ` upload_max_filesize ` ] ( https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize )
0 commit comments