|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: jairo.rodriguez <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace BFunky\HttpParser; |
|
7
|
|
|
|
|
8
|
|
|
use BFunky\HttpParser\Entity\HttpDataValidation; |
|
9
|
|
|
use BFunky\HttpParser\Entity\HttpField; |
|
10
|
|
|
use BFunky\HttpParser\Entity\HttpFieldCollection; |
|
11
|
|
|
use BFunky\HttpParser\Entity\HttpHeaderInterface; |
|
12
|
|
|
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection; |
|
13
|
|
|
use BFunky\HttpParser\Exception\HttpParserBadFormatException; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractHttpParser implements HttpParserInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $httpRaw; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var HttpHeaderInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $httpHeader; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var HttpFieldCollection |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $httpFieldCollection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* HttpParser constructor. |
|
34
|
|
|
* @param HttpFieldCollection $httpFieldCollection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(HttpFieldCollection $httpFieldCollection = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->httpFieldCollection = $httpFieldCollection ?? HttpFieldCollection::fromHttpFieldArray([]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $rawHttp |
|
43
|
|
|
*/ |
|
44
|
|
|
public function parse(string $rawHttp) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->process($rawHttp); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $headerFieldName |
|
51
|
|
|
* @return string |
|
52
|
|
|
* @throws HttpFieldNotFoundOnCollection |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get(string $headerFieldName): string |
|
55
|
|
|
{ |
|
56
|
|
|
$httpField = $this->httpFieldCollection->get($headerFieldName); |
|
57
|
|
|
return $httpField->getValue(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return HttpHeaderInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getHeader(): HttpHeaderInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->httpHeader; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $rawHttp |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function process(string $rawHttp) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->setHttpRaw($rawHttp); |
|
74
|
|
|
$this->extract(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Split the http string |
|
79
|
|
|
* @throws HttpParserBadFormatException |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function extract() |
|
82
|
|
|
{ |
|
83
|
|
|
$headers = explode("\n", $this->httpRaw); |
|
84
|
|
|
foreach ($headers as $i => $headerLine) { |
|
85
|
|
|
if (trim($headerLine) === '') { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
if (HttpDataValidation::isField($headerLine)) { |
|
89
|
|
|
$this->addField($headerLine); |
|
90
|
|
|
} else { |
|
91
|
|
|
$this->addHeader($headerLine); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $headerLine |
|
98
|
|
|
* @throws HttpParserBadFormatException |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function addHeader(string $headerLine) |
|
101
|
|
|
{ |
|
102
|
|
|
$data = preg_split('/ /', $headerLine); |
|
103
|
|
|
$data = array_merge($data, ['', '', '']); |
|
104
|
|
|
HttpDataValidation::checkHeaderOrRaiseError($data[0], $data[1], $data[2]); |
|
105
|
|
|
$this->setHttpHeader($data[0], $data[1], $data[2]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $headerLine |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function addField(string $headerLine) |
|
112
|
|
|
{ |
|
113
|
|
|
list($fieldKey, $fieldValue) = $this->splitRawLine($headerLine); |
|
114
|
|
|
$this->httpFieldCollection->add(HttpField::fromKeyAndValue($fieldKey, $fieldValue)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $method |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param string $protocol |
|
121
|
|
|
*/ |
|
122
|
|
|
abstract protected function setHttpHeader(string $method, string $path, string $protocol); |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $line |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function splitRawLine(string $line): array |
|
129
|
|
|
{ |
|
130
|
|
|
$parts = []; |
|
131
|
|
|
if (strpos($line, ': ') !== false) { |
|
132
|
|
|
$parts = explode(': ', $line); |
|
133
|
|
|
} else if (strpos($line, ':') !== false) { |
|
134
|
|
|
$parts = explode(':', $line); |
|
135
|
|
|
} |
|
136
|
|
|
return $parts; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $httpRaw |
|
141
|
|
|
* @return HttpParserInterface |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function setHttpRaw(string $httpRaw): HttpParserInterface |
|
144
|
|
|
{ |
|
145
|
|
|
$this->httpRaw = $httpRaw; |
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
} |