Skip to content

Commit 271dd95

Browse files
authored
Merge pull request #374 from clue-labs/expose-reactphp
Expose ReactPHP in `User-Agent` and `Server` header
2 parents f92abcd + 89a1d41 commit 271dd95

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,65 +1626,70 @@ create your own HTTP response message instead.
16261626
When a response is returned from the request handler function, it will be
16271627
processed by the [`Server`](#server) and then sent back to the client.
16281628

1629-
The `Server` will automatically add the protocol version of the request, so you
1630-
don't have to.
1631-
1632-
A `Date` header will be automatically added with the system date and time if none is given.
1633-
You can add a custom `Date` header yourself like this:
1629+
A `Server: ReactPHP/1` response header will be added automatically. You can add
1630+
a custom `Server` response header like this:
16341631

16351632
```php
1636-
$server = new Server($loop, function (ServerRequestInterface $request) {
1637-
return new Response(
1633+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1634+
return new React\Http\Message\Response(
16381635
200,
16391636
array(
1640-
'Date' => date('D, d M Y H:i:s T')
1637+
'Server' => 'PHP/3'
16411638
)
16421639
);
16431640
});
16441641
```
16451642

1646-
If you don't have a appropriate clock to rely on, you should
1647-
unset this header with an empty string:
1643+
If you do not want to send this `Sever` response header at all (such as when you
1644+
don't want to expose the underlying server software), you can use an empty
1645+
string value like this:
16481646

16491647
```php
1650-
$server = new Server($loop, function (ServerRequestInterface $request) {
1651-
return new Response(
1648+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1649+
return new React\Http\Message\Response(
16521650
200,
16531651
array(
1654-
'Date' => ''
1652+
'Server' => ''
16551653
)
16561654
);
16571655
});
16581656
```
16591657

1660-
Note that it will automatically assume a `X-Powered-By: react/alpha` header
1661-
unless your specify a custom `X-Powered-By` header yourself:
1658+
A `Date` response header will be added automatically with the current system
1659+
date and time if none is given. You can add a custom `Date` response header
1660+
like this:
16621661

16631662
```php
1664-
$server = new Server($loop, function (ServerRequestInterface $request) {
1665-
return new Response(
1663+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1664+
return new React\Http\Message\Response(
16661665
200,
16671666
array(
1668-
'X-Powered-By' => 'PHP 3'
1667+
'Date' => gmdate('D, d M Y H:i:s \G\M\T')
16691668
)
16701669
);
16711670
});
16721671
```
16731672

1674-
If you do not want to send this header at all, you can use an empty string as
1675-
value like this:
1673+
If you do not want to send this `Date` response header at all (such as when you
1674+
don't have an appropriate clock to rely on), you can use an empty string value
1675+
like this:
16761676

16771677
```php
1678-
$server = new Server($loop, function (ServerRequestInterface $request) {
1679-
return new Response(
1678+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1679+
return new React\Http\Message\Response(
16801680
200,
16811681
array(
1682-
'X-Powered-By' => ''
1682+
'Date' => ''
16831683
)
16841684
);
16851685
});
16861686
```
16871687

1688+
The `Server` class will automatically add the protocol version of the request,
1689+
so you don't have to. For instance, if the client sends the request using the
1690+
HTTP/1.1 protocol version, the response message will also use the same protocol
1691+
version, no matter what version is returned from the request handler function.
1692+
16881693
Note that persistent connections (`Connection: keep-alive`) are currently
16891694
not supported.
16901695
As such, HTTP/1.1 response messages will automatically include a

src/Client/RequestData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private function mergeDefaultheaders(array $headers)
2929
$defaults = array_merge(
3030
array(
3131
'Host' => $this->getHost().$port,
32-
'User-Agent' => 'React/alpha',
32+
'User-Agent' => 'ReactPHP/1',
3333
),
3434
$connectionHeaders,
3535
$authHeaders

src/Io/StreamingServer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
244244
$version = $request->getProtocolVersion();
245245
$response = $response->withProtocolVersion($version);
246246

247-
// assign default "X-Powered-By" header automatically
248-
if (!$response->hasHeader('X-Powered-By')) {
249-
$response = $response->withHeader('X-Powered-By', 'React/alpha');
250-
} elseif ($response->getHeaderLine('X-Powered-By') === ''){
251-
$response = $response->withoutHeader('X-Powered-By');
247+
// assign default "Server" header automatically
248+
if (!$response->hasHeader('Server')) {
249+
$response = $response->withHeader('Server', 'ReactPHP/1');
250+
} elseif ($response->getHeaderLine('Server') === ''){
251+
$response = $response->withoutHeader('Server');
252252
}
253253

254254
// assign default "Date" header from current time automatically

tests/Client/RequestDataTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function toStringReturnsHTTPRequestMessage()
1414

1515
$expected = "GET / HTTP/1.0\r\n" .
1616
"Host: www.example.com\r\n" .
17-
"User-Agent: React/alpha\r\n" .
17+
"User-Agent: ReactPHP/1\r\n" .
1818
"\r\n";
1919

2020
$this->assertSame($expected, $requestData->__toString());
@@ -27,7 +27,7 @@ public function toStringReturnsHTTPRequestMessageWithEmptyQueryString()
2727

2828
$expected = "GET /path?hello=world HTTP/1.0\r\n" .
2929
"Host: www.example.com\r\n" .
30-
"User-Agent: React/alpha\r\n" .
30+
"User-Agent: ReactPHP/1\r\n" .
3131
"\r\n";
3232

3333
$this->assertSame($expected, $requestData->__toString());
@@ -40,7 +40,7 @@ public function toStringReturnsHTTPRequestMessageWithZeroQueryStringAndRootPath(
4040

4141
$expected = "GET /?0 HTTP/1.0\r\n" .
4242
"Host: www.example.com\r\n" .
43-
"User-Agent: React/alpha\r\n" .
43+
"User-Agent: ReactPHP/1\r\n" .
4444
"\r\n";
4545

4646
$this->assertSame($expected, $requestData->__toString());
@@ -53,7 +53,7 @@ public function toStringReturnsHTTPRequestMessageWithOptionsAbsoluteRequestForm(
5353

5454
$expected = "OPTIONS / HTTP/1.0\r\n" .
5555
"Host: www.example.com\r\n" .
56-
"User-Agent: React/alpha\r\n" .
56+
"User-Agent: ReactPHP/1\r\n" .
5757
"\r\n";
5858

5959
$this->assertSame($expected, $requestData->__toString());
@@ -66,7 +66,7 @@ public function toStringReturnsHTTPRequestMessageWithOptionsAsteriskRequestForm(
6666

6767
$expected = "OPTIONS * HTTP/1.0\r\n" .
6868
"Host: www.example.com\r\n" .
69-
"User-Agent: React/alpha\r\n" .
69+
"User-Agent: ReactPHP/1\r\n" .
7070
"\r\n";
7171

7272
$this->assertSame($expected, $requestData->__toString());
@@ -80,7 +80,7 @@ public function toStringReturnsHTTPRequestMessageWithProtocolVersion()
8080

8181
$expected = "GET / HTTP/1.1\r\n" .
8282
"Host: www.example.com\r\n" .
83-
"User-Agent: React/alpha\r\n" .
83+
"User-Agent: ReactPHP/1\r\n" .
8484
"Connection: close\r\n" .
8585
"\r\n";
8686

@@ -131,7 +131,7 @@ public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConst
131131

132132
$expected = "GET / HTTP/1.1\r\n" .
133133
"Host: www.example.com\r\n" .
134-
"User-Agent: React/alpha\r\n" .
134+
"User-Agent: ReactPHP/1\r\n" .
135135
"Connection: close\r\n" .
136136
"\r\n";
137137

@@ -145,7 +145,7 @@ public function toStringUsesUserPassFromURL()
145145

146146
$expected = "GET / HTTP/1.0\r\n" .
147147
"Host: www.example.com\r\n" .
148-
"User-Agent: React/alpha\r\n" .
148+
"User-Agent: ReactPHP/1\r\n" .
149149
"Authorization: Basic am9objpkdW1teQ==\r\n" .
150150
"\r\n";
151151

tests/Io/StreamingServerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ public function testRequestEventWithPartialBodyWillEmitData()
650650
$this->connection->emit('data', array($data));
651651
}
652652

653-
public function testResponseContainsPoweredByHeader()
653+
public function testResponseContainsServerHeader()
654654
{
655655
$server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) {
656656
return new Response();
@@ -675,7 +675,7 @@ function ($data) use (&$buffer) {
675675
$data = $this->createGetRequest();
676676
$this->connection->emit('data', array($data));
677677

678-
$this->assertContainsString("\r\nX-Powered-By: React/alpha\r\n", $buffer);
678+
$this->assertContainsString("\r\nServer: ReactPHP/1\r\n", $buffer);
679679
}
680680

681681
public function testResponsePendingPromiseWillNotSendAnything()
@@ -931,7 +931,7 @@ public function testResponseUpgradeInResponseCanBeUsedToAdvertisePossibleUpgrade
931931
200,
932932
array(
933933
'date' => '',
934-
'x-powered-by' => '',
934+
'server' => '',
935935
'Upgrade' => 'demo'
936936
),
937937
'foo'
@@ -967,7 +967,7 @@ public function testResponseUpgradeWishInRequestCanBeIgnoredByReturningNormalRes
967967
200,
968968
array(
969969
'date' => '',
970-
'x-powered-by' => ''
970+
'server' => ''
971971
),
972972
'foo'
973973
);
@@ -1002,7 +1002,7 @@ public function testResponseUpgradeSwitchingProtocolIncludesConnectionUpgradeHea
10021002
101,
10031003
array(
10041004
'date' => '',
1005-
'x-powered-by' => '',
1005+
'server' => '',
10061006
'Upgrade' => 'demo'
10071007
),
10081008
'foo'
@@ -1042,7 +1042,7 @@ public function testResponseUpgradeSwitchingProtocolWithStreamWillPipeDataToConn
10421042
101,
10431043
array(
10441044
'date' => '',
1045-
'x-powered-by' => '',
1045+
'server' => '',
10461046
'Upgrade' => 'demo'
10471047
),
10481048
$stream
@@ -2171,7 +2171,7 @@ public function testResponseCanContainMultipleCookieHeaders()
21712171
'session=abc'
21722172
),
21732173
'Date' => '',
2174-
'X-Powered-By' => ''
2174+
'Server' => ''
21752175
)
21762176
);
21772177
});

0 commit comments

Comments
 (0)