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 Nekland\Woketo\Exception\Http\HttpException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractHttpMessage |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractHttpMessage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var HttpHeadersBag |
25
|
|
|
*/ |
26
|
|
|
private $headers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string for example "HTTP/1.1" |
30
|
|
|
*/ |
31
|
|
|
private $httpVersion; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $httpVersion |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
19 |
|
protected function setHttpVersion($httpVersion) |
38
|
|
|
{ |
39
|
19 |
|
$this->httpVersion = $httpVersion; |
40
|
|
|
|
41
|
19 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
* @param string $value |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
17 |
|
public function addHeader(string $name, string $value) |
50
|
|
|
{ |
51
|
17 |
|
if (null === $this->headers) { |
52
|
17 |
|
$this->headers = new HttpHeadersBag(); |
53
|
|
|
} |
54
|
17 |
|
$this->headers->add($name, $value); |
55
|
|
|
|
56
|
17 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $header |
61
|
|
|
* @param mixed $default |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
10 |
|
public function getHeader(string $header, $default = null) |
65
|
|
|
{ |
66
|
10 |
|
return $this->headers[$header] ?: $default; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
8 |
|
public function getHttpVersion() |
73
|
|
|
{ |
74
|
8 |
|
return $this->httpVersion; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array|HttpHeadersBag |
79
|
|
|
*/ |
80
|
12 |
|
public function getHeaders() |
81
|
|
|
{ |
82
|
12 |
|
return $this->headers ?: new HttpHeadersBag(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string[] $headers |
87
|
|
|
* @param AbstractHttpMessage $request |
88
|
|
|
*/ |
89
|
12 |
|
protected static function initHeaders(array $headers, AbstractHttpMessage $request) |
90
|
|
|
{ |
91
|
12 |
|
foreach ($headers as $header) { |
92
|
12 |
|
$cuttedHeader = \explode(':', $header); |
93
|
12 |
|
$request->addHeader(\trim($cuttedHeader[0]), trim(str_replace($cuttedHeader[0] . ':', '', $header))); |
94
|
|
|
} |
95
|
12 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $line |
99
|
|
|
* @return HttpException |
100
|
|
|
*/ |
101
|
2 |
|
protected static function createNotHttpException($line) |
102
|
|
|
{ |
103
|
2 |
|
return new HttpException( |
104
|
2 |
|
\sprintf('The message is not an http request. "%s" received.', $line) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|