1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Jairo Rodríguez <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace BFunky\HttpParser; |
10
|
|
|
|
11
|
|
|
use BFunky\HttpParser\Entity\HttpDataValidation; |
12
|
|
|
use BFunky\HttpParser\Entity\HttpField; |
13
|
|
|
use BFunky\HttpParser\Entity\HttpFieldCollection; |
14
|
|
|
use BFunky\HttpParser\Entity\HttpHeaderInterface; |
15
|
|
|
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection; |
16
|
|
|
use BFunky\HttpParser\Exception\HttpParserBadFormatException; |
17
|
|
|
|
18
|
|
|
abstract class AbstractHttpParser implements HttpParserInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $httpRaw; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var HttpHeaderInterface |
27
|
|
|
*/ |
28
|
|
|
protected $httpHeader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var HttpFieldCollection |
32
|
|
|
*/ |
33
|
|
|
protected $httpFieldCollection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
2 |
|
* HttpParser constructor. |
37
|
|
|
* @param HttpFieldCollection $httpFieldCollection |
38
|
2 |
|
*/ |
39
|
2 |
|
public function __construct(HttpFieldCollection $httpFieldCollection = null) |
40
|
|
|
{ |
41
|
|
|
$this->httpFieldCollection = $httpFieldCollection ?? HttpFieldCollection::fromHttpFieldArray([]); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
/** |
45
|
|
|
* @param string $rawHttp |
46
|
2 |
|
*/ |
47
|
|
|
public function parse(string $rawHttp): void |
48
|
|
|
{ |
49
|
|
|
$this->process($rawHttp); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $headerFieldName |
54
|
|
|
* @return string |
55
|
|
|
* @throws HttpFieldNotFoundOnCollection |
56
|
|
|
*/ |
57
|
|
|
public function get(string $headerFieldName): string |
58
|
|
|
{ |
59
|
|
|
$httpField = $this->httpFieldCollection->get($headerFieldName); |
60
|
|
|
return $httpField->getValue(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return HttpHeaderInterface |
65
|
|
|
*/ |
66
|
|
|
public function getHeader(): HttpHeaderInterface |
67
|
|
|
{ |
68
|
|
|
return $this->httpHeader; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
2 |
|
* @param string $rawHttp |
73
|
|
|
* @throws HttpParserBadFormatException |
74
|
2 |
|
*/ |
75
|
2 |
|
protected function process(string $rawHttp): void |
76
|
|
|
{ |
77
|
|
|
$this->setHttpRaw($rawHttp); |
78
|
|
|
$this->extract(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
2 |
|
* Split the http string |
83
|
|
|
* @throws HttpParserBadFormatException |
84
|
2 |
|
*/ |
85
|
2 |
|
protected function extract(): void |
86
|
2 |
|
{ |
87
|
|
|
$headers = explode("\n", $this->httpRaw); |
88
|
|
|
foreach ($headers as $i => $headerLine) { |
89
|
2 |
|
if (trim($headerLine) === '') { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
2 |
|
if (HttpDataValidation::isField($headerLine)) { |
93
|
|
|
$this->addField($headerLine); |
94
|
|
|
} else { |
95
|
|
|
$this->addHeader($headerLine); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
2 |
|
* @param string $headerLine |
102
|
|
|
* @throws HttpParserBadFormatException |
103
|
2 |
|
*/ |
104
|
2 |
|
protected function addHeader(string $headerLine): void |
105
|
2 |
|
{ |
106
|
|
|
$data = preg_split('/ /', $headerLine); |
107
|
|
|
$data = array_merge($data, ['', '', '']); |
108
|
|
|
HttpDataValidation::checkHeaderOrRaiseError($data[0], $data[1], $data[2]); |
109
|
|
|
$this->setHttpHeader($data[0], $data[1], $data[2]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $headerLine |
114
|
|
|
*/ |
115
|
|
|
protected function addField(string $headerLine): void |
116
|
|
|
{ |
117
|
|
|
list($fieldKey, $fieldValue) = $this->splitRawLine($headerLine); |
118
|
|
|
$this->httpFieldCollection->add(HttpField::fromKeyAndValue($fieldKey, $fieldValue)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $method |
123
|
|
|
* @param string $path |
124
|
|
|
* @param string $protocol |
125
|
|
|
*/ |
126
|
|
|
abstract protected function setHttpHeader(string $method, string $path, string $protocol): void; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $line |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function splitRawLine(string $line): array |
133
|
|
|
{ |
134
|
|
|
$parts = []; |
135
|
|
|
if (strpos($line, ': ') !== false) { |
136
|
|
|
$parts = explode(': ', $line); |
137
|
|
|
} else { |
138
|
|
|
if (strpos($line, ':') !== false) { |
139
|
|
|
$parts = explode(':', $line); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return $parts; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
2 |
|
* @param string $httpRaw |
147
|
|
|
* @return HttpParserInterface |
148
|
2 |
|
*/ |
149
|
2 |
|
protected function setHttpRaw(string $httpRaw): HttpParserInterface |
150
|
|
|
{ |
151
|
|
|
$this->httpRaw = $httpRaw; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.