1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of a nekland library |
4
|
|
|
* |
5
|
|
|
* (c) Nekland <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full license, take a look to the LICENSE file |
8
|
|
|
* on the root directory of this project |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Nekland\Woketo\Http; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use React\Socket\ConnectionInterface; |
15
|
|
|
|
16
|
|
|
class Response extends AbstractHttpMessage |
17
|
|
|
{ |
18
|
|
|
const SWITCHING_PROTOCOLS = '101 Switching Protocols'; |
19
|
|
|
const BAD_REQUEST = '400 Bad Request'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string For example "404 Not Found" |
23
|
|
|
*/ |
24
|
|
|
private $httpResponse; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->setHttpVersion('HTTP/1.1'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $httpResponse |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function setHttpResponse($httpResponse) |
36
|
|
|
{ |
37
|
|
|
$this->httpResponse = $httpResponse; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ConnectionInterface $stream |
44
|
|
|
*/ |
45
|
|
|
public function send(ConnectionInterface $stream) |
46
|
|
|
{ |
47
|
|
|
$stringResponse = $this->getHttpVersion() . ' ' . $this->httpResponse . "\r\n"; |
48
|
|
|
|
49
|
|
|
foreach ($this->getHeaders() as $name => $content) { |
50
|
|
|
$stringResponse .= $name . ': '. $content . "\r\n"; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// No content to concatenate |
54
|
|
|
$stringResponse .= "\r\n"; |
55
|
|
|
|
56
|
|
|
$stream->write($stringResponse); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function createSwitchProtocolResponse() |
60
|
|
|
{ |
61
|
|
|
$response = new Response(); |
62
|
|
|
|
63
|
|
|
$response->setHttpResponse(Response::SWITCHING_PROTOCOLS); |
64
|
|
|
$response->addHeader('Upgrade', 'websocket'); |
65
|
|
|
$response->addHeader('Connection', 'Upgrade'); |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|