1 | <?php |
||
17 | class HttpParser |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $httpRaw; |
||
23 | |||
24 | /** |
||
25 | * @var HttpHeaderInterface |
||
26 | */ |
||
27 | protected $httpHeader; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $httpFields; |
||
33 | |||
34 | /** |
||
35 | * HttpParser constructor. |
||
36 | */ |
||
37 | 3 | public function __construct() |
|
40 | |||
41 | /** |
||
42 | * @param string $rawHttpHeader |
||
43 | */ |
||
44 | 2 | public function parseHttpRequestHeader($rawHttpHeader) |
|
50 | |||
51 | /** |
||
52 | * @param string $rawHttpHeader |
||
53 | */ |
||
54 | 1 | public function parseHttpResponseHeader($rawHttpHeader) |
|
60 | |||
61 | /** |
||
62 | * @param string $headerFieldName |
||
63 | * @return string |
||
64 | */ |
||
65 | 2 | public function get($headerFieldName) |
|
80 | |||
81 | /** |
||
82 | * @return HttpHeaderInterface |
||
83 | */ |
||
84 | 2 | public function getHeader() |
|
88 | |||
89 | /** |
||
90 | * @param string $rawHttpHeader |
||
91 | * @return mixed |
||
92 | */ |
||
93 | 3 | protected function processHeader($rawHttpHeader) |
|
98 | |||
99 | /** |
||
100 | * Split the http string |
||
101 | * @return mixed |
||
102 | * @throws HttpParserBadFormatException |
||
103 | */ |
||
104 | 3 | protected function extract() |
|
105 | { |
||
106 | 3 | $header = array(); |
|
107 | 3 | $fieldList = array(); |
|
108 | 3 | $headers = explode("\n", $this->httpRaw); |
|
109 | 3 | foreach ($headers as $i => $headerLine) { |
|
110 | 3 | if ($headerLine === '') { |
|
111 | 2 | continue; |
|
112 | } |
||
113 | 3 | $parts = $this->splitRawLine($headerLine); |
|
114 | 3 | if (isset($parts[1])) { |
|
115 | 2 | $fieldList[$parts[0]] = $parts[1]; |
|
116 | 2 | } else { |
|
117 | 3 | $header = preg_split('/ /', $headerLine); |
|
118 | 3 | if (count($header) != 3) { |
|
119 | 1 | throw new HttpParserBadFormatException(); |
|
120 | } |
||
121 | } |
||
122 | 2 | } |
|
123 | 2 | return array($header, $fieldList); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param string $line |
||
128 | * @return array |
||
129 | */ |
||
130 | 3 | protected function splitRawLine($line) |
|
141 | |||
142 | /** |
||
143 | * @param array $fieldList |
||
144 | */ |
||
145 | 2 | protected function processFields($fieldList) |
|
151 | |||
152 | /** |
||
153 | * @param string $httpRaw |
||
154 | * @return HttpParser |
||
155 | */ |
||
156 | 3 | protected function setHttpRaw($httpRaw) |
|
161 | |||
162 | |||
163 | } |