Skip to content

Commit 89a1d41

Browse files
committed
Expose ReactPHP in User-Agent and Server header
The `Browser` now sends a `User-Agent: ReactPHP/1` request header (was `User-Agent: React/alpha`) by default. The `Server` now sends a `Server: ReactPHP/1` response header (was `X-Powered-By: React/alpha`) by default. Both can be overridden by explicitly assigning other header values as usual.
1 parent 4fbbeb0 commit 89a1d41

5 files changed

Lines changed: 49 additions & 44 deletions

File tree

README.md

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

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

16201617
```php
1621-
$server = new Server($loop, function (ServerRequestInterface $request) {
1622-
return new Response(
1618+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1619+
return new React\Http\Message\Response(
16231620
200,
16241621
array(
1625-
'Date' => date('D, d M Y H:i:s T')
1622+
'Server' => 'PHP/3'
16261623
)
16271624
);
16281625
});
16291626
```
16301627

1631-
If you don't have a appropriate clock to rely on, you should
1632-
unset this header with an empty string:
1628+
If you do not want to send this `Sever` response header at all (such as when you
1629+
don't want to expose the underlying server software), you can use an empty
1630+
string value like this:
16331631

16341632
```php
1635-
$server = new Server($loop, function (ServerRequestInterface $request) {
1636-
return new Response(
1633+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1634+
return new React\Http\Message\Response(
16371635
200,
16381636
array(
1639-
'Date' => ''
1637+
'Server' => ''
16401638
)
16411639
);
16421640
});
16431641
```
16441642

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

16481647
```php
1649-
$server = new Server($loop, function (ServerRequestInterface $request) {
1650-
return new Response(
1648+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1649+
return new React\Http\Message\Response(
16511650
200,
16521651
array(
1653-
'X-Powered-By' => 'PHP 3'
1652+
'Date' => gmdate('D, d M Y H:i:s \G\M\T')
16541653
)
16551654
);
16561655
});
16571656
```
16581657

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

16621662
```php
1663-
$server = new Server($loop, function (ServerRequestInterface $request) {
1664-
return new Response(
1663+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1664+
return new React\Http\Message\Response(
16651665
200,
16661666
array(
1667-
'X-Powered-By' => ''
1667+
'Date' => ''
16681668
)
16691669
);
16701670
});
16711671
```
16721672

1673+
The `Server` class will automatically add the protocol version of the request,
1674+
so you don't have to. For instance, if the client sends the request using the
1675+
HTTP/1.1 protocol version, the response message will also use the same protocol
1676+
version, no matter what version is returned from the request handler function.
1677+
16731678
Note that persistent connections (`Connection: keep-alive`) are currently
16741679
not supported.
16751680
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)